Class: AwskeyringCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/awskeyring_command.rb

Overview

AWSkeyring command line interface.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

default to returning an error on failure.

Returns:

  • (Boolean)


29
30
31
# File 'lib/awskeyring_command.rb', line 29

def self.exit_on_failure?
  true
end

Instance Method Details

#__versionObject

print the version number



46
47
48
49
50
51
52
# File 'lib/awskeyring_command.rb', line 46

def __version
  puts "Awskeyring v#{Awskeyring::VERSION}"
  if !options['no-remote'] && Awskeyring.latest_version != Awskeyring::VERSION
    puts "the latest version v#{Awskeyring.latest_version}"
  end
  puts "Homepage #{Awskeyring::HOMEPAGE}"
end

#add(account = nil) ⇒ Object

Add an Account



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/awskeyring_command.rb', line 217

def add( = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_not_exists)
  )
  key = ask_check(
    existing: options[:key], message: I18n.t('message.key'), validator: Awskeyring.method(:access_key_not_exists)
  )
  secret = ask_check(
    existing: options[:secret], message: I18n.t('message.secret'),
    flags: 'secure', validator: Awskeyring::Validate.method(:secret_access_key)
  )
  mfa = ask_check(
    existing: options[:mfa], message: I18n.t('message.mfa'),
    flags: 'optional', validator: Awskeyring::Validate.method(:mfa_arn)
  )
  Awskeyring::Awsapi.verify_cred(key: key, secret: secret) unless options['no-remote']
  Awskeyring.(
    account: ,
    key: key,
    secret: secret,
    mfa: mfa
  )
  puts I18n.t('message.addaccount', account: )
end

#add_role(role = nil) ⇒ Object

Add a role



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/awskeyring_command.rb', line 272

def add_role(role = nil)
  role = ask_check(
    existing: role, message: I18n.t('message.role'),
    validator: Awskeyring.method(:role_not_exists)
  )
  arn = ask_check(
    existing: options[:arn], message: I18n.t('message.arn'),
    validator: Awskeyring.method(:role_arn_not_exists)
  )

  Awskeyring.add_role(
    role: role,
    arn: arn
  )
  puts I18n.t('message.addrole', role: role)
end

#autocomplete(curr, prev = nil) ⇒ Object

autocomplete



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/awskeyring_command.rb', line 447

def autocomplete(curr, prev = nil)
  curr, prev = fix_args(curr, prev)
  comp_line = ENV.fetch('COMP_LINE', nil)
  comp_point_str = ENV.fetch('COMP_POINT', nil)
  unless comp_line && comp_point_str
    exec_name = File.basename($PROGRAM_NAME)
    warn I18n.t('message.awskeyring', path: $PROGRAM_NAME, bin: exec_name)
    exit 1
  end

  comp_lines = comp_line[0..(comp_point_str.to_i)].split

  comp_type, sub_cmd = comp_type(comp_lines: comp_lines, prev: prev, curr: curr)
  list = fetch_auto_resp(comp_type, sub_cmd)
  puts list.select { |elem| elem.start_with?(curr) }.sort!.join("\n")
end

#console(account = nil) ⇒ Object

Open the AWS Console



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/awskeyring_command.rb', line 405

