Skip to content

Commit 4d360db

Browse files
committed
Refactor AWS verification to enable reuse for owner resolution (Yelp#189)
* Refactor AWS verification to enable reuse for owner resolution Follow up of git-defenders/detect-secrets-stream#182 * Revert changes to tox.ini * Fix coverage issue
1 parent 753d001 commit 4d360db

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

detect_secrets/plugins/aws.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ def get_secret_access_keys(content):
5151
]
5252

5353

54-
def verify_aws_secret_access_key(key, secret): # pragma: no cover
54+
def verify_aws_secret_access_key(key, secret):
55+
response = get_caller_info(key, secret)
56+
if response.status_code == 403:
57+
return False
58+
59+
return True
60+
61+
62+
def get_caller_info(key, secret): # pragma: no cover
5563
"""
5664
Using requests, because we don't want to require boto3 for this one
5765
optional verification step.
@@ -170,10 +178,7 @@ def verify_aws_secret_access_key(key, secret): # pragma: no cover
170178
data=body,
171179
)
172180

173-
if response.status_code == 403:
174-
return False
175-
176-
return True
181+
return response
177182

178183

179184
def _sign(key, message, hex=False): # pragma: no cover

tests/plugins/aws_key_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from detect_secrets.core.constants import VerifiedResult
77
from detect_secrets.core.potential_secret import PotentialSecret
88
from detect_secrets.plugins.aws import AWSKeyDetector
9-
from detect_secrets.plugins.aws import get_secret_access_keys
9+
from detect_secrets.plugins.aws import get_secret_access_key
10+
from detect_secrets.plugins.aws import verify_aws_secret_access_key
1011
from testing.mocks import mock_file_object
1112

1213

@@ -101,6 +102,18 @@ def counter(*args, **kwargs):
101102
) == VerifiedResult.VERIFIED_TRUE
102103
assert potential_secret.other_factors['secret_access_key'] == EXAMPLE_SECRET
103104

105+
@mock.patch('detect_secrets.plugins.aws.get_caller_info')
106+
def test_verify_aws_secret_access_key_valid(self, mock_get_caller_info):
107+
mock_get_caller_info.return_value = mock.MagicMock(status_code=200)
108+
result = verify_aws_secret_access_key('test-access-key', 'test-secret-access-key')
109+
assert result is True
110+
111+
@mock.patch('detect_secrets.plugins.aws.get_caller_info')
112+
def test_verify_aws_secret_access_key_invalid(self, mock_get_caller_info):
113+
mock_get_caller_info.return_value = mock.MagicMock(status_code=403)
114+
result = verify_aws_secret_access_key('test-access-key', 'test-secret-access-key')
115+
assert result is False
116+
104117

105118
@pytest.mark.parametrize(
106119
'content, expected_output',

0 commit comments

Comments
 (0)