Class: Morpheus::Cli::Credentials

Inherits:
Object
  • Object
show all
Includes:
PrintHelper
Defined in:
lib/morpheus/cli/credentials.rb

Constant Summary collapse

@@appliance_credentials_map =
nil

Constants included from PrintHelper

PrintHelper::ALL_LABELS_UPCASE

Instance Method Summary collapse

Methods included from PrintHelper

#anded_list, #as_csv, #as_description_list, #as_json, #as_pretty_table, #as_yaml, #build_column_definitions, #current_terminal_width, #format_available_options, #format_boolean, #format_dt_dd, #format_results_pagination, #format_table_cell, #generate_usage_bar, included, #justify_string, #print_description_list, #print_dry_run, #print_green_success, #print_h1, #print_h2, #print_red_alert, #print_rest_exception, #print_results_pagination, #print_stats_usage, #quote_csv_value, #records_as_csv, #required_blue_prompt, terminal_width, terminal_width=, #truncate_string, #wrap

Constructor Details

#initialize(appliance_name, appliance_url) ⇒ Credentials

Returns a new instance of Credentials.



16
17
18
19
# File 'lib/morpheus/cli/credentials.rb', line 16

def initialize(appliance_name, appliance_url)
  @appliance_name = appliance_name ? appliance_name.to_sym : nil
  @appliance_url = appliance_url
end

Instance Method Details

#clear_saved_credentials(appliance_name) ⇒ Object



143
144
145
146
147
148
# File 'lib/morpheus/cli/credentials.rb', line 143

def clear_saved_credentials(appliance_name)
  @@appliance_credentials_map = load_credentials_file || {}
  @@appliance_credentials_map.delete(appliance_name)
  Morpheus::Logging::DarkPrinter.puts "clearing credentials for #{appliance_name} from file #{credentials_file_path}" if Morpheus::Logging.debug?
  File.open(credentials_file_path, 'w') {|f| f.write @@appliance_credentials_map.to_yaml } #Store
end

#credentials_file_pathObject



167
168
169
# File 'lib/morpheus/cli/credentials.rb', line 167

def credentials_file_path
  File.join(Morpheus::Cli.home_directory, "credentials")
end

#load_credentials_fileObject



157
158
159
160
161
162
163
164
165
# File 'lib/morpheus/cli/credentials.rb', line 157

def load_credentials_file
  fn = credentials_file_path
  if File.exist? fn
    Morpheus::Logging::DarkPrinter.puts "loading credentials file #{fn}" if Morpheus::Logging.debug?
    return YAML.load_file(fn)
  else
    return nil
  end
end

#load_saved_credentials(reload = false) ⇒ Object



150
151
152
153
154
155
# File 'lib/morpheus/cli/credentials.rb', line 150

def load_saved_credentials(reload=false)
  if reload || !defined?(@@appliance_credentials_map) || @@appliance_credentials_map.nil?
    @@appliance_credentials_map = load_credentials_file || {}
  end
  return @@appliance_credentials_map[@appliance_name]
end

#login(opts = {}) ⇒ Object



125
126
127
128
# File 'lib/morpheus/cli/credentials.rb', line 125

def (opts = {})
  clear_saved_credentials(@appliance_name)
  request_credentials(opts)
end

#logoutObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/morpheus/cli/credentials.rb', line 130

def logout()
  clear_saved_credentials(@appliance_name)
  # save pertinent session info to the appliance
  appliance = ::Morpheus::Cli::Remote.load_remote(@appliance_name)
  if appliance
    appliance.delete(:username) # could leave this...
    appliance[:authenticated] = false
    appliance[:last_logout_at] = Time.now.to_i
    ::Morpheus::Cli::Remote.save_remote(@appliance_name, appliance)
  end
  true
end

#request_credentials(opts = {}) ⇒ Object



21
22
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
# File 'lib/morpheus/cli/credentials.rb', line 21

