Skip to content

Commit b0d4cdb

Browse files
committed
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.
1 parent 9573c13 commit b0d4cdb

9 files changed

Lines changed: 174 additions & 43 deletions

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ Add this to your `.pre-commit-config.yaml`
4040
- `check-xml` - Attempts to load all xml files to verify syntax.
4141
- `check-yaml` - Attempts to load all yaml files to verify syntax.
4242
- `debug-statements` - Check for pdb / ipdb / pudb statements in code.
43-
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you have set up with the AWS CLI.
43+
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you
44+
have set up with the AWS CLI.
45+
The following arguments are available:
46+
- `--credential-file` - additional AWS CLI style configuration file in a
47+
non-standard location to fetch configured credentials from. Can be repeated
48+
multiple times.
4449
- `detect-private-key` - Checks for the existence of private keys.
4550
- `double-quote-string-fixer` - This hook replaces double quoted strings
4651
with single quoted strings.

pre_commit_hooks/detect_aws_credentials.py

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,118 @@
77
from six.moves import configparser
88

99

10-
def get_your_keys(credentials_file):
11-
"""reads the secret keys in your credentials file in order to be able to
12-
look for them in the submitted code.
10+
def get_aws_credential_files_from_env():
11+
"""Extract credential file paths from environment variables."""
12+
files = set()
13+
for env_var in {'AWS_CREDENTIAL_FILE', 'AWS_SHARED_CREDENTIALS_FILE',
14+
'BOTO_CONFIG'}:
15+
try:
16+
files.add(os.environ[env_var])
17+
except KeyError:
18+
pass
19+
return files
20+
21+
22+
def get_aws_secrets_from_env():
23+
"""Extract AWS secrets from environment variables."""
24+
keys = set()
25+
for env_var in {'AWS_SECRET_ACCESS_KEY', 'AWS_SECURITY_TOKEN',
26+
'AWS_SESSION_TOKEN'}:
27+
try:
28+
keys.add(os.environ[env_var])
29+
except KeyError:
30+
pass
31+
return keys
32+
33+
34+
def get_aws_secrets_from_file(credentials_file):
35+
"""Extract AWS secrets from configuration files.
36+
37+
Read an ini-style configuration file and return a set with all found AWS
38+
secret access keys.
1339
"""
1440
aws_credentials_file_path = os.path.expanduser(credentials_file)
1541
if not os.path.exists(aws_credentials_file_path):
16-
return None
42+
return set()
1743

1844
parser = configparser.ConfigParser()
19-
parser.read(aws_credentials_file_path)
45+
try:
46+
parser.read(aws_credentials_file_path)
47+
except configparser.MissingSectionHeaderError:
48+
return set()
2049

2150
keys = set()
2251
for section in parser.sections():
23-
keys.add(parser.get(section, 'aws_secret_access_key'))
52+
for var in {'aws_secret_access_key', 'aws_security_token',
53+
'aws_session_token'}:
54+
try:
55+
keys.add(parser.get(section, var))
56+
except configparser.NoOptionError:
57+
pass
2458
return keys
2559

2660

2761
def check_file_for_aws_keys(filenames, keys):
62+
"""Check if files contain AWS secrets.
63+
64+
Return a list of all files containing AWS secrets and keys found, with all
65+
but the first four characters obfuscated to ease debugging.
66+
"""
2867
bad_files = []
2968

3069
for filename in filenames:
3170
with open(filename, 'r') as content:
3271
text_body = content.read()
33-
if any(key in text_body for key in keys):
34-
# naively match the entire file, low chance of incorrect collision
35-
bad_files.append(filename)
36-
72+
for key in keys:
73+
# naively match the entire file, low chance of incorrect
74+
# collision
75+
if key in text_body:
76+
bad_files.append({'filename': filename,
77+
'key': key[:4].ljust(32, str('*'))})
3778
return bad_files
3879

3980

4081
def main(argv=None):
4182
parser = argparse.ArgumentParser()
42-
parser.add_argument('filenames', nargs='*', help='Filenames to run')
83+
parser.add_argument('filenames', nargs='+', help='Filenames to run')
4384
parser.add_argument(
4485
'--credentials-file',
45-
default='~/.aws/credentials',
86+
dest='credential_files',
87+
action='append',
88+
default=['~/.aws/config', '~/.aws/credentials', '/etc/boto.cfg',
89+
'~/.boto'],
4690
help=(
47-
'location of aws credentials file from which to get the secret '
48-
"keys we're looking for"
49-
),
91+
'Location of additional AWS credential files from which to get '
92+
'secret keys from'
93+
)
5094
)
5195
args = parser.parse_args(argv)
52-
keys = get_your_keys(args.credentials_file)
96+
97+
credential_files = set(args.credential_files)
98+
99+
# Add the credentials files configured via environment variables to the set
100+
# of files to to gather AWS secrets from.
101+
credential_files |= get_aws_credential_files_from_env()
102+
103+
keys = set()
104+
for credential_file in credential_files:
105+
keys |= get_aws_secrets_from_file(credential_file)
106+
107+
# Secrets might be part of environment variables, so add such secrets to
108+
# the set of keys.
109+
keys |= get_aws_secrets_from_env()
110+
53111
if not keys:
54-
print(
55-
'No aws keys were configured at {0}\n'
56-
'Configure them with --credentials-file'.format(
57-
args.credentials_file,
58-
),
59-
)
112+
print('No AWS keys were found in the configured credential files and '
113+
'environment variables.\nPlease ensure you have the correct '
114+
'setting for --credentials-file')
60115
return 2
61116

62117
bad_filenames = check_file_for_aws_keys(args.filenames, keys)
63118
if bad_filenames:
64119
for bad_file in bad_filenames:
65-
print('AWS secret key found: {0}'.format(bad_file))
120+
print('AWS secret found in {filename}: {key}'.format(
121+
**bad_file))
66122
return 1
67123
else:
68124
return 0

testing/resources/sample_aws_credentials renamed to testing/resources/aws_config_with_multiple_sections.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# this is an aws credentials configuration file. obviously not real credentials :P
1+
# file with AWS access key ids, AWS secret access keys and AWS session tokens in multiple sections
22
[default]
33
aws_access_key_id = AKIASLARTIBARTFAST11
44
aws_secret_access_key = 7xebzorgm5143ouge9gvepxb2z70bsb2rtrh099e
@@ -8,3 +8,5 @@ aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
88
[staging]
99
aws_access_key_id = AKIAJIMMINYCRICKET0A
1010
aws_secret_access_key = ixswosj8gz3wuik405jl9k3vdajsnxfhnpui38ez
11+
[test]
12+
aws_session_token = foo
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#file with a secret key, you'll notice it is a section of sample_aws_credentials
2-
1+
# file with an AWS access key id and an AWS secret access key
32
[production]
43
aws_access_key_id = AKIAVOGONSVOGONS0042
54
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# file with an AWS access key id, an AWS secret access key and an AWS session token
2+
[production]
3+
aws_access_key_id = AKIAVOGONSVOGONS0042
4+
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
5+
aws_session_token = foo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# file with an AWS session token
2+
[production]
3+
aws_session_token = foo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# file with an AWS access key id but no AWS secret access key
2+
[production]
3+
aws_access_key_id = AKIASLARTIBARTFAST11

testing/resources/with_no_secrets.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,101 @@
11
import pytest
22

3+
from pre_commit_hooks.detect_aws_credentials import get_aws_credential_files_from_env
4+
from pre_commit_hooks.detect_aws_credentials import get_aws_secrets_from_env
5+
from pre_commit_hooks.detect_aws_credentials import get_aws_secrets_from_file
36
from pre_commit_hooks.detect_aws_credentials import main
47
from testing.util import get_resource_path
58

69

10+
def test_get_aws_credentials_file_from_env(monkeypatch):
11+
"""Test that reading credential files names from environment variables works."""
12+
monkeypatch.delenv('AWS_CREDENTIAL_FILE', raising=False)
13+
monkeypatch.delenv('AWS_SHARED_CREDENTIALS_FILE', raising=False)
14+
monkeypatch.delenv('BOTO_CONFIG', raising=False)
15+
assert get_aws_credential_files_from_env() == set()
16+
monkeypatch.setenv('AWS_CREDENTIAL_FILE', '/foo')
17+
assert get_aws_credential_files_from_env() == {'/foo'}
18+
monkeypatch.setenv('AWS_SHARED_CREDENTIALS_FILE', '/bar')
19+
assert get_aws_credential_files_from_env() == {'/foo', '/bar'}
20+
monkeypatch.setenv('BOTO_CONFIG', '/baz')
21+
assert get_aws_credential_files_from_env() == {'/foo', '/bar', '/baz'}
22+
monkeypatch.setenv('AWS_DUMMY_KEY', 'foobar')
23+
assert get_aws_credential_files_from_env() == {'/foo', '/bar', '/baz'}
24+
25+
26+
def test_get_aws_secrets_from_env(monkeypatch):
27+
"""Test that reading secrets from environment variables works."""
28+
monkeypatch.delenv('AWS_SECRET_ACCESS_KEY', raising=False)
29+
monkeypatch.delenv('AWS_SESSION_TOKEN', raising=False)
30+
assert get_aws_secrets_from_env() == set()
31+
monkeypatch.setenv('AWS_SECRET_ACCESS_KEY', 'foo')
32+
assert get_aws_secrets_from_env() == {'foo'}
33+
monkeypatch.setenv('AWS_SESSION_TOKEN', 'bar')
34+
assert get_aws_secrets_from_env() == {'foo', 'bar'}
35+
monkeypatch.setenv('AWS_SECURITY_TOKEN', 'baz')
36+
assert get_aws_secrets_from_env() == {'foo', 'bar', 'baz'}
37+
monkeypatch.setenv('AWS_DUMMY_KEY', 'baz')
38+
assert get_aws_secrets_from_env() == {'foo', 'bar', 'baz'}
39+
40+
41+
@pytest.mark.parametrize(('filename', 'expected_keys'), (
42+
('aws_config_with_secret.ini', {
43+
'z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb'}),
44+
('aws_config_with_session_token.ini', {'foo'}),
45+
('aws_config_with_secret_and_session_token.ini',
46+
{'z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb', 'foo'}),
47+
('aws_config_with_multiple_sections.ini', {
48+
'7xebzorgm5143ouge9gvepxb2z70bsb2rtrh099e',
49+
'z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb',
50+
'ixswosj8gz3wuik405jl9k3vdajsnxfhnpui38ez',
51+
'foo'}),
52+
('aws_config_without_secrets.ini', set()),
53+
('nonsense.txt', set()),
54+
('ok_json.json', set()),
55+
))
56+
def test_get_aws_secrets_from_file(filename, expected_keys):
57+
"""Test that reading secrets from files works."""
58+
keys = get_aws_secrets_from_file(get_resource_path(filename))
59+
assert keys == expected_keys
60+
61+
762
# Input filename, expected return value
863
TESTS = (
9-
('with_no_secrets.txt', 0),
10-
('with_secrets.txt', 1),
64+
('aws_config_with_secret.ini', 1),
65+
('aws_config_with_session_token.ini', 1),
66+
('aws_config_with_multiple_sections.ini', 1),
67+
('aws_config_without_secrets.ini', 0),
1168
('nonsense.txt', 0),
1269
('ok_json.json', 0),
1370
)
1471

1572

1673
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
1774
def test_detect_aws_credentials(filename, expected_retval):
75+
"""Test if getting configured AWS secrets from files to be checked in works."""
76+
1877
# with a valid credentials file
1978
ret = main((
2079
get_resource_path(filename),
21-
"--credentials-file=testing/resources/sample_aws_credentials",
80+
"--credentials-file=testing/resources/aws_config_with_multiple_sections.ini",
2281
))
2382
assert ret == expected_retval
2483

2584

26-
def test_non_existent_credentials(capsys):
27-
# with a non-existent credentials file
85+
def test_non_existent_credentials(capsys, monkeypatch):
86+
"""Test behavior with no configured AWS secrets."""
87+
monkeypatch.setattr(
88+
'pre_commit_hooks.detect_aws_credentials.get_aws_secrets_from_env',
89+
lambda: set())
90+
monkeypatch.setattr(
91+
'pre_commit_hooks.detect_aws_credentials.get_aws_secrets_from_file',
92+
lambda x: set())
2893
ret = main((
29-
get_resource_path('with_secrets.txt'),
94+
get_resource_path('aws_config_without_secrets.ini'),
3095
"--credentials-file=testing/resources/credentailsfilethatdoesntexist"
3196
))
3297
assert ret == 2
3398
out, _ = capsys.readouterr()
34-
assert out == (
35-
'No aws keys were configured at '
36-
'testing/resources/credentailsfilethatdoesntexist\n'
37-
'Configure them with --credentials-file\n'
38-
)
99+
assert out == ('No AWS keys were found in the configured credential files '
100+
'and environment variables.\nPlease ensure you have the '
101+
'correct setting for --credentials-file\n')

0 commit comments

Comments
 (0)