Feature #4714: Add CLI support for token operations

This commit is contained in:
Jaime Melis committed 2016-09-01 17:35:20 +02:00
1 parent 94d9a0e35f
commit e17335d877
4 files changed
+243 -22

No files matched your search

+2
View File
@@ -416,6 +416,7 @@ INSTALL_FILES=(
EXAMPLE_SHARE_FILES:$SHARE_LOCATION/examples
WEBSOCKIFY_SHARE_FILES:$SHARE_LOCATION/websockify
INSTALL_GEMS_SHARE_FILE:$SHARE_LOCATION
ONETOKEN_SHARE_FILE:$SHARE_LOCATION
HOOK_FT_FILES:$VAR_LOCATION/remotes/hooks/ft
COMMON_CLOUD_LIB_FILES:$LIB_LOCATION/ruby/cloud
CLOUD_AUTH_LIB_FILES:$LIB_LOCATION/ruby/cloud/CloudAuth
@@ -1230,6 +1231,7 @@ HOOK_FT_FILES="share/hooks/host_error.rb"
#-------------------------------------------------------------------------------
INSTALL_GEMS_SHARE_FILE="share/install_gems/install_gems"
ONETOKEN_SHARE_FILE="share/onetoken/onetoken.sh"
#-------------------------------------------------------------------------------
# OCA Files
+36
View File
@@ -0,0 +1,36 @@
onetokenset(){
OUT=$(oneuser token --set $1)
if echo $OUT | grep -q export; then
eval "$OUT"
echo "Token loaded."
return 0
else
echo $OUT
return 1
fi
}
onetokencreate(){
echo -n "Password: "
stty -echo
read password
stty echo
echo
OUT=$(echo "$password" | oneuser token --create --stdin_password $*)
echo $OUT
if echo $OUT | grep -q "Authentication Token"; then
TOKEN=$(echo $OUT|tail -n1|cut -d: -f2)
if [ -n "$TOKEN" ]; then
onetokenset $TOKEN
else
echo "Invalid token."
return 1
fi
else
return 1
fi
}
+75 -20
View File
@@ -104,7 +104,6 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
# Generates a token and stores it in ONE_AUTH path as defined in this class
############################################################################
def login(username, options)
#-----------------------------------------------------------------------
# Init the associated Authentication class to generate the token.
#-----------------------------------------------------------------------
@@ -172,14 +171,21 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
sync = true
end
token = auth.login_token(username, options[:time])
if options[:stdin_password]
token = STDIN.read.strip
else
token = auth.login_token(username, options[:time])
end
login_client = OpenNebula::Client.new("#{username}:#{token}",
nil,
:sync => sync)
user = OpenNebula::User.new(User.build_xml, login_client)
token_oned = user.login(username, "", options[:time])
egid = options[:group] || -1
token_oned = user.login(username, "", options[:time], egid)
return -1, token_oned.message if OpenNebula.is_error?(token_oned)
@@ -378,6 +384,20 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
table
end
def find_token(user, token, show_expired=false)
user_hash = user.to_hash
valid_tokens = [user_hash["USER"]["LOGIN_TOKEN"]].flatten.map do |e|
next unless e["TOKEN"].start_with?(token)
if !show_expired
next unless Time.at(e["EXPIRATION_TIME"].to_i) > Time.now
end
e["TOKEN"]
end.compact
end
private
def factory(id=nil)
@@ -409,31 +429,64 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
puts str % ["SECONDARY GROUPS", groups.join(',') ] if groups.size > 1
puts str % ["PASSWORD", user['PASSWORD']]
puts str % ["AUTH_DRIVER", user['AUTH_DRIVER']]
if !user['LOGIN_TOKEN/TOKEN'].nil?
puts str % ["LOGIN_TOKEN", user['LOGIN_TOKEN/TOKEN']]
etime = user['LOGIN_TOKEN/EXPIRATION_TIME']
validity_str = case etime
when nil then ""
when "-1" then "forever"
else "not after #{Time.at(etime.to_i)}"
end
puts str % ["TOKEN VALIDITY", validity_str ]
end
puts str % ["ENABLED",
OpenNebulaHelper.boolean_to_str(user['ENABLED'])]
puts
user_hash = user.to_hash
client = @client
gid = user['GID']
tokens = [user_hash['USER']['LOGIN_TOKEN']].flatten.compact
CLIHelper.print_header(str_h1 % "TOKENS",false)
if tokens && !tokens.empty?
CLIHelper::ShowTable.new(nil, self) do
column :ID, "", :size=>7 do |d|
d["TOKEN"]
end
column :EGID, "", :left, :size=>5 do |d|
d["EGID"].to_i == -1 ? "*" + gid : d["EGID"]
end
column :EGROUP, "", :left, :size=>10 do |d|
client = OpenNebulaHelper::OneHelper.get_client
egid = d["EGID"].to_i == -1 ? gid : d["EGID"]
group = Group.new_with_id(egid, client)
rc = group.info
if OpenNebula.is_error?(rc)
"-"
else
group['NAME']
end
end
column :EXPIRATION, "", :left, :size=>20 do |d|
etime = d["EXPIRATION_TIME"]
expired = Time.now >= Time.at(d["EXPIRATION_TIME"].to_i)
case etime
when nil then ""
when "-1" then "forever"
else
if expired
"expired"
else
Time.at(etime.to_i).to_s
end
end
end
end.show(tokens,{})
end
puts
CLIHelper.print_header(str_h1 % "USER TEMPLATE",false)
puts user.template_str
user_hash = user.to_hash
default_quotas = nil
user.each('/USER/DEFAULT_USER_QUOTAS') { |elem|
@@ -443,4 +496,6 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
helper = OneQuotaHelper.new
helper.format_quota(user_hash['USER'], default_quotas, user.id)
end
end
+130 -2
View File
@@ -32,6 +32,7 @@ require 'one_helper/oneuser_helper'
require 'one_helper/onequota_helper'
require 'uri'
require 'uuidtools'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "`oneuser` <command> [<args>] [<options>]"
@@ -140,8 +141,56 @@ cmd=CommandParser::CmdParser.new(ARGV) do
:description => "Force one_auth file rewrite"
}
GROUP = {
:name => "group",
:large => "--group id|name" ,
:description => "Effective GID to use with this token.",
:format => String,
:proc => lambda { |o, options|
OpenNebulaHelper.rname_to_id(o, "GROUP")
}
}
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",
:description => "enable stdin password"
}
create_options = [READ_FILE, SHA1, SSH, X509, KEY, CERT, DRIVER]
login_options = [SSH, X509, X509_PROXY, KEY, CERT, PROXY, TIME, FORCE]
login_options = [SSH,
X509,
X509_PROXY,
KEY,
CERT,
PROXY,
TIME,
FORCE,
GROUP,
STDIN_PASSWORD]
token_options = login_options + [TOKEN_CREATE, TOKEN_DELETE, TOKEN_SET]
########################################################################
# Formatters for arguments
@@ -349,7 +398,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do
EOT
command :login, login_desc, :username, :options=>login_options do
options[:time] ||= 36000
helper.login(args[0], options)
@@ -530,4 +578,84 @@ 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.
EOT
command :token, token_desc, :options=>token_options do
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
if options[:set]
token = helper.find_token(user, options[:set])
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"]
# The token will be written to a file in $HOME/.one/<uuid>.token
# generate a random uuid
uuid = UUIDTools::UUID.random_create.to_s
auth_file = ENV['HOME'] + "/.one/#{uuid}.token"
begin
FileUtils.mkdir_p(File.dirname(auth_file))
rescue Errno::EEXIST
end
file = File.open(auth_file, "w")
file.write("#{user['NAME']}:#{token}")
file.close
File.chmod(0600, auth_file)
msg ="export ONE_AUTH=" + ENV['HOME'] + "/.one/#{uuid}.token"
msg << "; export ONE_EGID=#{egid}" if egid
exit_with_code 0, msg
elsif options[:delete]
token = helper.find_token(user, options[:delete], 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
exit_with_code 0, "Token removed."
end
# Remove the token files
Dir[ENV['HOME'] + "/.one/*.token"].each do |f|
auth = File.read(f).strip
if auth == "#{user['NAME']}:#{token}"
puts "Removing #{f}"
File.unlink(f)
end
end
0
else
options[:time] ||= 36000
helper.login(user['NAME'], options)
end
end
end