11import pytest
22from unittest import mock
3- from broadcast_alert import lambda_handler
3+ from broadcast_alert import lambda_handler , does_key_contain_all_zeros
44
55
66@mock .patch ("broadcast_alert.boto3.client" )
@@ -99,11 +99,47 @@ def test_lambda_handler_secret_detected_all_zeros(
9999 "cds_canada_notify_api_key" ,
100100 "https://github.com/cds-snc/some-repo" ,
101101 "commit" ,
102- )
102+ ),
103+ (
104+ "Secret detected: token='gcntfy-notify-test-key-11aa223-4455-6677-8899-aabbccddeeff' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/some-repo' source='commit'" ,
105+ "gcntfy-notify-test-key-11aa223-4455-6677-8899-aabbccddeeff" ,
106+ "cds_canada_notify_api_key" ,
107+ "https://github.com/cds-snc/some-repo" ,
108+ "commit" ,
109+ ),
110+ (
111+ "Secret detected: token='gcntfy-notify-secret-key-aaaabbb-cccc-dddd-eeee-56789abcdef0' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/some-repo' source='commit'" ,
112+ "gcntfy-notify-secret-key-aaaabbb-cccc-dddd-eeee-56789abcdef0" ,
113+ "cds_canada_notify_api_key" ,
114+ "https://github.com/cds-snc/some-repo" ,
115+ "commit" ,
116+ ),
117+ (
118+ "Secret detected: token='gcntfy-notify-access-key-abc123-def6-7890-ghij-klmnopqrstuv' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/some-repo' source='commit'" ,
119+ "gcntfy-notify-access-key-abc123-def6-7890-ghij-klmnopqrstuv" ,
120+ "cds_canada_notify_api_key" ,
121+ "https://github.com/cds-snc/some-repo" ,
122+ "commit" ,
123+ ),
124+ (
125+ "Secret detected: token='gcntfy-notify-api-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/some-repo' source='commit'" ,
126+ "gcntfy-notify-api-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e" ,
127+ "cds_canada_notify_api_key" ,
128+ "https://github.com/cds-snc/some-repo" ,
129+ "commit" ,
130+ ),
103131 ],
104132)
105133def test_lambda_handler_secret_detected_multiple_secrets (
106- mock_base64 , mock_gzip , mock_json_loads , mock_boto3_client , message , expected_token , expected_type , expected_url , expected_source
134+ mock_base64 ,
135+ mock_gzip ,
136+ mock_json_loads ,
137+ mock_boto3_client ,
138+ message ,
139+ expected_token ,
140+ expected_type ,
141+ expected_url ,
142+ expected_source ,
107143):
108144 mock_json_loads .return_value = {"logEvents" : [{"message" : message }]}
109145 event = {"awslogs" : {"data" : "foo" }}
@@ -112,9 +148,7 @@ def test_lambda_handler_secret_detected_multiple_secrets(
112148 mock_boto3_client .assert_called_once_with ("sns" )
113149
114150 # Build the expected message body
115- expected_body = (
116- f"API Key with value token='{ expected_token } ', type='{ expected_type } ' and source='{ expected_source } ' has been detected in url='{ expected_url } '!"
117- )
151+ expected_body = f"API Key with value token='{ expected_token } ', type='{ expected_type } ' and source='{ expected_source } ' has been detected in url='{ expected_url } '!"
118152 # Ensure the publish method is called with the correct arguments
119153 mock_boto3_client .return_value .publish .assert_called_once_with (
120154 TargetArn = "fake_topic_arn" ,
@@ -138,11 +172,13 @@ def test_lambda_handler_secret_detected_multiple_secrets(
138172@pytest .mark .parametrize (
139173 "message" ,
140174 [
141- "Secret detected: token='gcntfy-some-test-key-00000' type='cds_canada_notify_api_key' url='https://example.com/cds-snc/some-repo' source='commit'" ,
142- "Secret detected: token='gcntfy-some-test-key-00000' type='cds_canada_notify_api_key' url='https://github.com/dsp-testing/some-repo' source='commit'" ,
143- "Secret detected: token='gcntfy-some-test-key-00000' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/notification-documentation' source='commit'" ,
175+ "Secret detected: token='gcntfy-some-test-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://example.com/cds-snc/some-repo' source='commit'" ,
176+ "Secret detected: token='gcntfy-some-test-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://github.com/dsp-testing/some-repo' source='commit'" ,
177+ "Secret detected: token='gcntfy-some-test-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/notification-documentation' source='commit'" ,
178+ "Secret detected: token='gcntfy-some-test-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://github.com/cds-snc/notification-documentation' source='commit'" ,
144179 "Secret detected: token='gcntfy-github-test-revoked' type='cds_canada_notify_api_key' url='https://example.com/cds-snc/some-repo' source='commit'" ,
145- "Secret detected: token='gcntfy-notify-test-key-11111' type='cds_canada_notify_api_key' url='https://whatever.com/cds-snc/some-repo' source='commit'" ,
180+ "Secret detected: token='gcntfy-notify-test-key-0a0a0a0-1b1b-2c2c-3d3d-4e4e4e4e4e4e' type='cds_canada_notify_api_key' url='https://github.com/dry-runs-test/some-repo' source='commit'" ,
181+ "Secret detected: token='gcntfy-some-test-key-0000000-0000-0000-0000-000000000000' type='cds_canada_notify_api_key' url='https://whatever.com/cds-snc/some-repo' source='commit'" ,
146182 ],
147183)
148184def test_lambda_handler_secret_ignored (
@@ -152,3 +188,20 @@ def test_lambda_handler_secret_ignored(
152188 event = {"awslogs" : {"data" : "foo" }}
153189 lambda_handler (event , None )
154190 mock_boto3_client .assert_not_called ()
191+
192+
193+ @pytest .mark .parametrize (
194+ "key,expected" ,
195+ [
196+ # Alphanumeric in numeric positions (should be False)
197+ ("gcntfy-some-test-key-0000a00-0000-0000-0000-000000000000" , False ),
198+ ("gcntfy-some-test-key-0000000-00b0-0000-0000-000000000000" , False ),
199+ ("gcntfy-some-test-key-0000000-0000-0000-0000-00000000c000" , False ),
200+ ("gcntfy-some-test-key-1200000-0000-0000-0000-000000000000" , False ),
201+ ("gcntfy-some-test-key-aaaaaaa-bbbb-cccc-dddd-e00000000000" , False ),
202+ # All numeric and zeros (should be True)
203+ ("gcntfy-some-test-key-0000000-0000-0000-0000-000000000000" , True ),
204+ ],
205+ )
206+ def test_does_key_contain_all_zeros_alphanumeric (key , expected ):
207+ assert does_key_contain_all_zeros (key ) == expected
0 commit comments