限制对云端分发背后的 s3 静态网站的访问

Restrict access to s3 static website behind a cloudfront distribution

我想暂时限制用户访问我在 s3 中托管的静态网站,该网站位于云端分发版后面。

这可能吗?如果可以,我可以使用什么方法来实现它?

我已经能够通过在存储桶策略中使用如下所示的条件来限制对我的 s3 存储桶的特定访问:

{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Sid": "VisualEditor0",
          "Effect": "Allow",
          "Principal": {
              "AWS": "*"
          },
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::my-bucket/*",
          "Condition": {
              "IpAddress": {
                  "aws:SourceIp": "12.34.56.73/32"
              }
          }
      }
  ]
}

有效并将我的 s3 存储桶限制为我的 ip,但这意味着云端 url 获得 403 forbidden: access denied.

阅读 AWS 文档时,建议限制对 s3 资源的特定访问,使用 Origin Access Identity。但是他们指定了以下内容:

If you don't see the Restrict Bucket Access option, your Amazon S3 origin might be configured as a website endpoint. In that configuration, S3 buckets must be set up with CloudFront as custom origins and you can't use an origin access identity with them.

这表明我不能在这种情况下使用它。理想情况下,我想强制我的分发或存储桶策略使用特定的安全组并以这种方式控制它,这样我就可以轻松地 add/remove 批准 ip。

您可以在 CloudFront 上允许 CloudFront IP 地址,因为静态网站终端节点不支持 Origin 访问身份。 以下是 CloudFront IP 地址的列表: http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips

此 link 还解释了如何通过引荐限制访问 headers https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-serve-static-website/

您可以告诉 CloudFront 向每个请求添加 header,然后修改您的 S3 存储桶策略以要求 header.

例如

{
  "Version":"2012-10-17",
  "Id":"http referer policy example",
  "Statement":[
    {
      "Sid":"Allow get requests originating from www.example.com and example.com.",
      "Effect":"Allow",
      "Principal":"*",
      "Action":"s3:GetObject",
      "Resource":"arn:aws:s3:::examplebucket/*",
      "Condition":{
        "StringLike":{"aws:Referer":"mysecretvalue"}
      }
    }
  ]
}