Class: AwskeyringCommand
- Inherits:
 - 
      Thor
      
        
- Object
 - Thor
 - AwskeyringCommand
 
 
- Defined in:
 - lib/awskeyring_command.rb
 
Overview
AWSkeyring command line interface.
Class Method Summary collapse
- 
  
    
      .exit_on_failure?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
default to returning an error on failure.
 
Instance Method Summary collapse
- 
  
    
      #__version  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
print the version number.
 - 
  
    
      #add(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Add an Account.
 - 
  
    
      #add_role(role = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Add a role.
 - 
  
    
      #autocomplete(curr, prev = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
autocomplete.
 - 
  
    
      #console(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Open the AWS Console.
 - 
  
    
      #decode(key = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
decode account numbers.
 - 
  
    
      #default  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
default command to run.
 - 
  
    
      #env(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Print Env vars.
 - 
  
    
      #exec(account, *exec)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
execute an external command with env set.
 - 
  
    
      #import(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Import an Account.
 - 
  
    
      #initialise  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
initialise the keychain.
 - 
  
    
      #json(account)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Print JSON for use with credential_process.
 - 
  
    
      #list  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
list the accounts.
 - 
  
    
      #list_role  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
List roles.
 - 
  
    
      #remove(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Remove an account.
 - 
  
    
      #remove_role(role = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
remove a role.
 - 
  
    
      #remove_token(token = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
remove a session token.
 - 
  
    
      #rotate(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
rotate Account keys.
 - 
  
    
      #token(account = nil, role = nil, code = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
generate a sessiopn token.
 - 
  
    
      #update(account = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Update an Account.
 
Class Method Details
.exit_on_failure? ⇒ Boolean
default to returning an error on failure.
      29 30 31  | 
    
      # File 'lib/awskeyring_command.rb', line 29 def self.exit_on_failure? true end  | 
  
