Skip to content

Commit 1e8926f

Browse files
authored
fix(s3): logging bucket blocks KMS_MANAGED encryption (#23514)
---- closes #23513 ### All Submissions: * [ x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent adfe4fa commit 1e8926f

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -2104,12 +2104,14 @@ export class Bucket extends BucketBase {
21042104
if (!props.serverAccessLogsBucket && !props.serverAccessLogsPrefix) {
21052105
return undefined;
21062106
}
2107+
21072108
if (
2108-
// The current bucket is being used and is configured for default SSE-KMS
2109-
!props.serverAccessLogsBucket && (
2109+
// KMS can't be used for logging since the logging service can't use the key - logs don't write
2110+
// KMS_MANAGED can't be used for logging since the account can't access the logging service key - account can't read logs
2111+
(!props.serverAccessLogsBucket && (
21102112
props.encryptionKey ||
2111-
props.encryption === BucketEncryption.KMS ||
2112-
props.encryption === BucketEncryption.KMS_MANAGED) ||
2113+
props.encryption === BucketEncryption.KMS_MANAGED ||
2114+
props.encryption === BucketEncryption.KMS )) ||
21132115
// Another bucket is being used that is configured for default SSE-KMS
21142116
props.serverAccessLogsBucket?.encryptionKey
21152117
) {

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

+83-8
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,101 @@ describe('bucket', () => {
338338

339339
});
340340

341-
test('throws error if using KMS-Managed key and server access logging to self', () => {
341+
test('logs to self, no encryption does not throw error', () => {
342+
const stack = new cdk.Stack();
343+
expect(() => {
344+
new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.UNENCRYPTED, serverAccessLogsPrefix: 'test' });
345+
}).not.toThrowError();
346+
});
347+
348+
test('logs to self, S3 encryption does not throw error', () => {
349+
const stack = new cdk.Stack();
350+
expect(() => {
351+
new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.S3_MANAGED, serverAccessLogsPrefix: 'test' });
352+
}).not.toThrowError();
353+
});
354+
355+
test('logs to self, KMS_MANAGED encryption throws error', () => {
342356
const stack = new cdk.Stack();
343357
expect(() => {
344358
new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.KMS_MANAGED, serverAccessLogsPrefix: 'test' });
345-
}).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets');
359+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
360+
});
361+
362+
test('logs to self, KMS encryption without key throws error', () => {
363+
const stack = new cdk.Stack();
364+
expect(() => {
365+
new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.KMS, serverAccessLogsPrefix: 'test' });
366+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
367+
});
368+
369+
test('logs to self, KMS encryption with key throws error', () => {
370+
const stack = new cdk.Stack();
371+
const key = new kms.Key(stack, 'TestKey');
372+
expect(() => {
373+
new s3.Bucket(stack, 'MyBucket', { encryptionKey: key, encryption: s3.BucketEncryption.KMS, serverAccessLogsPrefix: 'test' });
374+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
346375
});
347-
test('throws error if using KMS CMK and server access logging to self', () => {
376+
377+
test('logs to self, KMS key with no specific encryption specified throws error', () => {
348378
const stack = new cdk.Stack();
349379
const key = new kms.Key(stack, 'TestKey');
350380
expect(() => {
351381
new s3.Bucket(stack, 'MyBucket', { encryptionKey: key, serverAccessLogsPrefix: 'test' });
352-
}).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets');
382+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
383+
});
384+
385+
test('logs to separate bucket, no encryption does not throw error', () => {
386+
const stack = new cdk.Stack();
387+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryption: s3.BucketEncryption.UNENCRYPTED });
388+
expect(() => {
389+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
390+
}).not.toThrowError();
391+
});
392+
393+
test('logs to separate bucket, S3 encryption does not throw error', () => {
394+
const stack = new cdk.Stack();
395+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryption: s3.BucketEncryption.S3_MANAGED });
396+
expect(() => {
397+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
398+
}).not.toThrowError();
399+
});
400+
401+
// When provided an external bucket (as an IBucket), we cannot detect KMS_MANAGED encryption. Since this
402+
// check is impossible, we skip thist test.
403+
// eslint-disable-next-line jest/no-disabled-tests
404+
test.skip('logs to separate bucket, KMS_MANAGED encryption throws error', () => {
405+
const stack = new cdk.Stack();
406+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryption: s3.BucketEncryption.KMS_MANAGED });
407+
expect(() => {
408+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
409+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
353410
});
354-
test('throws error if enabling server access logging to bucket with SSE-KMS', () => {
411+
412+
test('logs to separate bucket, KMS encryption without key throws error', () => {
413+
const stack = new cdk.Stack();
414+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryption: s3.BucketEncryption.KMS });
415+
expect(() => {
416+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
417+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
418+
});
419+
420+
test('logs to separate bucket, KMS encryption with key throws error', () => {
421+
const stack = new cdk.Stack();
422+
const key = new kms.Key(stack, 'TestKey');
423+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryptionKey: key, encryption: s3.BucketEncryption.KMS });
424+
expect(() => {
425+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
426+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
427+
});
428+
429+
test('logs to separate bucket, KMS key with no specific encryption specified throws error', () => {
355430
const stack = new cdk.Stack();
356431
const key = new kms.Key(stack, 'TestKey');
357-
const targetBucket = new s3.Bucket(stack, 'TargetBucket', { encryptionKey: key } );
432+
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryptionKey: key });
358433
expect(() => {
359-
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: targetBucket });
360-
}).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets');
434+
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
435+
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
361436
});
362437

363438
test('bucket with versioning turned on', () => {

0 commit comments

Comments
 (0)