Skip to content
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

iam: cannot pass iam.ManagedPolicy or iam.Policy to lambda.Function.grantInvoke #32980

Open
1 task
Tietew opened this issue Jan 17, 2025 · 1 comment · May be fixed by #32984
Open
1 task

iam: cannot pass iam.ManagedPolicy or iam.Policy to lambda.Function.grantInvoke #32980

Tietew opened this issue Jan 17, 2025 · 1 comment · May be fixed by #32984
Assignees
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. effort/medium Medium work item – several days of effort p3 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@Tietew
Copy link
Contributor

Tietew commented Jan 17, 2025

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

  • Select this option if this issue appears to be a regression.

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

@Tietew Tietew added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 17, 2025
@github-actions github-actions bot added the @aws-cdk/aws-iam Related to AWS Identity and Access Management label Jan 17, 2025
@pahud
Copy link
Contributor

pahud commented Jan 20, 2025

We generally func.grantInvoke() on principals, not managed policies.
See Resource-based Policies.

What is your intention and use case?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p3 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 20, 2025
@QuantumNeuralCoder QuantumNeuralCoder self-assigned this Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iam Related to AWS Identity and Access Management bug This issue is a bug. effort/medium Medium work item – several days of effort p3 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
3 participants