Skip to content

Commit 72e3d61

Browse files
committed
fix: fix parsing of non-JSON messages
This commit fixes the parsing of messages that aren't JSON-formatted. In specific, any non-JSON-formatted message that happened to contain the string `"attachments"` or `"text"` was incorrectly treated as a dictionary, and failed to parse. This commit also makes the typing a little stricter.
1 parent 4643a62 commit 72e3d61

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

functions/messages/text_message.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"MessageId": "f86e3c5b-cd17-1ab8-80e9-c0776d4f1e7a",
99
"TopicArn": "arn:aws:sns:us-gov-west-1:123456789012:ExampleTopic",
1010
"Subject": "All Fine",
11-
"Message": "This\nis\na typical multi-line\nmessage from SNS!\n\nHave a ~good~ amazing day! :)",
11+
"Message": "This\nis\na typical multi-line\ntext-formatted message from SNS!\n\nHave a ~good~ amazing day! :)",
1212
"Timestamp": "2019-02-12T15:45:24.091Z",
1313
"SignatureVersion": "1",
1414
"Signature": "EXAMPLE",

functions/notify_slack.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def format_default(
574574
return attachments
575575

576576

577-
def parse_notification(message: Dict[str, Any], subject: Optional[str], region: str) -> Optional[Dict]:
577+
def parse_notification(message: Union[str, Dict[str, Any]], subject: Optional[str], region: str) -> Dict[str, Any]:
578578
"""
579579
Parse notification message and format into Slack message payload
580580
@@ -583,7 +583,7 @@ def parse_notification(message: Dict[str, Any], subject: Optional[str], region:
583583
:params region: AWS region where the event originated from
584584
:returns: Slack message payload
585585
"""
586-
if "AlarmName" in message:
586+
if isinstance(message, Dict) and "AlarmName" in message:
587587
return format_cloudwatch_alarm(message=message, region=region)
588588
if isinstance(message, Dict) and message.get("detail-type") == "GuardDuty Finding":
589589
return format_guardduty_finding(message=message, region=message["region"])
@@ -619,23 +619,18 @@ def get_slack_message_payload(
619619
"username": slack_username,
620620
"icon_emoji": slack_emoji,
621621
}
622-
attachment = None
623622

624623
if isinstance(message, str):
625624
try:
626625
message = json.loads(message)
627626
except json.JSONDecodeError:
628627
logging.info("Not a structured payload, just a string message")
629628

630-
message = cast(Dict[str, Any], message)
631-
632-
if "attachments" in message or "text" in message:
629+
if isinstance(message, Dict) and ("attachments" in message or "text" in message):
630+
message = cast(Dict[str, Any], message)
633631
payload = {**payload, **message}
634632
else:
635-
attachment = parse_notification(message, subject, region)
636-
637-
if attachment:
638-
payload["attachments"] = [attachment] # type: ignore
633+
payload["attachments"] = [parse_notification(message, subject, region)]
639634

640635
return payload
641636

functions/snapshots/snap_notify_slack_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@
645645
'value': '''This
646646
is
647647
a typical multi-line
648-
message from SNS!
648+
text-formatted message from SNS!
649649
650650
Have a ~good~ amazing day! :)'''
651651
}

0 commit comments

Comments
 (0)