-
Notifications
You must be signed in to change notification settings - Fork 4k
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
(aws-cloudfront): Bucket policy permissions broke CloudFront integration #13272
Comments
As described by @njlynch in the PR, the official documentation states that only |
Hmm interesting... I've pushed this same change to all of our CF distros this morning w/o any issues (went from cdk 1.89.0 to 1.91.0 though). Just out of curiosity can you please check if your CloudFormation stack drifted by any chance? Also, have you also created a bucket using the same stack... or is it an imported pre-existing standalone bucket? ...
[~] AWS::S3::BucketPolicy dynamap-test-bucket/Policy dynamaptestbucketPolicy974ACB96
└─ [~] PolicyDocument
└─ [~] .Statement:
└─ @@ -25,11 +25,7 @@
[ ] "Sid": "HttpsOnly"
[ ] },
[ ] {
[-] "Action": [
[-] "s3:GetObject*",
[-] "s3:GetBucket*",
[-] "s3:List*"
[-] ],
[+] "Action": "s3:GetObject",
[ ] "Effect": "Allow",
[ ] "Principal": {
[ ] "CanonicalUser": {
@@ -39,27 +35,19 @@
[ ] ]
[ ] }
[ ] },
[-] "Resource": [
[-] {
[-] "Fn::GetAtt": [
[-] "dynamaptestbucket7FE81451",
[-] "Arn"
[+] "Resource": {
[+] "Fn::Join": [
[+] "",
[+] [
[+] {
[+] "Fn::GetAtt": [
[+] "dynamaptestbucket7FE81451",
[+] "Arn"
[+] ]
[+] },
[+] "/*"
[ ] ]
[-] },
[-] {
[-] "Fn::Join": [
[-] "",
[-] [
[-] {
[-] "Fn::GetAtt": [
[-] "dynamaptestbucket7FE81451",
[-] "Arn"
[-] ]
[-] },
[-] "/*"
[-] ]
[-] ]
[-] }
[-] ]
[+] ]
[+] }
[ ] }
[ ] ] |
@ferdingler - Sorry for the breakage here! While we work on finding the right balance and fix here, the work-around mentioned in the linked PR should work to get you back up and running: // Before
const bucket = new s3.Bucket(this, 'Bucket');
const s3Origin = new origins.S3Origin(this, 'S3Origin', { bucket });
// After
const bucket = new s3.Bucket(this, 'Bucket');
const originAccessIdentity = new cloudfront.OriginAccessIdentity(this, 'OAI');
bucket.grantRead(originAccessIdentity);
const s3Origin = new origins.S3Origin(this, 'S3Origin', { bucket, originAccessIdentity }); I'd like to learn more about your use case, as I'm not sure I understand what the behavior was like for you prior to the upgrade.
When you say
Is this all under a single behavior and origin, or are there multiple behaviors/origins set up? If so, is the behavior for /admin different from the one for the root ('/')? The only impact I can see from the bucket policy change is when visiting the domain root without a default root object; before, you got a XML listing of the contents, and after, you get an access denied message. I'd love to better understand what else changes that I haven't yet been able to discover or reproduce. |
@njlynch thanks for the recommended work-around. Here is some additional information about our usecase: We have a ReactJS application hosted on the S3 bucket that is fronted by the CloudFront distribution via OAI. The S3 bucket has encryption enabled using the default S3 master-key SSE-S3. The CloudFront distribution has only 1 origin (the S3 bucket) and it has index.html configured as default root object. Additionally, there is a Lambda@Edge function that runs on the CloudFront distribution on Origin Response that appends some security headers to the CloudFront responses. Our codebase is open source, so you can take a look at the CDK construct that creates the setup I just described if it helps:
I'm puzzled by this as well because like I mentioned above, we have a Single Page Application (React) that once it loads on the user's browser, any path after the domain name should be handled by react router within the app itself and it shouldn't trigger another request to CloudFront. I need to dive deeper on this part because I can't explain why it works on the root domain but it doesn't work on any path after that. What is true though is that adding back the |
Ah, the code is super helpful, thanks! In your setup before, a user going to |
Per offline conversation, I'm going to close this out. I think the above error handler change is a reasonable workaround. Please feel free to reopen if you have further questions. |
|
Thanks for the quick response on this ticket @njlynch . Just in case anyone runs into this situation in the future, here is the change we have to make in our CloudFront distribution as per Nick's suggestion: const distribution = new cloudFront.CloudFrontWebDistribution(
this,
"CloudFrontDistribution",
{
errorConfigurations: [
{
errorCode: 404,
responseCode: 200,
responsePagePath: "/index.html",
},
{
errorCode: 403, // this is the new addition due to the bucket policy returning 403
responseCode: 200,
responsePagePath: "/index.html",
},
],
originConfigs: [
{
s3OriginSource: {
s3BucketSource: this.frontendBucket,
originAccessIdentity: originAccess,
},
},
],
}
); |
After upgrading
@aws-cdk/aws-cloudfront
fromv1.74.0
tov1.90.1
our CloudFront distribution was no longer able to access contents on the S3 bucket via OAI getting an AccessDenied error.After doing some research I realized that the bucket policy changed from this:
to this:
Accessing the CloudFront URL from the root works fine https://123.cloudfront.net but the problem is when accessing a specific path like https://123.cloudfront.net/admin then it gives an AccessDenied error.
Reproduction Steps
Upgraded @aws-cdk/aws-cloudfront to v1.90.1
What did you expect to happen?
CloudFront distribution should be able to continue to access objects in the S3 bucket.
What actually happened?
Accessing CloudFront url now gives an Access Denied error:
Environment
This is 🐛 Bug Report
The text was updated successfully, but these errors were encountered: