Skip to content

Commit 02e8bdc

Browse files
add tests, test sample files and minor refactor of exit codes in actual hook in order to facilitate testing
1 parent 8872550 commit 02e8bdc

6 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎pre_commit_hooks/detect_aws_credentials.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import os
6+
67
from six.moves import configparser
78

89

@@ -12,7 +13,7 @@ def get_your_keys(credentials_file):
1213
"""
1314
aws_credentials_file_path = os.path.expanduser(credentials_file)
1415
if not os.path.exists(aws_credentials_file_path):
15-
exit(2)
16+
return None
1617

1718
parser = configparser.ConfigParser()
1819
parser.read(aws_credentials_file_path)
@@ -37,13 +38,15 @@ def main(argv=None):
3738
parser = argparse.ArgumentParser()
3839
parser.add_argument('filenames', nargs='*', help='Filenames to run')
3940
parser.add_argument(
40-
"--credentials-file",
41+
"--credentials-file",
4142
default='~/.aws/credentials',
4243
help="location of aws credentials file from which to get the secret "
4344
"keys we're looking for",
4445
)
4546
args = parser.parse_args(argv)
4647
keys = get_your_keys(args.credentials_file)
48+
if not keys:
49+
return 2
4750

4851
retv = 0
4952
for filename in args.filenames:

‎testing/resources/nonsense.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
some nonsense text generated at https://baconipsum.com/
2+
Bacon ipsum dolor amet ipsum fugiat pastrami pork belly, non ball tip flank est short loin. Fatback landjaeger meatloaf flank. Sunt boudin duis occaecat mollit velit. Capicola lorem frankfurter doner strip steak jerky rump elit laborum mollit. Venison cupidatat laboris duis ut chuck proident mollit. Minim do rump, eu jerky ham turkey chuck in tempor venison pariatur voluptate landjaeger beef.
3+
4+
Duis aliqua esse, exercitation in ball tip ut capicola sausage dolore frankfurter occaecat. Duis in nulla consequat salami. Est shoulder tempor commodo shankle short ribs. In meatball aliqua boudin tenderloin, meatloaf leberkas hamburger quis pig dolore ea eu. Ham hock ex laboris, filet mignon sunt doner cillum short loin prosciutto voluptate.
5+
6+
Occaecat pork doner meatloaf nulla biltong ullamco tenderloin culpa brisket. Culpa jowl ea shank t-bone shankle voluptate nostrud incididunt leberkas pork loin. Bacon kevin jerky pork belly t-bone labore duis. Boudin corned beef adipisicing aute, fatback ribeye nulla pancetta anim venison. Short ribs kevin pastrami cow drumstick velit. Turkey exercitation jowl, fatback labore swine do voluptate.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# this is an aws credentials configuration file. obviously not real credentials :P
2+
[default]
3+
aws_access_key_id = AKIASLARTIBARTFAST11
4+
aws_secret_access_key = 7xebzorgm5143ouge9gvepxb2z70bsb2rtrh099e
5+
[production]
6+
aws_access_key_id = AKIAVOGONSVOGONS0042
7+
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
8+
[staging]
9+
aws_access_key_id = AKIAJIMMINYCRICKET0A
10+
aws_secret_access_key = ixswosj8gz3wuik405jl9k3vdajsnxfhnpui38ez
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# file with an access key but no secrets
2+
# you'll notice it is a redacted section of sample_aws_credentials
3+
4+
[production]
5+
aws_access_key_id = AKIASLARTIBARTFAST11

‎testing/resources/with_secrets.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#file with a secret key, you'll notice it is a section of sample_aws_credentials
2+
3+
[production]
4+
aws_access_key_id = AKIAVOGONSVOGONS0042
5+
aws_secret_access_key = z2rpgs5uit782eapz5l1z0y2lurtsyyk6hcfozlb
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from pre_commit_hooks.detect_aws_credentials import main
4+
from testing.util import get_resource_path
5+
6+
7+
# Input filename, expected return value
8+
TESTS = (
9+
('with_no_secrets.txt', 0),
10+
('with_secrets.txt', 1),
11+
('nonsense.txt', 0),
12+
('ok_json.json', 0),
13+
)
14+
15+
NO_CREDENTIALS_TEST = (
16+
('with_secrets.txt', 2),
17+
)
18+
19+
20+
@pytest.mark.parametrize(('filename', 'expected_retval'), TESTS)
21+
def test_detect_aws_credentials(filename, expected_retval):
22+
# with a valid credentials file
23+
ret = main(
24+
[get_resource_path(filename), "--credentials-file=testing/resources/sample_aws_credentials"]
25+
)
26+
assert ret == expected_retval
27+
28+
29+
@pytest.mark.parametrize(('filename', 'expected_retval'), NO_CREDENTIALS_TEST)
30+
def test_non_existent_credentials(filename, expected_retval):
31+
# with a non-existent credentials file
32+
ret = main(
33+
[get_resource_path(filename), "--credentials-file=testing/resources/credentailsfilethatdoesntexist"]
34+
)
35+
assert ret == expected_retval

0 commit comments

Comments
 (0)