-
-
Notifications
You must be signed in to change notification settings - Fork 349
fix: Fix parsing of non-JSON messages #265
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -574,7 +574,7 @@ def format_default( | |
| return attachments | ||
|
|
||
|
|
||
| def parse_notification(message: Dict[str, Any], subject: Optional[str], region: str) -> Optional[Dict]: | ||
| def parse_notification(message: Union[str, Dict[str, Any]], subject: Optional[str], region: str) -> Dict[str, Any]: | ||
| """ | ||
| Parse notification message and format into Slack message payload | ||
|
|
||
|
|
@@ -583,7 +583,7 @@ def parse_notification(message: Dict[str, Any], subject: Optional[str], region: | |
| :params region: AWS region where the event originated from | ||
| :returns: Slack message payload | ||
| """ | ||
| if "AlarmName" in message: | ||
| if isinstance(message, Dict) and "AlarmName" in message: | ||
| return format_cloudwatch_alarm(message=message, region=region) | ||
| if isinstance(message, Dict) and message.get("detail-type") == "GuardDuty Finding": | ||
| return format_guardduty_finding(message=message, region=message["region"]) | ||
|
|
@@ -619,23 +619,18 @@ def get_slack_message_payload( | |
| "username": slack_username, | ||
| "icon_emoji": slack_emoji, | ||
| } | ||
| attachment = None | ||
|
|
||
| if isinstance(message, str): | ||
| try: | ||
| message = json.loads(message) | ||
| except json.JSONDecodeError: | ||
| logging.info("Not a structured payload, just a string message") | ||
|
|
||
| message = cast(Dict[str, Any], message) | ||
|
|
||
| if "attachments" in message or "text" in message: | ||
| if isinstance(message, Dict) and ("attachments" in message or "text" in message): | ||
| message = cast(Dict[str, Any], message) | ||
| payload = {**payload, **message} | ||
| else: | ||
| attachment = parse_notification(message, subject, region) | ||
|
|
||
| if attachment: | ||
| payload["attachments"] = [attachment] # type: ignore | ||
| payload["attachments"] = [parse_notification(message, subject, region)] | ||
|
|
||
| return payload | ||
|
Comment on lines
623
to
640
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of the bug. Imagine this "Hi this is a message about context-switching."In the original code, the So after the And then in the next line we were doing $ python
>>> "text" in "Hi this is a message about context-switching."
TrueI'm pretty sure this isn't what was intended. |
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
format_cloudwatch_alarm()is assuming it is receiving aDict, so I believe adding theisinstance(message, Dict)check here is correct.