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
40 changes: 40 additions & 0 deletions deployment/aws/lib/opentag-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import * as logs from "aws-cdk-lib/aws-logs";
import * as destinations from "aws-cdk-lib/aws-logs-destinations";
import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager";
import * as cr from "aws-cdk-lib/custom-resources";
import type { Construct } from "constructs";
import { resolveVpc } from "./vpc.js";

Expand Down Expand Up @@ -532,6 +533,7 @@ export class OpenTagStack extends cdk.Stack {
appName,
environmentName,
[agentLogGroup, runtimeLogGroup],
secretsKmsKey?.keyArn,
);
}

Expand Down Expand Up @@ -573,10 +575,22 @@ export class OpenTagStack extends cdk.Stack {
appName: string,
environmentName: string,
logGroups: logs.ILogGroup[],
secretsKmsKeyArn: string | undefined,
): void {
// The Datadog template grants kms:Decrypt on "*" when KmsKeyList is
// empty, which reaches every customer key in the account. Scope it to the
// keys the forwarder reads with: the AWS-managed Secrets Manager key (the
// API key secret), the AWS-managed S3 key (its own bucket), and the
// secrets key when one is configured.
const kmsKeyArns: string[] = (["secretsmanager", "s3"] as const).map(
(service) => this.awsManagedKeyArn(service),
);
if (secretsKmsKeyArn) kmsKeyArns.push(secretsKmsKeyArn);

const forwarderStack = new cdk.CfnStack(this, "DatadogForwarder", {
parameters: {
DdApiKeySecretArn: apiKeySecretArn,
KmsKeyList: cdk.Fn.join(",", kmsKeyArns),
DdSite: site,
DdTags: `service:${appName},application:${appName},env:${environmentName}`,
FunctionName: `${appName}-${environmentName}-datadog-forwarder`,
Expand Down Expand Up @@ -612,6 +626,32 @@ export class OpenTagStack extends cdk.Stack {
});
}

/**
* The key ARN behind `alias/aws/<service>` in this stack's account and
* region. IAM matches kms:Decrypt against key ARNs, not alias ARNs, so the
* alias is resolved at deploy time. DescribeKey creates the AWS-managed key
* if the service has not used it yet.
*/
private awsManagedKeyArn(service: "s3" | "secretsmanager"): string {
const lookup = new cr.AwsCustomResource(
this,
service === "s3" ? "AwsManagedKmsKeyS3" : "AwsManagedKmsKeySecretsManager",
{
installLatestAwsSdk: false,
onUpdate: {
action: "DescribeKey",
parameters: { KeyId: `alias/aws/${service}` },
physicalResourceId: cr.PhysicalResourceId.of(`alias/aws/${service}`),
service: "KMS",
},
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
}),
},
);
return lookup.getResponseField("KeyMetadata.Arn");
}

private containerImage(
contextKey: "agentImage" | "runtimeImage",
repositoryArnContextKey:
Expand Down
34 changes: 34 additions & 0 deletions deployment/aws/test/opentag-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,40 @@ test("forwards both awslogs groups through the official Datadog Forwarder", () =
);
});

test("scopes the forwarder's kms:Decrypt to its own keys, not every key", () => {
const template = Template.fromStack(stackWithContext());
const [forwarder] = Object.values(
template.findResources("AWS::CloudFormation::Stack"),
);
const parameters = forwarder?.Properties?.Parameters ?? {};

// An empty KmsKeyList makes the Datadog template grant kms:Decrypt on "*".
assert.ok(parameters.KmsKeyList, "KmsKeyList must be set");
const json = JSON.stringify(parameters.KmsKeyList);
assert.match(json, /AwsManagedKmsKeySecretsManager.*KeyMetadata\.Arn/);
assert.match(json, /AwsManagedKmsKeyS3.*KeyMetadata\.Arn/);
assert.doesNotMatch(json, /"\*"/);

// Each list entry is the key ARN behind the account's AWS-managed alias.
const lookups = JSON.stringify(template.findResources("Custom::AWS"));
assert.match(lookups, /DescribeKey/);
assert.match(lookups, /alias\/aws\/secretsmanager/);
assert.match(lookups, /alias\/aws\/s3/);
});

test("adds the secrets KMS key to the forwarder's key list when set", () => {
const keyArn =
"arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555";
const template = Template.fromStack(
stackWithContext({ secretsKmsKeyArn: keyArn }),
);
const [forwarder] = Object.values(
template.findResources("AWS::CloudFormation::Stack"),
);

assert.match(JSON.stringify(forwarder?.Properties?.Parameters?.KmsKeyList), new RegExp(keyArn));
});

test("injects application secrets without plaintext values", () => {
const template = Template.fromStack(stackWithContext());
const json = JSON.stringify(template.toJSON());
Expand Down