generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/alert notify leaked key #299
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,81 @@ def test_format_new_iam_user_extracts_the_user_and_inserts_it_into_blocks(): | |
assert "[email protected]" in response[2]["text"]["text"] | ||
|
||
|
||
@patch("server.event_handlers.aws.format_api_key_detected") | ||
def test_parse_returns_blocks_if_api_key_detected(format_api_key_detected_mock): | ||
# Test that the parse function returns the blocks returned by format_api_key_detected | ||
client = MagicMock() | ||
format_api_key_detected_mock.return_value = ["foo", "bar"] | ||
payload = mock_api_key_detected() | ||
response = aws.parse(payload, client) | ||
assert response == ["foo", "bar"] | ||
format_api_key_detected_mock.assert_called_once_with(payload, client) | ||
|
||
|
||
@patch("server.event_handlers.aws.alert_on_call") | ||
def test_format_api_key_detected_extracts_the_api_key_and_inserts_it_into_blocks( | ||
alert_on_call_mock, | ||
): | ||
# Test that the format_api_key_detected function extracts the api key properly | ||
client = MagicMock() | ||
payload = mock_api_key_detected() | ||
response = aws.format_api_key_detected(payload, client) | ||
assert "gcntfy-api-key-blah" in response[2]["text"]["text"] | ||
|
||
|
||
@patch("server.event_handlers.aws.alert_on_call") | ||
def test_format_api_key_detected_extracts_the_url_and_inserts_it_into_blocks( | ||
alert_on_call_mock, | ||
): | ||
# Test that the format_api_key_detected function extracts the url properly | ||
client = MagicMock() | ||
payload = mock_api_key_detected() | ||
response = aws.format_api_key_detected(payload, client) | ||
assert "https://github.com/blah" in response[2]["text"]["text"] | ||
|
||
|
||
@patch("server.event_handlers.aws.alert_on_call") | ||
def test_format_api_key_detected_extracts_the_on_call_message_and_inserts_it_into_blocks( | ||
alert_on_call_mock, | ||
): | ||
# Test that the format_api_key_detected function extracts the on call message properly | ||
client = MagicMock() | ||
alert_on_call_mock.return_value = "test message" | ||
payload = mock_api_key_detected() | ||
response = aws.format_api_key_detected(payload, client) | ||
assert "test message" in response[3]["text"]["text"] | ||
|
||
|
||
@patch("integrations.google_drive.get_google_service") | ||
@patch("commands.incident.google_drive.list_folders") | ||
@patch("commands.incident.google_drive.list_metadata") | ||
@patch("integrations.opsgenie.get_on_call_users") | ||
def test_alert_on_call_returns_message( | ||
get_on_call_users_mock, | ||
list_metadata_mock, | ||
google_list_folders_mock, | ||
get_google_service_mock, | ||
): | ||
# Test that the alert_on_call function returns the proper message | ||
client = MagicMock() | ||
product = "test" | ||
api_key = "test_api_key" | ||
github_repo = "test_repo" | ||
google_list_folders_mock.return_value = [ | ||
{ | ||
"name": "Notify", | ||
"id": 12345, | ||
"appProperties": {"genie_schedule": "test_schedule"}, | ||
} | ||
] | ||
list_metadata_mock.return_value = { | ||
"name": "Notify", | ||
"appProperties": {"genie_schedule": "test_schedule"}, | ||
} | ||
response = aws.alert_on_call(product, client, api_key, github_repo) | ||
assert "test on-call staff have been notified" in response | ||
|
||
|
||
def mock_abuse_alert(): | ||
return MagicMock( | ||
Type="Notification", | ||
|
@@ -253,3 +328,19 @@ def mock_new_iam_user(): | |
SigningCertURL="https://sns.ca-central-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem", | ||
UnsubscribeURL="https://sns.ca-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ca-central-1:412578375350:test-sre-bot:4636a013-5224-4207-91b2-d6d7c7ab7ea7", | ||
) | ||
|
||
|
||
# Mock the message returned from AWS when a API key has been compromised | ||
def mock_api_key_detected(): | ||
return MagicMock( | ||
Type="Notification", | ||
MessageId="1e5f5647g-adb5-5d6f-ab5e-c2e508881361", | ||
TopicArn="arn:aws:sns:ca-central-1:412578375350:test-sre-bot", | ||
Subject="API Key detected", | ||
Message="API Key with value token='gcntfy-api-key-blah' has been detected in url='https://github.com/blah'! This key needs to be revoked asap.", | ||
Timestamp="2023-09-25T20:50:37.868Z", | ||
SignatureVersion="1", | ||
Signature="EXAMPLEO0OA1HN4MIHrtym3N6SWqvotsY4EcG+Ty/wrfZcxpQ3mximWM7ZfoYlzZ8NBh4s1XTPuqbl5efK64TEuPgNWBMKsm5Gc2d8H6hoDpLqAOELGl2/xlvWf2CovLH/KPj8xrSwAgOS9jL4r/EEMdXYb705YMMBudu78gooatU9EpVl+1I2MCP2AW0ZJWrcSwYMqxo9yo7H6coyBRlmTxP97PlELXoqXLfufsfFBjZ0eFycndG5A0YHeue82uLF5fIHGpcTjqNzLF0PXuJoS9xVkGx3X7p+dzmRE4rp/swGyKCqbXvgldPRycuj7GSk3r8HLSfzjqHyThnDqMECA==", | ||
SigningCertURL="https://sns.ca-central-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem", | ||
UnsubscribeURL="https://sns.ca-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ca-central-1:412578375350:test-sre-bot:4636a013-5224-4207-91b2-d6d7c7ab7ea7", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this is interesting - I didn't know you could do this for Google Drive