Improve searching for configured AWS credentials

The previous approach for finding AWS credentials was pretty naive and
only covered contents of a single file (~/.aws/credentials by
default).

The AWS CLI documentation states various other ways to configure
credentials which weren't covered:
https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#credentials
Even that aren't all ways, a look into the code shows:
https://github.com/boto/botocore/blob/develop/botocore/credentials.py

This commit changes the behavior so the hook will behave in a way
that if the AWS CLI is able to obtain credentials from local files,
the hook will find them as well.

The changes in detail are:
- detect AWS session tokens and handle them like secret keys.
- always search credentials in the default AWS CLI file locations
  ( ~/.aws/config, ~/.aws/credentials, /etc/boto.cfg and ~/.boto)
- detect AWS credentials configured via environment variables in
  AWS_SECRET_ACCESS_KEY, AWS_SECURITY_TOKEN and AWS_SESSION_TOKEN
- check additional configuration files configured via environment
  variables (AWS_CREDENTIAL_FILE, AWS_SHARED_CREDENTIALS_FILE and
  BOTO_CONFIG)
- print out the first four characters of each secret found in files to
  be checked in, to make it easier to figure out, what the secrets
  were, which were going to be checked in
- improve error handling for parsing ini-files
- improve tests

There is a major functional change introduced by this commit:
Locations the AWS CLI gets credentials from are always searched and
there is no way to disable them. --credentials-file is still there to
specify one or more additional files to search credentials in. It's
the purpose of this hook to find and check files for found
credentials, so it should work in any case. As this commit also
improves error handling for not-existing or malformed configuration
files, it should be no big deal.

Receiving credentials via the EC2 and ECS meta data services is not
covered intentionally, to not further increase the amount of changes
in this commit and as it's probably an edge case anyway to have this
hook running in such an environment.
This commit is contained in:
Daniel Roschka 2016-12-30 08:41:24 +01:00
parent 9573c13884
commit b0d4cdb1ee
9 changed files with 174 additions and 43 deletions

View file

@ -1,4 +1,4 @@
# this is an aws credentials configuration file. obviously not real credentials :P
# file with AWS access key ids, AWS secret access keys and AWS session tokens in multiple sections
[default]
aws_access_key_id = AKIASLARTIBARTFAST11
aws_secret_access_key = 7xebzorgm5143ouge9gvepxb2z70bsb2rtrh099e
@ -8,3 +8,5 @@ aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
[staging]
aws_access_key_id = AKIAJIMMINYCRICKET0A
aws_secret_access_key = ixswosj8gz3wuik405jl9k3vdajsnxfhnpui38ez
[test]
aws_session_token = foo

View file

@ -1,5 +1,4 @@
#file with a secret key, you'll notice it is a section of sample_aws_credentials
# file with an AWS access key id and an AWS secret access key
[production]
aws_access_key_id = AKIAVOGONSVOGONS0042
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb

View file

@ -0,0 +1,5 @@
# file with an AWS access key id, an AWS secret access key and an AWS session token
[production]
aws_access_key_id = AKIAVOGONSVOGONS0042
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
aws_session_token = foo

View file

@ -0,0 +1,3 @@
# file with an AWS session token
[production]
aws_session_token = foo

View file

@ -0,0 +1,3 @@
# file with an AWS access key id but no AWS secret access key
[production]
aws_access_key_id = AKIASLARTIBARTFAST11

View file

@ -1,5 +0,0 @@
# file with an access key but no secrets
# you'll notice it is a redacted section of sample_aws_credentials
[production]
aws_access_key_id = AKIASLARTIBARTFAST11