具有服务器端加密 s3 存储桶的 AWS Cloudfront

2024-05-13

(这是对这个问题 https://stackoverflow.com/questions/50166557/how-can-a-cloudfront-distribution-an-aws-kms-key-to-get-an-s3-image-encrypted-at#基于我在解决这个问题时学到的知识。)

我使用具有 S3 源的 AWS Cloudfront(即为 S3 对象提供服务)。

我想添加服务器端加密 https://docs.aws.amazon.com/AmazonS3/latest/userguide/serv-side-encryption.html到我的存储桶,但继续能够通过 cloudfront 访问对象。

我对所使用的精确 SSE 策略不可知(只要它是安全的)。


如果您对 SSE 加密类型有自由度,则可以在不使用 lambda 的情况下添加 SSE。

关键是将桶上的加密类型设置为SSE-S3(亚马逊 S3 密钥)。

主要步骤是:

  1. 将存储桶加密设置为SSE-S3在“属性”(选项卡)~>“默认加密”(面板)~>“编辑”(按钮)中
  2. 创建 Cloudfront 发行版
  3. 通过链接链接存储桶和cloudfront分布来源访问身份 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
  4. 添加将源访问身份链接到存储桶的存储桶策略。

继文章之后,“使用 CloudFront 从 S3 提供 SSE-KMS 加密内容” https://aws.amazon.com/blogs/networking-and-content-delivery/serving-sse-kms-encrypted-content-from-s3-using-cloudfront/,这是与此发行版相对应的替代 CloudFormation 堆栈:

Resources:
  S3Bucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: !Join
        - "-"
        - - !Ref "AWS::StackName"
          - s3bucket
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256

  S3BucketPolicy:
    Type: "AWS::S3::BucketPolicy"
    Properties:
      Bucket: !Ref S3Bucket
      PolicyDocument:
        Statement:
          - Action:
              - "s3:GetObject"
            Effect: Allow
            Resource: !Join
              - ""
              - - "arn:aws:s3:::"
                - !Ref S3Bucket
                - /*
            Principal:
              CanonicalUser:
                Fn::GetAtt: [OAI, S3CanonicalUserId]

  OAI:
    Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity"
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: Origin Access Identity for S3

  Cloudfront:
    Type: "AWS::CloudFront::Distribution"
    Properties:
      DistributionConfig:
        Comment: How to serve content encrypted with SSE-S3 from S3 using CloudFront
        Origins:
          - DomainName: !Join
              - .
              - - !Ref S3Bucket
                - s3
                - !Ref "AWS::Region"
                - amazonaws.com
            Id: S3-regional-endpoint
            S3OriginConfig:
              OriginAccessIdentity: !Join
                - /
                - - origin-access-identity
                  - cloudfront
                  - !Ref OAI
        DefaultCacheBehavior:
          TargetOriginId: S3-regional-endpoint
          ForwardedValues:
            QueryString: "false"
          ViewerProtocolPolicy: redirect-to-https
        Enabled: "true"
  • 相对于上面引用的文章,这需要指定源访问身份(OAI)以及存储桶策略(S3BucketPolicy)但没有 KMS 资源。
  • 在 CloudFormation 和 terraform 中,您可以指定加密类型为SSE-S3通过指定AES256(见行SSEAlgorithm: AES256).

如果您使用 terraform,则有一些模块(例如here https://registry.terraform.io/modules/cloudposse/cloudfront-s3-cdn/aws/latest and here https://registry.terraform.io/modules/QuiNovas/cloudfront/aws/latest)本质上是通过一些额外的好处来完成上述工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有服务器端加密 s3 存储桶的 AWS Cloudfront 的相关文章

随机推荐