aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

S3 Bucket Policy Changes Not Recognized As A Change on CDK Deploy

Open bensoer opened this issue 6 years ago • 24 comments

When making changes to a bucket policy from a pre-existing bucket, applying changes to its Policy are not applied. The CDK seems to act as if no changes are needed

Reproduction Steps

Note that I have changed the names of things in this example to simplify and avoid disclosing

Adding the following code to my application to edit a pre-existing bucket's bucket policy so that other resources may get to it which may or may not have been created with the CDK

const myPreExistingBucket = s3.Bucket.fromBucketName(this, 'MyPreExistingBucket-Lookup-ID', "mypreexistingbucket");
myPreExistingBucket.addToResourcePolicy(new iam.PolicyStatement({
            actions:[
                "s3:*"
            ],
            resources:[
                "arn:aws:s3:::mypreexistingbucket",
                "arn:aws:s3:::mypreexistingbucket/*"
            ],
            principals:[
                new iam.AccountPrincipal("arn:aws:iam::XXXXXXXXXXXX:root")
            ]
        }));

Then deploy with the CDK: cdk -i --region us-east-1 --app 'npx --quiet ts-node app.ts' deploy --profile datascience

Error Log

Error message is not an error but a false positive in that there are no changes needing to be applied, when there are. Checking the account as well shows no updates in the cloud formation templates and the Bucket Policy not being applied to the Bucket

Environment

  • CLI Version : Attempted with v1.26.0 and v1.18.0
  • Framework Version: Nodejs - v12.16.1, NPM - v6.13.4
  • OS : MAC OS Mojave
  • Language : Typescript - v3.7.4

Other


This is :bug: Bug Report

bensoer avatar Mar 02 '20 21:03 bensoer

Hi @bensoer

This is actually the desired behavior. Imported/Existing resources are not strictly speaking a part of the CloudFormation stack. Notice that if you simply do:

s3.Bucket.fromBucketName(this, 'MyPreExistingBucket-Lookup-ID', 
  "mypreexistingbucket");

you will get an empty stack. Existing resources can be used for referencing by other stack resources, but they cannot be mutated. Since this might break its previous, unmanaged consumers. Having said that, we acknowledge the use case for this, and actually CloudFormation have added support for explicitly importing existing resources, but this isn't yet supported in the CDK.

There are ways around this, like using the AwsCustomResource construct.

We encourage you to open a feature request that details your use case :)

iliapolo avatar Mar 08 '20 16:03 iliapolo

If i am not mistaken, the ressource mutation fails silently. IMHO this should trigger en error message

BenoitDel avatar Mar 16 '20 14:03 BenoitDel

@BenoitDel You're right. Actually, the correct behavior would be to not even expose the addToResourcePolicy method in this case.

However, i want to make a small correction to a statement i previously made:

Existing resources can be used for referencing by other stack resources, but they cannot be mutated.

Thats not always the case, it actually depends on CloudFormation capabilities, but in principal, CDK does not enforce this (i.e, some imported resources can be updated).

So in reality, we should either properly support this scenario (in which case, this is just a bug), or, if this scenario is not supported, we should hide the addToResourcePolicy method altogether.

Thanks for reporting this, we will take a look.

iliapolo avatar Mar 29 '20 21:03 iliapolo

So in reality, we should either properly support this scenario (in which case, this is just a bug), or, if this scenario is not supported, we should hide the addToResourcePolicy method altogether.

Remember that a Bucket is also an IBucket, which means just having an IBucket, we don't know whether it's mutable or not. In many cases, it makes sense to have a method like addToResourcePolicy on IBucket, even if an imported bucket doesn't support it. One of the patterns we use is to return a boolean value indicating whether a change was actually made.

