【问题标题】:AWS: forward event bridge event to encrypted SQS (Amazon managed key)AWS:将事件桥接事件转发到加密的 SQS(亚马逊托管密钥)
【发布时间】:2022-03-05 05:45:40
【问题描述】:

我有一个事件总线并创建了一个将事件转发到 SQS 队列的事件规则。现在,我使用默认的亚马逊管理密钥(别名/aws/sqs)为我的队列启用了加密。

启用加密后,不再转发事件。研究 AWS 文档我只能找到关于使用 CMK 进行加密的信息,但没有关于亚马逊托管密钥的信息。

我猜这是权限问题,但不确定。这是我的活动规则和访问策略

  queueCreateInvoiceEvent:
    Type: AWS::Events::Rule
    DependsOn: [myQueue]
    Properties:
      Description: Forward INVOICE_CREATED event to SQS queue
      EventBusName: ${self:custom.eventBus.name}
      EventPattern: { "detail-type": ["INVOICE_CREATED"] }
      Name: ${self:service.name}-${self:provider.stage}-buffer-invoice-created-event
      State: ENABLED
      Targets:
        - Id: myQueue
          Arn:
            Fn::GetAtt: [myQueue, Arn]


  createReceiptQueueAccessPolicy:
    Type: AWS::SQS::QueuePolicy
    DependsOn: [queueCreateInvoiceEvent, myQueue]
    Properties:
      Queues:
        - { Ref: createReceiptQueue }
      PolicyDocument:
        Id: EventBridgeSqsAccessPolicy
        Version: "2012-10-17"
        Statement:
          - Sid: Allow-User-SendMessage
            Effect: Allow
            Principal:
              Service: "events.amazonaws.com"
            Action:
              - sqs:SendMessage
            Resource:
              - Fn::GetAtt: ["myQueue", "Arn"]
            Condition:
              ArnEquals:
                aws:SourceArn:
                  - Fn::GetAtt: ["queueCreateInvoiceEvent", "Arn"]

【问题讨论】:

    标签: amazon-web-services amazon-sqs aws-event-bridge


    【解决方案1】:

    根据EventBridge troubleshooting page,您的 KMS 密钥策略需要允许 EventBridge 访问密钥:

    {
        "Sid": "Allow EventBridge to use the key",
        "Effect": "Allow",
        "Principal": {
            "Service": "events.amazonaws.com"
        },
        "Action": [
            "kms:Decrypt",
            "kms:GenerateDataKey"
        ],
        "Resource": "*"
    }
    

    【讨论】:

    • EventBridge 需要kms:Decrypt 对我来说毫无意义,而它所要做的就是加密消息并将其放入队列中。但是是的,在我的测试中,kms:Decrypt 在这里真的很重要。
    • 我还注意到尝试使用 Condition 块测试 aws:SourceArn 总是会导致失败,因此如果您遇到问题,请尝试从 KMS 密钥策略中删除 Condition 块。
    【解决方案2】:

    补充上面所说的,但有更多细节。从今天 (2022-03-04) 开始,您需要具备以下条件才能允许 EventBridge 发送到加密的 SQS 队列。来自 AWS 文档https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-key-management.html#sqs-what-permissions-for-sse

    多个 AWS 服务充当事件源,可以将事件发送到 Amazon SQS 队列。要允许这些事件源使用加密队列,您必须创建一个客户托管的 KMS 密钥并在密钥策略中添加权限,以便服务使用所需的 AWS KMS API 方法。

    1. 客户管理的 KMS 密钥具有允许 events.amazonaws.com 某些操作的策略。
    2. 然后,SQS 队列必须使用该 KMS 密钥 ID 进行加密。

    这是所需的两个 CloudFormation。

    # KMS key is required to allow eventbridge to send to encrypted sqs queue
    # https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-key-management.html#sqs-what-permissions-for-sse
    KmsKey:
      Type: AWS::KMS::Key
      Properties:
        Description: my-key-name
        KeyPolicy:
          Version: "2012-10-17"
          Statement:
            - Sid: Allow EventBridge access
              Effect: Allow
              Principal:
                Service: events.amazonaws.com
              Action:
                - kms:GenerateDataKey
                - kms:Decrypt
              Resource: '*'
    
            - Sid: Allow access for Key Administrators
              Effect: Allow
              Principal:
                AWS:
                  - !Sub arn:aws:iam::${AWS::AccountId}:role/my-role-name
                  - !Sub arn:aws:iam::${AWS::AccountId}:root
              Action:
                - kms:*
              Resource: '*'
    
    EventRuleQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: my-queue-name
        KmsMasterKeyId: !Ref KmsKey
        KmsDataKeyReusePeriodSeconds: 43200 # 12 hours to reduce cost
    

    【讨论】:

      猜你喜欢
      • 2011-02-18
      • 2015-09-26
      • 2023-01-05
      • 2020-10-22
      • 2013-10-15
      • 2016-06-01
      • 1970-01-01
      • 2018-12-07
      • 2021-09-29
      相关资源
      最近更新 更多