3
votes

I am trying to setup Cloudflare to cache images from S3. I want to be as restrictive (least permissive) as possible in doing this. I assume I need to accept requests from Cloudflare to read my S3 images. I want all other requests to be rejected.

I followed this guide: https://support.cloudflare.com/hc/en-us/articles/360037983412-Configuring-an-Amazon-Web-Services-static-site-to-use-Cloudflare

I did not enable static website hosting on my bucket, because it's not necessary for my case.

In my bucket permissions I turned off "Block all public access" and temporarily turned off "Block public access to buckets and objects granted through new public bucket or access point policies". I needed to do this in order to add a bucket policy.

From the link above, I then added a bucket policy that looks something like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::www.example.com/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        <CLOUDFLARE_IP_0>,
                        <CLOUDFLARE_IP_1>,
                        <CLOUDFLARE_IP_2>,
                        ...
                    ]
                }
            }
        }
    ]
}

At this point, a message appeared in the AWS console stating:

"This bucket has public access You have provided public access to this bucket. We highly recommend that you never grant any kind of public access to your S3 bucket."

I then turned back on "Block public access to buckets and objects granted through new public bucket or access point policies" and turned off "Block public and cross-account access to buckets and objects through any public bucket or access point policies".

At this point, the S3 image request behavior seems to be working as intended, but I am not confident that I set everything up to be minimally permissive, especially given the warning message in the AWS console.

Given my description, did I properly set things up in this bucket to accept read requests only from Cloudflare and deny all other requests? I want to make sure that requests from any origin other than Cloudflare will be denied.

1

1 Answers

4
votes

Sounds good! If it works from CloudFlare, but not from somewhere else, then it meets your requirements.

Those Block Public Access warnings are intentionally scary to make people think twice before opening their buckets to the world.

Your policy is nicely limited to only GetObject and only to a limited range of IP addresses.