Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(lambda): grant invoke twice with different principals #20174

Merged
merged 7 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -445,7 +445,7 @@
"TargetType": "lambda"
},
"DependsOn": [
"FunInvokeServicePrincipalelasticloadbalancingamazonawscomD2CAC0C4"
"FunInvoke2UTWxhlfyqbT5FTn5jvgbLgjFfJwzswGk55DU1HY1CA1AAFB"
]
},
"FunServiceRole3CC876D7": {
Expand Down Expand Up @@ -498,7 +498,7 @@
"FunServiceRole3CC876D7"
]
},
"FunInvokeServicePrincipalelasticloadbalancingamazonawscomD2CAC0C4": {
"FunInvoke2UTWxhlfyqbT5FTn5jvgbLgjFfJwzswGk55DU1HY1CA1AAFB": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"18.0.0"}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "18.0.0",
"testCases": {
"aws-elasticloadbalancingv2-targets/test/integ.lambda-target": {
"integ.lambda-target": {
"stacks": [
"TestStack"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "17.0.0",
"version": "18.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
Expand Down Expand Up @@ -177,10 +177,19 @@
"data": "FunA2CCED21"
}
],
"/TestStack/Fun/InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)": [
"/TestStack/Fun/Invoke2UTWxhlfyqbT5FTn--5jvgbLgj+FfJwzswGk55DU1H--Y=": [
{
"type": "aws:cdk:logicalId",
"data": "FunInvokeServicePrincipalelasticloadbalancingamazonawscomD2CAC0C4"
"data": "FunInvoke2UTWxhlfyqbT5FTn5jvgbLgjFfJwzswGk55DU1HY1CA1AAFB"
}
],
"FunInvokeServicePrincipalelasticloadbalancingamazonawscomD2CAC0C4": [
{
"type": "aws:cdk:logicalId",
"data": "FunInvokeServicePrincipalelasticloadbalancingamazonawscomD2CAC0C4",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@
"version": "0.0.0"
}
},
"InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)": {
"id": "InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)",
"path": "TestStack/Fun/InvokeServicePrincipal(elasticloadbalancing.amazonaws.com)",
"Invoke2UTWxhlfyqbT5FTn--5jvgbLgj+FfJwzswGk55DU1H--Y=": {
"id": "Invoke2UTWxhlfyqbT5FTn--5jvgbLgj+FfJwzswGk55DU1H--Y=",
"path": "TestStack/Fun/Invoke2UTWxhlfyqbT5FTn--5jvgbLgj+FfJwzswGk55DU1H--Y=",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::Lambda::Permission",
"aws:cdk:cloudformation:props": {
Expand Down
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/function-base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'crypto';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
Expand Down Expand Up @@ -414,7 +415,13 @@ export abstract class FunctionBase extends Resource implements IFunction, ec2.IC
* Grant the given identity permissions to invoke this Lambda
*/
public grantInvoke(grantee: iam.IGrantable): iam.Grant {
const identifier = `Invoke${grantee.grantPrincipal}`; // calls the .toString() of the principal
const hash = createHash('sha256')
.update(JSON.stringify({
principal: grantee.grantPrincipal.toString(),
conditions: grantee.grantPrincipal.policyFragment.conditions,
}), 'utf8')
.digest('base64');
const identifier = `Invoke${hash}`;

// Memoize the result so subsequent grantInvoke() calls are idempotent
let grant = this._invocationGrants[identifier];
Expand Down
69 changes: 69 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,75 @@ describe('function', () => {
});
});
});

test('called twice for the same service principal but with different conditions', () => {
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'Function', {
code: lambda.Code.fromInline('xxx'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
});
const sourceArnA = 'some-arn-a';
const sourceArnB = 'some-arn-b';
const service = 's3.amazonaws.com';
const conditionalPrincipalA = new iam.PrincipalWithConditions(new iam.ServicePrincipal(service), {
ArnLike: {
'aws:SourceArn': sourceArnA,
},
StringEquals: {
'aws:SourceAccount': stack.account,
},
});
const conditionalPrincipalB = new iam.PrincipalWithConditions(new iam.ServicePrincipal(service), {
ArnLike: {
'aws:SourceArn': sourceArnB,
},
StringEquals: {
'aws:SourceAccount': stack.account,
},
});

// WHEN
fn.grantInvoke(conditionalPrincipalA);
fn.grantInvoke(conditionalPrincipalB);

// THEN
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 2);
Template.fromStack(stack).hasResource('AWS::Lambda::Permission', {
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'Function76856677',
'Arn',
],
},
Principal: service,
SourceAccount: {
Ref: 'AWS::AccountId',
},
SourceArn: sourceArnA,
},
});

Template.fromStack(stack).hasResource('AWS::Lambda::Permission', {
Properties: {
Action: 'lambda:InvokeFunction',
FunctionName: {
'Fn::GetAtt': [
'Function76856677',
'Arn',
],
},
Principal: service,
SourceAccount: {
Ref: 'AWS::AccountId',
},
SourceArn: sourceArnB,
},
});
});
});

