Class: DTK::Client::Account

Inherits:
CommandBaseThor show all
Includes:
ParseFile
Defined in:
lib/commands/thor/account.rb

Constant Summary collapse

KEY_EXISTS_ALREADY_CONTENT =
'key exists already'

Constants inherited from CommandBaseThor

CommandBaseThor::ALT_IDENTIFIER_SEPARATOR, CommandBaseThor::EXTENDED_TIMEOUT, CommandBaseThor::HIDE_FROM_BASE_CONTEXT, CommandBaseThor::TIME_DIFF

Constants included from CommandHelperMixin

CommandHelperMixin::Loaded

Constants included from ReparseMixin

ReparseMixin::YamlDTKMetaFiles

Constants included from Poller

Poller::PERIOD_WAIT_TIME

Constants inherited from Thor

Thor::HIDE_FROM_BASE_CONTEXT_HELP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ParseFile

#parse_key_value_file

Methods inherited from CommandBaseThor

action_on_revalidation_response, basename, create_context_arguments, execute_from_cli, generate_cached_id, get_cached_response, get_identifiers, get_usage_info, #help, #initialize, invalidate_entities, invisible_context_list, list_method_supported?, task_names, tiered_task_names, valid_id?

Methods included from CommandBase

#get, #get_connection, handle_argument_error, #post, #post_file, #rest_url, #rotate_args

Methods included from TaskStatusMixin

#list_task_info_aux, #task_status_aux, #task_status_stream

Methods included from Console

confirmation_prompt, confirmation_prompt_additional_options, confirmation_prompt_multiple_choice, confirmation_prompt_simple, unix_shell, wait_animation

Methods included from CommandHelperMixin

#Helper

Methods included from ReparseMixin

#reparse_aux

Methods included from PushCloneChangesMixin

#push_clone_changes_aux

Methods included from CommandBaseThor::CommonOptionDefs::ClassMixin

#version_method_option

Methods included from Poller

#poller_response, #print_response, #resolve_type

Methods inherited from Thor

get_alternative_identifiers, help, match_help_item_changes, overriden_help, printable_tasks, replace_if_matched!, set_context

Constructor Details

This class inherits a constructor from DTK::Client::CommandBaseThor

Class Method Details

.add_key(path_to_key, first_registration = false, name = 'dtk-client') ⇒ Object



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
# File 'lib/commands/thor/account.rb', line 67

def self.add_key(path_to_key, first_registration=false, name='dtk-client')
  match, matched_username = nil, nil

  unless File.file?(path_to_key)
    # OsUtil.put_warning "[ERROR]  " ,"No ssh key file found at (#{path_to_key}). Path is wrong or it is necessary to generate the public rsa key (e.g., run `ssh-keygen -t rsa`)", :red
    raise DtkError,"[ERROR] No ssh key file found at (#{path_to_key}). Path is wrong or it is necessary to generate the public rsa key (e.g., run `ssh-keygen -t rsa`)."
  end

  rsa_pub_key = SSHUtil.read_and_validate_pub_key(path_to_key)

  post_body = { :rsa_pub_key => rsa_pub_key.chomp }
  post_body.merge!(:username => name.chomp) if name
  post_body.merge!(:first_registration => first_registration)

  proper_response = nil
  response, key_exists_already = Account.internal_add_user_access("account/add_user_direct_access", post_body, 'service module')

  return response unless (response.ok? || key_exists_already)

  match = response.data['match']
  matched_username = response.data['matched_username']

  if response && !match
    repo_manager_fingerprint,repo_manager_dns = response.data_ret_and_remove!(:repo_manager_fingerprint,:repo_manager_dns)

    SSHUtil.update_ssh_known_hosts(repo_manager_dns,repo_manager_fingerprint)
    name = response.data["new_username"]

    OsUtil.print("SSH key '#{name}' added successfully!", :yellow)

  end

  return response, match, matched_username
end

.extended_contextObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/commands/thor/account.rb', line 46

def self.extended_context()
  {
    :context => {
      :delete_ssh_key => {
        :field => "display_name",
        :url => "account/list_ssh_keys",
        :opts => { :username => "#{Configurator.client_username}" }
      }
    },
    :command => {
    }
  }
end

.internal_add_user_access(url, post_body, component_name) ⇒ Object



60
61
62
63
64
65
# File 'lib/commands/thor/account.rb', line 60

def self.internal_add_user_access(url, post_body, component_name)
  response = post(rest_url(url),post_body)
  key_exists_already = (response.error_message||'').include?(KEY_EXISTS_ALREADY_CONTENT)
  puts "Key exists already for #{component_name}" if key_exists_already
  [response, key_exists_already]
end

Instance Method Details

#add_ssh_key(context_params) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/commands/thor/account.rb', line 147

