23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/morpheus/cli/commands/login.rb', line 23
def login(args)
options = {}
username, password = nil, nil
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = usage
opts.on( '-u', '--username USERNAME', "Username. Sub-tenant users must format their username with a prefix like {subdomain}\\{username}" ) do |val|
username = val
end
opts.on( '-p', '--password PASSWORD', "Password" ) do |val|
password = val
end
opts.on( '--password-file FILE', String, "Password File, read a file containing the password." ) do |val|
password_file = File.expand_path(val)
if !File.exist?(password_file) || !File.file?(password_file) raise ::OptionParser::InvalidOption.new("File not found: #{password_file}")
end
password = File.read(password_file) end
opts.on( '-t', '--test', "Test credentials only, does not update stored credentials for the appliance." ) do
options[:test_only] = true
end
opts.on( '--client-id CLIENT', "Used to test authentication with a client_id other than #{Morpheus::APIClient::CLIENT_ID}. Currently behaves like --test, credentials are not stored." ) do |val|
options[:client_id] = val.to_s
options[:test_only] = true
end
opts.on( '-T', '--token ACCESS_TOKEN', "Use an existing access token to login instead of authenticating with a username and password." ) do |val|
options[:remote_token] = val
end
build_common_options(opts, options, [:json, :dry_run, :remote, :quiet], [:remote_username, :remote_password, :remote_token])
opts. = <<-EOT
Login to a remote appliance with a username and password or using an access token.
Logging in with username and password will make an authentication api request to obtain an access token.
[username] is required, this is the username of the Morpheus User
[password] is required, this is the password of the Morpheus User
Sub-tenant users will need to pass their tenant subdomain prefix. ie. {subdomain}\\{username}
By default, the subdomain is the tenant account ID. Example: 2\\neo
The --token option can be used to login with a valid access token instead of username and password.
The specified token will be verified by making a whoami api request
If successful, the access token will be saved with the active session for the remote appliance.
This command will first logout any active session before attempting authentication.
The --test option can be used to test credentials without updating the stored credentials for the appliance, neither logging you in or out.
EOT
end
optparse.parse!(args)
verify_args!(args:args, max:2, optparse:optparse)
username = args[0] if args[0]
password = args[1] if args[1]
@api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
if options[:test_only]
puts "Testing credentials, your current session will not be modified."
elsif @remote_appliance[:authenticated]
puts "You will be automatically logged out of your current session as '#{@remote_appliance[:username]}'"
end
begin
if (options[:quiet] && (!options[:remote_token]) && !(username && password))
print_error yellow,"Please specify username and password, or token.", reset, "\n"
return 1
end
options[:username] = username if username
options[:password] = password if password
do_save = true
if options[:test_only] || options[:remote_url]
do_save = false
end
login_result = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).login(options, do_save)
if options[:dry_run]
return 0
end
wallet = login_result
::Morpheus::Cli::Remote.recalculate_variable_map()
if wallet && wallet['access_token']
if !options[:quiet]
if options[:test_only]
print green,"Success! Test Credentials verified for #{wallet['username']}", reset, "\n"
else
::Morpheus::Cli::Whoami.clear_whoami(@appliance_name, wallet['username'])
print green,"Success! Logged in as #{wallet['username']}", reset, "\n"
end
end
return 0 else
if !options[:quiet]
if options[:test_only]
else
end
end
return 1, "Login failed"
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
return 1
end
end
|