From ad31bf98ab6833249386c367e3cd0170e353fc1d Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 9 Sep 2016 13:59:20 +0200 Subject: [PATCH] F #4714: Refactor and rename the actions --- share/onetoken/onetoken.sh | 34 +++- src/cli/one_helper/oneuser_helper.rb | 56 ++++-- src/cli/oneuser | 257 +++++++++++++++++---------- 3 files changed, 233 insertions(+), 114 deletions(-) diff --git a/share/onetoken/onetoken.sh b/share/onetoken/onetoken.sh index a5f963f94c..7636e3fd82 100644 --- a/share/onetoken/onetoken.sh +++ b/share/onetoken/onetoken.sh @@ -1,5 +1,22 @@ onetokenset(){ - OUT=$(oneuser token --set $1) + USER=$1 + TOKEN=$2 + PASSWORD=$3 + + if [ -z "$USER" -o -z "$TOKEN" ]; then + echo "Usage: $0 []" >&2 + exit 1 + fi + + if [ -z "$PASSWORD" ]; then + echo -n "Password: " + stty -echo + read PASSWORD + stty echo + echo + fi + + OUT=$(echo "$PASSWORD" | oneuser token-set --stdin_password $USER $TOKEN) if echo $OUT | grep -q export; then eval "$OUT" @@ -13,19 +30,28 @@ onetokenset(){ } onetokencreate(){ + USER=$1 + + if [ -z "$USER" ]; then + echo "Usage: $0 " >&2 + exit 1 + fi + + shift + echo -n "Password: " stty -echo - read password + read PASSWORD stty echo echo - OUT=$(echo "$password" | oneuser token --create --stdin_password $*) + OUT=$(echo "$PASSWORD" | oneuser token-create --stdin_password $USER $*) echo $OUT if echo $OUT | grep -q "Authentication Token"; then TOKEN=$(echo $OUT|tail -n1|cut -d: -f2) if [ -n "$TOKEN" ]; then - onetokenset $TOKEN + onetokenset $USER $TOKEN $PASSWORD else echo "Invalid token." return 1 diff --git a/src/cli/one_helper/oneuser_helper.rb b/src/cli/one_helper/oneuser_helper.rb index 5315b9fd2e..a23951e2c5 100644 --- a/src/cli/one_helper/oneuser_helper.rb +++ b/src/cli/one_helper/oneuser_helper.rb @@ -42,11 +42,6 @@ class OneUserHelper < OpenNebulaHelper::OneHelper "User password" end - def self.auth_file(auth_string) - auth_filename = Digest::MD5.hexdigest(auth_string) - ENV['HOME'] + "/.one/#{auth_filename}.token" - end - def self.password_to_str(arg, options) if options[:read_file] begin @@ -107,10 +102,12 @@ class OneUserHelper < OpenNebulaHelper::OneHelper return 0, auth.password end - ############################################################################ - # Generates a token and stores it in ONE_AUTH path as defined in this class - ############################################################################ - def login(username, options) + def auth_file(auth_string) + auth_filename = Digest::MD5.hexdigest(auth_string) + ENV['HOME'] + "/.one/#{auth_filename}.token" + end + + def get_login_client(username, options) #----------------------------------------------------------------------- # Init the associated Authentication class to generate the token. #----------------------------------------------------------------------- @@ -184,9 +181,18 @@ class OneUserHelper < OpenNebulaHelper::OneHelper token = auth.login_token(username, options[:time]) end - login_client = OpenNebula::Client.new("#{username}:#{token}", - nil, - :sync => sync) + OpenNebula::Client.new("#{username}:#{token}", nil, :sync => sync) + end + + ############################################################################ + # Generates a token and stores it in ONE_AUTH path as defined in this class + ############################################################################ + def login(username, options, use_client=false) + if use_client + login_client = OpenNebulaHelper::OneHelper.get_client + else + login_client = self.get_login_client(username, options) + end user = OpenNebula::User.new(User.build_xml, login_client) @@ -411,6 +417,31 @@ class OneUserHelper < OpenNebulaHelper::OneHelper end.compact if !valid_tokens.empty? end + def get_client_user + user = self.retrieve_resource(OpenNebula::User::SELF) + rc = user.info + if OpenNebula.is_error?(rc) + puts rc.message + exit_with_code 1, rc.message + end + user + end + + def token_create(args, options) + options[:time] ||= 36000 + + if args[0] + username = args[0] + use_client = false + else + user = self.get_client_user + username = user['NAME'] + use_client = true + end + + self.login(username, options, use_client) + end + private def factory(id=nil) @@ -509,4 +540,5 @@ class OneUserHelper < OpenNebulaHelper::OneHelper helper = OneQuotaHelper.new helper.format_quota(user_hash['USER'], default_quotas, user.id) end + end diff --git a/src/cli/oneuser b/src/cli/oneuser index 62d9c18700..094a5b462c 100755 --- a/src/cli/oneuser +++ b/src/cli/oneuser @@ -150,26 +150,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do } } - TOKEN_CREATE = { - :name => "create", - :large => "--create", - :description => "Create a new token" - } - - TOKEN_DELETE = { - :name => "delete", - :large => "--delete token", - :format => String, - :description => "Delete a token" - } - - TOKEN_SET = { - :name => "set", - :large => "--set token", - :format => String, - :description => "Set a token" - } - STDIN_PASSWORD = { :name => "stdin_password", :large => "--stdin_password", @@ -189,8 +169,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do GROUP, STDIN_PASSWORD] - token_options = login_options + [TOKEN_CREATE, TOKEN_DELETE, TOKEN_SET] - ######################################################################## # Formatters for arguments ######################################################################## @@ -376,30 +354,11 @@ cmd=CommandParser::CmdParser.new(ARGV) do end login_desc = <<-EOT.unindent - Creates the login token for authentication. The token can be used - together with any authentication driver. The token will be stored in - $HOME/.one/one_auth, and can be used subsequently to authenticate with - oned through API, CLI or Sunstone. - - Example, request a valid token for a generic driver (e.g. core auth, LDAP...): - oneuser login my_user --time 3600 - - Example, generate and set a token for SSH based authentication: - oneuser login my_user --ssh --key /tmp/id_rsa --time 72000 - - Example, same using X509 certificates: - oneuser login my_user --x509 --cert /tmp/my_cert.pem - --key /tmp/my_key.pk --time 72000 - - Example, now with a X509 proxy certificate - oneuser login my_user --x509_proxy --proxy /tmp/my_cert.pem - --time 72000 + Alias of token-create. EOT - command :login, login_desc, :username, :options=>login_options do - options[:time] ||= 36000 - - helper.login(args[0], options) + command :login, login_desc, [:username, nil], :options=>login_options do + helper.token_create(args, options) end key_desc = <<-EOT.unindent @@ -578,80 +537,182 @@ cmd=CommandParser::CmdParser.new(ARGV) do helper.list_pool(options) end - token_desc = <<-EOT.unindent - Manages user tokens. Tokens may be created, removed or set. + token_add_desc = <<-EOT.unindent + Creates the login token for authentication. The token can be used + together with any authentication driver. The token will be stored in + $HOME/.one/one_auth, and can be used subsequently to authenticate with + oned through API, CLI or Sunstone. + + If is ommited, it will infer it from the ONE_AUTH file. + + Example, request a valid token for a generic driver (e.g. core auth, LDAP...): + oneuser token-create my_user --time 3600 + + Example, request a group spefici token (new resources will be created in that + group and only resources that belong to that group will be listed): + oneuser token-create my_user --group + + Example, generate and set a token for SSH based authentication: + oneuser token-create my_user --ssh --key /tmp/id_rsa --time 72000 + + Example, same using X509 certificates: + oneuser token-create my_user --x509 --cert /tmp/my_cert.pem + --key /tmp/my_key.pk --time 72000 + + Example, now with a X509 proxy certificate + oneuser token-create my_user --x509_proxy --proxy /tmp/my_cert.pem + --time 72000 EOT - command :token, token_desc, :options=>token_options do + command :"token-create", token_add_desc, [:username, nil], + :options=>login_options do + + helper.token_create(args, options) + end + + token_set_desc = <<-EOT.unindent + Generates a ONE_AUTH file that contains the token. You can use the shortest + string to select the token. As long as the token starts with that string and + there is no other token that does, it will select the token. + + You can omit the username and use only one argument, the token, it will infer + the user from the ONE_AUTH file. + + Example, set a token: + $ oneuser token-set my_user 1d47 + export ONE_AUTH=/var/lib/one/.one/.token; export ONE_EGID=-1 + + You can copy & paste the output of the command and will load the proper + environment variables. + + EOT + + command :"token-set", token_set_desc, [:username, nil], :token, + :options=>login_options do + + if args.length == 1 + token = args[0] + else + username, token = args + + if username =~ /^\d+$/ + exit_with_code 1, "The argument should be the username, not the ID." + end + + helper.client = helper.get_login_client(username, options) + end + user = helper.retrieve_resource(OpenNebula::User::SELF) - rc = user.info + + rc = user.info if OpenNebula.is_error?(rc) puts rc.message exit_with_code 1, rc.message end - if options[:set] - token = helper.find_token(user, options[:set]) + token = helper.find_token(user, token) - if token.count > 1 - exit_with_code 1, "More than one token starting with '#{options[:set]}' found." - elsif token.count == 0 - exit_with_code 1, "No valid tokens found." - end + if token.count > 1 + exit_with_code 1, "More than one token starting with '#{options[:set]}' found." + elsif token.count == 0 + exit_with_code 1, "No valid tokens found." + end - token = token[0] - egid = user["LOGIN_TOKEN[TOKEN='#{token}']/EGID"] + token = token[0] + egid = user["LOGIN_TOKEN[TOKEN='#{token}']/EGID"] - auth_string = "#{user['NAME']}:#{token}" - auth_file = OneUserHelper::auth_file(auth_string) + auth_string = "#{user['NAME']}:#{token}" + auth_file = helper.auth_file(auth_string) - begin - FileUtils.mkdir_p(File.dirname(auth_file)) - rescue Errno::EEXIST - end + begin + FileUtils.mkdir_p(File.dirname(auth_file)) + rescue Errno::EEXIST + end - file = File.open(auth_file, "w") - file.write(auth_string) - file.close + file = File.open(auth_file, "w") + file.write(auth_string) + file.close - File.chmod(0600, auth_file) + File.chmod(0600, auth_file) - msg ="export ONE_AUTH=" + auth_file - msg << "; export ONE_EGID=#{egid}" if egid + msg ="export ONE_AUTH=" + auth_file + msg << "; export ONE_EGID=#{egid}" if egid - exit_with_code 0, msg - elsif options[:delete] - token = helper.find_token(user, options[:delete], true) + exit_with_code 0, msg + end - auth_string = "#{user['NAME']}:#{token}" - auth_file = OneUserHelper::auth_file(auth_string) + token_delete_desc = <<-EOT.unindent + Expires a token and removes the associated ONE_AUTH file if present. + EOT - if token.count > 1 - exit_with_code 1, "More than one token starting with '#{options[:delete]}' found." - elsif token.count == 0 - exit_with_code 1, "No tokens found." - end + command :"token-delete", token_delete_desc, [:username, nil], :token, + :options=>login_options do - token = token[0] - rc = user.login(user['NAME'], token, 0) - - if OpenNebula.is_error?(rc) - puts rc.message - exit_with_code 1, rc.message - else - puts "Token removed." - end - - begin - File.unlink(auth_file) - puts "Removing #{auth_file}" - rescue Errno::ENOENT - end - - 0 + if args.length == 1 + token = args[0] else - options[:time] ||= 36000 - helper.login(user['NAME'], options) + username, token = args + end + + if username + helper.client = helper.get_login_client(username, options) + end + + user = helper.retrieve_resource(OpenNebula::User::SELF) + + rc = user.info + if OpenNebula.is_error?(rc) + puts rc.message + exit_with_code 1, rc.message + end + + token = helper.find_token(user, token, true) + + if token.count > 1 + exit_with_code 1, "More than one token starting with '#{options[:delete]}' found." + elsif token.count == 0 + exit_with_code 1, "No tokens found." + end + + token = token[0] + + rc = user.login(user['NAME'], token, 0) + + if OpenNebula.is_error?(rc) + puts rc.message + exit_with_code 1, rc.message + else + puts "Token removed." + end + + auth_string = "#{user['NAME']}:#{token}" + auth_file = helper.auth_file(auth_string) + + begin + File.unlink(auth_file) + puts "Removing #{auth_file}" + rescue Errno::ENOENT + end + + 0 + end + + token_delete_all = <<-EOT.unindent + Delete all the tokens of a user. This command is intented to be executed by a + user that has MANAGE permissions of the target user. + EOT + + command :"token-delete-all", token_delete_all, :username, + :options=>login_options do + + username = args[0] + + if username =~ /^\d+$/ + exit_with_code 1, "The argument should be the username, not the ID." + end + + helper.perform_action(username, options, "Tokens expired") do |user| + user.login(username, "", 0) end end end