diff --git a/aws_lambda_powertools/utilities/data_classes/sns_event.py b/aws_lambda_powertools/utilities/data_classes/sns_event.py index 828b6e18264..47e9322c433 100644 --- a/aws_lambda_powertools/utilities/data_classes/sns_event.py +++ b/aws_lambda_powertools/utilities/data_classes/sns_event.py @@ -23,9 +23,11 @@ def value(self) -> str: class SNSMessage(DictWrapper): @property - def signature_version(self) -> str: - """Version of the Amazon SNS signature used.""" - return self["SignatureVersion"] + def signature_version(self) -> str | None: + """Version of the Amazon SNS signature used. + + None when the message has no signature, e.g. for FIFO topics with content-based deduplication.""" + return self.get("SignatureVersion") @property def timestamp(self) -> str: @@ -33,14 +35,19 @@ def timestamp(self) -> str: return self["Timestamp"] @property - def signature(self) -> str: - """Base64-encoded "SHA1withRSA" signature of the Message, MessageId, Type, Timestamp, and TopicArn values.""" - return self["Signature"] + def signature(self) -> str | None: + """Base64-encoded "SHA1withRSA" signature of the Message, MessageId, Type, Timestamp, and TopicArn values. + + None when the message has no signature, e.g. for FIFO topics with content-based deduplication.""" + return self.get("Signature") @property - def signing_cert_url(self) -> str: - """The URL to the certificate that was used to sign the message.""" - return self["SigningCertUrl"] + def signing_cert_url(self) -> str | None: + """The URL to the certificate that was used to sign the message. + + None when the message has no signature, e.g. for FIFO topics with content-based deduplication.""" + # SNS sends this key as SigningCertURL when it delivers the message to SQS + return self.get("SigningCertUrl") or self.get("SigningCertURL") @property def message_id(self) -> str: @@ -56,7 +63,8 @@ def message(self) -> str: @property def message_attributes(self) -> dict[str, SNSMessageAttribute]: - return {k: SNSMessageAttribute(v) for (k, v) in self["MessageAttributes"].items()} + """The message attributes. Empty when the message was delivered to SQS without any.""" + return {k: SNSMessageAttribute(v) for (k, v) in (self.get("MessageAttributes") or {}).items()} @property def get_type(self) -> str: @@ -71,7 +79,8 @@ def unsubscribe_url(self) -> str: """A URL that you can use to unsubscribe the endpoint from this topic. If you visit this URL, Amazon SNS unsubscribes the endpoint and stops sending notifications to this endpoint.""" - return self["UnsubscribeUrl"] + # SNS sends this key as UnsubscribeURL when it delivers the message to SQS + return self.get("UnsubscribeUrl") or self["UnsubscribeURL"] @property def topic_arn(self) -> str: @@ -79,9 +88,11 @@ def topic_arn(self) -> str: return self["TopicArn"] @property - def subject(self) -> str: - """The Subject parameter specified when the notification was published to the topic.""" - return self["Subject"] + def subject(self) -> str | None: + """The Subject parameter specified when the notification was published to the topic. + + None when the notification was published without a subject.""" + return self.get("Subject") class SNSEventRecord(DictWrapper): diff --git a/tests/unit/data_classes/required_dependencies/test_sqs_event.py b/tests/unit/data_classes/required_dependencies/test_sqs_event.py index 7ac42093988..9cebbc4bb1d 100644 --- a/tests/unit/data_classes/required_dependencies/test_sqs_event.py +++ b/tests/unit/data_classes/required_dependencies/test_sqs_event.py @@ -166,6 +166,41 @@ def test_decode_nested_sns_event(): assert message["username"] == raw_message["username"] +def test_decode_nested_sns_event_reads_sqs_payload_keys(): + # GIVEN an SNS message delivered to SQS, which uses UnsubscribeURL and SigningCertURL + # and has no Subject nor MessageAttributes + raw_event = load_event("snsSqsEvent.json") + raw_body = json.loads(raw_event["Records"][0]["body"]) + + # WHEN decoding the nested SNS message + sns_message: SNSMessage = next(SQSEvent(raw_event).records).decoded_nested_sns_event + + # THEN every property reads the SQS payload + assert sns_message.unsubscribe_url == raw_body["UnsubscribeURL"] + assert sns_message.signing_cert_url == raw_body["SigningCertURL"] + assert sns_message.signature == raw_body["Signature"] + assert sns_message.subject is None + assert sns_message.message_attributes == {} + + +def test_decode_nested_sns_event_from_fifo_topic(): + # GIVEN an SNS message from a FIFO topic delivered to SQS, which has no signature fields + raw_event = load_event("snsSqsFifoEvent.json") + raw_body = json.loads(raw_event["Records"][0]["body"]) + + # WHEN decoding the nested SNS message + sns_message: SNSMessage = next(SQSEvent(raw_event).records).decoded_nested_sns_event + + # THEN the signature fields are None and the other properties read the SQS payload + assert sns_message.signature is None + assert sns_message.signature_version is None + assert sns_message.signing_cert_url is None + assert sns_message.unsubscribe_url == raw_body["UnsubscribeURL"] + assert sns_message.message_id == raw_body["MessageId"] + assert sns_message.subject is None + assert sns_message.message_attributes == {} + + def test_sqs_event_typing(): attributes = SQSMessageAttributes({"key": {"stringValue": "value", "dataType": "String"}})