Skip to content

X-account S3 grantRead() does not respect objectKeysPattern #34545

Open
@AaronLeon

Description

@AaronLeon

Describe the bug

Using S3 bucket.grantRead to grant x-account access successfully creates a bucket policy statement but does not restrict the resource correctly

bucket.grantRead(new ArnPrincipal(arn:aws:iam::XXXXXXXXXX:role/x-account-role), 's3-prefix/*');

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

The bucket policy restricts to the S3 prefix I provided

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXXXXX:role/x-account-role"
            },
            "Action": [
                "s3:GetObject*",
                "s3:GetBucket*",
                "s3:List*"
            ],
            "Resource": [
                "arn:aws:s3:::bucket-name/s3-prefix/*"
            ]
        }
    ]
}

Current Behavior

The bucket policy grants read to the entire bucket

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXXXXX:role/x-account-role"
            },
            "Action": [
                "s3:GetObject*",
                "s3:GetBucket*",
                "s3:List*"
            ],
            "Resource": [
                "arn:aws:s3:::bucket-name",
                "arn:aws:s3:::bucket-name/s3-prefix/*"
            ]
        }
    ]
}

Reproduction Steps

dataBucket = new SecureBucket(this, "DataBucket", {
    bucketName: props.bucketName,
    encryption: BucketEncryption.KMS,
    enforceSSL: true,
    versioned: true,
    bucketKeyEnabled: true,
    lifecycleRules: [
        {
            expiredObjectDeleteMarker: true,
            noncurrentVersionExpiration: Duration.days(30),
        },
    ],
});
bucket.grantRead(new ArnPrincipal(arn:aws:iam::XXXXXXXXXX:role/x-account-role), 's3-prefix/*');

Possible Solution

No response

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

2.180.0

AWS CDK CLI version

2.1016.1

Node.js Version

20

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-s3Related to Amazon S3bugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions