Skip to content

Commit

Permalink
feat(lambda): add grantInvokeLatestVersion to grant invoke only to la…
Browse files Browse the repository at this point in the history
…test function version (#29856)

### Issue # (if applicable)

Closes #20177

### Reason for this change



`fn.grantInvoke()` will grant invoke permission to invoke both the latest version and all pervious version of the lambda function. We can see this behavior could bring some security concern for some of our customers.

### Description of changes



We provides a new function `fn.grantInvokeLatestVersion()`  to grant invoke only to the Latest version of function and the unqualified lambda arn

Example:
```ts
// Grant permissions to a service
declare const fn: lambda.Function;
const principal = new iam.ServicePrincipal('my-service');

fn.grantInvokeLatestVersion(principal);

```

### Description of how you validated changes


Added unit tests and integration tests.
When using `fn.grantInvokeLatestVersion()` granted principle to invoke a function's past version, it will get the following error:
```
An error occurred (AccessDeniedException) when calling the Invoke operation: User: {$principle} is not authorized to perform: lambda:InvokeFunction on resource: {$LambdaArn:$version} because no identity-based policy allows the lambda:InvokeFunction action
```

### Alternative design (to discuss)
setup a `grantInvokeProp` including `grantVersionAccess` flag to pass in the `grantInvokeLatestVersion` instead using `grantVersionAccess` flag directly on `grantInvokeLatestVersion`
-> This is discussed in the comments, I agree having props will have future extensibility but usually for grant methods specifically we haven't seen before. So we will not add prop to the new function `grantInvokeLatestVersion`

### 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*
  • Loading branch information
roger-zhangg committed Jul 2, 2024
1 parent e525de3 commit 61c28dd
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 57 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@
"Principal": "apigateway.amazonaws.com"
}
},
"MyLambdaInvokeZQDUzUWqDuiGYFrGB6ik9uIPJSgF8GSLIg6NTO8jHPIBE1EB3A4": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
"Principal": "*",
"PrincipalOrgID": "o-xxxxxxxxxx2"
}
},
"MyRoleF48FFE04": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -148,6 +162,52 @@
"Arn"
]
}
},
{
"Action": "lambda:InvokeFunction",
"Effect": "Allow",
"Resource": [
{
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
":",
{
"Fn::GetAtt": [
"v192FF9954",
"Version"
]
}
]
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"MyLambdaCCE802FB",
"Arn"
]
},
":$LATEST"
]
]
}
]
}
],
"Version": "2012-10-17"
Expand All @@ -159,6 +219,14 @@
}
]
}
},
"v192FF9954": {
"Type": "AWS::Lambda::Version",
"Properties": {
"FunctionName": {
"Ref": "MyLambdaCCE802FB"
}
}
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 61c28dd

Please sign in to comment.