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

[Bug] Access denied when allowing public reads on newly created bucket #56

Open
iamogbz opened this issue Jan 6, 2024 · 1 comment
Open

Comments

@iamogbz
Copy link

iamogbz commented Jan 6, 2024

Failure while deploying build using aws-spa

[S3] ✏️ Allow public read to "s3.bucket.domain"...
💥 Access Denied

Reason due to initial bucket creation having the Block public access (bucket settings) - All setting enabled.

Can be fixed by adding a remove block public access step before the allow public read bucket policy update.

export const setBucketPolicy = async (bucketName: string) => {
  logger.info(`[S3] ✏️ Allow public read to "${bucketName}"...`);
  // remove public access block
  await s3
    .putPublicAccessBlock({
      Bucket: bucketName,
      PublicAccessBlockConfiguration: {
        BlockPublicAcls: false,
        IgnorePublicAcls: false,
        BlockPublicPolicy: false,
        RestrictPublicBuckets: false,
      },
    })
    .promise();
  // allow public reads
  return s3
    .putBucketPolicy({
      Bucket: bucketName,
      Policy: JSON.stringify({
        Statement: [
          {
            Sid: "AllowPublicRead",
            Effect: "Allow",
            Principal: {
              AWS: "*",
            },
            Action: "s3:GetObject",
            Resource: `arn:aws:s3:::${bucketName}/*`,
          },
        ],
      }),
    })
    .promise();
};

at

aws-spa/src/s3.ts

Lines 117 to 137 in 6031af3

export const setBucketPolicy = (bucketName: string) => {
logger.info(`[S3] ✏️ Allow public read to "${bucketName}"...`);
return s3
.putBucketPolicy({
Bucket: bucketName,
Policy: JSON.stringify({
Statement: [
{
Sid: "AllowPublicRead",
Effect: "Allow",
Principal: {
AWS: "*",
},
Action: "s3:GetObject",
Resource: `arn:aws:s3:::${bucketName}/*`,
},
],
}),
})
.promise();
};

@iamogbz iamogbz changed the title Access denied when allowing public reads on newly created bucket [Bug] Access denied when allowing public reads on newly created bucket Jan 6, 2024
@GregdTd
Copy link
Contributor

GregdTd commented May 27, 2024

Should be resolved by: #58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants