diff --git a/packages/@aws-cdk/aws-s3/lib/bucket.ts b/packages/@aws-cdk/aws-s3/lib/bucket.ts index c910d93eeff0a..0a7b7c029e63b 100644 --- a/packages/@aws-cdk/aws-s3/lib/bucket.ts +++ b/packages/@aws-cdk/aws-s3/lib/bucket.ts @@ -2044,6 +2044,17 @@ export class Bucket extends BucketBase { if (!props.serverAccessLogsBucket && !props.serverAccessLogsPrefix) { return undefined; } + if ( + // The current bucket is being used and is configured for default SSE-KMS + !props.serverAccessLogsBucket && ( + props.encryptionKey || + props.encryption === BucketEncryption.KMS || + props.encryption === BucketEncryption.KMS_MANAGED) || + // Another bucket is being used that is configured for default SSE-KMS + props.serverAccessLogsBucket?.encryptionKey + ) { + throw new Error('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets'); + } return { destinationBucketName: props.serverAccessLogsBucket?.bucketName, diff --git a/packages/@aws-cdk/aws-s3/test/bucket.test.ts b/packages/@aws-cdk/aws-s3/test/bucket.test.ts index ed727d615578a..e412ceb2bc6ad 100644 --- a/packages/@aws-cdk/aws-s3/test/bucket.test.ts +++ b/packages/@aws-cdk/aws-s3/test/bucket.test.ts @@ -337,6 +337,28 @@ describe('bucket', () => { }); + test('throws error if using KMS-Managed key and server access logging to self', () => { + const stack = new cdk.Stack(); + expect(() => { + new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.KMS_MANAGED, serverAccessLogsPrefix: 'test' }); + }).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets'); + }); + test('throws error if using KMS CMK and server access logging to self', () => { + const stack = new cdk.Stack(); + const key = new kms.Key(stack, 'TestKey'); + expect(() => { + new s3.Bucket(stack, 'MyBucket', { encryptionKey: key, serverAccessLogsPrefix: 'test' }); + }).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets'); + }); + test('throws error if enabling server access logging to bucket with SSE-KMS', () => { + const stack = new cdk.Stack(); + const key = new kms.Key(stack, 'TestKey'); + const targetBucket = new s3.Bucket(stack, 'TargetBucket', { encryptionKey: key } ); + expect(() => { + new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: targetBucket }); + }).toThrow('SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets'); + }); + test('bucket with versioning turned on', () => { const stack = new cdk.Stack(); new s3.Bucket(stack, 'MyBucket', {