-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(iam): cannot grant lambda:InvokeFunction on ManagedPolicy or Policy via grantInvoke() method
#32984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #32984 +/- ##
=======================================
Coverage 82.20% 82.20%
=======================================
Files 119 119
Lines 6862 6862
Branches 1158 1158
=======================================
Hits 5641 5641
Misses 1118 1118
Partials 103 103
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state. |
1 similar comment
|
This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state. |
| * Adds an IAM statement to the policy document. | ||
| */ | ||
| public addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult { | ||
| this.addStatements(statement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best Practices for AWS IAM Managed Policies
From a best practices perspective, an AWS IAM ManagedPolicy functions as a shared state. Since it can be attached to multiple entities—users, roles, and groups—any change to the policy affects all consumers simultaneously.
The recommended approach is to define all required permissions at creation time and treat the policy as immutable throughout its lifecycle. This offers several key benefits:
-
Predictability
Changes to a shared policy propagate instantly, which can unintentionally alter access across your environment. -
Versioning Complexity
Although AWS supports managed policy versioning, relying on it for rollback or audits introduces additional complexity. -
Least Privilege Enforcement
Updating a shared policy risks granting broader permissions than intended. -
Governance and Control
Because of their wide impact, shared policies should be subject to formal review and change control processes.
💡 Note: If a policy truly needs to change after creation, it may indicate the need for a dedicated IAM role or a purpose-built policy to better isolate and manage those permissions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@5d Thank you for reviewing.
This method is required by IPrincipal.
Constructing permissions step by step (calling addStatements multiple times) in CDK apps does not mean the managed policy is to be mutated after creation.
Are you saying that we should avoid to expose this method directly in ManagedPolicy, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fundamental issue is that Policy and ManagedPolicy are not principals in the AWS IAM model. In AWS, policies are permission documents that are attached to principals (such as users, roles, or services), but they are not principals themselves.
By implementing the IPrincipal interface on Policy and ManagedPolicy, the CDK introduces a conceptual mismatch with the underlying AWS service model. While this implementation may add some convenience, such as allowing policies to be passed directly to grant methods, it creates an abstraction that does not align with how IAM actually works. This can lead to confusion for users who understand the underlying AWS IAM concepts.
The concern here is not primarily about method mutability, but about the architectural decision to make policy objects implement an interface meant for principals, when policies are fundamentally not principals in AWS IAM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
I'll withdraw implementing IPrincipal directly, and work on them for a few days.
1679955 to
dc8b2dc
Compare
|
@5d
And also I've updated the PR description. |
Sorry @Tietew, I think there may be a misunderstanding. In your latest commit, it looks like you're treating
In IAM terms: principals define who can act, while policies define what actions are allowed. Could you clarify the reasoning behind granting permissions to a policy object? In what scenarios would this be necessary? |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Re-adding approval of @rix0rrr after merge conflict fix. |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Issue # (if applicable)
Closes #32980.
Reason for this change
lambda.Function.grantInvoke()throws an error when aManagedPolicyor aPolicyis passed.It should add a policy statement to grant
lambda:InvokeFunctionon the policy document.Description of changes
Core changes in
(Managed)PolicyGrantPrincipalinternal classesSince
grantInvoke()wantspolicyFragment.conditions,policyFragmentnow returns aPolicyFragmentobject 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
assumeRoleActionnow throws an error instead of'sts:AssumeRole'.Describe any new or updated permissions being added
N/A
Description of how you validated changes
grantInvoke()works.app.synth()throws instead ofGrant.addTo...().class DummyResource implements IResourceWithPolicyare replaced by S3 bucket to ensure to create a resource policy (S3 bucket policy).Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license