Describe the bug
Following code fails:
const func = new lambda.Function(this, 'Function', { /* ... */ });
const managedPolicy = new iam.ManagedPolicy(this, 'ManagedPolicy');
func.grantInvoke(managedPolicy);
The error message is:
Cannot use a ManagedPolicy 'MyStack/Function' as the 'Principal' or 'NotPrincipal' in an IAM Policy
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
Grant lambda:InvokeFunction to ManagedPolicy or Policy.
Current Behavior
grantInvoke() throws an error described above.
Reproduction Steps
See the description above.
Possible Solution
grantInvoke() calls grantee.grantPrincipal.policyFragment.conditions to create a dedupe hash.
|
public grantInvoke(grantee: iam.IGrantable): iam.Grant { |
|
const hash = createHash('sha256') |
|
.update(JSON.stringify({ |
|
principal: grantee.grantPrincipal.toString(), |
|
conditions: grantee.grantPrincipal.policyFragment.conditions, |
|
}), 'utf8') |
|
.digest('base64'); |
|
const identifier = `Invoke${hash}`; |
But policyFragment getter of ManagedPolicy and Policy throws an error. (see #22712)
It should return a dummy policy fragment like Group.
|
public get policyFragment(): PrincipalPolicyFragment { |
|
return new ArnPrincipal(this.groupArn).policyFragment; |
|
} |
Group is blocked in PolicyStatement
|
private validatePolicyPrincipal(principal: IPrincipal) { |
|
if (principal instanceof Group) { |
|
throw new Error('Cannot use an IAM Group as the \'Principal\' or \'NotPrincipal\' in an IAM Policy'); |
|
} |
|
} |
Additional Information/Context
No response
CDK CLI Version
2.176.0
Framework Version
2.176.0
Node.js Version
22.13.0
OS
Ubuntu
Language
TypeScript
Language Version
No response
Other information
Related to #32795
Describe the bug
Following code fails:
The error message is:
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
Grant
lambda:InvokeFunctiontoManagedPolicyorPolicy.Current Behavior
grantInvoke()throws an error described above.Reproduction Steps
See the description above.
Possible Solution
grantInvoke()callsgrantee.grantPrincipal.policyFragment.conditionsto create a dedupe hash.aws-cdk/packages/aws-cdk-lib/aws-lambda/lib/function-base.ts
Lines 435 to 442 in 2b2443d
But
policyFragmentgetter ofManagedPolicyandPolicythrows an error. (see #22712)It should return a dummy policy fragment like
Group.aws-cdk/packages/aws-cdk-lib/aws-iam/lib/group.ts
Lines 82 to 84 in 2b2443d
Groupis blocked inPolicyStatementaws-cdk/packages/aws-cdk-lib/aws-iam/lib/policy-statement.ts
Lines 240 to 244 in 2b2443d
Additional Information/Context
No response
CDK CLI Version
2.176.0
Framework Version
2.176.0
Node.js Version
22.13.0
OS
Ubuntu
Language
TypeScript
Language Version
No response
Other information
Related to #32795