Instance Method Details
#__version ⇒ Object
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 !['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
      233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256  | 
    
      # File 'lib/awskeyring_command.rb', line 233 def add(account = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_not_exists) ) key = ask_check( existing: [:key], message: I18n.t('message.key'), validator: Awskeyring.method(:access_key_not_exists) ) secret = ask_check( existing: [:secret], message: I18n.t('message.secret'), flags: 'secure', validator: Awskeyring::Validate.method(:secret_access_key) ) mfa = ask_check( existing: [:mfa], message: I18n.t('message.mfa'), flags: 'optional', validator: Awskeyring::Validate.method(:mfa_arn) ) Awskeyring::Awsapi.verify_cred(key: key, secret: secret) unless ['no-remote'] Awskeyring.add_account( account: account, key: key, secret: secret, mfa: mfa ) puts I18n.t('message.addaccount', account: account) end  | 
  
#add_role(role = nil) ⇒ Object
Add a role
      288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303  | 
    
      # File 'lib/awskeyring_command.rb', line 288 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: [: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
      463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478  | 
    
      # File 'lib/awskeyring_command.rb', line 463 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
      421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448  | 
    
      # File 'lib/awskeyring_command.rb', line 421 def console(account = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) cred = age_check_and_get(account: account, no_token: ['no-token']) path = [:path] || 'console' login_url = Awskeyring::Awsapi.get_login_url( key: cred[:key], secret: cred[:secret], token: cred[:token], path: path, user: ENV.fetch('USER', 'awskeyring') ) if ['no-open'] puts login_url else spawn_cmd = [:browser] ? "open -a \"#{[:browser]}\" \"#{login_url}\"" : "open \"#{login_url}\"" 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
      452 453 454 455 456 457 458  | 
    
      # File 'lib/awskeyring_command.rb', line 452 def decode(key = nil) key = ask_check( existing: key, message: I18n.t('message.key'), validator: Awskeyring::Validate.method(:access_key) ) puts Awskeyring::Awsapi.get_account_id(key: key) end  | 
  
#default ⇒ Object
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
      115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132  | 
    
      # File 'lib/awskeyring_command.rb', line 115 def env(account = nil) if [:test] account ||= 'fakeaccount' cred = Awskeyring::Awsapi.gen_test_credentials(account: account) put_env_string(cred) elsif [:unset] put_env_string(account: nil, key: nil, secret: nil, token: nil) else output_safe([:force]) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) cred = age_check_and_get(account: account, no_token: ['no-token']) put_env_string(cred) end end  | 
  
#exec(account, *exec) ⇒ Object
execute an external command with env set
      205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225  | 
    
      # File 'lib/awskeyring_command.rb', line 205 def exec(account, *exec) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize if exec.empty? warn I18n.t('message.exec') exit 1 end account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) cred = age_check_and_get(account: account, no_token: ['no-token']) env_vars = Awskeyring::Awsapi.get_env_array(cred) unbundle if ['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
      168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199  | 
    
      # File 'lib/awskeyring_command.rb', line 168 def import(account = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_not_exists) ) new_creds = Awskeyring::Awsapi.get_credentials_from_file(account: account) unless ['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.add_account( account: new_creds[:account], key: new_creds[:key], secret: new_creds[:secret], mfa: '' ) puts I18n.t('message.addaccount', account: 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: account, time: Time.at(new_creds[:expiry].to_i)) end end  | 
  
#initialise ⇒ Object
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: [: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
      139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163  | 
    
      # File 'lib/awskeyring_command.rb', line 139 def json(account) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength if [:test] cred = Awskeyring::Awsapi.gen_test_credentials(account: account) puts Awskeyring::Awsapi.get_cred_json( key: cred[:key], secret: cred[:secret], token: cred[:token], expiry: (Time.new + Awskeyring::Awsapi::TWELVE_HOUR).iso8601 ) else output_safe([:force]) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) cred = age_check_and_get(account: account, no_token: ['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 end  | 
  
#list ⇒ Object
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.list_account_names.empty? warn I18n.t('message.missing_account', bin: File.basename($PROGRAM_NAME)) exit 1 end if [:detail] puts Awskeyring.list_account_names_plus.join("\n") else puts Awskeyring.list_account_names.join("\n") end end  | 
  
#list_role ⇒ Object
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 [: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
      307 308 309 310 311 312 313  | 
    
      # File 'lib/awskeyring_command.rb', line 307 def remove(account = nil) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) Awskeyring.delete_account(account: account, message: I18n.t('message.delaccount', account: account)) end  | 
  
#remove_role(role = nil) ⇒ Object
remove a role
      327 328 329 330 331 332 333  | 
    
      # File 'lib/awskeyring_command.rb', line 327 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
      317 318 319 320 321 322 323  | 
    
      # File 'lib/awskeyring_command.rb', line 317 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
      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  | 
    
      # File 'lib/awskeyring_command.rb', line 337 def rotate(account = nil) # rubocop:disable Metrics/MethodLength account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) cred = Awskeyring.get_valid_creds(account: 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: account) ) rescue Aws::Errors::ServiceError => e warn e exit 1 end Awskeyring.update_account( account: account, key: new_key[:key], secret: new_key[:secret] ) puts I18n.t('message.upaccount', account: account) end  | 
  
#token(account = nil, role = nil, code = nil) ⇒ Object
generate a sessiopn token
      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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413  | 
    
      # File 'lib/awskeyring_command.rb', line 371 def token(account = nil, role = nil, code = nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) 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 ||= [: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: 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([:duration], role, code), mfa: item_hash[:mfa], key: item_hash[:key], secret: item_hash[:secret], user: ENV.fetch('USER', 'awskeyring') ) Awskeyring.delete_token(account: account, message: '# Removing STS credentials') Awskeyring.add_token( account: 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: account, time: Time.at(new_creds[:expiry].to_i)) end  | 
  
#update(account = nil) ⇒ Object
Update an Account
      263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283  | 
    
      # File 'lib/awskeyring_command.rb', line 263 def update(account = nil) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists), limited_to: Awskeyring.list_account_names ) key = ask_check( existing: [:key], message: I18n.t('message.key'), validator: Awskeyring.method(:access_key_not_exists) ) secret = ask_check( existing: [:secret], message: I18n.t('message.secret'), flags: 'secure', validator: Awskeyring::Validate.method(:secret_access_key) ) Awskeyring::Awsapi.verify_cred(key: key, secret: secret) unless ['no-remote'] Awskeyring.update_account( account: account, key: key, secret: secret ) puts I18n.t('message.upaccount', account: account) end  |