def request_credentials(opts = {})
  #puts "request_credentials(#{opts})"
  username = nil
  password = nil
  access_token = nil
  skip_save = false
  # We should return an access Key for Morpheus CLI Here
  if !opts[:remote_username].nil?
    username = opts[:remote_username]
    password = opts[:remote_password]
    skip_save = opts[:remote_url] ? true : false
  else
    access_token = load_saved_credentials
  end
  if access_token
    return access_token
  end
  unless opts[:quiet] || opts[:no_prompt]
    # if username.empty? || password.empty?
      print "Enter Morpheus Credentials for #{display_appliance(@appliance_name, @appliance_url)}\n",reset
    # end
    if username.empty?
      print "Username: #{required_blue_prompt} "
      username = $stdin.gets.chomp!
    else
      print "Username: #{required_blue_prompt} #{username}\n"
    end
    if password.empty?
      print "Password: #{required_blue_prompt} "
      password = STDIN.noecho(&:gets).chomp!
      print "\n"
    else
      print "Password: #{required_blue_prompt} \n"
    end
  end
  if username.empty? || password.empty?
    print_red_alert "Username and password are required to login."
    return nil
  end
  begin
    auth_interface = Morpheus::AuthInterface.new(@appliance_url)
    json_response = auth_interface.(username, password)
    if opts[:json]
      print JSON.pretty_generate(json_response)
      print reset, "\n"
    end
    access_token = json_response['access_token']
    if access_token && access_token != ""
      unless skip_save
        save_credentials(@appliance_name, access_token)
      end
      # return access_token
    else
      print_red_alert "Credentials not verified."
      # return nil
    end
  rescue ::RestClient::Exception => e
    #raise e
    if (e.response && e.response.code == 400)
      print_red_alert "Credentials not verified."
      if opts[:json]
        json_response = JSON.parse(e.response.to_s)
        print JSON.pretty_generate(json_response)
        print reset, "\n"
      end
    else
      print_rest_exception(e, opts)
    end
    access_token = nil
  end


  unless skip_save
    begin
    # save pertinent session info to the appliance
      now = Time.now.to_i
      appliance = ::Morpheus::Cli::Remote.load_remote(@appliance_name)
      if appliance
        if access_token
          appliance = ::Morpheus::Cli::Remote.load_remote(@appliance_name)
          appliance[:authenticated] = true
          appliance[:username] = username
          appliance[:status] = "ready"
          appliance[:last_login_at] = now
          appliance[:last_success_at] = now
          ::Morpheus::Cli::Remote.save_remote(@appliance_name, appliance)
        else
          now = Time.now.to_i
          appliance = ::Morpheus::Cli::Remote.load_remote(@appliance_name)
          appliance[:authenticated] = false
          #appliance[:username] = username
          #appliance[:last_login_at] = now
          #appliance[:error] = "Credentials not verified"
          ::Morpheus::Cli::Remote.save_remote(@appliance_name, appliance)
        end
      end
    rescue => e
      #puts "failed to update remote appliance config: (#{e.class}) #{e.message}"
    end
  end

  return access_token
end

#save_credentials(appliance_name, token) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/morpheus/cli/credentials.rb', line 171

def save_credentials(appliance_name, token)
  # credential_map = appliance_credentials_map
  # reloading file is better for now, otherwise you can lose credentials with multiple shells.
  credential_map = load_credentials_file || {}
  if credential_map.nil?
    credential_map = {}
  end
  credential_map[appliance_name] = token
  begin
    fn = credentials_file_path
    if !Dir.exists?(File.dirname(fn))
      FileUtils.mkdir_p(File.dirname(fn))
    end
    Morpheus::Logging::DarkPrinter.puts "adding credentials for #{appliance_name} to #{fn}" if Morpheus::Logging.debug?
    File.open(fn, 'w') {|f| f.write credential_map.to_yaml } #Store
    FileUtils.chmod(0600, fn)
    @@appliance_credentials_map = credential_map
  rescue => e
    puts "failed to save #{fn}. #{e}"  if Morpheus::Logging.debug?
  end
end