Skip to content

Commit

Permalink
switch to currently suggested ec2-cli format ENV variables: AWS_ACCES…
Browse files Browse the repository at this point in the history
…S_KEY/AWS_SECRET_KEY.

Backward compatible with previous formats.
  • Loading branch information
kadwanev committed Apr 15, 2014
1 parent 539f513 commit d8d7113
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -29,8 +29,8 @@ AWS.config(access_key_id: '...', secret_access_key: '...', region: 'us-west-2')

You can also specify these values via `ENV`:

export AWS_ACCESS_KEY_ID='...'
export AWS_SECRET_ACCESS_KEY='...'
export AWS_ACCESS_KEY='...'
export AWS_SECRET_KEY='...'
export AWS_REGION='us-west-2'

## Basic Usage
Expand Down
11 changes: 5 additions & 6 deletions lib/aws/core/configuration.rb
Expand Up @@ -30,14 +30,13 @@ module Core
# You can also export them into your environment and they will be picked up
# automatically:
#
# export AWS_ACCESS_KEY_ID='YOUR_KEY_ID_HERE'
# export AWS_SECRET_ACCESS_KEY='YOUR_SECRET_KEY_HERE'
# export AWS_ACCESS_KEY='YOUR_KEY_ID_HERE'
# export AWS_SECRET_KEY='YOUR_SECRET_KEY_HERE'
#
# For compatability with other AWS gems, the credentials can also be
# exported like:
# Previous variable names are also checked:
#
# export AMAZON_ACCESS_KEY_ID='YOUR_KEY_ID_HERE'
# export AMAZON_SECRET_ACCESS_KEY='YOUR_SECRET_KEY_HERE'
# AMAZON_ACCESS_KEY_ID / AMAZON_SECRET_ACCESS_KEY
# AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
#
# ## Modifying a Configuration
#
Expand Down
9 changes: 5 additions & 4 deletions lib/aws/core/credential_providers.rb
Expand Up @@ -101,8 +101,7 @@ def get_credentials
# * Static credentials from AWS.config (e.g. AWS.config.access_key_id,
# AWS.config.secret_access_key)
#
# * The environment (e.g. ENV['AWS_ACCESS_KEY_ID'] or
# ENV['AMAZON_ACCESS_KEY_ID'])
# * The environment (ENV['AWS_ACCESS_KEY'] and ENV['AWS_SECRET_KEY']
#
# * EC2 metadata service (checks for credentials provided by
# roles for instances).
Expand All @@ -115,6 +114,7 @@ class DefaultProvider
def initialize static_credentials = {}
@providers = []
@providers << StaticProvider.new(static_credentials)
@providers << ENVProvider.new('AWS', :access_key_id => 'ACCESS_KEY', :secret_access_key => 'SECRET_KEY', :session_token => 'SESSION_TOKEN')
@providers << ENVProvider.new('AWS')
@providers << ENVProvider.new('AMAZON')
@providers << EC2Provider.new
Expand Down Expand Up @@ -192,8 +192,9 @@ class ENVProvider
include Provider

# @param [String] prefix The prefix to apply to the ENV variable.
def initialize prefix
def initialize(prefix, suffixes=Hash[KEYS.map{|key| [key, key.to_s.upcase]}])
@prefix = prefix
@suffixes = suffixes
end

# @return [String]
Expand All @@ -203,7 +204,7 @@ def initialize prefix
def get_credentials
credentials = {}
KEYS.each do |key|
if value = ENV["#{@prefix}_#{key.to_s.upcase}"]
if value = ENV["#{@prefix}_#{@suffixes[key]}"]
credentials[key] = value
end
end
Expand Down
27 changes: 25 additions & 2 deletions spec/aws/core/credential_providers_spec.rb
Expand Up @@ -35,8 +35,10 @@ module CredentialProviders
provider.providers[1].should be_a(ENVProvider)
provider.providers[1].prefix.should == 'AWS'
provider.providers[2].should be_a(ENVProvider)
provider.providers[2].prefix.should == 'AMAZON'
provider.providers[3].should be_a(EC2Provider)
provider.providers[2].prefix.should == 'AWS'
provider.providers[3].should be_a(ENVProvider)
provider.providers[3].prefix.should == 'AMAZON'
provider.providers[4].should be_a(EC2Provider)
end

it 'passes static credentials to a static credential provider' do
Expand Down Expand Up @@ -303,6 +305,27 @@ module CredentialProviders

end

describe ENVProvider do

let(:env_variables) {{
'AWS_ACCESS_KEY' => 'akid',
'AWS_SECRET_KEY' => 'secret',
}}

before(:each) do
ENV.stub(:[]).and_return{|key| env_variables[key] }
end

it 'reads credentials with supplied suffixes' do
ENVProvider.new('AWS', :access_key_id => 'ACCESS_KEY', :secret_access_key => 'SECRET_KEY', :session_token => 'SESSION_TOKEN').
credentials.should == {
:access_key_id => 'akid',
:secret_access_key => 'secret',
}
end

end

describe CredentialFileProvider do

let(:mock_credential_file) { File.expand_path('../../../mock-credential-file.txt', __FILE__) }
Expand Down

0 comments on commit d8d7113

Please sign in to comment.