Skip to content

Commit e17dc8f

Browse files
authored
chore: Fix KeyError when accessing PolicyDocument (#3522)
If an intrinsic is within the Function.Policies Property, the d["PolicyDocument"] will fail because the valid policy is backed within the FN:If. Instead, we access PolicyDocument through a .get() call, which allows us to handle the intrinsic case. Co-authored-by: Jacob Fuss <[email protected]>
1 parent d11dd12 commit e17dc8f

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

samtranslator/model/eventsources/pull.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def _link_policy(self, role, destination_config_policy=None): # type: ignore[no
233233
if role.Policies and destination_config_policy not in role.Policies:
234234
policy_document = destination_config_policy.get("PolicyDocument")
235235
# do not add the policy if the same policy document is already present
236-
if policy_document not in [d["PolicyDocument"] for d in role.Policies]:
236+
if policy_document not in [d.get("PolicyDocument", {}) for d in role.Policies]:
237237
role.Policies.append(destination_config_policy)
238238

239239
def _validate_filter_criteria(self) -> None:

tests/translator/input/function_with_event_dest_basic.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ Resources:
3838
Handler: index.handler
3939
Runtime: nodejs12.x
4040
MemorySize: 1024
41+
Policies:
42+
- Fn::If:
43+
- SomeCondition
44+
- Version: '2012-10-17'
45+
Statement:
46+
- Effect: Deny
47+
Action:
48+
- logs:CreateLogGroup
49+
- logs:CreateLogStream
50+
- logs:PutLogEvents
51+
Resource: arn:aws:logs:*:*:*
52+
- Ref: AWS::NoValue

tests/translator/output/aws-cn/function_with_event_dest_basic.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@
125125
]
126126
},
127127
"PolicyName": "MyTestFunctionEventInvokeConfigOnFailureSNSPolicy"
128+
},
129+
{
130+
"Fn::If": [
131+
"SomeCondition",
132+
{
133+
"PolicyDocument": {
134+
"Statement": [
135+
{
136+
"Action": [
137+
"logs:CreateLogGroup",
138+
"logs:CreateLogStream",
139+
"logs:PutLogEvents"
140+
],
141+
"Effect": "Deny",
142+
"Resource": "arn:aws:logs:*:*:*"
143+
}
144+
],
145+
"Version": "2012-10-17"
146+
},
147+
"PolicyName": "MyTestFunctionRolePolicy0"
148+
},
149+
{
150+
"Ref": "AWS::NoValue"
151+
}
152+
]
128153
}
129154
],
130155
"Tags": [

tests/translator/output/aws-us-gov/function_with_event_dest_basic.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@
125125
]
126126
},
127127
"PolicyName": "MyTestFunctionEventInvokeConfigOnFailureSNSPolicy"
128+
},
129+
{
130+
"Fn::If": [
131+
"SomeCondition",
132+
{
133+
"PolicyDocument": {
134+
"Statement": [
135+
{
136+
"Action": [
137+
"logs:CreateLogGroup",
138+
"logs:CreateLogStream",
139+
"logs:PutLogEvents"
140+
],
141+
"Effect": "Deny",
142+
"Resource": "arn:aws:logs:*:*:*"
143+
}
144+
],
145+
"Version": "2012-10-17"
146+
},
147+
"PolicyName": "MyTestFunctionRolePolicy0"
148+
},
149+
{
150+
"Ref": "AWS::NoValue"
151+
}
152+
]
128153
}
129154
],
130155
"Tags": [

tests/translator/output/function_with_event_dest_basic.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@
125125
]
126126
},
127127
"PolicyName": "MyTestFunctionEventInvokeConfigOnFailureSNSPolicy"
128+
},
129+
{
130+
"Fn::If": [
131+
"SomeCondition",
132+
{
133+
"PolicyDocument": {
134+
"Statement": [
135+
{
136+
"Action": [
137+
"logs:CreateLogGroup",
138+
"logs:CreateLogStream",
139+
"logs:PutLogEvents"
140+
],
141+
"Effect": "Deny",
142+
"Resource": "arn:aws:logs:*:*:*"
143+
}
144+
],
145+
"Version": "2012-10-17"
146+
},
147+
"PolicyName": "MyTestFunctionRolePolicy0"
148+
},
149+
{
150+
"Ref": "AWS::NoValue"
151+
}
152+
]
128153
}
129154
],
130155
"Tags": [

0 commit comments

Comments
 (0)