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): KMS encryption works fine for server access logging target buckets #24789

Closed
wants to merge 0 commits into from

Conversation

zdf1230
Copy link
Contributor

@zdf1230 zdf1230 commented Mar 26, 2023

KMS encryption works fine for server access logging target buckets with proper permission being setup

There were 2 changes wants to solve the issue that buckets with SSE-KMS silently fail to receive logs.
#23514 & #23385

The changes would let it fail early in build time when SSE-KMS being set for server access logging bucket. But it actually works when user use SSE-KMS with customized encryption key. Just need to add KMS Read Write policy for logging.s3.amazonaws.com to the key and add S3 Put Object permission for logging.s3.amazonaws.com.

So this change is removing the condition failing for the SSE-KMS with customized encryption key case.

However, it is not possible to know which encryption type for the server access logging bucket, so the only checking can be applied after this change merged is failing when logging to self case using BucketEncryption.KMS_MANAGED.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 labels Mar 26, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team March 26, 2023 06:12
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

@zdf1230 zdf1230 changed the title KMS encryption works fine for server access logging target buckets fix(s3): KMS encryption works fine for server access logging target buckets Mar 26, 2023
@zdf1230
Copy link
Contributor Author

zdf1230 commented Mar 26, 2023

Exemption Request

Integration tests succeeded and this change won't change snapshot.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Mar 26, 2023
Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution. Please see my comments inline.

Comment on lines 366 to 382
}).not.toThrowError();
});

test('logs to self, KMS encryption with key throws error', () => {
test('logs to self, KMS encryption with key does nor throw error', () => {
const stack = new cdk.Stack();
const key = new kms.Key(stack, 'TestKey');
expect(() => {
new s3.Bucket(stack, 'MyBucket', { encryptionKey: key, encryption: s3.BucketEncryption.KMS, serverAccessLogsPrefix: 'test' });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
});

test('logs to self, KMS key with no specific encryption specified throws error', () => {
test('logs to self, KMS key with no specific encryption specified does not throw error', () => {
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/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

Comment on lines 2110 to 2111
if (!props.serverAccessLogsBucket && props.encryption === BucketEncryption.KMS_MANAGED) {
throw new Error('Default bucket encryption with KMS managed key is not supported for Server Access Logging target buckets');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the case, we should have integ tests where encryption is set to BucketEncryption.KMS with an encryptionKey and without an encryptionKey set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case you talked about is checked under function parseEncryption.

    // if encryption key is set, encryption must be set to KMS.
    if (encryptionType !== BucketEncryption.KMS && props.encryptionKey) {
      throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`);
    }

The change I modified here is mean to fail the build when the server access logs bucket being set as itself and the bucket encryption is set to KMS_MANAGED. In this case, though the resource can be successfully deployed, we cannot modified the encryption key owned by KMS. The key and the bucket need proper permission to make the whole logging workflow work. So, people won't able to get access log from the bucket or the log would be unreadable.

This change won't affect snapshot at all.

const stack = new cdk.Stack();
expect(() => {
new s3.Bucket(stack, 'MyBucket', { encryption: s3.BucketEncryption.KMS, serverAccessLogsPrefix: 'test' });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original logic would throw the error for the case which should not be. And none of these changes are affecting template output. They just mean to fail the build earlier to prevent some non-working situation.

My change is just remove some failed checking condition which is actually working case.

As my change won't affect generating template output, I don't think there is anything I can do there.

const stack = new cdk.Stack();
const key = new kms.Key(stack, 'TestKey');
expect(() => {
new s3.Bucket(stack, 'MyBucket', { encryptionKey: key, encryption: s3.BucketEncryption.KMS, serverAccessLogsPrefix: 'test' });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

const stack = new cdk.Stack();
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryption: s3.BucketEncryption.KMS });
expect(() => {
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

const stack = new cdk.Stack();
const key = new kms.Key(stack, 'TestKey');
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryptionKey: key, encryption: s3.BucketEncryption.KMS });
expect(() => {
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

const stack = new cdk.Stack();
const key = new kms.Key(stack, 'TestKey');
const logBucket = new s3.Bucket(stack, 'testLogBucket', { encryptionKey: key });
expect(() => {
new s3.Bucket(stack, 'MyBucket', { serverAccessLogsBucket: logBucket });
}).toThrow(/SSE-S3 is the only supported default bucket encryption for Server Access Logging target buckets/);
}).not.toThrowError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should test against the expected template output, not just that is doesn't throw an error.

@TheRealAmazonKendra TheRealAmazonKendra removed the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Mar 27, 2023
@TheRealAmazonKendra
Copy link
Contributor

Removing request for exemption per my comments in my review.

@zdf1230
Copy link
Contributor Author

zdf1230 commented Mar 27, 2023

Thanks for the comments. I replied them. Please let me know if anything is uncleared. As I commented, I still think I need the Exemption Request.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Mar 27, 2023
@zdf1230
Copy link
Contributor Author

zdf1230 commented Mar 29, 2023

Clarification Request

@aws-cdk-automation aws-cdk-automation added the pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run label Mar 29, 2023
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

2 similar comments
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

❌ Fixes must contain a change to a test file.
❌ Fixes must contain a change to an integration test file and the resulting snapshot.

PRs must pass status checks before we can provide a meaningful review.

If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing Exemption Request and/or Clarification Request.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: a9d6966
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants