Skip to content

Commit f914da9

Browse files
committed
update
1 parent e340755 commit f914da9

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

packages/aws-cdk-lib/aws-synthetics/lib/canary.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export interface CanaryProps {
269269
* Artifact encryption is only supported for canaries that use Synthetics runtime
270270
* version `syn-nodejs-puppeteer-3.3` or later.
271271
*
272-
* @default - Artifacts are encrypted at rest using an AWS managed key
272+
* @default - `ArtifactsEncryptionMode.KMS` is set if you specify `artifactS3KmsKey`, otherwise artifacts are encrypted at rest using an AWS managed key
273273
*
274274
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_artifact_encryption.html
275275
*/
@@ -676,7 +676,7 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
676676
const isNodeRuntime = props.runtime.family === RuntimeFamily.NODEJS;
677677

678678
if (
679-
props.artifactS3EncryptionMode !== ArtifactsEncryptionMode.KMS &&
679+
props.artifactS3EncryptionMode === ArtifactsEncryptionMode.S3_MANAGED &&
680680
props.artifactS3KmsKey
681681
) {
682682
throw new Error(`A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS, got: ${props.artifactS3EncryptionMode}.`);
@@ -688,15 +688,27 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {
688688
}
689689

690690
let encryptionKey: kms.IKey | undefined;
691-
if (props.artifactS3EncryptionMode === ArtifactsEncryptionMode.KMS) {
692-
encryptionKey = props.artifactS3KmsKey ?? new kms.Key(this, 'Key', { description: `Created by ${this.node.path}` });
691+
if (props.artifactS3EncryptionMode === ArtifactsEncryptionMode.KMS && !props.artifactS3KmsKey) {
692+
encryptionKey = new kms.Key(this, 'Key', { description: `Created by ${this.node.path}` });
693+
} else {
694+
encryptionKey = props.artifactS3KmsKey;
693695
}
694696

695697
encryptionKey?.grantEncryptDecrypt(this.role);
696698

699+
let encryptionMode: ArtifactsEncryptionMode | undefined;
700+
encryptionMode = props.artifactS3EncryptionMode ? props.artifactS3EncryptionMode
701+
: props.artifactS3KmsKey ? ArtifactsEncryptionMode.KMS : undefined;
702+
703+
if (props.artifactS3KmsKey && !props.artifactS3EncryptionMode) {
704+
encryptionMode = ArtifactsEncryptionMode.KMS;
705+
} else {
706+
encryptionMode = props.artifactS3EncryptionMode;
707+
}
708+
697709
return {
698710
s3Encryption: {
699-
encryptionMode: props.artifactS3EncryptionMode,
711+
encryptionMode,
700712
kmsKeyArn: encryptionKey?.keyArn,
701713
},
702714
};

packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,24 +1031,33 @@ describe('artifact encryption test', () => {
10311031
});
10321032
});
10331033

1034-
test('SSE-S3 with a key throws', () => {
1034+
test('No artifactS3EncryptionMode setting with a key is set to SSE_KMS', () => {
1035+
// GIVEN
10351036
const stack = new Stack();
10361037
const key = new kms.Key(stack, 'myKey');
10371038

1038-
expect(() => {
1039-
new synthetics.Canary(stack, 'Canary', {
1040-
test: synthetics.Test.custom({
1041-
handler: 'index.handler',
1042-
code: synthetics.Code.fromInline('/* Synthetics handler code */'),
1043-
}),
1044-
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0,
1045-
artifactS3EncryptionMode: synthetics.ArtifactsEncryptionMode.S3_MANAGED,
1046-
artifactS3KmsKey: key,
1047-
});
1048-
}).toThrow('A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS, got: SSE_S3.');
1039+
// WHEN
1040+
new synthetics.Canary(stack, 'Canary', {
1041+
test: synthetics.Test.custom({
1042+
handler: 'index.handler',
1043+
code: synthetics.Code.fromInline('/* Synthetics handler code */'),
1044+
}),
1045+
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0,
1046+
artifactS3KmsKey: key,
1047+
});
1048+
1049+
// THEN
1050+
Template.fromStack(stack).hasResourceProperties('AWS::Synthetics::Canary', {
1051+
ArtifactConfig: {
1052+
S3Encryption: {
1053+
EncryptionMode: 'SSE_KMS',
1054+
KmsKeyArn: stack.resolve(key.keyArn),
1055+
},
1056+
},
1057+
});
10491058
});
10501059

1051-
test('No artifactS3EncryptionMode setting with a key throws', () => {
1060+
test('SSE-S3 with a key throws', () => {
10521061
const stack = new Stack();
10531062
const key = new kms.Key(stack, 'myKey');
10541063

@@ -1059,9 +1068,10 @@ describe('artifact encryption test', () => {
10591068
code: synthetics.Code.fromInline('/* Synthetics handler code */'),
10601069
}),
10611070
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0,
1071+
artifactS3EncryptionMode: synthetics.ArtifactsEncryptionMode.S3_MANAGED,
10621072
artifactS3KmsKey: key,
10631073
});
1064-
}).toThrow('A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS, got: undefined.');
1074+
}).toThrow('A customer-managed KMS key was provided, but the encryption mode is not set to SSE-KMS, got: SSE_S3.');
10651075
});
10661076

10671077
test('Artifact encryption for non-Node.js runtime throws an error', () => {

0 commit comments

Comments
 (0)