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

Open
ktwbc opened this issue Apr 19, 2023 · 13 comments · May be fixed by #384
Open

static-site construct will not deploy #320

ktwbc opened this issue Apr 19, 2023 · 13 comments · May be 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

@kevincerro kevincerro linked a pull request Feb 20, 2024 that will close this issue
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.

9 participants