Class: Dev::Aws::Credentials
- Defined in:
- lib/firespring_dev_commands/aws/credentials.rb
Overview
Class contains methods for interacting with your Aws credentials
Constant Summary collapse
- CONFIG_FILE =
The local file where temporary credentials are stored
"#{Dev::Aws::CONFIG_DIR}/credentials".freeze
Instance Method Summary collapse
-
#active?(profile = Dev::Aws::Profile.new.current) ⇒ Boolean
Whether or not the current credentials are still active.
-
#base_setup! ⇒ Object
Setup base Aws credential settings.
-
#export! ⇒ Object
Export our current credentials into the ruby environment.
-
#export_container_credentials! ⇒ Object
Exports the credentials if there is an active credentials uri.
-
#export_profile_credentials! ⇒ Object
Exports the credentials if there is a configured aws profile.
-
#logged_in_account ⇒ Object
The account the profile is currently logged in to.
-
#logged_in_region ⇒ Object
The region associated with the current login.
-
#write!(account, creds) ⇒ Object
Write Aws account specific settings to the credentials file.
Instance Method Details
#active?(profile = Dev::Aws::Profile.new.current) ⇒ Boolean
Whether or not the current credentials are still active
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 25 def active?(profile = Dev::Aws::Profile.new.current) # If there is a metadata uri then we are in an AWS env - assume we are good return true if ENV.fetch('ECS_CONTAINER_METADATA_URI', nil) # Otherwise there should either be an aws config directory or access key configured return false unless File.exist?(Dev::Aws::CONFIG_DIR) || ENV.fetch('AWS_ACCESS_KEY_ID', nil) # TODO: I'd prefer to still validate creds if using a METADATA_URI # However this appears to require additional permissions which might not be present. Is there a better check here? # return false if !ENV.fetch('ECS_CONTAINER_METADATA_URI', nil) && !(File.exist?(Dev::Aws::CONFIG_DIR) || ENV.fetch('AWS_ACCESS_KEY_ID', nil)) # Check for expired credentials begin ::Aws::STS::Client.new(profile: profile).get_caller_identity rescue return false end # Check for invalid credentials begin # TODO: Is there a better check we can do here? ::Aws::SSM::Client.new(profile: profile).describe_parameters(max_results: 1) rescue return false end # If the credentials are valid, make sure they are set in the ruby process environment for use later export! true end |
#base_setup! ⇒ Object
Setup base Aws credential settings
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 57 def base_setup! # Make the base config directory FileUtils.mkdir_p(Dev::Aws::CONFIG_DIR) puts puts 'Configuring default credential values' # Write access key / secret key in the credentials file credini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/credentials", default: 'default') defaultini = credini['default'] access_key_default = defaultini['aws_access_key_id'] defaultini['aws_access_key_id'] = Dev::Common.new.ask('AWS Access Key ID', access_key_default) secret_key_default = defaultini['aws_secret_access_key'] defaultini['aws_secret_access_key'] = Dev::Common.new.ask('AWS Secret Access Key', secret_key_default) credini.write end |
#export! ⇒ Object
Export our current credentials into the ruby environment
91 92 93 94 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 91 def export! export_profile_credentials! export_container_credentials! end |
#export_container_credentials! ⇒ Object
Exports the credentials if there is an active credentials uri
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 97 def export_container_credentials! # If we already have creds defined, don't do anything return if ENV.fetch('AWS_ACCESS_KEY_ID', nil) # If a container credentials url is not present, don't do anything ecs_creds = ENV.fetch('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI', nil) return unless ecs_creds # Otherwise query the local creds, parse the json response, and store in the environment response = Net::HTTP.get_response(URI.parse("http://169.254.170.2#{ecs_creds}")) raise 'Error getting container credentials' unless response.is_a?(Net::HTTPSuccess) creds = JSON.parse(response.body) ENV['AWS_ACCESS_KEY_ID'] = creds['AccessKeyId'] ENV['AWS_SECRET_ACCESS_KEY'] = creds['SecretAccessKey'] ENV['AWS_SESSION_TOKEN'] = creds['Token'] ENV['AWS_DEFAULT_REGION'] = logged_in_region end |
#export_profile_credentials! ⇒ Object
Exports the credentials if there is a configured aws profile
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 117 def export_profile_credentials! # If we already have creds defined, don't do anything return if ENV.fetch('AWS_ACCESS_KEY_ID', nil) # If a profile config file is not present, don't do anything return unless File.exist?(CONFIG_FILE) # Otherwise load access key / secret key / session token from the credentials file into the environment credini = IniFile.new(filename: CONFIG_FILE, default: 'default') profile_credentials = credini[Dev::Aws::Profile.new.current] return unless profile_credentials ENV['AWS_ACCESS_KEY_ID'] = profile_credentials['aws_access_key_id'] ENV['AWS_SECRET_ACCESS_KEY'] = profile_credentials['aws_secret_access_key'] ENV['AWS_SESSION_TOKEN'] = profile_credentials['aws_session_token'] ENV['AWS_DEFAULT_REGION'] = logged_in_region end |
#logged_in_account ⇒ Object
The account the profile is currently logged in to
15 16 17 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 15 def logged_in_account ::Aws::STS::Client.new.get_caller_identity.account end |
#logged_in_region ⇒ Object
The region associated with the current login
20 21 22 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 20 def logged_in_region ::Aws::STS::Client.new.send(:config).region end |
#write!(account, creds) ⇒ Object
Write Aws account specific settings to the credentials file
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/firespring_dev_commands/aws/credentials.rb', line 78 def write!(account, creds) # Write access key / secret key / session token in the credentials file credini = IniFile.new(filename: CONFIG_FILE, default: 'default') defaultini = credini[account] defaultini['aws_access_key_id'] = creds.access_key_id defaultini['aws_secret_access_key'] = creds.secret_access_key defaultini['aws_session_token'] = creds.session_token credini.write end |