Skip to content

Commit

Permalink
Added support for reading AWS_CREDENTIAL_FILE
Browse files Browse the repository at this point in the history
  • Loading branch information
paulegan committed Jan 3, 2013
1 parent 18ca65d commit 9c3c02a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
32 changes: 26 additions & 6 deletions botocore/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ def search_environment(**kwargs):
return credentials


def search_credentials_file(**kwargs):
credentials = None
if 'AWS_CREDENTIAL_FILE' in os.environ:
full_path = os.path.expanduser(os.environ['AWS_CREDENTIAL_FILE'])
try:
lines = map(str.strip, open(full_path).readlines())
except IOError:
logger.warn('Unable to load AWS_CREDENTIAL_FILE (%s)', full_path)
else:
config = dict(line.split('=', 1) for line in lines if '=' in line)
access_key = config.get('AWSAccessKeyId')
secret_key = config.get('AWSSecretKey')
if access_key and secret_key:
credentials = Credentials(access_key, secret_key)
credentials.method = 'credentials-file'
logger.info('Found credentials in AWS_CREDENTIAL_FILE')
return credentials


def search_file(**kwargs):
"""
If there is are credentials in the configuration associated with
Expand Down Expand Up @@ -146,20 +165,21 @@ def search_boto_config(**kwargs):
return credentials

AllCredentialFunctions = [search_environment,
search_credentials_file,
search_file,
search_boto_config,
search_iam_role]

_credential_methods = {'env': search_environment,
'config': search_file,
'boto': search_boto_config,
'iam-role': search_iam_role}
_credential_methods = (('env', search_environment),
('credentials-file', search_credentials_file),
('config', search_file),
('boto', search_boto_config),
('iam-role', search_iam_role))


def get_credentials(config, metadata=None):
credentials = None
for cred_method in _credential_methods:
cred_fn = _credential_methods[cred_method]
for cred_method, cred_fn in _credential_methods:
credentials = cred_fn(config=config,
access_key_name='aws_access_key_id',
secret_key_name='aws_secret_access_key',
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/aws_credentials
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AWSAccessKeyId=foo
AWSSecretKey=bar
17 changes: 17 additions & 0 deletions tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ def test_envvar(self):
assert credentials.method == 'env'


class CredentialsFileTest(unittest.TestCase):

def test_credentials_file(self):
config_path = os.path.join(os.path.dirname(__file__),
'aws_credentials')
for var in ('AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY',
'BOTO_CONFIG', 'AWS_CONFIG_FILE'):
os.environ.pop(var, None)
os.environ['AWS_CREDENTIAL_FILE'] = config_path
session = botocore.session.get_session()
credentials = session.get_credentials()
assert credentials.access_key == 'foo'
assert credentials.secret_key == 'bar'
assert credentials.method == 'credentials-file'


class ConfigTest(unittest.TestCase):

def test_config(self):
Expand Down Expand Up @@ -109,6 +125,7 @@ def test_iam_role(self):
if 'AWS_CONFIG_FILE' in os.environ:
del os.environ['AWS_CONFIG_FILE']
os.environ['BOTO_CONFIG'] = ''
os.environ['AWS_CREDENTIAL_FILE'] = ''
session = botocore.session.get_session()
credentials = session.get_credentials(metadata=metadata)
assert credentials.access_key == 'foo'
Expand Down

0 comments on commit 9c3c02a

Please sign in to comment.