Skip to content

Commit

Permalink
web UI auth: decouple auth_user from session
Browse files Browse the repository at this point in the history
Sessions are processed in web UI part only. Pcsd backend does not work
with sessions so it only gets who is logged in and not the whole session.
  • Loading branch information
tomjelinek committed Feb 15, 2016
1 parent b9e7f06 commit bc6ad90
Show file tree
Hide file tree
Showing 10 changed files with 525 additions and 490 deletions.
50 changes: 32 additions & 18 deletions pcsd/auth.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.validUser(username, password, generate_token = false)


def self.getUsersGroups(username) def self.getUsersGroups(username)
stdout, stderr, retval = run_cmd( stdout, stderr, retval = run_cmd(
getSuperuserSession, "id", "-Gn", username getSuperuserAuth(), "id", "-Gn", username
) )
if retval != 0 if retval != 0
$logger.info( $logger.info(
Expand Down Expand Up @@ -94,41 +94,43 @@ def self.validToken(token)
return false return false
end end


def self.loginByToken(session, cookies) def self.loginByToken(cookies)
auth_user = {}
if username = validToken(cookies["token"]) if username = validToken(cookies["token"])
if SUPERUSER == username if SUPERUSER == username
if cookies['CIB_user'] and cookies['CIB_user'].strip != '' if cookies['CIB_user'] and cookies['CIB_user'].strip != ''
session[:username] = cookies['CIB_user'] auth_user[:username] = cookies['CIB_user']
if cookies['CIB_user_groups'] and cookies['CIB_user_groups'].strip != '' if cookies['CIB_user_groups'] and cookies['CIB_user_groups'].strip != ''
session[:usergroups] = cookieUserDecode( auth_user[:usergroups] = cookieUserDecode(
cookies['CIB_user_groups'] cookies['CIB_user_groups']
).split(nil) ).split(nil)
else else
session[:usergroups] = [] auth_user[:usergroups] = []
end end
else else
session[:username] = SUPERUSER auth_user[:username] = SUPERUSER
session[:usergroups] = [] auth_user[:usergroups] = []
end end
return true return auth_user
else else
session[:username] = username auth_user[:username] = username
success, groups = getUsersGroups(username) success, groups = getUsersGroups(username)
session[:usergroups] = success ? groups : [] auth_user[:usergroups] = success ? groups : []
return true return auth_user
end end
end end
return false return nil
end end


def self.loginByPassword(session, username, password) def self.loginByPassword(username, password)
if validUser(username, password) if validUser(username, password)
session[:username] = username auth_user = {}
auth_user[:username] = username
success, groups = getUsersGroups(username) success, groups = getUsersGroups(username)
session[:usergroups] = success ? groups : [] auth_user[:usergroups] = success ? groups : []
return true return auth_user
end end
return false return nil
end end


def self.isLoggedIn(session) def self.isLoggedIn(session)
Expand All @@ -141,7 +143,7 @@ def self.isLoggedIn(session)
return false return false
end end


def self.getSuperuserSession() def self.getSuperuserAuth()
return { return {
:username => SUPERUSER, :username => SUPERUSER,
:usergroups => [], :usergroups => [],
Expand All @@ -162,5 +164,17 @@ def self.cookieUserEncode(text)
def self.cookieUserDecode(text) def self.cookieUserDecode(text)
return Base64.decode64(text) return Base64.decode64(text)
end end

def self.sessionToAuthUser(session)
return {
:username => session[:username],
:usergroups => session[:usergroups],
}
end

def self.authUserToSession(auth_user, session)
session[:username] = auth_user[:username]
session[:usergroups] = auth_user[:usergroups]
end
end end


20 changes: 10 additions & 10 deletions pcsd/cfgsync.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -425,15 +425,15 @@ def self.save(data)




class ConfigPublisher class ConfigPublisher
def initialize(session, configs, nodes, cluster_name, tokens={}) def initialize(auth_user, configs, nodes, cluster_name, tokens={})
@configs = configs @configs = configs
@nodes = nodes @nodes = nodes
@cluster_name = cluster_name @cluster_name = cluster_name
@published_configs_names = @configs.collect { |cfg| @published_configs_names = @configs.collect { |cfg|
cfg.class.name cfg.class.name
} }
@additional_tokens = tokens @additional_tokens = tokens
@session = session @auth_user = auth_user
end end


def send(force=false) def send(force=false)
Expand All @@ -451,7 +451,7 @@ def send(force=false)
@nodes.each { |node| @nodes.each { |node|
threads << Thread.new { threads << Thread.new {
code, out = send_request_with_token( code, out = send_request_with_token(
@session, node, 'set_configs', true, data, true, nil, 30, @auth_user, node, 'set_configs', true, data, true, nil, 30,
@additional_tokens @additional_tokens
) )
if 200 == code if 200 == code
Expand Down Expand Up @@ -535,11 +535,11 @@ def get_old_local_configs(node_response, published_configs_names)




class ConfigFetcher class ConfigFetcher
def initialize(session, config_classes, nodes, cluster_name) def initialize(auth_user, config_classes, nodes, cluster_name)
@config_classes = config_classes @config_classes = config_classes
@nodes = nodes @nodes = nodes
@cluster_name = cluster_name @cluster_name = cluster_name
@session = session @auth_user = auth_user
end end


def fetch_all() def fetch_all()
Expand Down Expand Up @@ -591,7 +591,7 @@ def get_configs_cluster(nodes, cluster_name)
nodes.each { |node| nodes.each { |node|
threads << Thread.new { threads << Thread.new {
code, out = send_request_with_token( code, out = send_request_with_token(
@session, node, 'get_configs', false, data @auth_user, node, 'get_configs', false, data
) )
if 200 == code if 200 == code
begin begin
Expand Down Expand Up @@ -700,13 +700,13 @@ def self.save_sync_new_version(config, nodes, cluster_name, fetch_on_conflict, t
else else
# we run in a cluster so we need to sync the config # we run in a cluster so we need to sync the config
publisher = ConfigPublisher.new( publisher = ConfigPublisher.new(
PCSAuth.getSuperuserSession(), [config], nodes, cluster_name, tokens PCSAuth.getSuperuserAuth(), [config], nodes, cluster_name, tokens
) )
old_configs, node_responses = publisher.publish() old_configs, node_responses = publisher.publish()
if old_configs.include?(config.class.name) if old_configs.include?(config.class.name)
if fetch_on_conflict if fetch_on_conflict
fetcher = ConfigFetcher.new( fetcher = ConfigFetcher.new(
PCSAuth.getSuperuserSession(), [config.class], nodes, cluster_name PCSAuth.getSuperuserAuth(), [config.class], nodes, cluster_name
) )
cfgs_to_save, _ = fetcher.fetch() cfgs_to_save, _ = fetcher.fetch()
cfgs_to_save.each { |cfg_to_save| cfgs_to_save.each { |cfg_to_save|
Expand Down Expand Up @@ -751,7 +751,7 @@ def self.save_sync_new_tokens(config, new_tokens, nodes, cluster_name)
end end
# we run in a cluster so we need to sync the config # we run in a cluster so we need to sync the config
publisher = ConfigPublisher.new( publisher = ConfigPublisher.new(
PCSAuth.getSuperuserSession(), [config_new], nodes, cluster_name, PCSAuth.getSuperuserAuth(), [config_new], nodes, cluster_name,
new_tokens new_tokens
) )
old_configs, node_responses = publisher.publish() old_configs, node_responses = publisher.publish()
Expand All @@ -761,7 +761,7 @@ def self.save_sync_new_tokens(config, new_tokens, nodes, cluster_name)
end end
# get tokens from all nodes and merge them # get tokens from all nodes and merge them
fetcher = ConfigFetcher.new( fetcher = ConfigFetcher.new(
PCSAuth.getSuperuserSession(), [config_new.class], nodes, cluster_name PCSAuth.getSuperuserAuth(), [config_new.class], nodes, cluster_name
) )
fetched_tokens = fetcher.fetch_all()[config_new.class.name] fetched_tokens = fetcher.fetch_all()[config_new.class.name]
config_new = Cfgsync::merge_tokens_files(config, fetched_tokens, new_tokens) config_new = Cfgsync::merge_tokens_files(config, fetched_tokens, new_tokens)
Expand Down
2 changes: 1 addition & 1 deletion pcsd/cluster_entity.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ def initialize
@pcsd_enabled = false @pcsd_enabled = false
end end


def self.load_current_node(session, crm_dom=nil) def self.load_current_node(crm_dom=nil)
node = ClusterEntity::Node.new node = ClusterEntity::Node.new
node.corosync = corosync_running? node.corosync = corosync_running?
node.corosync_enabled = corosync_enabled? node.corosync_enabled = corosync_enabled?
Expand Down
8 changes: 4 additions & 4 deletions pcsd/fenceagent.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
def getFenceAgents(session, fence_agent = nil) def getFenceAgents(auth_user, fence_agent = nil)
fence_agent_list = {} fence_agent_list = {}
agents = Dir.glob('/usr/sbin/fence_' + '*') agents = Dir.glob('/usr/sbin/fence_' + '*')
agents.each { |a| agents.each { |a|
Expand All @@ -7,7 +7,7 @@ def getFenceAgents(session, fence_agent = nil)
next if fa.name == "fence_ack_manual" next if fa.name == "fence_ack_manual"


if fence_agent and a.sub(/.*\//,"") == fence_agent.sub(/.*:/,"") if fence_agent and a.sub(/.*\//,"") == fence_agent.sub(/.*:/,"")
required_options, optional_options, advanced_options, info = getFenceAgentMetadata(session, fa.name) required_options, optional_options, advanced_options, info = getFenceAgentMetadata(auth_user, fa.name)
fa.required_options = required_options fa.required_options = required_options
fa.optional_options = optional_options fa.optional_options = optional_options
fa.advanced_options = advanced_options fa.advanced_options = advanced_options
Expand All @@ -18,7 +18,7 @@ def getFenceAgents(session, fence_agent = nil)
fence_agent_list fence_agent_list
end end


def getFenceAgentMetadata(session, fenceagentname) def getFenceAgentMetadata(auth_user, fenceagentname)
options_required = {} options_required = {}
options_optional = {} options_optional = {}
options_advanced = { options_advanced = {
Expand All @@ -43,7 +43,7 @@ def getFenceAgentMetadata(session, fenceagentname)
return [options_required, options_optional, options_advanced] return [options_required, options_optional, options_advanced]
end end
stdout, stderr, retval = run_cmd( stdout, stderr, retval = run_cmd(
session, "/usr/sbin/#{fenceagentname}", '-o', 'metadata' auth_user, "/usr/sbin/#{fenceagentname}", '-o', 'metadata'
) )
metadata = stdout.join metadata = stdout.join
begin begin
Expand Down
Loading

0 comments on commit bc6ad90

Please sign in to comment.