This will become even more important, as we move to our improved CfnInclude experience (#7304), and we will want to have an L1-to-L2 transition, which will result in mutable IBuckets.

skinny85 avatar Apr 29 '20 23:04 skinny85

@skinny85 Do you think it makes sense to specifically have addToResourcePolicy on IBucket at the moment? even though always returns statementAdded: false?

iliapolo avatar Aug 20 '20 10:08 iliapolo

@skinny85 Do you think it makes sense to specifically have addToResourcePolicy on IBucket at the moment? even though always returns statementAdded: false?

Yes. Not sure what you mean by "it always returns statementAdded: false"...? It doesn't: https://github.com/aws/aws-cdk/blob/f5dbed6c2615cff4c7c868d5f7073e42665e07ca/packages/%40aws-cdk/aws-s3/lib/bucket.ts#L459-L459

skinny85 avatar Aug 31 '20 15:08 skinny85

@skinny85 Notice that the full code reads:

https://github.com/aws/aws-cdk/blob/745922aa5a5a0195869830b54d7e529bec83e37c/packages/%40aws-cdk/aws-s3/lib/bucket.ts#L452-L462

When the bucket is an Import, policy is undefined, and autoCreatePolicy is always false:

https://github.com/aws/aws-cdk/blob/745922aa5a5a0195869830b54d7e529bec83e37c/packages/%40aws-cdk/aws-s3/lib/bucket.ts#L1155-L1156

So we end up here:

https://github.com/aws/aws-cdk/blob/745922aa5a5a0195869830b54d7e529bec83e37c/packages/%40aws-cdk/aws-s3/lib/bucket.ts#L462

iliapolo avatar Sep 01 '20 14:09 iliapolo

That is true that today, but might not be in the future. For example, we might have a a fromCfnBucket(): IBucket in the future (https://github.com/aws/aws-cdk/issues/9719) that returns a mutable implementation of IBucket.

skinny85 avatar Sep 01 '20 16:09 skinny85

just experienced this issue. An error would be nice as cdk fails silently :(

philhorrocks avatar Oct 12 '20 21:10 philhorrocks

The silent failure made this issue difficult to identify, so +1 on that.

There does seem to be a workaround for this specific goal assuming you want to CREATE a new bucket policy on an existing bucket and don't mind overwriting a bucket policy that already exists.

  1. The bucket policy on the target s3 bucket was empty for me.
  2. The "BucketPolicy" resource in cloudformation is separate from the Bucket resource, so I hoped that it could be managed independently (eg. the "bucket.grantRead" policy could have an overrideExistingBucketPolicy flag). That doesn't seem to be the case with the aws-s3.BucketPolicy resource.

However, there seems to be a different resource type named CfnBucketPolicy. I was able to create a CfnBucketPolicy that puts the BucketPolicy on an existing bucket. My goal was to attach a cloudfront origin access identity, but you can use a different PolicyStatement to grant access to whatever resource you want.

   // hastily copied the imports over, so some might be missing, sorry!
  import { Bucket, CfnBucketPolicy, IBucket } from '@aws-cdk/aws-s3';
  import { OriginAccessIdentity } from '@aws-cdk/aws-cloudfront';
  import { S3Origin } from '@aws-cdk/aws-cloudfront-origins'
  import { PolicyDocument, PolicyStatement } from '@aws-cdk/aws-iam';
  ...

  const existingBucketName = 'asfhaosfhasofhaowhfoawfhawf'

    const existingBucket = Bucket.fromBucketAttributes(this, 'ExistingBucket', {
      bucketArn: `arn:aws:s3:::${existingBucketName}`,
    });

    const originAccessIdentity = new OriginAccessIdentity(this, 'originAccessIdentity', {
      comment: `CloudFront access to ${existingBucketName}`,
    });

    // See: https://github.com/aws/aws-cdk/issues/941
    const policyStatement = new PolicyStatement();
    policyStatement.addActions('s3:GetBucket*');
    policyStatement.addActions('s3:GetObject*');
    policyStatement.addActions('s3:List*');
    policyStatement.addResources(existingBucket.bucketArn);
    policyStatement.addResources(`${existingBucket.bucketArn}/*`);
    policyStatement.addCanonicalUserPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId);

    const bucketPolicy = new CfnBucketPolicy(this, 'cloudfrontAccessBucketPolicy', {
      bucket: existingBucket.bucketName,
      policyDocument: new PolicyDocument({
        statements: [
          policyStatement
        ]
      })
    });

Disclaimer, I'm not sure how CfnBucketPolicy is different from BucketPolicy, so I'm a little suspicious of this approach... but it appears to be working. It would be nice to have a sanity check from the cdk team on this one

ajhool avatar Mar 10 '21 19:03 ajhool

CfnBucketPolicy is the underlying resource implementation of BucketPolicy. See Using L1 constructs.

It's perfectly valid and safe to use. Though in this case BucketPolicy could also be used:

const bucketPolicy = new BucketPolicy(this, 'cloudfrontAccessBucketPolicy', {
  bucket: existingBucket,
})
bucketPolicy.document.addStatements(...)

iliapolo avatar Mar 11 '21 19:03 iliapolo

would love it if anyone can share a workaround that doesn't overwrite the bucket policy of the imported bucket. @ajhool's snippet works great apart from overwriting the existing policy (as they point out).

Is there any way to get the existing policies on an imported bucket? .policy on an IBucket is undefined, as @iliapolo points out above

alex9311 avatar Apr 15 '21 17:04 alex9311

Yup, this also applies to imported KMS keys. I'm trying to grand a lambda function in Stack B permissions to decrypt a key on a bucket (both in Stack A) by importing the key from A into B and adding to the resource policy. But when printing a response I always get statementAdded: false.

Changing the allow/deny on the resource policy has no effect (CDK reports no changes) so yes, this silent failing is quite hard to notice and rather annoying.

JamieMcKernanKaizen avatar Jun 02 '21 08:06 JamieMcKernanKaizen

I was in what can best be described as psychic pain trying to get this working: having problems with CloudFront OAI to S3 restricted. The silently failing bit tripped me up for a while. Finally, noticed the failure to apply the policy response. When I found this thread.

I was all prepared to implement the @ajhool recommendation when I scrolled a bit further and saw the @iliapolo comment. It worked like a charm. Here's some code if anyone needs it:

const oai = new cloudfront.OriginAccessIdentity(this, "cf-oai", {
  comment: `Cloudfront to reach ${bucketName}`,
});

const policyStatement = new iam.PolicyStatement({
  actions:    [ 's3:GetObject' ],
  resources:  [ this.bucket.arnForObjects("*") ],
  principals: [ oai.grantPrincipal ],
});

const bucketPolicy = new s3.BucketPolicy(this, 'cloudfrontAccessBucketPolicy', {
  bucket: bucket,
})
bucketPolicy.document.addStatements(policyStatement);

codeedog avatar Jun 27 '21 02:06 codeedog

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jul 27 '21 08:07 github-actions[bot]

I'm kinda facing similar issue. Is there a way to append the statements to existing policy in the existing bucket? The solutions discussed here just overwrites the whole bucket policy.

omishagupta avatar Mar 23 '22 07:03 omishagupta

To anyone running into an Access Denied permissions error trying to hit their s3 bucket via Cloudfront using this fix, make sure pass in the origin access identity to which you granted bucket permissions to your S3 origin:

const oai = new cloudfront.OriginAccessIdentity(this, "cf-oai", {
  comment: `Cloudfront to reach ${bucketName}`,
});
...
const myS3Origin = new origins.S3Origin(myBucket, {
  originAccessIdentity: oai,
});

David-Tamrazov avatar Aug 15 '22 23:08 David-Tamrazov

@iliapolo - I was having a new issue, the stack was failing saying a policy already exists - new workarounds..?

const bucketPolicy = new BucketPolicy(this, 'cloudfrontAccessBucketPolicy', {
  bucket: existingBucket,
})
bucketPolicy.document.addStatements(...)

asri6725 avatar Sep 02 '22 01:09 asri6725

@otaviomacedo Thanks for the docs! But I'm not sure the linked issue should count to close this issue – having to check for a success value isn't very ergonomic and isn't obvious to an end user. (Obviously we should RTFM but as far as I know, checking for success isn't a pattern that is generally accepted or useful in CDK; we want things to yell at us if they aren't applied.)

ostrowr avatar Jun 01 '23 00:06 ostrowr

As @asri6725 said a year ago, it is still throwing "policy already exists" error. Any new workarounds? I am about to attempt to use a custom resource to update the policy... But it's quite frustrating having to use a lambda & custom code just to do it 🤡 🤷‍♂️

renatoargh avatar Aug 14 '23 04:08 renatoargh