

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

# Amazon Keyspaces 中的靜態加密
<a name="EncryptionAtRest"></a>

Amazon Keyspaces （適用於 Apache Cassandra) *靜態加密*使用 [AWS Key Management Service (AWS KMS)](https://aws.amazon.com/kms/) 中存放的加密金鑰來加密所有靜態資料，以提供增強的安全性。此功能協助降低了保護敏感資料所涉及的操作負擔和複雜性。透過靜態加密，您可以建置安全敏感的應用程式，以滿足嚴格的資料保護合規和法規要求。

 Amazon Keyspaces 靜態加密會使用 256 位元進階加密標準 (AES-256) 來加密您的資料。這有助於保護您的資料免於未經授權存取基礎儲存體。

Amazon Keyspaces 會以透明的方式加密和解密資料表中的資料和串流。Amazon Keyspaces 使用信封加密和金鑰階層來保護資料加密金鑰。它與 AWS KMS 整合，用於存放和管理根加密金鑰。如需加密金鑰階層的詳細資訊，請參閱 [靜態加密：在 Amazon Keyspaces 中運作的方式](encryption.howitworks.md)。如需信封加密等 AWS KMS 概念的詳細資訊，請參閱《 *AWS Key Management Service 開發人員指南*》中的[AWS KMS 管理服務概念](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html)。

 建立新資料表時，您可以選擇下列其中一個*AWS KMS 金鑰 (KMS 金鑰）*：
+ AWS 擁有的金鑰 – 這是預設加密類型。金鑰由 Amazon Keyspaces 擁有 （不收取額外費用）。
+ 客戶受管金鑰 – 此金鑰會存放在您的帳戶中，並由您建立、擁有和管理。您可以完全控制客戶受管金鑰 （需支付AWS KMS 費用）。

Amazon Keyspaces 會使用與基礎資料表相同的金鑰自動加密變更資料擷取 (CDC) 串流。如需 CDC 的詳細資訊，請參閱 [在 Amazon Keyspaces 中使用變更資料擷取 (CDC) 串流](cdc.md)。

 您可以隨時在 AWS 擁有的金鑰 和客戶受管金鑰之間切換。您可以在建立新資料表時指定客戶受管金鑰，或使用主控台或以程式設計方式使用 CQL 陳述式變更現有資料表的 KMS 金鑰。如要瞭解如何作業，請參閱[靜態加密：如何使用客戶自管金鑰來加密 Amazon Keyspaces 中的表格](encryption.customermanaged.md)。

 使用 的預設選項 AWS 擁有的金鑰 進行靜態加密無需額外費用。不過，客戶受管金鑰會 AWS KMS 收取費用。如需定價的詳細資訊，請參閱 [AWS KMS 定價](https://aws.amazon.com/kms/pricing)。

Amazon Keyspaces 靜態加密適用於所有 AWS 區域，包括 AWS 中國 （北京） 和 AWS 中國 （寧夏） 區域。如需詳細資訊，請參閱[靜態加密：在 Amazon Keyspaces 中運作的方式](encryption.howitworks.md)。

**Topics**
+ [靜態加密：在 Amazon Keyspaces 中運作的方式](encryption.howitworks.md)
+ [靜態加密：如何使用客戶自管金鑰來加密 Amazon Keyspaces 中的表格](encryption.customermanaged.md)

# 靜態加密：在 Amazon Keyspaces 中運作的方式
<a name="encryption.howitworks"></a>

Amazon Keyspaces （適用於 Apache Cassandra) *靜態加密*會使用 256 位元進階加密標準 (AES-256) 加密您的資料。這有助於保護您的資料免於未經授權存取基礎儲存體。根據預設，Amazon Keyspaces 資料表中的所有客戶資料都會靜態加密，且伺服器端加密是透明的，這表示不需要變更應用程式。

靜態加密與 AWS Key Management Service (AWS KMS) 整合，用於管理用來加密資料表的加密金鑰。建立新資料表或更新現有資料表時，您可以選擇下列其中一個*AWS KMS 金鑰*選項：
+ AWS 擁有的金鑰 – 這是預設加密類型。金鑰由 Amazon Keyspaces 擁有 （不收取額外費用）。
+ 客戶受管金鑰 – 此金鑰存放在您的帳戶中，由您建立、擁有和管理。您可以完全控制客戶受管金鑰 （需支付AWS KMS 費用）。

**AWS KMS 金鑰 (KMS 金鑰）**  
靜態加密使用 AWS KMS 金鑰保護您的所有 Amazon Keyspaces 資料。根據預設，Amazon Keyspaces 會使用 [AWS 擁有的金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#aws-owned-cmk)，這是在 Amazon Keyspaces 服務帳戶中建立和管理的多租戶加密金鑰。  
不過，您可以使用 中的[客戶受管金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk)來加密 Amazon Keyspaces 資料表 AWS 帳戶。您可以為金鑰空間中的每個資料表選取不同的 KMS 金鑰。您為資料表選取的 KMS 金鑰也會用來加密其所有中繼資料和可還原的備份。  
建立或更新資料表時，您可以選取資料表的 KMS 金鑰。您可以隨時在 Amazon Keyspaces 主控台或使用 [ALTER TABLE](cql.ddl.keyspace.md#cql.ddl.keyspace.alter) 陳述式來變更資料表的 KMS 金鑰。切換 KMS 金鑰的程序是無縫的，不需要停機時間或導致服務降級。

**金鑰階層**  
Amazon Keyspaces 使用金鑰階層來加密資料。在此金鑰階層中，KMS 金鑰是根金鑰。它用於加密和解密 Amazon Keyspaces 資料表加密金鑰。資料表加密金鑰用於加密 Amazon Keyspaces 在內部使用的加密金鑰，以在執行讀取和寫入操作時加密和解密資料。  
使用加密金鑰階層，您可以對 KMS 金鑰進行變更，而無需重新加密資料或影響應用程式和持續的資料操作。  

![\[金鑰階層顯示根金鑰、資料表加密金鑰，以及用於靜態加密的資料加密金鑰。\]](http://docs.aws.amazon.com/zh_tw/keyspaces/latest/devguide/images/keyspaces_encryption.png)


**資料表索引鍵**  
Amazon Keyspaces 資料表金鑰用作金鑰加密金鑰。Amazon Keyspaces 使用資料表金鑰來保護內部資料加密金鑰，這些金鑰用於加密存放在資料表、日誌檔案和可還原備份中的資料。Amazon Keyspaces 會為資料表中的每個基礎結構產生唯一的資料加密金鑰。不過，多個資料表資料列可能受到相同的資料加密金鑰保護。  
當您第一次將 KMS 金鑰設定為客戶受管金鑰時， AWS KMS 會產生*資料金鑰*。 AWS KMS 資料金鑰是指 Amazon Keyspaces 中的資料表金鑰。  
當您存取加密的資料表時，Amazon Keyspaces 會傳送請求至 AWS KMS ，以使用 KMS 金鑰解密資料表金鑰。然後，它會使用純文字資料表金鑰來解密 Amazon Keyspaces 資料加密金鑰，並使用純文字資料加密金鑰來解密資料表資料。  
Amazon Keyspaces 使用 並將資料表金鑰和資料加密金鑰存放在 外部 AWS KMS。它使用[進階加密標準](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) (AES) 加密和 256 位元加密金鑰來保護所有金鑰。然後，它會將加密的金鑰與加密的資料一起存放，以便他們可以隨需解密資料表資料。

**資料表金鑰快取**  
為了避免 AWS KMS 呼叫每個 Amazon Keyspaces 操作，Amazon Keyspaces 會快取記憶體中每個連線的純文字資料表金鑰。如果 Amazon Keyspaces 在閒置五分鐘後收到快取資料表金鑰的請求，則會將新的請求傳送至 AWS KMS 以解密資料表金鑰。此呼叫會擷取自上次請求解密資料表金鑰以來，對 AWS KMS 或 AWS Identity and Access Management (IAM) 中 KMS 金鑰的存取政策所做的任何變更。

**封套加密**  
如果您變更資料表的客戶受管金鑰，Amazon Keyspaces 會產生新的資料表金鑰。然後，它會使用新的資料表金鑰來重新加密資料加密金鑰。它也會使用新的資料表金鑰來加密先前用於保護可還原備份的資料表金鑰。此程序稱為信封加密。這可確保即使您輪換客戶受管金鑰，也可以存取可還原的備份。如需信封加密的詳細資訊，請參閱《 *AWS Key Management Service 開發人員指南*》中的[信封加密](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#enveloping)。

**Topics**
+ [AWS 擁有的金鑰](#keyspaces-owned)
+ [客戶受管金鑰](#customer-managed)
+ [靜態加密用量備註](#encryption.usagenotes)

## AWS 擁有的金鑰
<a name="keyspaces-owned"></a>

AWS 擁有的金鑰 不會存放在您的 中 AWS 帳戶。它們是 KMS 金鑰集合的一部分，這些金鑰 AWS 擁有和管理用於 multiple AWS 帳戶。 AWS services 可用來 AWS 擁有的金鑰 保護您的資料。

您無法檢視、管理或使用 AWS 擁有的金鑰或稽核其使用。不過，您不需要進行任何工作或變更任何程式，即可保護加密資料的金鑰。

您不需要支付每月費用或使用費 AWS 擁有的金鑰，也不會計入您帳戶的 AWS KMS 配額。

## 客戶受管金鑰
<a name="customer-managed"></a>

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

使用客戶受管金鑰來取得下列功能：
+ 您可以建立和管理客戶受管金鑰，包括設定和維護[金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)、[IAM 政策和](https://docs.aws.amazon.com/kms/latest/developerguide/iam-policies.html)[授予](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html) 以控制對客戶受管金鑰的存取。您可以[啟用和停用](https://docs.aws.amazon.com/kms/latest/developerguide/enabling-keys.html)客戶受管金鑰、啟用和停用[自動金鑰輪換](https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html)，並在不再使用時[排定刪除客戶受管金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/deleting-keys.html)。您可以為您管理的客戶受管金鑰建立標籤和別名。
+ 您可以搭配[匯入的金鑰材料](https://docs.aws.amazon.com/kms/latest/developerguide/importing-keys.html)使用客戶受管金鑰，或是使用位於您所擁有及管理[自訂金鑰存放區](https://docs.aws.amazon.com/kms/latest/developerguide/custom-key-store-overview.html)中的客戶受管金鑰。
+ 您可以使用 AWS CloudTrail 和 Amazon CloudWatch Logs 來追蹤 Amazon Keyspaces AWS KMS 代表您傳送到 的請求。如需詳細資訊，請參閱[步驟 6：使用 設定監控 AWS CloudTrail](encryption.customermanaged.md#encryption-cmk-trail)。

客戶受管金鑰會針對每個 API 呼叫[產生費用](https://aws.amazon.com/kms/pricing/)，而配額適用於這些 KMS AWS KMS 金鑰。如需詳細資訊，請參閱[AWS KMS 資源或請求配額](https://docs.aws.amazon.com/kms/latest/developerguide/limits.html)。

當您將客戶受管金鑰指定為資料表的根加密金鑰時，可還原備份會使用建立備份時為資料表指定的相同加密金鑰進行加密。如果輪換資料表的 KMS 金鑰，金鑰包圍可確保最新的 KMS 金鑰可存取所有可還原的備份。

Amazon Keyspaces 必須能夠存取您的客戶受管金鑰，才能讓您存取資料表資料。如果加密金鑰的狀態設定為停用或排定刪除，Amazon Keyspaces 將無法加密或解密資料。因此，您無法在資料表上執行讀取和寫入操作。一旦服務偵測到您的加密金鑰無法存取，Amazon Keyspaces 就會傳送電子郵件通知，提醒您。

您必須在七天內還原加密金鑰的存取權，否則 Amazon Keyspaces 會自動刪除您的資料表。基於預防措施，Amazon Keyspaces 會在刪除資料表之前建立資料表資料的可還原備份。Amazon Keyspaces 會維護可還原備份 35 天。35 天後，您無法再還原資料表資料。您不需要支付可還原備份的費用，但[需支付標準還原費用](https://aws.amazon.com/keyspaces/pricing)。

您可以使用此可還原備份，將資料還原至新的資料表。若要啟動還原，必須啟用用於資料表的最後一個客戶受管金鑰，Amazon Keyspaces 必須具有其存取權。

**注意**  
當您建立使用客戶受管金鑰加密的資料表時，在建立程序完成之前無法存取或排程刪除，會發生錯誤。建立資料表操作失敗，且您會收到電子郵件通知。

## 靜態加密用量備註
<a name="encryption.usagenotes"></a>

當您在 Amazon Keyspaces 中使用靜態加密時，請考慮下列事項。
+ 所有 Amazon Keyspaces 資料表上都會啟用靜態伺服器端加密，且無法停用。整個資料表會靜態加密，您無法選取特定資料欄或資料列進行加密。
+ 根據預設，Amazon Keyspaces 會使用單一服務預設金鑰 (AWS 擁有的金鑰) 來加密所有資料表。如果此金鑰不存在，則會為您建立。無法停用服務預設金鑰。
+ 靜態加密只會在資料在持久性儲存媒體上靜態 （靜態） 時加密資料。如果資料安全是傳輸中資料或使用中資料的考量，您必須採取其他措施：
  + 傳輸中資料：您在 Amazon Keyspaces 中的所有資料都會在傳輸中加密。根據預設，Amazon Keyspaces 的通訊會使用 Secure Sockets Layer (SSL)/Transport Layer Security (TLS) 加密進行保護。
  + 使用中的資料：使用用戶端加密，在將資料傳送到 Amazon Keyspaces 之前保護資料。
  + 客戶受管金鑰：資料表中的靜態資料一律使用客戶受管金鑰加密。不過，在處理 AWS 擁有的金鑰 期間執行多列原子更新的操作會暫時使用 來加密資料。這包括範圍刪除操作和同時存取靜態和非靜態資料的操作。
+ 單一客戶受管金鑰最多可有 50，000 個[授予](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html)。與客戶受管金鑰相關聯的每個 Amazon Keyspaces 資料表都會耗用 2 個授與。刪除資料表時，會釋出一個授予。第二個授權用於建立資料表的自動快照，以防止資料遺失，以防 Amazon Keyspaces 意外遺失對客戶受管金鑰的存取。此授與會在刪除資料表後 42 天發行。

# 靜態加密：如何使用客戶自管金鑰來加密 Amazon Keyspaces 中的表格
<a name="encryption.customermanaged"></a>

您可以使用 主控台或 CQL 陳述式， AWS KMS key 為新資料表指定 ，並更新 Amazon Keyspaces 中現有資料表的加密金鑰。下列主題概述如何實作新資料表和現有資料表的客戶受管金鑰。

**Topics**
+ [先決條件：使用 建立客戶受管金鑰， AWS KMS 並將許可授予 Amazon Keyspaces](#encryption.createCMKMS)
+ [步驟 3：指定新資料表的客戶受管金鑰](#encryption.tutorial-creating)
+ [步驟 4：更新現有資料表的加密金鑰](#encryption.tutorial-update)
+ [步驟 5：在日誌中使用 Amazon Keyspaces 加密內容](#encryption-context)
+ [步驟 6：使用 設定監控 AWS CloudTrail](#encryption-cmk-trail)

## 先決條件：使用 建立客戶受管金鑰， AWS KMS 並將許可授予 Amazon Keyspaces
<a name="encryption.createCMKMS"></a>

您必須先在 AWS Key Management Service (AWS KMS) 中建立金鑰，然後授權 Amazon Keyspaces 使用該金鑰，才能使用[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表。

### 步驟 1：使用 建立客戶受管金鑰 AWS KMS
<a name="encryption-create-key"></a>

若要建立用於保護 Amazon Keyspaces 資料表的客戶受管金鑰，您可以遵循使用主控台或 AWS API [建立對稱加密 KMS 金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html#create-symmetric-cmk)中的步驟。

### 步驟 2：授權使用客戶受管金鑰
<a name="encryption-authz"></a>

在您可以選擇[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表之前，該客戶受管金鑰上的政策必須授予 Amazon Keyspaces 代表您使用它的許可。您可以完全控制客戶受管金鑰上的政策和授予。您可以在[金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)、[IAM 政策](https://docs.aws.amazon.com/kms/latest/developerguide/iam-policies.html)或[授予](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html)中提供這些許可。

Amazon Keyspaces 不需要額外的授權，即可使用預設值[AWS 擁有的金鑰](encryption.howitworks.md#keyspaces-owned)來保護您 AWS 帳戶中的 Amazon Keyspaces 資料表。

下列主題說明如何使用 IAM 政策和授予來設定必要的許可，以允許 Amazon Keyspaces 資料表使用客戶受管金鑰。

**Topics**
+ [客戶受管金鑰的金鑰政策](#encryption-customer-managed-policy)
+ [範例金鑰政策](#encryption-customer-managed-policy-sample)
+ [使用授予授權 Amazon Keyspaces](#encryption-grants)

#### 客戶受管金鑰的金鑰政策
<a name="encryption-customer-managed-policy"></a>

當您選取[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表時，Amazon Keyspaces 會取得代表進行選取之主體使用客戶受管金鑰的許可。該委託人、使用者或角色必須擁有 Amazon Keyspaces 所需客戶受管金鑰的許可。

Amazon Keyspaces 至少需要客戶受管金鑰的下列許可：
+ [kms:Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html)
+ [kms:Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)
+ [kms:ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html)\$1 (適用於 kms:ReEncryptFrom and kms:ReEncryptTo)
+ kms:GenerateDataKey\$1 (適用於 [kms:GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html) 和 [kms:GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html))
+ [kms:DescribeKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DescribeKey.html)
+ [kms:CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html)

#### 範例金鑰政策
<a name="encryption-customer-managed-policy-sample"></a>

例如，以下範例金鑰政策只會提供必要許可。政策具有下列效果：
+ 允許 Amazon Keyspaces 在密碼編譯操作中使用客戶受管金鑰並建立授予，但僅限於代表帳戶中有權使用 Amazon Keyspaces 的主體時。如果政策陳述式中指定的委託人沒有使用 Amazon Keyspaces 的許可，呼叫會失敗，即使它來自 Amazon Keyspaces 服務。
+ [kms:ViaService](https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html#conditions-kms-via-service) 條件金鑰只有在請求來自 Amazon Keyspaces 時，才允許 許可，這些請求代表政策陳述式中列出的委託人。這些主體無法直接呼叫這些操作。請注意，`kms:ViaService` 值 (`cassandra.*.amazonaws.com`) 在區域位置中有星號 (\$1)。Amazon Keyspaces 需要 許可，才能獨立於任何特定項目 AWS 區域。
+ 授予客戶受管金鑰管理員 （可擔任該`db-team`角色的使用者） 對客戶受管金鑰的唯讀存取權，以及撤銷授予的許可，包括 [Amazon Keyspaces 保護資料表所需的授予](#encryption-grants)。
+ 授予 Amazon Keyspaces 對客戶受管金鑰的唯讀存取權。在這種情況下，Amazon Keyspaces 可以直接呼叫這些操作。它不必代表帳戶委託人採取行動。

使用範例金鑰政策之前，請將範例主體取代為來自您的實際主體 AWS 帳戶。

```
{
  "Id": "key-policy-cassandra",
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid" : "Allow access through Amazon Keyspaces for all principals in the account that are authorized to use Amazon Keyspaces",
      "Effect": "Allow",
      "Principal": {"AWS": "arn:aws:iam::111122223333:user/db-lead"},
      "Action": [
        "kms:Encrypt",
        "kms:Decrypt",
        "kms:ReEncrypt*",
        "kms:GenerateDataKey*",
        "kms:DescribeKey",
        "kms:CreateGrant"
      ],
      "Resource": "*",      
      "Condition": { 
         "StringLike": {
           "kms:ViaService" : "cassandra.*.amazonaws.com"
         }
      }
    },
    {
      "Sid":  "Allow administrators to view the customer managed key and revoke grants",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::111122223333:role/db-team"
       },
      "Action": [
        "kms:Describe*",
        "kms:Get*",
        "kms:List*",
        "kms:RevokeGrant"
      ],
      "Resource": "*"
    }
  ]
}
```

#### 使用授予授權 Amazon Keyspaces
<a name="encryption-grants"></a>

除了金鑰政策之外，Amazon Keyspaces 還使用授予來設定客戶受管金鑰的許可。若要檢視您帳戶中客戶受管金鑰的授予，請使用 [ListGrants](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListGrants.html) 操作。Amazon Keyspaces 不需要授予或任何其他許可，即可使用 [AWS 擁有的金鑰](encryption.howitworks.md#keyspaces-owned)來保護您的資料表。

Amazon Keyspaces 會在執行背景系統維護和持續資料保護任務時使用授予許可。它還使用授與來產生資料表金鑰。

每個授與都專屬於一個資料表。如果帳戶包含以相同客戶受管金鑰加密的多個資料表，則會授予每個資料表的每種類型。[Amazon Keyspaces 加密內容](https://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html)會限制授予，其中包含資料表名稱和 AWS 帳戶 ID。授予包含不再需要時[淘汰授予的](https://docs.aws.amazon.com/kms/latest/APIReference/API_RetireGrant.html)許可。

若要建立授予，Amazon Keyspaces 必須具有`CreateGrant`代表建立加密資料表的使用者呼叫 的許可。

金鑰政策也會允許帳戶[撤銷客戶受管金鑰上的授予](https://docs.aws.amazon.com/kms/latest/APIReference/API_RevokeGrant.html)。不過，如果您撤銷作用中加密資料表的授予，Amazon Keyspaces 將無法保護和維護資料表。

## 步驟 3：指定新資料表的客戶受管金鑰
<a name="encryption.tutorial-creating"></a>

請依照下列步驟，使用 Amazon Keyspaces 主控台或 CQL 在新資料表上指定客戶受管金鑰。

### 使用客戶受管金鑰建立加密的資料表 （主控台）
<a name="encryption.tutorial-console"></a>

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在導覽窗格中，選擇 **Tables** (資料表)，然後選擇 **Create table** (建立資料表)。

1. 在**資料表詳細資訊區段的建立**資料表頁面上，選取金鑰空間，並提供新資料表的名稱。 ****

1. 在**結構描述**區段中，建立資料表的結構描述。

1. 在**資料表設定**區段中，選擇**自訂設定**。

1. 繼續至**加密設定**。

   在此步驟中，您會選取資料表的加密設定。

   在**選擇 AWS KMS key**下的**靜態加密**區段中，選擇**選擇不同的 KMS 金鑰 （進階）** 選項，然後在搜尋欄位中，選擇 AWS KMS key 或輸入 Amazon Resource Name (ARN)。
**注意**  
如果您選取的金鑰無法存取或缺少必要的許可，請參閱[《 開發人員指南》中的對金鑰存取進行故障診斷](https://docs.aws.amazon.com/kms/latest/developerguide/policy-evaluation.html)。 AWS Key Management Service 

1. 選擇 **Create** (建立) 以建立加密資料表。

### 使用客戶受管金鑰建立新資料表以進行靜態加密 (CQL)
<a name="encryption.tutorial-cql"></a>

若要建立使用客戶受管金鑰進行靜態加密的新資料表，您可以使用 `CREATE TABLE`陳述式，如下列範例所示。請務必使用授予 Amazon Keyspaces 之許可的有效金鑰的 ARN 取代金鑰 ARN。

```
CREATE TABLE my_keyspace.my_table(id bigint, name text, place text STATIC, PRIMARY KEY(id, name)) WITH CUSTOM_PROPERTIES = {
        'encryption_specification':{
                'encryption_type': 'CUSTOMER_MANAGED_KMS_KEY', 
                'kms_key_identifier':'arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111'
            }
    };
```

如果您收到 `Invalid Request Exception`，則需要確認客戶受管金鑰有效，且 Amazon Keyspaces 具有必要的許可。若要確認金鑰已正確設定，請參閱《 AWS Key Management Service 開發人員指南》中的對[金鑰存取進行故障診斷](https://docs.aws.amazon.com/kms/latest/developerguide/policy-evaluation.html)。

## 步驟 4：更新現有資料表的加密金鑰
<a name="encryption.tutorial-update"></a>

您也可以使用 Amazon Keyspaces 主控台或 CQL 隨時變更 AWS 擁有的金鑰 和客戶受管 KMS 金鑰之間現有資料表的加密金鑰。

### 使用新的客戶受管金鑰更新現有的資料表 （主控台）
<a name="encryption.tutorial-update-console"></a>

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1.  在導覽窗格中，選擇 **Tables** (資料表)。

1. 選擇您要更新的資料表，然後選擇**其他設定**索引標籤。

1. 在**靜態加密**區段中，選擇**管理加密**以編輯資料表的加密設定。

   在**選擇 AWS KMS key**下，選擇選項 **選擇不同的 KMS 金鑰 （進階）**，然後在搜尋欄位中選擇 AWS KMS key 或輸入 Amazon Resource Name (ARN)。
**注意**  
如果您選取的金鑰無效，請參閱《 AWS Key Management Service 開發人員指南》中的[對金鑰存取進行故障診斷](https://docs.aws.amazon.com/kms/latest/developerguide/policy-evaluation.html)。

   或者，您可以 AWS 擁有的金鑰 為使用客戶受管金鑰加密的資料表選擇 。

1. 選擇**儲存變更**以儲存您對資料表的變更。

### 更新用於現有資料表的加密金鑰
<a name="encryption.tutorial-update-cql"></a>

若要變更現有資料表的加密金鑰，您可以使用 `ALTER TABLE`陳述式來指定用於靜態加密的客戶受管金鑰。請務必使用授予 Amazon Keyspaces 之許可的有效金鑰的 ARN 取代金鑰 ARN。

```
ALTER TABLE my_keyspace.my_table WITH CUSTOM_PROPERTIES = {     
              'encryption_specification':{ 
                      'encryption_type': 'CUSTOMER_MANAGED_KMS_KEY', 
                      'kms_key_identifier':'arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111'     
                  } 
         };
```

如果您收到 `Invalid Request Exception`，則需要確認客戶受管金鑰有效，且 Amazon Keyspaces 具有必要的許可。若要確認金鑰已正確設定，請參閱《 AWS Key Management Service 開發人員指南》中的對[金鑰存取進行故障診斷](https://docs.aws.amazon.com/kms/latest/developerguide/policy-evaluation.html)。

若要使用 將加密金鑰變更回預設靜態加密選項 AWS 擁有的金鑰，您可以使用 `ALTER TABLE`陳述式，如下列範例所示。

```
ALTER TABLE my_keyspace.my_table WITH CUSTOM_PROPERTIES = {
                'encryption_specification':{
                      'encryption_type' : 'AWS_OWNED_KMS_KEY' 
                    } 
         };
```

## 步驟 5：在日誌中使用 Amazon Keyspaces 加密內容
<a name="encryption-context"></a>

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

Amazon Keyspaces 在所有 AWS KMS 密碼編譯操作中使用相同的加密內容。如果您使用[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表，您可以使用加密內容來識別稽核記錄和日誌中客戶受管金鑰的使用。它也會以純文字顯示在日誌中，例如 [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)的日誌中。

在對 的請求中 AWS KMS，Amazon Keyspaces 使用具有三個鍵/值對的加密內容。

```
"encryptionContextSubset": {
    "aws:cassandra:keyspaceName": "my_keyspace",
    "aws:cassandra:tableName": "mytable"
    "aws:cassandra:subscriberId": "111122223333"
}
```
+ **金鑰空間** – 第一個金鑰/值對會識別包含 Amazon Keyspaces 正在加密之資料表的金鑰空間。金鑰為 `aws:cassandra:keyspaceName`。值是金鑰空間的名稱。

  ```
  "aws:cassandra:keyspaceName": "<keyspace-name>"
  ```

  例如：

  ```
  "aws:cassandra:keyspaceName": "my_keyspace"
  ```
+ **資料表** – 第二個金鑰/值對可識別 Amazon Keyspaces 正在加密的資料表。金鑰為 `aws:cassandra:tableName`。值是資料表的名稱。

  ```
  "aws:cassandra:tableName": "<table-name>"
  ```

  例如：

  ```
  "aws:cassandra:tableName": "my_table"
  ```
+ **帳戶** – 第三個鍵/值對識別 AWS 帳戶。金鑰為 `aws:cassandra:subscriberId`。值是帳戶 ID。

  ```
  "aws:cassandra:subscriberId": "<account-id>"
  ```

  例如：

  ```
  "aws:cassandra:subscriberId": "111122223333"
  ```

## 步驟 6：使用 設定監控 AWS CloudTrail
<a name="encryption-cmk-trail"></a>

如果您使用[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表，您可以使用 AWS CloudTrail 日誌來追蹤 Amazon Keyspaces AWS KMS 代表您傳送給 的請求。

本節會討論 `GenerateDataKey`、`Decrypt`、 `DescribeKey`和 `CreateGrant`請求。此外，Amazon Keyspaces 會使用 [RetireGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RetireGrant.html) 操作，在刪除資料表時移除授予。

**注意**  
當您使用 Amazon Keyspaces 時，某些操作可能會產生具有 `invokedBy` 欄位的 CloudTrail 事件`dynamodb.amazonaws.com`。這是預期會發生的，因為 Amazon Keyspaces 與 Amazon DynamoDB 整合以提供其服務。

**GenerateDataKey**  
Amazon Keyspaces 會建立唯一的資料表金鑰來加密靜態資料。它會將 *[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)* 請求傳送至 AWS KMS ，以指定資料表的 KMS 金鑰。  
記錄 `GenerateDataKey` 操作的事件類似於以下範例事件。使用者是 Amazon Keyspaces 服務帳戶。這些參數包括客戶受管金鑰的 Amazon Resource Name (ARN)、需要 256 位元金鑰的金鑰指標，以及識別金鑰空間、資料表和 的[加密內容](#encryption-context) AWS 帳戶。  

```
{
    "eventVersion": "1.08",
    "userIdentity": {
        "type": "AWSService",
        "invokedBy": "AWS Internal"
    },
    "eventTime": "2021-04-16T04:56:05Z",
    "eventSource": "kms.amazonaws.com",
    "eventName": "GenerateDataKey",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "AWS Internal",
    "userAgent": "AWS Internal",
    "requestParameters": {
        "keySpec": "AES_256",
        "encryptionContext": {
            "aws:cassandra:keyspaceName": "my_keyspace",
            "aws:cassandra:tableName": "my_table",
            "aws:cassandra:subscriberId": "123SAMPLE012"
        },
        "keyId": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
    },
    "responseElements": null,
    "requestID": "5e8e9cb5-9194-4334-aacc-9dd7d50fe246",
    "eventID": "49fccab9-2448-4b97-a89d-7d5c39318d6f",
    "readOnly": true,
    "resources": [
        {
            "accountId": "123SAMPLE012",
            "type": "AWS::KMS::Key",
            "ARN": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
        }
    ],
    "eventType": "AwsApiCall",
    "managementEvent": true,
    "eventCategory": "Management",
    "recipientAccountId": "123SAMPLE012",
    "sharedEventID": "84fbaaf0-9641-4e32-9147-57d2cb08792e"
}
```

**DescribeKey**  
Amazon Keyspaces 使用 [DescribeKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DescribeKey.html) 操作來判斷您選取的 KMS 金鑰是否存在於帳戶和區域中。  
記錄 `DescribeKey` 操作的事件類似於以下範例事件。使用者是 Amazon Keyspaces 服務帳戶。這些參數包括客戶受管金鑰的 ARN，以及需要 256 位元金鑰的金鑰指標。  

```
{
    "eventVersion": "1.08",
    "userIdentity": {
        "type": "IAMUser",
        "principalId": "AIDAZ3FNIIVIZZ6H7CFQG",
        "arn": "arn:aws:iam::123SAMPLE012:user/admin",
        "accountId": "123SAMPLE012",
        "accessKeyId": "AKIAIOSFODNN7EXAMPLE",
        "userName": "admin",
        "sessionContext": {
            "sessionIssuer": {},
            "webIdFederationData": {},
            "attributes": {
                "mfaAuthenticated": "false",
                "creationDate": "2021-04-16T04:55:42Z"
            }
        },
        "invokedBy": "AWS Internal"
    },
    "eventTime": "2021-04-16T04:55:58Z",
    "eventSource": "kms.amazonaws.com",
    "eventName": "DescribeKey",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "AWS Internal",
    "userAgent": "AWS Internal",
    "requestParameters": {
        "keyId": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
    },
    "responseElements": null,
    "requestID": "c25a8105-050b-4f52-8358-6e872fb03a6c",
    "eventID": "0d96420e-707e-41b9-9118-56585a669658",
    "readOnly": true,
    "resources": [
        {
            "accountId": "123SAMPLE012",
            "type": "AWS::KMS::Key",
            "ARN": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
        }
    ],
    "eventType": "AwsApiCall",
    "managementEvent": true,
    "eventCategory": "Management",
    "recipientAccountId": "123SAMPLE012"
}
```

**解密**  
當您存取 Amazon Keyspaces 資料表時，Amazon Keyspaces 需要解密資料表金鑰，以便在階層中解密其下方的金鑰。然後解密資料表中的資料。若要解密資料表金鑰，Amazon Keyspaces 會將[解密](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)請求傳送至 AWS KMS ，以指定資料表的 KMS 金鑰。  
記錄 `Decrypt` 操作的事件類似於以下範例事件。使用者是 中存取資料表 AWS 帳戶 的主體。這些參數包括加密的資料表金鑰 （做為加密文字 Blob)，以及識別資料表和 的[加密內容](#encryption-context) AWS 帳戶。 會從加密文字 AWS KMS 衍生客戶受管金鑰的 ID。  

```
{
    "eventVersion": "1.08",
    "userIdentity": {
        "type": "AWSService",
        "invokedBy": "AWS Internal"
    },
    "eventTime": "2021-04-16T05:29:44Z",
    "eventSource": "kms.amazonaws.com",
    "eventName": "Decrypt",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "AWS Internal",
    "userAgent": "AWS Internal",
    "requestParameters": {
        "encryptionContext": {
            "aws:cassandra:keyspaceName": "my_keyspace",
            "aws:cassandra:tableName": "my_table",
            "aws:cassandra:subscriberId": "123SAMPLE012"
        },
        "encryptionAlgorithm": "SYMMETRIC_DEFAULT"
    },
    "responseElements": null,
    "requestID": "50e80373-83c9-4034-8226-5439e1c9b259",
    "eventID": "8db9788f-04a5-4ae2-90c9-15c79c411b6b",
    "readOnly": true,
    "resources": [
        {
            "accountId": "123SAMPLE012",
            "type": "AWS::KMS::Key",
            "ARN": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
        }
    ],
    "eventType": "AwsApiCall",
    "managementEvent": true,
    "eventCategory": "Management",
    "recipientAccountId": "123SAMPLE012",
    "sharedEventID": "7ed99e2d-910a-4708-a4e3-0180d8dbb68e"
}
```

**CreateGrant**  
當您使用[客戶受管金鑰](encryption.howitworks.md#customer-managed)來保護 Amazon Keyspaces 資料表時，Amazon Keyspaces 會使用[授予](#encryption-grants)來允許服務執行持續的資料保護和維護和耐久性任務。在 上不需要這些授權[AWS 擁有的金鑰](encryption.howitworks.md#keyspaces-owned)。  
Amazon Keyspaces 建立的授予專屬於資料表。[CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html) 請求中的主體是建立資料表的使用者。  
記錄 `CreateGrant` 操作的事件類似於以下範例事件。這些參數包括資料表的客戶受管金鑰 ARN、承授者委託人和淘汰委託人 (Amazon Keyspaces 服務），以及授予涵蓋的操作。它還包含需要所有加密操作使用指定[加密內容](#encryption-context)的限制。  

```
{
    "eventVersion": "1.08",
    "userIdentity": {
        "type": "IAMUser",
        "principalId": "AIDAZ3FNIIVIZZ6H7CFQG",
        "arn": "arn:aws:iam::arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111:user/admin",
        "accountId": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111",
        "accessKeyId": "AKIAI44QH8DHBEXAMPLE",
        "userName": "admin",
        "sessionContext": {
            "sessionIssuer": {},
            "webIdFederationData": {},
            "attributes": {
                "mfaAuthenticated": "false",
                "creationDate": "2021-04-16T04:55:42Z"
            }
        },
        "invokedBy": "AWS Internal"
    },
    "eventTime": "2021-04-16T05:11:10Z",
    "eventSource": "kms.amazonaws.com",
    "eventName": "CreateGrant",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "AWS Internal",
    "userAgent": "AWS Internal",
    "requestParameters": {
        "keyId": "a7d328af-215e-4661-9a69-88c858909f20",
        "operations": [
            "DescribeKey",
            "GenerateDataKey",
            "Decrypt",
            "Encrypt",
            "ReEncryptFrom",
            "ReEncryptTo",
            "RetireGrant"
        ],
        "constraints": {
            "encryptionContextSubset": {
                "aws:cassandra:keyspaceName": "my_keyspace",
                "aws:cassandra:tableName": "my_table",
                "aws:cassandra:subscriberId": "123SAMPLE012"
            }
        },
        "retiringPrincipal": "cassandratest.us-east-1.amazonaws.com",
        "granteePrincipal": "cassandratest.us-east-1.amazonaws.com"
    },
    "responseElements": {
        "grantId": "18e4235f1b07f289762a31a1886cb5efd225f069280d4f76cd83b9b9b5501013"
    },
    "requestID": "b379a767-1f9b-48c3-b731-fb23e865e7f7",
    "eventID": "29ee1fd4-28f2-416f-a419-551910d20291",
    "readOnly": false,
    "resources": [
        {
            "accountId": "123SAMPLE012",
            "type": "AWS::KMS::Key",
            "ARN": "arn:aws:kms:eu-west-1:5555555555555:key/11111111-1111-111-1111-111111111111"
        }
    ],
    "eventType": "AwsApiCall",
    "managementEvent": true,
    "eventCategory": "Management",
    "recipientAccountId": "123SAMPLE012"
}
```