Skip to content

Commit d48acab

Browse files
committed
refactor: parsing of each notification into its own function
1 parent 0357c20 commit d48acab

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

functions/notify_slack.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,27 @@ def format_default(
503503
return attachments
504504

505505

506+
def parse_notification(message: Dict[str, Any], subject: Optional[str], region: str) -> Optional[Dict]:
507+
"""
508+
Parse notification message and format into Slack message payload
509+
510+
:params message: SNS message body notification payload
511+
:params subject: Optional subject line for Slack notification
512+
:params region: AWS region where the event originated from
513+
:returns: Slack message payload
514+
"""
515+
if "AlarmName" in message:
516+
return format_cloudwatch_alarm(message=message, region=region)
517+
if message.get("detail-type") == "GuardDuty Finding":
518+
return format_guardduty_finding(message=message, region=message["region"])
519+
if message.get("detail-type") == "Security Hub Findings - Imported":
520+
return format_aws_security_hub(message=message, region=message["region"])
521+
if message.get("detail-type") == "AWS Health Event":
522+
return format_aws_health(message=message, region=message["region"])
523+
if subject == "Notification from AWS Backup":
524+
return format_aws_backup(message=str(message))
525+
return format_default(message=message, subject=subject)
526+
506527
def get_slack_message_payload(
507528
message: Union[str, Dict], region: str, subject: Optional[str] = None
508529
) -> Dict:
@@ -534,35 +555,10 @@ def get_slack_message_payload(
534555

535556
message = cast(Dict[str, Any], message)
536557

537-
if "AlarmName" in message:
538-
notification = format_cloudwatch_alarm(message=message, region=region)
539-
attachment = notification
540-
541-
elif (
542-
isinstance(message, Dict) and message.get("detail-type") == "GuardDuty Finding"
543-
):
544-
notification = format_guardduty_finding(
545-
message=message, region=message["region"]
546-
)
547-
attachment = notification
548-
549-
elif isinstance(message, Dict) and message.get("detail-type") == "Security Hub Findings - Imported":
550-
notification = format_aws_security_hub(message=message, region=message["region"])
551-
attachment = notification
552-
553-
elif isinstance(message, Dict) and message.get("detail-type") == "AWS Health Event":
554-
notification = format_aws_health(message=message, region=message["region"])
555-
attachment = notification
556-
557-
elif subject == "Notification from AWS Backup":
558-
notification = format_aws_backup(message=str(message))
559-
attachment = notification
560-
561-
elif "attachments" in message or "text" in message:
558+
if "attachments" in message or "text" in message:
562559
payload = {**payload, **message}
563-
564560
else:
565-
attachment = format_default(message=message, subject=subject)
561+
attachment = parse_notification(message, subject, region)
566562

567563
if attachment:
568564
payload["attachments"] = [attachment] # type: ignore
@@ -602,7 +598,6 @@ def lambda_handler(event: Dict[str, Any], context: Dict[str, Any]) -> str:
602598
:param context: lambda expected context object
603599
:returns: none
604600
"""
605-
logging.warning(f"Event logging enabled: `{json.dumps(event)}`")
606601

607602
for record in event["Records"]:
608603
sns = record["Sns"]

0 commit comments

Comments
 (0)