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

feat(lambda): add grantInvokeLatestVersion to grant invoke only to latest function version #29856

Merged
merged 26 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4d90557
add grantInvokeV2
roger-zhangg Apr 16, 2024
a2ca2f2
add readme
roger-zhangg Apr 16, 2024
287ccc5
Merge branch 'main' into grant_invoke_v2
roger-zhangg Apr 16, 2024
1052cf1
Merge branch 'grant_invoke_v2' of github.com:roger-zhangg/aws-cdk int…
roger-zhangg Apr 16, 2024
aa9d4ca
add integ test
roger-zhangg Apr 16, 2024
fc612e3
change to grantinvoke
roger-zhangg Apr 18, 2024
3aa8b6f
Merge branch 'main' into grant_invoke_v2
roger-zhangg Apr 18, 2024
679e6a6
lint
roger-zhangg Apr 18, 2024
0857f65
typo
roger-zhangg Apr 18, 2024
34d1506
add fn
roger-zhangg Apr 18, 2024
a502cd0
test
roger-zhangg Apr 18, 2024
5e5e6c9
add grantInvokeLatestVersion
roger-zhangg May 3, 2024
c734032
add grantInvokeLatestVersion
roger-zhangg May 3, 2024
74130bb
Merge branch 'main' into grant_invoke_v2
roger-zhangg May 3, 2024
f4b873f
fix lint
roger-zhangg May 3, 2024
a7b2b36
fix integ
roger-zhangg May 3, 2024
cd2f33b
Merge branch 'main' into grant_invoke_v2
roger-zhangg May 6, 2024
2e60174
revert to original snapshot
roger-zhangg May 6, 2024
36ee9a9
fix integ test
roger-zhangg May 6, 2024
6b4a5eb
Merge branch 'main' into grant_invoke_v2
roger-zhangg May 29, 2024
aa3cd0e
Merge branch 'main' into grant_invoke_v2
roger-zhangg Jun 25, 2024
7501abd
address comments
roger-zhangg Jun 26, 2024
734aae6
add desc
roger-zhangg Jun 26, 2024
15e3e8e
Merge branch 'main' into grant_invoke_v2
roger-zhangg Jun 26, 2024
e332150
Merge branch 'main' into grant_invoke_v2
roger-zhangg Jul 1, 2024
f84e77c
Merge branch 'main' into grant_invoke_v2
mergify[bot] Jul 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export class EdgeFunction extends Resource implements lambda.IVersion {
public grantInvoke(identity: iam.IGrantable): iam.Grant {
return this.lambda.grantInvoke(identity);
}
public grantInvokeV2(identity: iam.IGrantable, grantVersionAccess?: boolean): iam.Grant {
return this.lambda.grantInvokeV2(identity, grantVersionAccess);
}
public grantInvokeUrl(identity: iam.IGrantable): iam.Grant {
return this.lambda.grantInvokeUrl(identity);
}
Expand Down
43 changes: 41 additions & 2 deletions packages/aws-cdk-lib/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ export interface IFunction extends IResource, ec2.IConnectable, iam.IGrantable {
/**
* Grant the given identity permissions to invoke this Lambda
*/
grantInvoke(identity: iam.IGrantable): iam.Grant;
grantInvoke(grantee: iam.IGrantable): iam.Grant;

/**
* Grant the given identity permissions to invoke to $Latest version when grantVersionAccess is false
* Grant the given identity permissions to invoke All version when grantVersionAccess is true
*/
grantInvokeV2(grantee: iam.IGrantable, grantVersionAccess?: boolean): iam.Grant;

/**
* Grant the given identity permissions to invoke this Lambda Function URL
*/
grantInvokeUrl(identity: iam.IGrantable): iam.Grant;
grantInvokeUrl(grantee: iam.IGrantable): iam.Grant;
roger-zhangg marked this conversation as resolved.
Show resolved Hide resolved

/**
* Grant multiple principals the ability to invoke this Lambda via CompositePrincipal
Expand Down Expand Up @@ -437,6 +443,39 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
return grant;
}

/**
* Grants the specified identity permissions to invoke this Lambda function.
*
* **Important:** Avoid using `grantInvokeV2` in conjunction with `grantInvoke`.
*
* @param grantee The principal (identity) to grant invocation permission.
* @param grantVersionAccess (Optional) Controls whether to grant access to all function versions. Defaults to `false`.
* - When set to `false`, only the function without a specific version (`$Latest`) can be invoked.
* - When set to `true`, both the function and functions with specific versions can be invoked.
*/
public grantInvokeV2(grantee: iam.IGrantable, grantVersionAccess?: boolean): iam.Grant {
const hash = createHash('sha256')
.update(JSON.stringify({
principal: grantee.grantPrincipal.toString(),
conditions: grantee.grantPrincipal.policyFragment.conditions,
grantVersionAccess: grantVersionAccess,
}), 'utf8')
.digest('base64');
const identifier = `Invoke${hash}`;

// Memoize the result so subsequent grantInvokeV2() calls are idempotent
let grant = this._invocationGrants[identifier];
if (!grant) {
let resouceArns = [this.functionArn];
if (grantVersionAccess) {
resouceArns = this.resourceArnsForGrantInvoke;
}
grant = this.grant(grantee, identifier, 'lambda:InvokeFunction', resouceArns);
this._invocationGrants[identifier] = grant;
}
return grant;
}

/**
* Grant the given identity permissions to invoke this Lambda Function URL
*/
Expand Down
Loading
Loading