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 5 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"MySSES3Bucket6973690D": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketEncryption": {
"ServerSideEncryptionConfiguration": [
{
"BucketKeyEnabled": true,
"ServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}
]
}
},
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ new s3.Bucket(stack, 'MyDSSEBucket', {
encryption: s3.BucketEncryption.DSSE_MANAGED,
});

new s3.Bucket(stack, 'MySSES3Bucket', {
encryption: s3.BucketEncryption.S3_MANAGED,
bucketKeyEnabled: true,
});

new integ.IntegTest(app, 'IntegTestDSSEBucket', {
testCases: [stack],
});
Expand Down
21 changes: 16 additions & 5 deletions packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,8 @@ export interface BucketProps {
* attendant cost implications of that).
* - If enabled, S3 will use its own time-limited key instead.
*
* Only relevant, when Encryption is set to `BucketEncryption.KMS` or `BucketEncryption.KMS_MANAGED`.
* Only relevant, when Encryption is set to `BucketEncryption.KMS`, `BucketEncryption.KMS_MANAGED` or
* `BucketEncryption.S3_MANAGED`.
pahud marked this conversation as resolved.
Show resolved Hide resolved
*
* @default - false
*/
Expand Down Expand Up @@ -2105,6 +2106,7 @@ export class Bucket extends BucketBase {
* | KMS | undefined | e | SSE-KMS, bucketKeyEnabled = e | new key |
* | KMS_MANAGED | undefined | e | SSE-KMS, bucketKeyEnabled = e | undefined |
* | S3_MANAGED | undefined | false | SSE-S3 | undefined |
* | S3_MANAGED | undefined | e | SSE-S3, bucketKeyEnabled = e | undefined |
* | UNENCRYPTED | undefined | true | ERROR! | ERROR! |
* | UNENCRYPTED | k | e | ERROR! | ERROR! |
* | KMS_MANAGED | k | e | ERROR! | ERROR! |
Expand All @@ -2127,12 +2129,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 @@ -2161,7 +2169,10 @@ export class Bucket extends BucketBase {
if (encryptionType === BucketEncryption.S3_MANAGED) {
const bucketEncryption = {
serverSideEncryptionConfiguration: [
{ serverSideEncryptionByDefault: { sseAlgorithm: 'AES256' } },
{
bucketKeyEnabled: props.bucketKeyEnabled,
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