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

feat: disallow cross account asset publishing in some scenarios #31623

Merged
merged 14 commits into from
Oct 14, 2024

Conversation

iliapolo
Copy link
Contributor

@iliapolo iliapolo commented Oct 2, 2024

Issue # (if applicable)

Closes #.

Reason for this change

Description of changes

Description of how you validated changes

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 October 2, 2024 16:09
@github-actions github-actions bot added the p2 label Oct 2, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 2, 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.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Oct 2, 2024
@iliapolo
Copy link
Contributor Author

iliapolo commented Oct 2, 2024

For whoever ends up picking this PR up: hopefully there are just a couple of methods to implements:

private stagingBucketInToolkitStack(): boolean {
// determine if the staging bucket exists in the
// bootstrap stack (user might have removed it in case of a custom bootstrap template)
throw new Error('TODO');
}

private bootstrapVersion(): number {
// grab the current bootstrap stack version from the customer account
throw new Error('TODO');
}

@mrgrain mrgrain changed the title feat: disallow cross account asset publishing is some scenarios feat: disallow cross account asset publishing in some scenarios Oct 3, 2024
@rix0rrr rix0rrr self-assigned this Oct 3, 2024
@jiayiwang7 jiayiwang7 assigned HBobertz and unassigned rix0rrr Oct 9, 2024
@@ -141,7 +141,7 @@ The following json shows the current recommended set of flags, as `cdk init` wou
"@aws-cdk/aws-s3:keepNotificationInImportedBucket": false,
"@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true,
"@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true,
"@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true
"@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true,
Copy link
Contributor

Choose a reason for hiding this comment

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

This was automatically done as part of the build 🤷‍♂️

packages/aws-cdk/lib/api/util/checks.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/util/checks.ts Outdated Show resolved Hide resolved
Comment on lines 36 to 39
// Check if we can retrieve the bucketName from the output
const bucketNameOutput = response.Stacks[0].Outputs?.find(
output => output.OutputKey === 'BucketName',
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the logical ID of the resource (DescribeStackResources).

packages/aws-cdk/lib/api/util/checks.ts Outdated Show resolved Hide resolved
packages/aws-cdk/lib/api/util/checks.ts Outdated Show resolved Hide resolved
@HBobertz HBobertz added pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested and removed pr/needs-cli-test-run This PR needs CLI tests run against it. labels Oct 10, 2024
@@ -287,8 +288,15 @@ export async function deployStack(options: DeployStackOptions): Promise<DeploySt
options.envResources,
options.sdk,
options.overrideTemplate);
let bootstrapStackName: string | undefined;
try {
bootstrapStackName = (await options.envResources.lookupToolkit()).stackName;
Copy link
Contributor

Choose a reason for hiding this comment

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

This performs a lookupToolkit, which returns ToolkitInfo, which has most of the information already.

Why can't the logic live there?

Copy link
Contributor

@HBobertz HBobertz Oct 11, 2024

Choose a reason for hiding this comment

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

I actually tried that first, but it started getting a bit ugly because that's an abstract class and we have a "we don't exist yet" version of that class, which kept breaking tests and was difficult to mock properly. So I just said screw it and refactored the change into functional util, which was much easier to test and mock.

@HBobertz HBobertz added the pr-linter/exempt-test The PR linter will not require test changes label Oct 12, 2024
@HBobertz
Copy link
Contributor

HBobertz commented Oct 12, 2024

Due to the nature of the change this is essentially not integ-testable so we will not be adding an integ test for this. Added pr-linter/exempt-test flag

@HBobertz
Copy link
Contributor

Recreated the vulnerability. Got this exception when my bootstrap version is less than 21:

TempAppStack: fail: ❗❗ UNEXPECTED BUCKET OWNER DETECTED ❗❗ 
        
              We've detected that the S3 bucket cdk-hnb659fds-assets-446584840139-us-east-1 was 
              originally created in account 446584840139 as part of the CloudFormation stack CDKToolkit, 
              but now resides in a different AWS account. To prevent cross-account asset bucket access of your 
              deployments, CDK will stop now.

              If this situation is intentional and you own the AWS account that the bucket has moved to, remove the 
              resource named StagingBucket from the template of CloudFormation stack CDKToolkit and try again. 
              
              If this situation is not intentional, we strongly recommend auditing your account to make sure all 
              resources are configured the way you expect them [1]. For questions or concerns, please contact 
              AWS Support [2]. 
              
              [1] https://repost.aws/knowledge-center/potential-account-compromise
              
              [2] https://aws.amazon.com/support

And no such failure when our bootstrap version has been updated

This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:

IAM Statement Changes
┌───┬─────────────────────────────────┬────────┬────────────────┬──────────────────────────────┬───────────┐
│   │ Resource                        │ Effect │ Action         │ Principal                    │ Condition │
├───┼─────────────────────────────────┼────────┼────────────────┼──────────────────────────────┼───────────┤
│ + │ ${rootFunction/ServiceRole.Arn} │ Allow  │ sts:AssumeRole │ Service:lambda.amazonaws.com │           │
└───┴─────────────────────────────────┴────────┴────────────────┴──────────────────────────────┴───────────┘
IAM Policy Changes
┌───┬─────────────────────────────┬────────────────────────────────────────────────────────────────────────────────┐
│   │ Resource                    │ Managed Policy ARN                                                             │
├───┼─────────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤
│ + │ ${rootFunction/ServiceRole} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole │

Which is the intended functionality

@HBobertz HBobertz marked this pull request as ready for review October 12, 2024 20:49
@aws-cdk-automation
Copy link
Collaborator

➡️ PR build request submitted to test-main-pipeline ⬅️

A maintainer must now check the pipeline and add the pr-linter/cli-integ-tested label once the pipeline succeeds.

@HBobertz HBobertz added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Oct 12, 2024
@HBobertz HBobertz requested a review from rix0rrr October 12, 2024 20:52
@HBobertz HBobertz added pr-linter/exempt-readme The PR linter will not require README changes and removed pr-linter/exempt-test The PR linter will not require test changes labels Oct 12, 2024
@HBobertz
Copy link
Contributor

I also added readme change since this isn't really a feat, more like a security fix but I'm keeping what eli designated the pr as. If we want readme changes feel free to request changes or remove the flag

@aws-cdk-automation aws-cdk-automation dismissed their stale review October 12, 2024 20:56

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

Copy link
Contributor

mergify bot commented Oct 14, 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).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@mergify mergify bot merged commit edd031d into main Oct 14, 2024
11 of 12 checks passed
Copy link
Contributor

mergify bot commented Oct 14, 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).

@mergify mergify bot deleted the epolon/maybe-allow-cross-account-publishing branch October 14, 2024 14:54
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution/core This is a PR that came from AWS. p2 pr-linter/cli-integ-tested Assert that any CLI changes have been integ tested pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exempt-readme The PR linter will not require README changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants