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)


32
33
34
# File 'lib/awskeyring_command.rb', line 32

def self.exit_on_failure?
  true
end

Instance Method Details

#__versionObject

print the version number



39
40
41
42
43
44
45
# File 'lib/awskeyring_command.rb', line 39

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

#add(account = nil) ⇒ Object

Add an Account



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

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, token: nil) 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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/awskeyring_command.rb', line 243

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

#awskeyring(curr, prev) ⇒ Object

autocomplete



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/awskeyring_command.rb', line 417

def awskeyring(curr, prev)
  comp_line = ENV['COMP_LINE']
  unless comp_line
    exec_name = File.basename($PROGRAM_NAME)
    warn I18n.t('message.awskeyring', path: $PROGRAM_NAME, bin: exec_name)
    exit 1
  end

  curr, comp_len, sub_cmd = comp_type(comp_line: comp_line, curr: curr, prev: prev)
  print_auto_resp(curr, comp_len, sub_cmd)
end

#console(account = nil) ⇒ Object

Open the AWS Console



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/awskeyring_command.rb', line 383

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'

  begin
     = Awskeyring::Awsapi.(
      key: cred[:key],
      secret: cred[:secret],
      token: cred[:token],
      path: path,
      user: ENV['USER']
    )
  rescue Aws::Errors::ServiceError => e
    warn e.to_s
    exit 1
  end

  if options['no-open']
    puts 
  else
    pid = Process.spawn("open \"#{login_url}\"")
    Process.wait pid
  end
end

#env(account = nil) ⇒ Object

Print Env vars



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

def env( = nil)
  if options['unset']
    put_env_string(account: nil, key: nil, secret: nil, token: nil)
  else
     = 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, *command) ⇒ Object

execute an external command with env set



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/awskeyring_command.rb', line 164

def exec(, *command)
  if command.empty?
    warn I18n.t('message.exec')
    exit 1
  end
  cred = age_check_and_get(account: , no_token: options['no-token'])
  env_vars = Awskeyring::Awsapi.get_env_array(cred)
  begin
    pid = Process.spawn(env_vars, command.join(' '))
    Process.wait pid
    $CHILD_STATUS
  rescue Errno::ENOENT => e
    warn e.to_s
    exit 1
  end
end

#import(account = nil) ⇒ Object

Import an Account



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/awskeyring_command.rb', line 128

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/awskeyring_command.rb', line 50

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



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/awskeyring_command.rb', line 111

def json()
   = ask_check(
    existing: , message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists)
  )
  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



74
75
76
# File 'lib/awskeyring_command.rb', line 74

def list
  puts Awskeyring..join("\n")
end

#list_roleObject

List roles



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

def list_role
  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



262
263
264
265
266
267
268
# File 'lib/awskeyring_command.rb', line 262

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



283
284
285
286
287
288
289
# File 'lib/awskeyring_command.rb', line 283

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(account = nil) ⇒ Object

remove a session token



272
273
274
275
276
277
278
# File 'lib/awskeyring_command.rb', line 272

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

#rotate(account = nil) ⇒ Object

rotate Account keys



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/awskeyring_command.rb', line 293

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.to_s
    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



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/awskeyring_command.rb', line 328

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.
  )
  role ||= options[:role]
  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)

  begin
    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['USER']
    )
    Awskeyring.delete_token(account: , message: '# Removing STS credentials')
  rescue Aws::Errors::ServiceError => e
    warn e.to_s
    exit 1
  end

  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



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

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