Class: AwsSessionToken::CLI
- Inherits:
-
Object
- Object
- AwsSessionToken::CLI
- Defined in:
- lib/aws_session_token/cli.rb
Overview
Execute the process for getting & updating the session token.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #mfa_device ⇒ Object
- #run ⇒ Object
- #session_token(mfa_device, otp) ⇒ Object
- #set_aws_creds ⇒ Object
- #token_prompt ⇒ Object
- #validate_creds_file ⇒ Object
Constructor Details
#initialize ⇒ CLI
28 29 30 31 |
# File 'lib/aws_session_token/cli.rb', line 28 def initialize = Options.new @creds_file = CredentialsFile.new end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
26 27 28 |
# File 'lib/aws_session_token/cli.rb', line 26 def end |
Instance Method Details
#mfa_device ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/aws_session_token/cli.rb', line 64 def mfa_device iam_client = Aws::IAM::Client.new params = { max_items: 1 } params[:user_name] = .user if .user response = iam_client.list_mfa_devices(params) list = response.mfa_devices return list[0].serial_number unless list.nil? || list.empty? warn "\nSpecified profile/user doesn't have MFA device." warn "\nScript execution unnecessary." exit end |
#run ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/aws_session_token/cli.rb', line 33 def run .parse(ARGV) validate_creds_file set_aws_creds mfa = mfa_device token = .token || token_prompt creds = session_token(mfa, token) @creds_file.write(.credentials_file, .session_profile, creds) end |
#session_token(mfa_device, otp) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/aws_session_token/cli.rb', line 81 def session_token(mfa_device, otp) @sts_client = Aws::STS::Client.new resp = @sts_client.get_session_token( duration_seconds: .duration, serial_number: mfa_device, token_code: otp ) resp.credentials end |
#set_aws_creds ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/aws_session_token/cli.rb', line 56 def set_aws_creds credentials = Aws::SharedCredentials.new(path: .credentials_file, profile_name: .profile) Aws.config.update(credentials: credentials) rescue Aws::Errors::NoSuchProfileError warn "\nSpecified AWS Profile doesn't exist: #{@options.profile}" exit 1 end |
#token_prompt ⇒ Object
76 77 78 79 |
# File 'lib/aws_session_token/cli.rb', line 76 def token_prompt cli = HighLine.new cli.ask "Specify the OTP Token for the profile #{@options.profile}:" end |
#validate_creds_file ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/aws_session_token/cli.rb', line 43 def validate_creds_file return if File.exist?(.credentials_file) && File.writable?(.credentials_file) unless File.exist?(.credentials_file) raise( ArgumentError, "Specified credentials file is missing: #{@options.credentials_file}" ) end raise( ArgumentError, "Specified credentials file cannot be modified by current user: #{@options.credentials_file}" ) end |