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.
-
#default ⇒ Object
default command to run.
-
#env(account = nil) ⇒ Object
Print Env vars.
-
#exec(account, *command) ⇒ 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/awskeyring_command.rb', line 212 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
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/awskeyring_command.rb', line 267 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
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/awskeyring_command.rb', line 432 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) 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
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/awskeyring_command.rb', line 400 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 \"#{options[:browser]}\" \"#{login_url}\"" : "open \"#{login_url}\"" pid = Process.spawn(spawn_cmd) Process.wait pid exit 1 if Process.last_status.exitstatus.positive? end 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/awskeyring_command.rb', line 109 def env(account = nil) if [: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, *command) ⇒ Object
execute an external command with env set
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/awskeyring_command.rb', line 184 def exec(account, *command) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize if command.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, command.join(' ')) Process.wait pid exit 1 if Process.last_status.exitstatus.positive? rescue Errno::ENOENT => e warn e.to_s exit 1 end end |
#import(account = nil) ⇒ Object
Import an Account
147 148 149 150 151 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 |
# File 'lib/awskeyring_command.rb', line 147 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/awskeyring_command.rb', line 128 def json(account) # rubocop:disable Metrics/AbcSize 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 |
#list ⇒ Object
list the accounts
81 82 83 84 85 86 87 |
# File 'lib/awskeyring_command.rb', line 81 def list if Awskeyring.list_account_names.empty? warn I18n.t('message.missing_account', bin: File.basename($PROGRAM_NAME)) exit 1 end puts Awskeyring.list_account_names.join("\n") end |
#list_role ⇒ Object
List roles
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/awskeyring_command.rb', line 92 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
286 287 288 289 290 291 292 |
# File 'lib/awskeyring_command.rb', line 286 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
306 307 308 309 310 311 312 |
# File 'lib/awskeyring_command.rb', line 306 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
296 297 298 299 300 301 302 |
# File 'lib/awskeyring_command.rb', line 296 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
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/awskeyring_command.rb', line 316 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.to_s 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
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/awskeyring_command.rb', line 350 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
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/awskeyring_command.rb', line 242 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 |