Skip to content
Open
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
1 change: 1 addition & 0 deletions src/datadog-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function setTagsForFunction(lambdaFunction: lambda.Function, props: DatadogLambd

function grantReadLambda(secret: ISecret, lambdaFunction: lambda.Function): void {
secret.grantRead(lambdaFunction);
secret.encryptionKey?.grantDecrypt(lambdaFunction);
}

function grantReadLambdaFromSecretArn(construct: Construct, arn: string, lambdaFunction: lambda.Function): void {
Expand Down
48 changes: 47 additions & 1 deletion test/datadog-lambda.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
DD_HANDLER_ENV_VAR,
DD_TAGS,
} from "../src/index";
import { Secret } from "aws-cdk-lib/aws-secretsmanager";
import { Key } from "aws-cdk-lib/aws-kms";
const { ISecret } = require("aws-cdk-lib/aws-secretsmanager");
const versionJson = require("../version.json");
const EXTENSION_LAYER_VERSION = 5;
const CUSTOM_EXTENSION_LAYER_ARN = "arn:aws:lambda:us-east-1:123456789:layer:Datadog-Extension-custom:1";
const NODE_LAYER_VERSION = 91;
const REPO_REGEX = /git\.repository_url:.*\/DataDog\/datadog-cdk-constructs(\.git)?/;
const REPO_REGEX = /git\.repository_url:.*\/[^/]+\/datadog-cdk-constructs(\.git)?/;

describe("validateProps", () => {
it("throws an error when the site is set to an invalid site URL", () => {
Expand Down Expand Up @@ -619,6 +621,50 @@ describe("apiKeySecret", () => {
"arn:aws:secretsmanager:sa-east-1:123:secret:test-key-from-isecret",
);
});

it("grants decrypt access to the encryption key when apiKeySecret has an encryption key", () => {
const app = new App();
const stack = new Stack(app, "stack");
const hello = new lambda.Function(stack, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_18_X,
code: lambda.Code.fromInline("test"),
handler: "hello.handler",
});

const secret = new Secret(stack, "DatadogApiKeySecret", {
encryptionKey: new Key(stack, "DatadogApiKeySecretKey"),
});

const datadogLambda = new DatadogLambda(stack, "Datadog", {
nodeLayerVersion: NODE_LAYER_VERSION,
extensionLayerVersion: EXTENSION_LAYER_VERSION,
apiKeySecret: secret,
enableDatadogTracing: false,
flushMetricsToLogs: false,
});
datadogLambda.addLambdaFunctions([hello]);

Template.fromStack(stack).hasResourceProperties("AWS::IAM::Policy", {
PolicyDocument: {
Statement: [
{
Action: ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"],
Effect: "Allow",
Resource: {
Ref: "DatadogApiKeySecret1B324DAE",
},
},
{
Action: "kms:Decrypt",
Effect: "Allow",
Resource: {
"Fn::GetAtt": ["DatadogApiKeySecretKey14C5CA29", "Arn"],
},
},
],
},
});
});
});

describe("addLambdaFunctions", () => {
Expand Down