def add_ssh_key(context_params)
  name, path_to_key = context_params.retrieve_arguments([:option_1!, :option_2],method_argument_names)
  path_to_key ||= SSHUtil.default_rsa_pub_key_path()

  response, matched, matched_username = Account.add_key(path_to_key, false, name)

  if matched
    OsUtil.print("Provided SSH pub key has already been added.", :yellow)
  elsif matched_username
    OsUtil.print("User ('#{matched_username}') already exists.", :yellow)
  else
    Configurator.add_current_user_to_direct_access() if response.ok?
  end

  if response.ok? && response.data(:repoman_registration_error)
    OsUtil.print("Warning: We were not able to register your key with remote catalog! #{response.data(:repoman_registration_error)}", :yellow)
  end

  response.ok? ? nil : response
end

#delete_ssh_key(context_params) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/commands/thor/account.rb', line 170

def delete_ssh_key(context_params)
  name = context_params.retrieve_arguments([:option_1!],method_argument_names)

  unless options.force?
    is_go = Console.confirmation_prompt("Are you sure you want to delete SSH key '#{name}'"+"?")
    return nil unless is_go
  end

  response = post rest_url("account/remove_user_direct_access"), { :username => name.chomp }
  return response unless response.ok?

  if response.ok? && response.data(:repoman_registration_error)
    OsUtil.print("Warning: We were not able to unregister your key with remote catalog! #{response.data(:repoman_registration_error)}", :yellow)
  end

  OsUtil.print("SSH key '#{name}' removed successfully!", :yellow)
  nil
end

#list_ssh_keys(context_params) ⇒ Object



140
141
142
143
144
# File 'lib/commands/thor/account.rb', line 140

def list_ssh_keys(context_params)
  post_body = {:username => Configurator.client_username }
  response = post rest_url("account/list_ssh_keys"), post_body
  response.render_table(:account_ssh_keys)
end

#register_catalog_user(context_params) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/commands/thor/account.rb', line 213

def register_catalog_user(context_params)
  body_params = Shell::InteractiveWizard.interactive_user_input([
   {:username => { :required => true} },
   {:password => { :type => :password }},
   {:repeat_password => { :type => :repeat_password }},
   {:email => { :type => :email, :required => true }},
   {:first_name => {}},
   {:last_name => {}}
  ])
  OsUtil.print("Creating account please wait ...", :white)
  response = post rest_url("account/register_catalog_account"), body_params

  if response.ok?
    OsUtil.print("You have successfully created catalog account!", :green)
    if Console.confirmation_prompt_simple("Do you want to make this account active?")
      response = post rest_url("account/set_catalog_credentials"), { :username => body_params[:username], :password => body_params[:password], :validate => true }
      OsUtil.print("Catalog user '#{body_params[:username]}' is currently active user!", :green)
    end
  else
    return response
  end

  nil
end

#set_catalog_credentials(context_params) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/commands/thor/account.rb', line 202

def set_catalog_credentials(context_params)
  creds = Configurator.enter_catalog_credentials()

  response = post rest_url("account/set_catalog_credentials"), { :username => creds[:username], :password => creds[:password], :validate => true }
  return response unless response.ok?

  OsUtil.print("Your catalog credentials have been set!", :yellow)
  nil
end

#set_default_namespace(context_params) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/commands/thor/account.rb', line 190

def set_default_namespace(context_params)
  default_namespace = context_params.retrieve_arguments([:option_1!],method_argument_names)
  post_body = { :namespace => default_namespace.chomp }

  response = post rest_url("account/set_default_namespace"), post_body
  return response unless response.ok?

  OsUtil.print("Your default namespace has been set to '#{default_namespace}'!", :yellow)
  nil
end

#set_password(context_params) ⇒ Object



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
# File 'lib/commands/thor/account.rb', line 103

def set_password(context_params)
  old_pass_prompt, old_pass, new_pass_prompt, confirm_pass_prompt = nil
  cred_file = Configurator::CRED_FILE
  old_pass = parse_key_value_file(cred_file)[:password]
  username = parse_key_value_file(cred_file)[:username]

  if old_pass.nil?
    OsUtil.print("Unable to retrieve your current password!", :yellow)
    return
  end

  3.times do
    old_pass_prompt = password_prompt("Enter old password: ")

    break if (old_pass.eql?(old_pass_prompt) || old_pass_prompt.nil?)
    OsUtil.print("Incorrect old password!", :yellow)
  end
  return unless old_pass.eql?(old_pass_prompt)

  new_pass_prompt = password_prompt("Enter new password: ")
  return if new_pass_prompt.nil?
  confirm_pass_prompt = password_prompt("Confirm new password: ")

  if new_pass_prompt.eql?(confirm_pass_prompt)
    post_body = {:new_password => new_pass_prompt}
    response = post rest_url("account/set_password"), post_body
    return response unless response.ok?

    Configurator.regenerate_conf_file(cred_file, [['username', "#{username.to_s}"], ['password', "#{new_pass_prompt.to_s}"]], '')
    OsUtil.print("Password changed successfully!", :yellow)
  else
    OsUtil.print("Entered passwords don't match!", :yellow)
    return
  end
end