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.
-
#awskeyring(curr, prev) ⇒ Object
autocomplete.
-
#console(account = nil) ⇒ Object
Open the AWS Console.
-
#env(account = nil) ⇒ Object
Print Env vars.
-
#exec(account, *command) ⇒ Object
execute an external command with env set.
-
#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(account = 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.
32 33 34 |
# File 'lib/awskeyring_command.rb', line 32 def self.exit_on_failure? true end |
Instance Method Details
#__version ⇒ Object
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 !['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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/awskeyring_command.rb', line 151 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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/awskeyring_command.rb', line 207 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 |
#awskeyring(curr, prev) ⇒ Object
autocomplete
381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/awskeyring_command.rb', line 381 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
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 377 |
# File 'lib/awskeyring_command.rb', line 347 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' begin login_url = Awskeyring::Awsapi.get_login_url( 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 ['no-open'] puts login_url 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(account = nil) if ['unset'] put_env_string(account: nil, key: nil, secret: nil, token: nil) else 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/awskeyring_command.rb', line 128 def exec(account, *command) if command.empty? warn I18n.t('message.exec') exit 1 end cred = age_check_and_get(account: account, no_token: ['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 |
#initialise ⇒ Object
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: [: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(account) account = ask_check( existing: account, message: I18n.t('message.account'), validator: Awskeyring.method(:account_exists) ) 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
74 75 76 |
# File 'lib/awskeyring_command.rb', line 74 def list puts Awskeyring.list_account_names.join("\n") end |
#list_role ⇒ Object
List roles
82 83 84 85 86 87 88 |
# File 'lib/awskeyring_command.rb', line 82 def list_role 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
226 227 228 229 230 231 232 |
# File 'lib/awskeyring_command.rb', line 226 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
247 248 249 250 251 252 253 |
# File 'lib/awskeyring_command.rb', line 247 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
236 237 238 239 240 241 242 |
# File 'lib/awskeyring_command.rb', line 236 def remove_token(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_token(account: account, message: I18n.t('message.deltoken', account: account)) end |
#rotate(account = nil) ⇒ Object
rotate Account keys
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/awskeyring_command.rb', line 257 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
292 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/awskeyring_command.rb', line 292 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 ) role ||= [: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 ||= [: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) begin 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['USER'] ) Awskeyring.delete_token(account: account, message: '# Removing STS credentials') rescue Aws::Errors::ServiceError => e warn e.to_s exit 1 end 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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/awskeyring_command.rb', line 181 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 |