test('throws if ephemeral storage size is out of bound', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/test/lambda-version.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Template } from '@aws-cdk/assertions';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';

describe('lambda version', () => {
test('can import a Lambda version by ARN', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Template } from '@aws-cdk/assertions';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as s3 from '@aws-cdk/aws-s3';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';

describe('singleton lambda', () => {
test('can add same singleton Lambda multiple times, only instantiated once in template', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
"LambdaServiceRoleA8ED4D3B"
]
},
"LambdaInvokeServicePrincipalsecretsmanageramazonawscomB927629E": {
"LambdaInvokeN0a2GKfZP0JmDqDEVhhu6A0TUv3NyNbk4YMFKNc69846677": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"17.0.0"}
{"version":"18.0.0"}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "18.0.0",
"testCases": {
"aws-secretsmanager/test/integ.lambda-rotation": {
"integ.lambda-rotation": {
"stacks": [
"cdk-integ-secret-lambda-rotation"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "17.0.0",
"version": "18.0.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
Expand Down Expand Up @@ -57,10 +57,19 @@
"data": "LambdaD247545B"
}
],
"/cdk-integ-secret-lambda-rotation/Lambda/InvokeServicePrincipal(secretsmanager.amazonaws.com)": [
"/cdk-integ-secret-lambda-rotation/Lambda/InvokeN0--a2GKfZP0JmDqDE--Vhhu6+A0TUv3NyNbk4YM+FKNc=": [
{
"type": "aws:cdk:logicalId",
"data": "LambdaInvokeServicePrincipalsecretsmanageramazonawscomB927629E"
"data": "LambdaInvokeN0a2GKfZP0JmDqDEVhhu6A0TUv3NyNbk4YMFKNc69846677"
}
],
"LambdaInvokeServicePrincipalsecretsmanageramazonawscomB927629E": [
{
"type": "aws:cdk:logicalId",
"data": "LambdaInvokeServicePrincipalsecretsmanageramazonawscomB927629E",
"trace": [
"!!DESTRUCTIVE_CHANGES: WILL_DESTROY"
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@
"version": "0.0.0"
}
},
"InvokeServicePrincipal(secretsmanager.amazonaws.com)": {
"id": "InvokeServicePrincipal(secretsmanager.amazonaws.com)",
"path": "cdk-integ-secret-lambda-rotation/Lambda/InvokeServicePrincipal(secretsmanager.amazonaws.com)",
"InvokeN0--a2GKfZP0JmDqDE--Vhhu6+A0TUv3NyNbk4YM+FKNc=": {
"id": "InvokeN0--a2GKfZP0JmDqDE--Vhhu6+A0TUv3NyNbk4YM+FKNc=",
"path": "cdk-integ-secret-lambda-rotation/Lambda/InvokeN0--a2GKfZP0JmDqDE--Vhhu6+A0TUv3NyNbk4YM+FKNc=",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::Lambda::Permission",
"aws:cdk:cloudformation:props": {
Expand Down