Class: Awshark::ProfileResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/awshark/profile_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProfileResolver

Returns a new instance of ProfileResolver.



5
6
7
8
9
10
11
12
# File 'lib/awshark/profile_resolver.rb', line 5

def initialize(options)
  @profile = options[:profile] || ENV.fetch('AWS_PROFILE')
  @shared_config = ::Aws::SharedConfig.new(
    profile_name: @profile,
    config_enabled: true
  )
  @region = options[:region] || ENV.fetch('REGION', nil)
end

Instance Method Details

#credentialsObject



14
15
16
# File 'lib/awshark/profile_resolver.rb', line 14

def credentials
  user_credentials || role_credentials
end

#mfa_tokenObject



54
55
56
57
# File 'lib/awshark/profile_resolver.rb', line 54

def mfa_token
  print %(Please enter MFA token for AWS account #{@profile}: )
  $stdin.gets.strip
end

#regionObject

Returns Aws region from option, env or .aws/config



36
37
38
# File 'lib/awshark/profile_resolver.rb', line 36

def region
  @region || @shared_config.region || 'eu-central-1'
end

#role_credentialsObject

Returns Aws credentials for configuration with

[profile]
role_arn = ROLE_ARN
source_profile = SOURCE_PROFILE


47
48
49
50
51
52
# File 'lib/awshark/profile_resolver.rb', line 47

def role_credentials
  @shared_config.assume_role_credentials_from_config(
    region: region,
    token_code: mfa_token
  )
end

#user_credentialsObject

Returns Aws credentials for configuration with

[profile]
aws_access_key_id=AWS_ACCESS_KEY_ID
aws_secret_access_key=AWS_SECRET_ACCESS_KEY

Returns nil for configuration with

[profile]
role_arn = ROLE_ARN
source_profile = SOURCE_PROFILE


29
30
31
# File 'lib/awshark/profile_resolver.rb', line 29

def user_credentials
  @shared_config.credentials
end