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

static-site construct will not deploy #320

Closed
ktwbc opened this issue Apr 19, 2023 · 18 comments · Fixed by #384
Closed

static-site construct will not deploy #320

ktwbc opened this issue Apr 19, 2023 · 18 comments · Fixed by #384
Labels
bug Something isn't working

Comments

@ktwbc
Copy link

ktwbc commented Apr 19, 2023

Description

As of April 2023, the default security on an S3 bucket is to Block Public access. This creates a conflict with the native behavior of the static-site construct which sets a Policy s3:GetObject using AWS:"*" as the Principle. The deployment will fail with an API: Access Denied error when using this with lift and serverless.

How to Reproduce

1

Additional Information

No response

@ktwbc ktwbc added the bug Something isn't working label Apr 19, 2023
@joconor
Copy link

joconor commented Apr 20, 2023

I'm also running into this. 100% fatal issue for the static-website construct

@ktwbc
Copy link
Author

ktwbc commented Apr 20, 2023

single-page-app will work as a workaround for now

@mnapoli
Copy link
Member

mnapoli commented Apr 20, 2023

Thanks for the report, anyone has a good suggestion for a solution/PR?

@joconor
Copy link

joconor commented Apr 21, 2023

FYI, it looks like this is Amazon's announcement of the change in default policy: Advanced Notice: Amazon S3 will automatically enable S3 Block Public Access and disable access control lists for all new buckets starting in April 2023

The exact error that occurs is API: s3:PutBucketPolicy Access Denied while creating the webBucketPolicy for the publicly accessible web bucket.

I wish I could suggest a solution, but I'm afraid that at this time, the problem exceeds the limits of my AWS policy/permissions knowledge. Of course, this is a big reason why I use Serverless Framework & serverless-lift.

@peebam
Copy link

peebam commented Apr 24, 2023

Workaround : you can disable the BlocPublicPolicy with the PublicAccessBlockConfiguration property of the S3 bucket CloudFormation structure. Use the extension property of your Lift construct :

extensions: {
  bucket: {
    Properties: {
      PublicAccessBlockConfiguration: {
        BlockPublicPolicy: false,
      },
    },
  },
},

@joconor
Copy link

joconor commented Apr 24, 2023

@peebam Since the bucket is created by serverless-lift, how do you know the BucketName?

@peebam
Copy link

peebam commented Apr 25, 2023

The BuckName property is not mandatory. In this extract of code, we manage the bucket name. I fixed my comment.

@joconor
Copy link

joconor commented Apr 25, 2023

@peebam Thanks for that workaround! Looks like that's working

@hacknaked
Copy link

Workaround : you can disable the BlocPublicPolicy with the PublicAccessBlockConfiguration property of the S3 bucket CloudFormation structure. Use the extension property of your Lift construct :

extensions: {
  bucket: {
    Properties: {
      PublicAccessBlockConfiguration: {
        BlockPublicPolicy: false,
      },
    },
  },
},

(dummy question here)
Where should I put this snippet? I guess is not in serverless.yml given that is not in yml format.

@raffclar
Copy link

Workaround : you can disable the BlocPublicPolicy with the PublicAccessBlockConfiguration property of the S3 bucket CloudFormation structure. Use the extension property of your Lift construct :

extensions: {
  bucket: {
    Properties: {
      PublicAccessBlockConfiguration: {
        BlockPublicPolicy: false,
      },
    },
  },
},

(dummy question here) Where should I put this snippet? I guess is not in serverless.yml given that is not in yml format.

Like so:

constructs:
  landing:
    type: static-website
    path: public
    extensions:
      bucket:
        Properties:
          PublicAccessBlockConfiguration:
            BlockPublicPolicy: false

@InvisibleKind
Copy link

For me even the suggested snippet doesn't help. The basic static-website construct fails with a message:

Error:
CREATE_FAILED: buildpublicBucketHASH (AWS::S3::Bucket)
Resource handler returned message: "Access Denied (Service: S3, Status Code: 403, Request ID: -cut-, Extended Request ID: -cut-)" (RequestToken: -cut-, HandlerErrorCode: GeneralServiceException)

The serverless user is allowed to create S3 Buckets, of course. Moreover, if I change the static-website to single-page-app, no error appears and deploy works in a normal way.

@sean-ac
Copy link

sean-ac commented Dec 28, 2023

The issue is here:

getBucketProps(): BucketProps {
return {
// Enable static website hosting
websiteIndexDocument: "index.html",
websiteErrorDocument: this.errorPath(),
// public read access is required when enabling static website hosting
publicReadAccess: true,
// For a static website, the content is code that should be versioned elsewhere
removalPolicy: RemovalPolicy.DESTROY,
};
}

This object needs to now return a BlockPublicAccess object, with every field set to false.

Docs: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.BlockPublicAccess.html

Example:

      blockPublicAccess: new BlockPublicAccess({
        blockPublicAcls: false,
        blockPublicPolicy: false,
        ignorePublicAcls: false,
        restrictPublicBuckets: false
      })

(You will need to import BlockPublicAccess from aws-cdk-lib/aws-s3)

I solved this by doing a local patch (via yarn patch serverless-lift), and modifying the .js files directly.

@kevincerro-dvrv
Copy link

kevincerro-dvrv commented Feb 19, 2024

I will try to fix this on a PR

@richard-stafflink
Copy link

The latest update of aws-cdk-lib v2.144.0 now checks this and breaks.

@rj-xy
Copy link

rj-xy commented Jun 5, 2024

The latest update of aws-cdk-lib v2.144.0 now checks this and breaks.

An update on this: the latest aws-cdk-lib package has had an update, so now even single-page-app will not work.

@mnapoli are there plans of updating the new defaults required when creating the bucket?

I'm happy to help with the change/update to resolve this, if you provide some direction.

ATM the only workarounds are:

  1. Using the extensions example above - preferred
  2. Pin package.json resolution version for aws-cdk-lib (not desirable)
  3. Yarn patch - not convenient
  4. Use one of the forked versions of this lib - not ideal long term

@mnapoli
Copy link
Member

mnapoli commented Jun 5, 2024

Please confirm if #384 fixes the problem.

@kizza
Copy link

kizza commented Jun 22, 2024

Please confirm if #384 fixes the problem.

Happy to confirm it does indeed 🎈 My static-website construct was raising the following during deployment:

Error: Cannot use 'publicReadAccess' property on a bucket without allowing bucket-level public access through 'blockPublicAceess' property.

Which while not the precise error ("Access denied" above, seems to be the same problem). It deployed perfectly using yarn link serverless-lift to @kevincerro's #384 locally

I'll use this brief opportunity to say thank you @mnapoli - this repo has been awesome to discover 🙏

@ktwbc
Copy link
Author

ktwbc commented Jul 7, 2024

yes worked for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.