Skip to content

Commit ae4a04f

Browse files
author
Elad Ben-Israel
authored
feat(s3): default to KMS if encryptionKey is specified (#2719)
If `encryptionKey` is specified, defaults to KMS encryption. Fixes #2714
1 parent 0593d51 commit ae4a04f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ export interface BucketProps {
604604
* If you choose KMS, you can specify a KMS key via `encryptionKey`. If
605605
* encryption key is not specified, a key will automatically be created.
606606
*
607-
* @default BucketEncryption.Unencrypted
607+
* @default - `Kms` if `encryptionKey` is specified, or `Unencrypted` otherwise.
608608
*/
609609
readonly encryption?: BucketEncryption;
610610

@@ -934,8 +934,11 @@ export class Bucket extends BucketBase {
934934
encryptionKey?: kms.IKey
935935
} {
936936

937-
// default to unencrypted.
938-
const encryptionType = props.encryption || BucketEncryption.Unencrypted;
937+
// default based on whether encryptionKey is specified
938+
let encryptionType = props.encryption;
939+
if (encryptionType === undefined) {
940+
encryptionType = props.encryptionKey ? BucketEncryption.Kms : BucketEncryption.Unencrypted;
941+
}
939942

940943
// if encryption key is set, encryption must be set to KMS.
941944
if (encryptionType !== BucketEncryption.Kms && props.encryptionKey) {

packages/@aws-cdk/aws-s3/test/test.bucket.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,4 +1370,14 @@ export = {
13701370
});
13711371
test.done();
13721372
},
1373+
1374+
'if a kms key is specified, it implies bucket is encrypted with kms (dah)'(test: Test) {
1375+
// GIVEN
1376+
const stack = new Stack();
1377+
const key = new kms.Key(stack, 'k');
1378+
1379+
// THEN
1380+
new Bucket(stack, 'b', { encryptionKey: key });
1381+
test.done();
1382+
}
13731383
};

0 commit comments

Comments
 (0)