def console( = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
   = ask_check(
    existing: ,
    message: I18n.t('message.account'),
    validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  cred = age_check_and_get(account: , no_token: options['no-token'])

  path = options[:path] || 'console'

   = Awskeyring::Awsapi.(
    key: cred[:key],
    secret: cred[:secret],
    token: cred[:token],
    path: path,
    user: ENV.fetch('USER', 'awskeyring')
  )

  if options['no-open']
    puts 
  else
    spawn_cmd = options[:browser] ? "open -a \"#{options[:browser]}\" \"#{}\"" : "open \"#{}\""
    pid = Process.spawn(spawn_cmd)
    Process.wait pid
    exit 1 if Process.last_status.exitstatus.positive?
  end
end

#decode(key = nil) ⇒ Object

decode account numbers



436
437
438
439
440
441
442
# File 'lib/awskeyring_command.rb', line 436

def decode(key = nil)
  key = ask_check(
    existing: key, message: I18n.t('message.key'), validator: Awskeyring::Validate.method(:access_key)
  )

  puts Awskeyring::Awsapi.(key: key)
end

#defaultObject

default command to run



35
36
37
38
39
40
41
# File 'lib/awskeyring_command.rb', line 35

def default
  if Awskeyring.prefs.empty?
    invoke :initialise
  else
    invoke :help
  end
end

#env(account = nil) ⇒ Object

Print Env vars



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/awskeyring_command.rb', line 114

def env( = nil)
  if options[:unset]
    put_env_string(account: nil, key: nil, secret: nil, token: nil)
  else
    output_safe(options[:force])
     = ask_check(
      existing: , message: I18n.t('message.account'),
      validator: Awskeyring.method(:account_exists),
      limited_to: Awskeyring.
    )
    cred = age_check_and_get(account: , no_token: options['no-token'])
    put_env_string(cred)
  end
end

#exec(account, *exec) ⇒ Object

execute an external command with env set



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/awskeyring_command.rb', line 189

def exec(, *exec) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  if exec.empty?
    warn I18n.t('message.exec')
    exit 1
  end
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  cred = age_check_and_get(account: , no_token: options['no-token'])
  env_vars = Awskeyring::Awsapi.get_env_array(cred)
  unbundle if options['no-bundle']
  begin
    pid = Process.spawn(env_vars, exec.join(' '))
    Process.wait pid
    exit 1 if Process.last_status.exitstatus.positive?
  rescue Errno::ENOENT => e
    warn e
    exit 1
  end
end

#import(account = nil) ⇒ Object

Import an Account



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/awskeyring_command.rb', line 152

def import( = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_not_exists)
  )
  new_creds = Awskeyring::Awsapi.get_credentials_from_file(account: )
  unless options['no-remote']
    Awskeyring::Awsapi.verify_cred(
      key: new_creds[:key],
      secret: new_creds[:secret],
      token: new_creds[:token]
    )
  end
  if new_creds[:token].nil?
    Awskeyring.(
      account: new_creds[:account],
      key: new_creds[:key],
      secret: new_creds[:secret],
      mfa: ''
    )
    puts I18n.t('message.addaccount', account: )
  else
    Awskeyring.add_token(
      account: new_creds[:account],
      key: new_creds[:key],
      secret: new_creds[:secret],
      token: new_creds[:token],
      expiry: new_creds[:expiry].to_i.to_s,
      role: nil
    )
    puts I18n.t('message.addtoken', account: , time: Time.at(new_creds[:expiry].to_i))
  end
end

#initialiseObject

initialise the keychain



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/awskeyring_command.rb', line 57

def initialise
  unless Awskeyring.prefs.empty?
    puts I18n.t('message.initialise', file: Awskeyring::PREFS_FILE)
    exit 1
  end

  keychain = ask_check(
    existing: options[:keychain],
    flags: 'optional',
    message: I18n.t('message.keychain'),
    validator: Awskeyring::Validate.method(:account_name)
  )
  keychain = 'awskeyring' if keychain.empty?

  puts I18n.t('message.newkeychain')
  Awskeyring.init_keychain(awskeyring: keychain)

  exec_name = File.basename($PROGRAM_NAME)

  puts I18n.t('message.addkeychain', keychain: keychain, exec_name: exec_name)
end

#json(account) ⇒ Object

Print JSON for use with credential_process



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/awskeyring_command.rb', line 133

def json() # rubocop:disable Metrics/AbcSize
  output_safe(options[:force])
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  cred = age_check_and_get(account: , no_token: options['no-token'])
  expiry = Time.at(cred[:expiry]) unless cred[:expiry].nil?
  puts Awskeyring::Awsapi.get_cred_json(
    key: cred[:key],
    secret: cred[:secret],
    token: cred[:token],
    expiry: (expiry || (Time.new + Awskeyring::Awsapi::ONE_HOUR)).iso8601
  )
end

#listObject

list the accounts



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/awskeyring_command.rb', line 82

def list
  if Awskeyring..empty?
    warn I18n.t('message.missing_account', bin: File.basename($PROGRAM_NAME))
    exit 1
  end
  if options[:detail]
    puts Awskeyring..join("\n")
  else
    puts Awskeyring..join("\n")
  end
end

#list_roleObject

List roles



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/awskeyring_command.rb', line 97

def list_role
  if Awskeyring.list_role_names.empty?
    warn I18n.t('message.missing_role', bin: File.basename($PROGRAM_NAME))
    exit 1
  end
  if options[:detail]
    puts Awskeyring.list_role_names_plus.join("\n")
  else
    puts Awskeyring.list_role_names.join("\n")
  end
end

#remove(account = nil) ⇒ Object

Remove an account



291
292
293
294
295
296
297
# File 'lib/awskeyring_command.rb', line 291

def remove( = nil)
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  Awskeyring.(account: , message: I18n.t('message.delaccount', account: ))
end

#remove_role(role = nil) ⇒ Object

remove a role



311
312
313
314
315
316
317
# File 'lib/awskeyring_command.rb', line 311

def remove_role(role = nil)
  role = ask_check(
    existing: role, message: I18n.t('message.role'), validator: Awskeyring.method(:role_exists),
    limited_to: Awskeyring.list_role_names
  )
  Awskeyring.delete_role(role_name: role, message: I18n.t('message.delrole', role: role))
end

#remove_token(token = nil) ⇒ Object

remove a session token



301
302
303
304
305
306
307
# File 'lib/awskeyring_command.rb', line 301

def remove_token(token = nil)
  token = ask_check(
    existing: token, message: I18n.t('message.account'), validator: Awskeyring.method(:token_exists),
    limited_to: Awskeyring.list_token_names
  )
  Awskeyring.delete_token(account: token, message: I18n.t('message.deltoken', account: token))
end

#rotate(account = nil) ⇒ Object

rotate Account keys



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/awskeyring_command.rb', line 321

def rotate( = nil) # rubocop:disable Metrics/MethodLength
   = ask_check(
    existing: ,
    message: I18n.t('message.account'),
    validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  cred = Awskeyring.get_valid_creds(account: , no_token: true)

  begin
    new_key = Awskeyring::Awsapi.rotate(
      account: cred[:account],
      key: cred[:key],
      secret: cred[:secret],
      key_message: I18n.t('message.rotate', account: )
    )
  rescue Aws::Errors::ServiceError => e
    warn e
    exit 1
  end

  Awskeyring.(
    account: ,
    key: new_key[:key],
    secret: new_key[:secret]
  )

  puts I18n.t('message.upaccount', account: )
end

#token(account = nil, role = nil, code = nil) ⇒ Object

generate a sessiopn token



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/awskeyring_command.rb', line 355

def token( = nil, role = nil, code = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
   = ask_check(
    existing: ,
    message: I18n.t('message.account'),
    validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  if role
    role = ask_check(
      existing: role, message: I18n.t('message.role'), validator: Awskeyring.method(:role_exists),
      limited_to: Awskeyring.list_role_names
    )
  end
  code ||= options[:code]
  if code
    code = ask_check(
      existing: code, message: I18n.t('message.code'), validator: Awskeyring::Validate.method(:mfa_code)
    )
  end
  item_hash = age_check_and_get(account: , no_token: true)

  new_creds = Awskeyring::Awsapi.get_token(
    code: code,
    role_arn: (Awskeyring.get_role_arn(role_name: role) if role),
    duration: default_duration(options[:duration], role, code),
    mfa: item_hash[:mfa],
    key: item_hash[:key],
    secret: item_hash[:secret],
    user: ENV.fetch('USER', 'awskeyring')
  )
  Awskeyring.delete_token(account: , message: '# Removing STS credentials')

  Awskeyring.add_token(
    account: ,
    key: new_creds[:key],
    secret: new_creds[:secret],
    token: new_creds[:token],
    expiry: new_creds[:expiry].to_i.to_s,
    role: role
  )

  puts I18n.t('message.addtoken', account: , time: Time.at(new_creds[:expiry].to_i))
end

#update(account = nil) ⇒ Object

Update an Account



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/awskeyring_command.rb', line 247

def update( = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
   = ask_check(
    existing: , message: I18n.t('message.account'),
    validator: Awskeyring.method(:account_exists),
    limited_to: Awskeyring.
  )
  key = ask_check(
    existing: options[:key], message: I18n.t('message.key'), validator: Awskeyring.method(:access_key_not_exists)
  )
  secret = ask_check(
    existing: options[:secret], message: I18n.t('message.secret'),
    flags: 'secure', validator: Awskeyring::Validate.method(:secret_access_key)
  )
  Awskeyring::Awsapi.verify_cred(key: key, secret: secret) unless options['no-remote']
  Awskeyring.(
    account: ,
    key: key,
    secret: secret
  )
  puts I18n.t('message.upaccount', account: )
end