Skip to content

Commit a07d75a

Browse files
authored
fix(iam): cannot grant lambda:InvokeFunction on ManagedPolicy or Policy via grantInvoke() method (#32984)
### Issue # (if applicable) Closes #32980. ### Reason for this change `lambda.Function.grantInvoke()` throws an error when a `ManagedPolicy` or a `Policy` is passed. It should add a policy statement to grant `lambda:InvokeFunction` on the policy document. ### Description of changes #### Core changes in `(Managed)PolicyGrantPrincipal` internal classes Since `grantInvoke()` wants `policyFragment.conditions`, `policyFragment` now returns a `PolicyFragment` object with the principal ARN refers a token. When the grantPrincipal is used as a resource policy, the token will cause a resolution error. #### Additional changes see also [rix0rrr's comment](#32984 (review)) - `assumeRoleAction` now throws an error instead of `'sts:AssumeRole'`. - Updated the error message. ### Describe any new or updated permissions being added N/A ### Description of how you validated changes - Added/updated tests to verify `grantInvoke()` works. - Updated unit tests to verify `app.synth()` throws instead of `Grant.addTo...()`. - Some `class DummyResource implements IResourceWithPolicy` are replaced by S3 bucket to ensure to create a resource policy (S3 bucket policy). ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b39ccf3 commit a07d75a

20 files changed

+1794
-733
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.managed-policy.js.snapshot/ManagedPolicyIntegDefaultTestDeployAssert27007DC6.assets.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.managed-policy.js.snapshot/aws-cdk-iam-managed-policy.assets.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.managed-policy.js.snapshot/aws-cdk-iam-managed-policy.template.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@
4444
"Arn"
4545
]
4646
}
47+
},
48+
{
49+
"Action": "lambda:InvokeFunction",
50+
"Effect": "Allow",
51+
"Resource": [
52+
{
53+
"Fn::GetAtt": [
54+
"Function76856677",
55+
"Arn"
56+
]
57+
},
58+
{
59+
"Fn::Join": [
60+
"",
61+
[
62+
{
63+
"Fn::GetAtt": [
64+
"Function76856677",
65+
"Arn"
66+
]
67+
},
68+
":*"
69+
]
70+
]
71+
}
72+
]
4773
}
4874
],
4975
"Version": "2012-10-17"
@@ -118,6 +144,75 @@
118144
"Version": "2012-10-17"
119145
}
120146
}
147+
},
148+
"FunctionServiceRole675BB04A": {
149+
"Type": "AWS::IAM::Role",
150+
"Properties": {
151+
"AssumeRolePolicyDocument": {
152+
"Statement": [
153+
{
154+
"Action": "sts:AssumeRole",
155+
"Effect": "Allow",
156+
"Principal": {
157+
"Service": "lambda.amazonaws.com"
158+
}
159+
}
160+
],
161+
"Version": "2012-10-17"
162+
},
163+
"ManagedPolicyArns": [
164+
{
165+
"Fn::Join": [
166+
"",
167+
[
168+
"arn:",
169+
{
170+
"Ref": "AWS::Partition"
171+
},
172+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
173+
]
174+
]
175+
}
176+
]
177+
}
178+
},
179+
"Function76856677": {
180+
"Type": "AWS::Lambda::Function",
181+
"Properties": {
182+
"Code": {
183+
"ZipFile": "export const handler = async () => null"
184+
},
185+
"Handler": "index.handler",
186+
"Role": {
187+
"Fn::GetAtt": [
188+
"FunctionServiceRole675BB04A",
189+
"Arn"
190+
]
191+
},
192+
"Runtime": "nodejs22.x"
193+
},
194+
"DependsOn": [
195+
"FunctionServiceRole675BB04A"
196+
]
197+
},
198+
"FunctionLogGroup55B80E27": {
199+
"Type": "AWS::Logs::LogGroup",
200+
"Properties": {
201+
"LogGroupName": {
202+
"Fn::Join": [
203+
"",
204+
[
205+
"/aws/lambda/",
206+
{
207+
"Ref": "Function76856677"
208+
}
209+
]
210+
]
211+
},
212+
"RetentionInDays": 731
213+
},
214+
"UpdateReplacePolicy": "Retain",
215+
"DeletionPolicy": "Retain"
121216
}
122217
},
123218
"Parameters": {

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.managed-policy.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.managed-policy.js.snapshot/integ.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)