Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions aws_lambda_powertools/utilities/data_classes/sns_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ 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:
"""The time (GMT) when the subscription confirmation was sent."""
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:
Expand All @@ -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:
Expand All @@ -71,17 +79,20 @@ 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:
"""The Amazon Resource Name (ARN) for the topic that this endpoint is subscribed to."""
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):
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/data_classes/required_dependencies/test_sqs_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}})

Expand Down