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(cloudtrail): do not attach s3 bucket permission when orgId is not set for organization trail #30778

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sarisia
Copy link
Contributor

@sarisia sarisia commented Jul 7, 2024

Recreate #30490

Issue # (if applicable)

no open issue

Reason for this change

Organization trail without orgId attaches improper s3 bucket policy
which allows cloudtrail to write to /AWSLogs/undefined/* prefix.

                        {
                            "Action": "s3:PutObject",
                            "Condition": {
                                "StringEquals": {
                                    "s3:x-amz-acl": "bucket-owner-full-control",
                                    "aws:SourceArn": {
                                        "Fn::Join": [
                                            "",
                                            [
                                                "arn:",
                                                {
                                                    "Ref": "AWS::Partition"
                                                },
                                                ":cloudtrail:us-east-1:123456789012:trail/undefined"
                                            ]
                                        ]
                                    }
                                }
                            },
                            "Effect": "Allow",
                            "Principal": {
                                "Service": "cloudtrail.amazonaws.com"
                            },
                            "Resource": {
                                "Fn::Join": [
                                    "",
                                    [
                                        {
                                            "Fn::GetAtt": [
                                                "TrailS30071F172",
                                                "Arn"
                                            ]
                                        },
                                        "/AWSLogs/undefined/*"
                                    ]
                                ]
                            }
                        }

Description of changes

  • do not attach s3 bucket policy if isOrganizationTrail is set and orgId is not set
    • generate warning if attaching s3 bucket policy is skipped

Description of how you validated changes

  • added unit test

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 July 7, 2024 18:29
@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 labels Jul 7, 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.

@sarisia
Copy link
Contributor Author

sarisia commented Jul 7, 2024

Exemption Request: deploying this requires AWS organization management account

@aws-cdk-automation aws-cdk-automation added pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Jul 7, 2024
@sarisia
Copy link
Contributor Author

sarisia commented Oct 15, 2024

could someone review this please? I'm waiting this for 4 months, thanks!

@GavinZZ
Copy link
Contributor

GavinZZ commented Dec 12, 2024

@sarisia Hello, apologize for the extraa long wait time on the PR review. This seems to be an accident when we introduced orgId field and somehow missed the validation. I think this PR makes sense to me, but just curious what's your use case? Are you using organization trail without orgId?

The reason I asked this is because I'm thinking why don't we validate and raise an error if organization trail is used but no orgId is provided.

@GavinZZ GavinZZ added pr-linter/exempt-readme The PR linter will not require README changes pr-linter/exempt-integ-test The PR linter will not require integ test changes labels Dec 12, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review December 12, 2024 23:53

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

@sarisia
Copy link
Contributor Author

sarisia commented Dec 13, 2024

@GavinZZ Thank you for reviewing!

I think this PR makes sense to me, but just curious what's your use case? Are you using organization trail without orgId?

Actually yes. Back in 2023 there were no orgId prop in cloudtrail.Trail so I have to write the code which manually attaches insufficient permissions, like this:

        # trail
        trail = cloudtrail.Trail(
            self,
            "Trail",
            bucket=bucket,
            is_organization_trail=True
        )

        # add bucket policy
        # https://docs.aws.amazon.com/ja_jp/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html
        cloudtrail_principal = iam.ServicePrincipal(
            "cloudtrail.amazonaws.com"
        )
        bucket.add_to_resource_policy(
            iam.PolicyStatement(
                actions=["s3:GetBucketAcl"],
                principals=[cloudtrail_principal],
                resources=[bucket.bucket_arn],
            )
        )
        bucket.add_to_resource_policy(
            iam.PolicyStatement(
                actions=["s3:PutObject"],
                principals=[cloudtrail_principal],
                resources=[
                    bucket.arn_for_objects(f"AWSLogs/{Stack.of(self).account}/*"),
                    bucket.arn_for_objects(
                        f"AWSLogs/{organization_principal.organization_id}/*"
                    ),
                ],
                conditions={
                    "StringEquals": {"s3:x-amz-acl": "bucket-owner-full-control"}
                },
            )
        )

Recently I upgraded CDK and found there's a diff with new weird permission attached to the bucket, so I'm trying to get rid of them without breaking deploys and forcing code changes.

@sarisia
Copy link
Contributor Author

sarisia commented Dec 13, 2024

Actually there's another issue which props.trailName in generated policy becomes undefined too.

Initially I tried to fix them in separated PR (#30489) which makes props.trailName required but now I think it's better to fix this PR to check both props.orgId and props.trailName without making them required. WDYT?

@xazhao xazhao removed the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Jan 13, 2025
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.41%. Comparing base (64b865b) to head (d7574c8).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #30778   +/-   ##
=======================================
  Coverage   81.41%   81.41%           
=======================================
  Files         223      223           
  Lines       13721    13721           
  Branches     2416     2416           
=======================================
  Hits        11171    11171           
  Misses       2271     2271           
  Partials      279      279           
Flag Coverage Δ
suite.unit 81.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 80.75% <ø> (ø)
packages/aws-cdk-lib/core 82.10% <ø> (ø)

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@GavinZZ
Copy link
Contributor

GavinZZ commented Jan 16, 2025

@sarisia sorry for the delayed response. I think that makes sense. Let's fix both issue here.

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/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. 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.

5 participants