

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 AWS KMS 金鑰加密 EventBridge 事件匯流排
<a name="eb-encryption-event-bus-cmkey"></a>

您可以指定 EventBridge 使用 AWS KMS 來加密儲存在事件匯流排上的資料，而不是使用預設 AWS 擁有的金鑰 的 。您可以在建立或更新事件匯流排時指定客戶受管金鑰。您也可以更新預設事件匯流排，以使用客戶受管金鑰進行加密。如需詳細資訊，請參閱[KMS 金鑰選項](eb-encryption-at-rest-key-options.md)。

 當您為事件匯流排指定客戶受管金鑰時，EventBridge 會使用該金鑰來加密下列項目：
+ 儲存在事件匯流排上的[自訂](eb-putevents.md)和[合作夥伴](eb-saas.md)事件。

  來自 AWS 服務的事件會使用 加密 AWS 擁有的金鑰。

  EventBridge 不會加密事件中繼資料。如需事件中繼資料的詳細資訊，請參閱 [AWS 事件參考中的服務事件中繼資料](https://docs.aws.amazon.com/eventbridge/latest/ref/events-structure.html)。 **
+ 對於匯流排上的每個[規則](eb-rules.md)：
  + 規則[事件模式](eb-event-patterns.md)。
  + [目標](eb-targets.md)資訊，包括目標輸入、[輸入轉換器](eb-transform-target-input.md)和[組態參數](eb-create-rule-wizard.md#eb-create-rule-target)。
+ 如果啟用[事件匯流排記錄](eb-event-bus-logs.md)，日誌記錄的 `detail`和 `error`區段。

如果您為事件匯流排指定客戶受管金鑰，您可以選擇為事件匯流排指定無效字母佇列 (DLQ)。然後EventBridge 會將產生加密或解密錯誤的任何自訂或合作夥伴事件交付至該 DLQ。如需詳細資訊，請參閱[加密事件DLQs ](eb-encryption-event-bus-dlq.md)。

**注意**  
我們強烈建議為事件匯流排指定 DLQ，以確保在發生加密或解密錯誤時保留事件。

您也可以指定使用客戶受管金鑰來加密事件匯流排封存。如需詳細資訊，請參閱[加密封存](encryption-archives.md)。

**注意**  
使用客戶受管金鑰加密的事件匯流排不支援結構描述探索。若要在事件匯流排上啟用結構描述探索，請選擇使用 AWS 擁有的金鑰。如需詳細資訊，請參閱[KMS 金鑰選項](eb-encryption-at-rest-key-options.md)。

## 事件匯流排加密內容
<a name="eb-encryption-at-rest-context-bus"></a>

[加密內容](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#encrypt_context)是一組金鑰/值對，其中包含任意非私密資料。在加密資料的請求中包含加密內容時， AWS KMS 會以密碼編譯方式將加密內容繫結至加密的資料。若要解密資料，您必須傳遞相同的加密內容。

您也可以使用加密內容做為政策和授權的條件。

如果您使用客戶受管金鑰來保護 EventBridge 資源，您可以使用加密內容來識別稽核記錄和日誌 KMS key 中 的使用。它也會以純文字顯示在日誌中，例如 [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 和 [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)。

對於事件匯流排，EventBridge 在所有 AWS KMS 密碼編譯操作中使用相同的加密內容。內容包含單一索引鍵/值對，其中包含事件匯流排 ARN。

```
"encryptionContext": {
    "kms:EncryptionContext:aws:events:event-bus:arn": "event-bus-arn"
}
```

## AWS KMS 事件匯流排的金鑰政策
<a name="eb-encryption-key-policy-bus"></a>

下列範例金鑰政策提供事件匯流排的必要許可：
+ `kms:DescribeKey`
+ `kms:GenerateDataKey`
+ `kms:Decrypt`

作為安全最佳實務，我們建議您在金鑰政策中包含條件金鑰，以協助確保 EventBridge 僅針對指定的資源或帳戶使用 KMS 金鑰。如需詳細資訊，請參閱[安全考量](eb-encryption-key-policy.md#eb-encryption-event-bus-confused-deputy)。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowEventBridgeToValidateKeyPermission",
      "Effect": "Allow",
      "Principal": {
      "Service": "events.amazonaws.com"
       },
      "Action": [
        "kms:DescribeKey"
        ],
      "Resource": "*"
    },
  {
    "Sid": "AllowEventBridgeToEncryptEvents",
    "Effect": "Allow",
    "Principal": {
      "Service": "events.amazonaws.com"
    },
    "Action": [
      "kms:GenerateDataKey",
      "kms:Decrypt"
    ],
    "Resource": "*",
    "Condition": {
      "StringEquals": {
          "kms:EncryptionContext:aws:events:event-bus:arn": "arn:aws:events:us-east-1:123456789012:event-bus/event-bus-arn",
          "aws:SourceArn": "arn:aws:events:us-east-1:123456789012:event-bus/event-bus-name"
        }
      } 
    }
  ] }
```

------

## AWS KMS 事件匯流排動作的金鑰許可
<a name="eb-encryption-key-permissions-bus"></a>

若要建立或更新使用客戶受管金鑰加密的事件匯流排，您必須具有指定客戶受管金鑰的下列許可：
+ `kms:GenerateDataKeyWithoutPlaintext`
+ `kms:Decrypt`
+ `kms:Encrypt`
+ `kms:ReEncryptFrom`
+ `kms:ReEncryptTo`
+ `kms:DescribeKey`

此外，若要在使用客戶受管金鑰加密的事件匯流排上執行特定事件匯流排動作，您必須擁有指定客戶受管金鑰的`kms:Decrypt`許可。這些動作包括：
+ `[DescribeRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_DescribeRule.html)`
+ `[DisableRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_DisableRule.html)`
+ `[EnableRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_EnableRule.html)`
+ `[ListRules](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_ListRules.html)`
+ `[ListTargetsByRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_ListTargetsByRule.html)`
+ `[PutRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html)`
+ `[ListRuleNamesByTarget](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_ListRuleNamesByTarget.html)`
+ `[PutTargets](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutTargets.html)`

# 在 EventBridge 事件匯流排上設定加密
<a name="eb-encryption-event-bus-cmkey-configure"></a>

您可以為 EventBridge 指定 KMS 金鑰，以便在建立或更新事件匯流排時使用。您也可以更新預設事件匯流排，以使用客戶受管金鑰。

## 指定建立事件匯流排時用於加密的 AWS KMS 金鑰
<a name="eb-encryption-event-bus-cmkey-create"></a>

選擇用於加密的 AWS KMS 金鑰是建立事件匯流排的一部分。預設為使用 EventBridge AWS 擁有的金鑰 提供的 。

**在建立事件匯流排時指定用於加密的客戶受管金鑰 （主控台）**
+ 請遵循以下說明：

  [建立事件匯流排](eb-create-event-bus.md).

**在建立事件匯流排 (CLI) 時指定用於加密的客戶受管金鑰**
+ 呼叫 時`[create-event-bus](https://docs.aws.amazon.com/cli/latest/reference/events/create-event-bus.html)`，請使用 `kms-key-identifier`選項指定 EventBridge 的客戶受管金鑰，以用於事件匯流排上的加密。

  或者，使用 `dead-letter-config`指定無效字母佇列 (DLQ)。

## 更新用於在事件匯流排上加密的 AWS KMS 金鑰
<a name="eb-encryption-event-bus-cmkey-update"></a>

您可以更新用於現有事件匯流排上靜態加密的 AWS KMS 金鑰。其中包含：
+ 從預設值變更為客戶受 AWS 擁有的金鑰 管金鑰。
+ 從客戶受管金鑰變更為預設值 AWS 擁有的金鑰。
+ 從一個客戶受管金鑰變更為另一個。

當您更新事件匯流排以使用不同的 AWS KMS 金鑰時，EventBridge 會解密存放在事件匯流排上的任何資料，然後使用新的金鑰對其進行加密。

**更新用於在事件匯流排上加密的 KMS 金鑰 （主控台）**

1. 訪問 [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/) 開啟 Amazon EventBridge 主控台。

1. 在導覽窗格中，選擇**事件匯流排**。

1. 選擇您要更新的事件匯流排。

1. 在事件匯流排詳細資訊頁面上，選擇**加密**索引標籤。

1. 選擇 EventBridge 在加密存放在事件匯流排上的事件資料時要使用的 KMS 金鑰：
   + 選擇**用於 AWS 擁有的金鑰** EventBridge，以使用 加密資料 AWS 擁有的金鑰。

      AWS 擁有的金鑰 這是 EventBridge 擁有和管理用於多個 AWS 帳戶的 KMS 金鑰。一般而言，除非您需要稽核或控制保護 資源的加密金鑰，否則 AWS 擁有的金鑰 是不錯的選擇。

     這是預設值。
   + 選擇**使用 EventBridge 的客戶受管金鑰**，以使用您指定或建立的客戶受管金鑰來加密資料。 EventBridge 

     客戶自管金鑰為您在 AWS 帳戶中建立、擁有和管理的 KMS 金鑰。您可以完全控制這些 KMS 金鑰。

     1. 指定現有的客戶受管金鑰，或選擇**建立新的 KMS 金鑰**。

        EventBridge 會顯示金鑰狀態，以及與指定客戶受管金鑰相關聯的任何金鑰別名。

     1. 如果有的話，請選擇要用作此事件匯流排無效字母佇列 (DLQ) 的 Amazon SQS 佇列。

        EventBridge 會將未成功加密的事件傳送至 DLQ，以便您稍後處理。

**更新用於在事件匯流排上加密的 KMS 金鑰 (CLI)**
+ 呼叫 時`[update-event-bus](https://docs.aws.amazon.com/cli/latest/reference/events/create-event-bus.html)`，請使用 `kms-key-identifier`選項指定 EventBridge 的客戶受管金鑰，以用於事件匯流排上的加密。

  或者，使用 `dead-letter-config`指定無效字母佇列 (DLQ)。

**使用 CloudFormation 更新用於預設事件匯流排加密的 KMS 金鑰**

由於 EventBridge 會自動將預設事件匯流排佈建至您的帳戶，因此您無法使用 CloudFormation 範本建立它，如同您通常想要包含在 CloudFormation 堆疊中的任何資源一樣。若要在 CloudFormation 堆疊中包含預設事件匯流排，您必須先將其*匯入*堆疊。將預設事件匯流排匯入堆疊後，您可以視需要更新事件匯流排屬性。
+ 請遵循以下說明：

  [使用 CloudFormation 更新預設匯流排](event-bus-update-default-cfn.md).

# 當事件匯流排是規則目標時，在 EventBridge 中加密
<a name="eb-encryption-event-bus-target"></a>

將自訂或合作夥伴事件傳送至事件匯流排時，EventBridge 會根據該事件匯流排的靜態加密 KMS 金鑰組態來加密該事件，如果已指定預設 AWS 擁有的金鑰 金鑰或客戶受管金鑰。如果事件符合規則，除非*規則目標是另一個事件匯流排，否則* EventBridge 會使用該事件匯流排的 KMS 金鑰組態來加密事件，直到事件傳送到規則目標為止。
+ 如果規則的目標是相同區域中的另一個事件匯流排 AWS ：

  如果目標事件匯流排具有指定的客戶受管金鑰，EventBridge 會使用目標事件匯流排的客戶受管金鑰來加密事件，以改為交付。  
![\[從一個事件匯流排傳送至另一個事件匯流排的事件，使用目標事件匯流排的 金鑰加密。\]](http://docs.aws.amazon.com/zh_tw/eventbridge/latest/userguide/images/cmkms-bus-same-region_eventbridge_conceptual.svg)
+ 如果規則的目標是不同區域中的另一個事件匯流排 AWS ：

  EventBridge 會根據第一個事件匯流排上的 KMS 金鑰組態來加密靜態事件。EventBridge 使用 TLS 將事件傳送至不同區域中的第二個事件匯流排，然後根據為目標事件匯流排指定的 KMS 金鑰組態進行加密。  
![\[使用傳輸中的 TLS 傳送至不同區域中目標事件匯流排的事件。\]](http://docs.aws.amazon.com/zh_tw/eventbridge/latest/userguide/images/cmkms-bus-cross-region_eventbridge_conceptual.svg)

# EventBridge 中受管規則的事件加密
<a name="eb-encryption-event-bus-managed-rules"></a>

AWS 服務可以在您的帳戶中建立和管理這些服務中特定函數 AWS 所需的事件匯流排規則。作為受管規則的一部分， AWS 服務可以指定 EventBridge 使用為規則目標指定的客戶受管金鑰。這可讓您根據規則目標彈性指定要使用的客戶受管金鑰。

在這些情況下，一旦自訂或合作夥伴事件符合受管規則，EventBridge 會使用受管規則指定的目標客戶受管金鑰來加密事件，直到事件傳送至規則目標為止。無論事件匯流排是否已設定為使用自己的客戶受管金鑰進行加密，都一樣。即使受管規則的目標是另一個事件匯流排，而且該事件匯流排有自己的客戶受管金鑰指定用於加密，也是如此。EventBridge 會繼續使用受管規則中指定的目標客戶受管金鑰，直到事件傳送至不是事件匯流排的目標為止。

![\[符合受管規則的事件，使用規則目標的 金鑰加密。\]](http://docs.aws.amazon.com/zh_tw/eventbridge/latest/userguide/images/cmkms-bus-managed-rule_eventbridge_conceptual.svg)


對於規則目標是另一個區域中的事件匯流排的情況，您必須提供[多區域金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/multi-region-keys-overview.html)。第一個區域中的事件匯流排會使用受管規則中指定的客戶受管金鑰來加密事件。然後，它會將事件傳送至第二個區域中的目標事件匯流排。該事件匯流排必須能夠繼續使用客戶受管金鑰，直到將事件傳送到其目標為止。

# 在 EventBridge AWS KMS 中使用 加密事件匯流排日誌
<a name="encryption-bus-logs"></a>

傳送日誌時，EventBridge 會使用為事件匯流排指定的 KMS 金鑰來加密每個日誌記錄的 `detail`和 `error`區段。如果您已為事件匯流排指定客戶受管金鑰，EventBridge 會使用該金鑰進行傳輸中的加密。交付後，記錄會解密，然後使用為日誌目的地指定的 KMS 金鑰重新加密。

## 事件匯流排日誌加密內容
<a name="encryption-bus-logs-context"></a>

[加密內容](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#encrypt_context)是一組金鑰/值對，其中包含任意非私密資料。在加密資料的請求中包含加密內容時， AWS KMS 會以密碼編譯方式將加密內容繫結至加密的資料。若要解密資料，您必須傳遞相同的加密內容。

您也可以使用加密內容做為政策和授權的條件。

如果您使用客戶受管金鑰來保護 EventBridge 資源，則可以使用加密內容來識別稽核記錄和日誌 KMS key 中的 使用情況。它也會以純文字顯示在日誌中，例如 [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-user-guide.html) 和 [Amazon CloudWatch Logs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)。

對於事件匯流排日誌， EventBridge 會在所有 AWS KMS 密碼編譯操作中使用相同的加密內容。

```
"encryptionContext": {
    "kms:EncryptionContext:SourceArn": "arn:partition:logs:region:account:*"
}
```

## AWS KMS 事件匯流排記錄的金鑰政策許可
<a name="encryption-bus-logs-permissions"></a>

對於使用客戶受管金鑰的事件匯流排，您必須將下列許可新增至金鑰政策。
+ 允許 EventBridge 使用客戶受管金鑰加密日誌。

  ```
  {
    "Sid": "Enable log service encryption",
    "Effect": "Allow",
    "Principal": {
      "Service": "events.amazonaws.com"  
    },
    "Action": [
      "kms:GenerateDataKey"
    ],
    "Resource": "*",
    "Condition": {
      "StringLike": {
        "kms:EncryptionContext:SourceArn": "arn:partition:logs:region:account:*"
      }
    }
  }
  ```
+ 允許記錄服務解密 EventBridge 傳送的日誌。

  ```
  {
    "Sid": "Enable log delivery decryption",
    "Effect": "Allow",
    "Principal": {
      "Service": "delivery.logs.amazonaws.com"
    },
    "Action": [
      "kms:Decrypt",
      "kms:GenerateDataKey"
    ],
    "Resource": "*",
    "Condition": {
      "StringLike": {
        "kms:EncryptionContext:SourceArn": "arn:partition:logs:region:account:*"
      }
    }
  }
  ```

# 使用無效字母佇列擷取 EventBridge 中的加密事件錯誤
<a name="eb-encryption-event-bus-dlq"></a>

如果您在事件匯流排上設定客戶受管金鑰加密，建議您為該事件匯流排指定無效字母佇列 (DLQ)。如果在事件匯流排上處理事件時遇到不可重試的錯誤，EventBridge 會將自訂和合作夥伴事件傳送至此 DLQ。不可重試的錯誤是需要使用者動作才能解決基礎問題，例如已停用或遺失指定的客戶受管金鑰。
+ 如果 EventBridge 在事件匯流排上處理事件時發生無法重試的加密或解密錯誤，如果指定了事件匯流排，則事件會傳送至*事件匯流排*的 DLQ。
+ 如果在 EventBridge 嘗試將事件傳送至目標時發生無法重試的加密或解密錯誤，包括輸入轉換和目標特定設定，如果指定目標，則會將事件傳送至*目標*的 DLQ。

![\[事件匯流排處理期間不可重試的錯誤，傳送至事件匯流排 DLQ。\]](http://docs.aws.amazon.com/zh_tw/eventbridge/latest/userguide/images/cmkms-bus-dlq_eventbridge_conceptual.svg)


如需詳細資訊，包括使用 DLQs時的考量事項，以及設定許可的指示，請參閱 [使用無效字母佇列](eb-rule-dlq.md)。

# 解密 EventBridge 無效字母佇列中的事件
<a name="eb-encryption-event-bus-dlq-decrypt"></a>

解決導致無法重試錯誤的基礎問題後，您就可以處理傳送至事件匯流排或目標 DLQs 的事件。對於加密的事件，您必須先解密事件，才能處理它。

下列範例示範如何解密 EventBridge 已交付至事件匯流排或目標 DLQ 的事件。

```
// You will receive an encrypted event in the following json format.
        // ```
        //   {
        //     "version": "0", 
        //     "id": "053afa53-cdd7-285b-e754-b0dfd0ac0bfb",  // New event id not the same as the original one
        //     "account": "123456789012",  
        //     "time": "2020-02-10T10:22:00Z",  
        //     "resources": [ ],
        //     "region": "us-east-1",
        //     "source": "aws.events",
        //     "detail-type": "Encrypted Events",
        //     "detail": { 
        //       "event-bus-arn": "arn:aws:events:region:account:event-bus/bus-name", 
        //       "rule-arn": "arn:aws:events:region:account:event-bus/bus-name/rule-name", 
        //       "kms-key-arn": "arn:aws:kms:region:account:key/key-arn", 
        //       "encrypted-payload": "AgR4qiru/XNwTUyCgRHqP7rbbHn/xpmVeVeRIAd12TDYYVwAawABABRhd3M6ZXZlbnRzOmV2ZW50LWJ1cwB
        //                             RYXJuOmF3czpldmVudHM6dXMtZWFzdC0xOjE0NjY4NjkwNDY3MzpldmVudC1idXMvY21rbXMtZ2EtY3Jvc3
        //                             MtYWNjb3VudC1zb3VyY2UtYnVzAAEAB2F3cy1rbXMAS2Fybjphd3M6a21zOnVzLWVhc3QtMToxNDY2ODY5"
        //     }
        //   }
        // ```
 
        // Construct an AwsCrypto object with the encryption algorithm `ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY` which 
        // is used by EventBridge for encryption operation. This object is an entry point for decryption operation. 
        // It can later use decryptData(MasterKeyProvider, byte[]) method to decrypt data.
        final AwsCrypto crypto = AwsCrypto.builder()
                .withEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY)
                .build();
 
        // Construct AWS KMS master key provider with AWS KMS Client Supplier and AWS KMS Key ARN. The KMS Client Supplier can 
        // implement a RegionalClientSupplier interface. The AWS KMS Key ARN can be fetched from kms-key-arn property in 
        // encrypted event json detail.
        final KmsMasterKeyProvider kmsMasterKeyProvider = KmsMasterKeyProvider.builder()
                .customRegionalClientSupplier(...)
                .buildStrict(KMS_KEY_ARN);
 
        // The string of encrypted-payload is base64 encoded. Decode it into byte array, so it can be furthur 
        // decrypted. The encrypted payload can be fetched from encrypted-payload field in encrypted event json detail.
        byte[] encryptedByteArray = Base64.getDecoder().decode(ENCRYPTED_PAYLOAD);
 
        // The decryption operation. It retrieves the encryption context and encrypted data key from the cipher 
        // text headers, which is parsed from byte array encrypted data. Then it decrypts the data key, and 
        // uses it to finally decrypt event payload. This encryption/decryption strategy is called envelope 
        // encryption, https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#enveloping
        final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(kmsMasterKeyProvider, encryptedByteArray);
 
        final byte[] decryptedByteArray = decryptResult.getResult();
    
        // Decode the event json plaintext from byte array into string with UTF_8 standard.
        String eventJson = new String(decryptedByteArray, StandardCharsets.UTF_8);
```