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-deployment): BucketDeployment fails when bootstrap stack's StagingBucket is encrypted with customer managed KMS key #29540

Merged
merged 2 commits into from
Apr 10, 2024

Conversation

kxue-godaddy
Copy link
Contributor

Issue #25100

Closes #25100.

Reason for this change

When the CDK bootstrap stack's StagingBucket is encrypted with a customer managed KMS key whose key policy does not include wildcard KMS permissions similar to those of the S3 managed KMS key, BucketDeployment fails because the Lambda's execution role doesn't get the kms:Decrypt permission necessary to download from the bucket.

In addition, if one of the sources for BucketDeployment comes from the Source.bucket static method, and the bucket is an "out-of-app imported reference" created from s3.Bucket.fromBucketName, the bucket's encryptionKey attribute is null and the current code won't add the kms:Decrypt permission on the bucket's encryption key to the Lambda's execution role. If this bucket is additionally encrypted with a customer managed KMS key without sufficient resource-based policy, the deployment fails as well.

Description of changes

It's not easy to make the code "just work" in every situation, because there's no way to pinpoint the source bucket's encryption key ARN without using another custom resource, which is a heavy-lifting and it's hard to give this new Lambda a reasonable and minimal set of execution role permissions.

Therefore, this PR resolves the issue by changing BucketDeployment.handlerRole from private readonly to public readonly, and adding documentations on how to handle errors resulting from "not authorized to perform: kms:Decrypt". The current code allows customizing handlerRole by passing in BucketDeploymentProps.role. This change makes the customization easier because users don't need to manually add the S3 permissions.

The only code change is on the visibility of BucketDeployment.handlerRole. All other changes are documentations.

I proposed 4 possible changes in my comment to Issue #25100, and only the first one (changing visibility) is pursued in this PR. The second one was abandoned because the CFN export CdkBootstrap-hnb659fds-FileAssetKeyArn is deprecated.

Description of how you validated changes

I wrote a CDK app which uses the BucketDeployment construct. After manually adding relevant KMS permissions to the Lambda execution role, I verified that the bucket deployment worked in the following two scenarios:

  • My personal account; bootstrap stack's StagingBucket encrypted with a custom KMS key which only has the default key policy.
  • GoDaddy corporate account; StagingBucket encrypted with a KMS key from an AWS Organization management account.

Checklist


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

@aws-cdk-automation aws-cdk-automation requested a review from a team March 19, 2024 18:05
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Mar 19, 2024
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.

@kxue-godaddy
Copy link
Contributor Author

Exemption Request

I'm requesting an exemption from updating test and snapshot files in this PR.

The only code change in this PR is changing BucketDeployment.handlerRole from private to public. Other parts of the PR are pure documentation explaining when and how to use this newly exposed attribute to add onto the Lambda's execution role permissions.

@aws-cdk-automation aws-cdk-automation added pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. pr/needs-maintainer-review This PR needs a review from a Core Team Member labels Mar 19, 2024
This was referenced Mar 25, 2024
@@ -4,6 +4,8 @@ import { Construct } from 'constructs';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as ec2 from'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as kms from 'aws-cdk-lib/aws-kms';
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this change needed?

Copy link
Contributor Author

@kxue-godaddy kxue-godaddy Apr 8, 2024

Choose a reason for hiding this comment

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

Thanks for reviewing! @GavinZZ

I'm following the guidelines on writing docs in the Rosetta section. Without adding these lines, the new code snippets I added won't compile (e.g. Line 98 of packages/aws-cdk-lib/aws-s3-deployment/README.md, which uses iam.PolicyStatement). The section also recommends "Utilize the default.ts-fixture that already exists rather than writing new .ts-fixture files", so I'm updating the default ts-fixture file.

It seems that the CI (when I was making the PR) doesn't check if Rosetta compiles, but since this PR is mainly doc changes, I want to make sure the updates can be reflected on the CDK doc site. <-- By this I mean not all committed docs compile when I ran yarn rosetta:extract --strict locally. I only made sure all aws-s3-deployment docs compile but didn't touch other subpackages.

Copy link
Contributor

Choose a reason for hiding this comment

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

@kxue-godaddy thanks for getting back to me so quickly. I agree that the import statement for iam is necessary since it's used in the README file, but I don't think I see any usage of kms (other than the IAM actions). Would you please try removing the kms import statement and re-ran the command?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@GavinZZ

I removed the kms line and reran the command. It shows the following error:

aws-cdk-lib.aws_s3_deployment.Source#bucket-example.ts:24:18 - error TS2304: Cannot find name 'kms'.

24   encryptionKey: kms.Key.fromKeyArn(

The kms import line is needed by packages/aws-cdk-lib/aws-s3-deployment/lib/source.ts, where I added an example for Source.bucket via the @example tag.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh nice, thank you. Didn't notice that there's a kms usage here.

@GavinZZ
Copy link
Contributor

GavinZZ commented Apr 8, 2024

Agree that no test is needed. Just have one question on the ts-fixture file change.

@GavinZZ GavinZZ added pr-linter/exempt-test The PR linter will not require test changes pr-linter/exempt-integ-test The PR linter will not require integ test changes and removed pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. labels Apr 8, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review April 8, 2024 22:18

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

GavinZZ
GavinZZ previously approved these changes Apr 10, 2024
Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for contributing and answering my questions.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 10, 2024
Copy link
Contributor

mergify bot commented Apr 10, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@kxue-godaddy
Copy link
Contributor Author

kxue-godaddy commented Apr 10, 2024

Looks like "Queue: Embarked in merge queue" was not successful because I didn't allow workflow runs in my fork repo. Just updated that and I will trigger mergify again.

@kxue-godaddy
Copy link
Contributor Author

@Mergifyio refresh

Copy link
Contributor

mergify bot commented Apr 10, 2024

refresh

✅ Pull request refreshed

@kxue-godaddy
Copy link
Contributor Author

kxue-godaddy commented Apr 10, 2024

Mergify still doesn't work. Error message is:

Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/repo-metrics-monthly.yml without workflows permission

I'm going to click the "Update branch" button on this PR and see what happens.

@mergify mergify bot dismissed GavinZZ’s stale review April 10, 2024 12:36

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Apr 10, 2024
@kxue-godaddy
Copy link
Contributor Author

@GavinZZ

Mergify reports an error for "Queue: Embarked in merge queue" after PR approval. I tried clicking the "Update branch" button and mergify dismissed your approval as stale. Could you approve again? The changes are the same. Sorry!

Copy link
Contributor

mergify bot commented Apr 10, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

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 bug This issue is a bug. effort/medium Medium work item – several days of effort p1 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-test The PR linter will not require test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-s3-deployment): bucket deployment fails when toolkit stack uses Customer KMS
4 participants