Skip to content

Commit abb8043

Browse files
authored
fix(parser): loose validation on SNS fields to support FIFO (#1606)
1 parent e2d4744 commit abb8043

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

Diff for: aws_lambda_powertools/utilities/parser/models/sns.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class SnsNotificationModel(BaseModel):
2222
MessageAttributes: Optional[Dict[str, SnsMsgAttributeModel]]
2323
Message: Union[str, TypingType[BaseModel]]
2424
MessageId: str
25-
SigningCertUrl: HttpUrl
26-
Signature: str
25+
SigningCertUrl: Optional[HttpUrl] # NOTE: FIFO opt-in removes attribute
26+
Signature: Optional[str] # NOTE: FIFO opt-in removes attribute
2727
Timestamp: datetime
28-
SignatureVersion: str
28+
SignatureVersion: Optional[str] # NOTE: FIFO opt-in removes attribute
2929

3030
@root_validator(pre=True, allow_reuse=True)
3131
def check_sqs_protocol(cls, values):

Diff for: tests/events/snsSqsFifoEvent.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "69bc4bbd-ed69-4325-a434-85c3b428ceab",
5+
"receiptHandle": "AQEBbfAqjhrgIdW3HGWYPz57mdDatG/dT9LZhRPAsNQ1pJmw495w4esDc8ZSbOwMZuPBol7wtiNWug8U25GpSQDDLY1qv//8/lfmdzXOiprG6xRVeiXSHj0j731rJQ3xo+GPdGjOzjIxI09CrE3HtZ4lpXY9NjjHzP8hdxkCLlbttumc8hDBUR365/Tk+GfV2nNP9qvZtLGEbKCdTm/GYdTSoAr+ML9HnnGrS9T25Md71ASiZMI4DZqptN6g7CYYojFPs1LVM9o1258ferA72zbNoQ==",
6+
"body": "{\n \"Type\" : \"Notification\",\n \"MessageId\" : \"a7c9d2fa-77fa-5184-9de9-89391027cc7d\",\n \"SequenceNumber\" : \"10000000000000004000\",\n \"TopicArn\" : \"arn:aws:sns:eu-west-1:231436140809:Test.fifo\",\n \"Message\" : \"{\\\"message\\\": \\\"hello world\\\", \\\"username\\\": \\\"lessa\\\"}\",\n \"Timestamp\" : \"2022-10-14T13:35:25.419Z\",\n \"UnsubscribeURL\" : \"https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:231436140809:Test.fifo:bb81d3de-a0f9-46e4-b619-d3152a4d545f\"\n}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1665754525442",
10+
"SequenceNumber": "18873177232222703872",
11+
"MessageGroupId": "powertools-test",
12+
"SenderId": "AIDAWYJAWPFU7SUQGUJC6",
13+
"MessageDeduplicationId": "4e0a0f61eed277a4b9e4c01d5722b07b0725e42fe782102abee5711adfac701f",
14+
"ApproximateFirstReceiveTimestamp": "1665754525442"
15+
},
16+
"messageAttributes": {},
17+
"md5OfBody": "f3c788e623445e3feb263e80c1bffc0b",
18+
"eventSource": "aws:sqs",
19+
"eventSourceARN": "arn:aws:sqs:eu-west-1:231436140809:Test.fifo",
20+
"awsRegion": "eu-west-1"
21+
}
22+
]
23+
}

Diff for: tests/functional/parser/test_sns.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,6 @@ def test_handle_sns_sqs_trigger_event_json_body(): # noqa: F811
110110
handle_sns_sqs_json_body(event_dict, LambdaContext())
111111

112112

113-
def test_handle_sns_sqs_trigger_event_json_body_missing_signing_cert_url():
114-
# GIVEN an event is tampered with a missing SigningCertURL
115-
event_dict = load_event("snsSqsEvent.json")
116-
payload = json.loads(event_dict["Records"][0]["body"])
117-
payload.pop("SigningCertURL")
118-
event_dict["Records"][0]["body"] = json.dumps(payload)
119-
120-
# WHEN parsing the payload
121-
# THEN raise a ValidationError error
122-
with pytest.raises(ValidationError):
123-
handle_sns_sqs_json_body(event_dict, LambdaContext())
124-
125-
126113
def test_handle_sns_sqs_trigger_event_json_body_missing_unsubscribe_url():
127114
# GIVEN an event is tampered with a missing UnsubscribeURL
128115
event_dict = load_event("snsSqsEvent.json")
@@ -134,3 +121,8 @@ def test_handle_sns_sqs_trigger_event_json_body_missing_unsubscribe_url():
134121
# THEN raise a ValidationError error
135122
with pytest.raises(ValidationError):
136123
handle_sns_sqs_json_body(event_dict, LambdaContext())
124+
125+
126+
def test_handle_sns_sqs_fifo_trigger_event_json_body():
127+
event_dict = load_event("snsSqsFifoEvent.json")
128+
handle_sns_sqs_json_body(event_dict, LambdaContext())

0 commit comments

Comments
 (0)