Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(s3): bucketKey does not support SSE-S3 #30184

Merged
merged 8 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2127,12 +2127,18 @@ export class Bucket extends BucketBase {
throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS or DSSE (value: ${encryptionType})`);
}

// if bucketKeyEnabled is set, encryption must be set to KMS or DSSE.
// if bucketKeyEnabled is set, encryption must be set to KMS, DSSE or S3.
if (
props.bucketKeyEnabled &&
![BucketEncryption.KMS, BucketEncryption.KMS_MANAGED, BucketEncryption.DSSE, BucketEncryption.DSSE_MANAGED].includes(encryptionType)
![
BucketEncryption.KMS,
BucketEncryption.KMS_MANAGED,
BucketEncryption.DSSE,
BucketEncryption.DSSE_MANAGED,
BucketEncryption.S3_MANAGED,
].includes(encryptionType)
pahud marked this conversation as resolved.
Show resolved Hide resolved
) {
throw new Error(`bucketKeyEnabled is specified, so 'encryption' must be set to KMS or DSSE (value: ${encryptionType})`);
throw new Error(`bucketKeyEnabled is specified, so 'encryption' must be set to KMS, DSSE or S3 (value: ${encryptionType})`);
}

if (encryptionType === BucketEncryption.UNENCRYPTED) {
Expand Down Expand Up @@ -2160,6 +2166,7 @@ export class Bucket extends BucketBase {

if (encryptionType === BucketEncryption.S3_MANAGED) {
const bucketEncryption = {
bucketKeyEnabled: props.bucketKeyEnabled,
serverSideEncryptionConfiguration: [
{ serverSideEncryptionByDefault: { sseAlgorithm: 'AES256' } },
],
Expand Down
25 changes: 20 additions & 5 deletions packages/aws-cdk-lib/aws-s3/test/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,15 +574,30 @@ describe('bucket', () => {
});
});

test('throws error if bucketKeyEnabled is set, but encryption is not KMS', () => {
test('bucketKeyEnabled can be enabled with SSE-S3', () => {
const stack = new cdk.Stack();

// WHEN
new s3.Bucket(stack, 'MyBucket', { bucketKeyEnabled: true, encryption: s3.BucketEncryption.S3_MANAGED, });
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
BucketEncryption: {
ServerSideEncryptionConfiguration: [
{
ServerSideEncryptionByDefault: { SSEAlgorithm: 'AES256', },
BucketKeyEnabled: true,
},
],
}
});

});

test('bucketKeyEnabled can NOT be enabled with encryption undefined', () => {
const stack = new cdk.Stack();

expect(() => {
new s3.Bucket(stack, 'MyBucket', { bucketKeyEnabled: true, encryption: s3.BucketEncryption.S3_MANAGED });
}).toThrow("bucketKeyEnabled is specified, so 'encryption' must be set to KMS or DSSE (value: S3_MANAGED)");
expect(() => {
new s3.Bucket(stack, 'MyBucket3', { bucketKeyEnabled: true });
}).toThrow("bucketKeyEnabled is specified, so 'encryption' must be set to KMS or DSSE (value: UNENCRYPTED)");
}).toThrow("bucketKeyEnabled is specified, so 'encryption' must be set to KMS, DSSE or S3 (value: UNENCRYPTED)");

});

Expand Down
Loading