

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

# Amazon OpenSearch Serverless 中的加密
<a name="serverless-encryption"></a>

## 靜態加密
<a name="serverless-encryption-at-rest"></a>

您建立的每個 Amazon OpenSearch Serverless 集合的保護都是使用靜態資料加密來實現，靜態資料加密是一種安全功能，可協助防止您的資料遭到未經授權存取。靜態加密使用 AWS Key Management Service (AWS KMS) 來存放和管理加密金鑰。其會使用 256 位元金鑰的進階加密標準演算法 (AES-256) 來執行加密。

**Topics**
+ [加密政策](#serverless-encryption-policies)
+ [考量事項](#serverless-encryption-considerations)
+ [必要許可](#serverless-encryption-permissions)
+ [客戶自管金鑰的金鑰政策](#serverless-customer-cmk-policy)
+ [OpenSearch Serverless 如何在 中使用授予 AWS KMS](#serverless-encryption-grants)
+ [建立加密政策 (主控台)](#serverless-encryption-console)
+ [建立加密政策 (AWS CLI)](#serverless-encryption-cli)
+ [檢視加密政策](#serverless-encryption-list)
+ [更新加密政策](#serverless-encryption-update)
+ [刪除加密政策](#serverless-encryption-delete)

### 加密政策
<a name="serverless-encryption-policies"></a>

您可以使用加密政策，透過自動將加密金鑰指派給符合特定名稱或模式的新建集合，大規模管理許多集合。

建立加密政策時，您可以指定*字首*，這是以萬用字元為基礎的比對規則 (例如 `MyCollection*`)，或輸入單一集合名稱。然後，當您建立符合該名稱或字首模式的集合時，系統會將該政策和對應的 KMS 金鑰自動指派給該集合。

建立集合時，您可以透過兩種方式指定 AWS KMS 金鑰：透過安全政策或直接在`CreateCollection`請求中。如果您在`CreateCollection`請求中提供 AWS KMS 金鑰，則優先於任何相符的安全政策。透過此方法，您可以在需要時靈活地覆寫特定集合的政策型加密設定。

![\[Encryption policy creation process with rules and collection matching to KMS key.\]](http://docs.aws.amazon.com/zh_tw/opensearch-service/latest/developerguide/images/serverless-encryption.png)


加密政策包含下列元素：
+ `Rules`：一個或多個集合比對規則，每個規則都包含下列子元素：
  + `ResourceType`：目前唯一的選項是「集合」。加密政策僅套用至集合資源。
  + `Resource`：政策套用至的一個或多個集合名稱或模式 (格式為 `collection/<collection name|pattern>`)。
+ `AWSOwnedKey`：是否要使用 AWS 擁有的金鑰。
+ `KmsARN`：如果您將 `AWSOwnedKey` 設定為 false，請指定用於加密關聯集合之 KMS 金鑰的 Amazon Resource Name (ARN)。如果您包含此參數，OpenSearch Serverless 會忽略 `AWSOwnedKey` 參數。

下列範例政策會將客戶受管金鑰指派給任何名為 `autopartsinventory` 的未來集合，以及以「銷售」一詞開頭的集合：

```
{
   "Rules":[
      {
         "ResourceType":"collection",
         "Resource":[
            "collection/autopartsinventory",
            "collection/sales*"
         ]
      }
   ],
   "AWSOwnedKey":false,
   "KmsARN":"arn:aws:kms:us-east-1:123456789012:key/93fd6da4-a317-4c17-bfe9-382b5d988b36"
}
```

即使政策符合集合名稱，如果資源模式包含萬用字元 (\$1)，您也可以選擇在集合建立期間覆寫此自動指派。如果您選擇覆寫自動金鑰指派，OpenSearch Serverless 會為您建立名為 **auto-<*collection-name*>** 的加密政策，並將其連接到集合。該政策最初只適用於單一集合，但您可以加以修改以包含其他集合。

如果您將政策規則修改為不再符合某個集合，則系統不會將關聯的 KMS 金鑰從該集合取消指派。集合一律會使用其初始加密金鑰保持加密的狀態。如果您想要變更集合的加密金鑰，您必須重新建立集合。

如果來自多個政策的規則符合集合，則會使用更明確的規則。例如，如果某個政策包含的規則適用於 `collection/log*`，而另一個適用於 `collection/logSpecial`，則會使用第二個政策的加密金鑰，因為該金鑰更加明確。

如果某個政策中已有名稱或字首，則無法在另一個政策中使用該名稱或字首。如果您嘗試在不同的加密政策中設定相同的資源模式，則 OpenSearch Serverless 會顯示錯誤。

### 考量事項
<a name="serverless-encryption-considerations"></a>

設定集合的加密時應考慮以下事項：
+ 所有無伺服器集合都*需要*靜態加密。
+ 您可以選擇使用客戶受管金鑰或 AWS 擁有的金鑰。如果您選擇客戶受管金鑰，建議您啟用[自動金鑰輪換](https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html)。
+ 建立集合之後，便無法變更集合的加密金鑰。第一次設定集合時，請仔細選擇要使用的 AWS KMS 集合。
+ 集合只能符合單一加密政策。
+ 具有唯一 KMS 金鑰的集合無法與其他集合共用 OpenSearch 運算單元 (OCU)。每個具有唯一金鑰的集合都需要專屬的 4 個 OCU。
+ 如果您更新加密政策中的 KMS 金鑰，則變更不會影響已指派 KMS 金鑰的現有相符集合。
+ OpenSearch Serverless 不會明確檢查客戶受管金鑰的使用者許可。如果使用者有權透過資料存取政策存取集合，他們就能夠擷取和查詢透過關聯金鑰加密的資料。

### 必要許可
<a name="serverless-encryption-permissions"></a>

OpenSearch Serverless 的靜態加密會使用下列 AWS Identity and Access Management (IAM) 許可。您可以指定 IAM 條件，將使用者限制在特定集合內。
+ `aoss:CreateSecurityPolicy`：建立加密政策。
+ `aoss:ListSecurityPolicies`：列出連接的所有加密政策和集合。
+ `aoss:GetSecurityPolicy`：查看特定加密政策的詳細資訊。
+ `aoss:UpdateSecurityPolicy`：修改加密政策。
+ `aoss:DeleteSecurityPolicy`：刪除加密政策。

下列身分型存取政策範例提供使用者透過資源模式 `collection/application-logs` 管理加密政策所需的最低許可。

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

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "aoss:CreateSecurityPolicy",
            "aoss:UpdateSecurityPolicy",
            "aoss:DeleteSecurityPolicy",
            "aoss:GetSecurityPolicy"
         ],
         "Resource":"*",
         "Condition":{
            "StringEquals":{
               "aoss:collection":"application-logs"
            }
         }
      },
      {
         "Effect":"Allow",
         "Action":[
            "aoss:ListSecurityPolicies"
         ],
         "Resource":"*"
      }
   ]
}
```

------

### 客戶自管金鑰的金鑰政策
<a name="serverless-customer-cmk-policy"></a>

當您選取[客戶受管金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#customer-cmk)來保護集合時，OpenSearch Serverless 會取得許可，以代表進行選取的主體使用 KMS 金鑰。該主體 (使用者或角色) 必須具備 OpenSearch Serverless 所需的 KMS 金鑰許可。您可以在[金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)或 [IAM 政策](https://docs.aws.amazon.com/kms/latest/developerguide/iam-policies.html)中提供這些許可。

OpenSearch Serverless 會在維護操作期間進行 `GenerateDataKey` 和 `Decrypt` KMS API 呼叫，例如自動擴展和軟體更新。您可能會在一般流量模式之外看到這些呼叫。這些呼叫是正常服務操作的一部分，不表示作用中的使用者流量。

當 OpenSearch Serverless 無法存取加密靜態資料的 KMS 金鑰`KMSKeyInaccessibleException`時，會擲回 。當您停用或刪除 KMS 金鑰，或撤銷允許 OpenSearch Serverless 使用金鑰的授予時，就會發生這種情況。

至少，OpenSearch Serverless 需要具備客戶受管金鑰的下列許可：
+ [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)

例如：

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
        "Action": "kms:DescribeKey",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::123456789012:user/Dale"
        },
        "Resource": "*",
        "Condition": {
            "StringEquals": {
                "kms:ViaService": "aoss.us-east-1.amazonaws.com"
            }
        }
    },
    {
        "Action": "kms:CreateGrant",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::123456789012:user/Dale"
        },
        "Resource": "*",
        "Condition": {
            "StringEquals": {
                "kms:ViaService": "aoss.us-east-1.amazonaws.com"
            },
            "ForAllValues:StringEquals": {
                "kms:GrantOperations": [
                    "Decrypt",
                    "GenerateDataKey"
                ]
            },
            "Bool": {
                "kms:GrantIsForAWSResource": "true"
            }
        }
    }
  ]
}
```

------

OpenSearch Serverless 會使用 [kms:GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html) 和 [kms:Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) 許可建立授予。

如需詳細資訊，請參閱 *AWS Key Management Service 開發人員指南*中的 [在 AWS KMS中使用金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)。

### OpenSearch Serverless 如何在 中使用授予 AWS KMS
<a name="serverless-encryption-grants"></a>

OpenSearch Serverless 需要[授予](https://docs.aws.amazon.com/kms/latest/developerguide/grants.html)才能使用客戶受管金鑰。

當您使用新金鑰在帳戶中建立加密政策時，OpenSearch Serverless 會透過傳送 [CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html) 請求至 來代表您建立授予 AWS KMS。中的授予 AWS KMS 用於授予 OpenSearch Serverless 存取客戶帳戶中 KMS 金鑰的權限。

OpenSearch Serverless 需要授予，才能在下列內部操作中使用客戶受管金鑰：
+ 將 [DescribeKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DescribeKey.html) 請求傳送至 AWS KMS ，以驗證提供的對稱客戶受管金鑰 ID 是否有效。
+ 將 [GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html) 請求傳送至 KMS 金鑰，以建立加密物件所用的資料金鑰。
+ 將 [Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) 請求傳送至 AWS KMS 以解密加密的資料金鑰，以便用來加密您的資料。

您可以隨時撤銷授予的存取權，或移除服務對客戶受管金鑰的存取權。如果您這麼做，OpenSearch Serverless 將無法存取由客戶受管金鑰加密的任何資料，這會影響與該資料有關的所有操作，進而導致非同步工作流程中的 `AccessDeniedException` 錯誤和失敗。

當指定的客戶受管金鑰未與任何安全政策或集合相關聯時，OpenSearch Serverless 會淘汰非同步工作流程中的授予。

### 建立加密政策 (主控台)
<a name="serverless-encryption-console"></a>

在加密政策中，您可以指定政策套用至的 KMS 金鑰和一系列集合模式。當您建立集合時，系統會將對應的 KMS 金鑰指派給符合政策中定義模式之一的任何新集合。建議您先建立加密政策，*然後*再開始建立集合。

**建立 OpenSearch Serverless 加密政策**

1. 開啟位於 https：//[https://console.aws.amazon.com/aos/home](https://console.aws.amazon.com/aos/home) 的 Amazon OpenSearch Service 主控台。

1. 在左側導覽面板中，展開 **Serverless** (無伺服器)，然後選擇 **Encryption policies** (加密政策)。

1. 選擇 **Create encryption policy** (建立加密政策)。

1. 提供政策的名稱和描述。

1. 在 **Resources** (資源) 下，輸入此加密政策的一個或多個資源模式。目前 AWS 帳戶 和區域中符合其中一種模式的任何新建集合都會自動指派給此政策。例如，如果您輸入 `ApplicationLogs` (不含萬用字元)，然後再使用該名稱建立集合，則會將該政策和對應的 KMS 金鑰指派給該集合。

   您也可以提供字首，例如 `Logs*`，此字首會將政策指派給名稱以 `Logs` 開頭的任何新集合。透過使用萬用字元，您可以大規模管理多個集合的加密設定。

1. 在 **Encryption** (加密) 下，選擇要使用的 KMS 金鑰。

1. 選擇**建立**。

#### 下一步：建立集合
<a name="serverless-encryption-next"></a>

設定一個或多個加密政策後，您就可以開始建立與這些政策中定義之規則相符的集合。如需說明，請參閱[建立集合](serverless-create.md)。

在集合建立的 **Encryptions** (加密) 步驟中，OpenSearch Serverless 會通知您所輸入的名稱與加密政策中定義的模式相符，並自動將對應的 KMS 金鑰指派給該集合。如果資源模式包含萬用字元 (\$1)，您可以選擇覆寫相符項目並選取自己的金鑰。

### 建立加密政策 (AWS CLI)
<a name="serverless-encryption-cli"></a>

若要使用 OpenSearch Serverless API 操作建立加密政策，您需指定資源模式和 JSON 格式的加密金鑰。[CreateSecurityPolicy](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_CreateSecurityPolicy.html) 請求同時接受內嵌政策和 .json 檔案。

加密政策採用下列格式。此範例 `my-policy.json` 檔案符合任何名為 `autopartsinventory` 的未來集合，以及名稱以 `sales` 開頭的任何集合。

```
{
   "Rules":[
      {
         "ResourceType":"collection",
         "Resource":[
            "collection/autopartsinventory",
            "collection/sales*"
         ]
      }
   ],
   "AWSOwnedKey":false,
   "KmsARN":"arn:aws:kms:us-east-1:123456789012:key/93fd6da4-a317-4c17-bfe9-382b5d988b36"
}
```

若要使用服務擁有的金鑰，請將 `AWSOwnedKey` 設定為 `true`：

```
{
   "Rules":[
      {
         "ResourceType":"collection",
         "Resource":[
            "collection/autopartsinventory",
            "collection/sales*"
         ]
      }
   ],
   "AWSOwnedKey":true
}
```

下列請求會建立加密政策：

```
aws opensearchserverless create-security-policy \
    --name sales-inventory \
    --type encryption \
    --policy file://my-policy.json
```

然後，使用 [CreateCollection](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_CreateCollection.html) API 操作建立一個或多個符合其中一個資源模式的集合。

### 檢視加密政策
<a name="serverless-encryption-list"></a>

在建立集合之前，您可能想要預覽帳戶中現有的加密政策，以查看哪個政策的資源模式與集合名稱相符。下列 [ListSecurityPolicies](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_ListSecurityPolicies.html) 請求會列出您帳戶中的所有加密政策：

```
aws opensearchserverless list-security-policies --type encryption
```

該請求會傳回所有已設定加密政策的相關資訊。使用 `policy` 元素的內容以檢視政策中定義的模式規則：

```
{
   "securityPolicyDetails": [ 
      { 
         "createdDate": 1663693217826,
         "description": "Sample encryption policy",
         "lastModifiedDate": 1663693217826,
         "name": "my-policy",
         "policy": "{\"Rules\":[{\"ResourceType\":\"collection\",\"Resource\":[\"collection/autopartsinventory\",\"collection/sales*\"]}],\"AWSOwnedKey\":true}",
         "policyVersion": "MTY2MzY5MzIxNzgyNl8x",
         "type": "encryption"
      }
   ]
}
```

若要檢視有關特定政策 (包括 KMS 金鑰) 的詳細資訊，請使用 [GetSecurityPolicy](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_GetSecurityPolicy.html) 命令。

### 更新加密政策
<a name="serverless-encryption-update"></a>

如果您更新加密政策中的 KMS 金鑰，則變更只會套用至與設定的名稱或模式相符的新建集合。這不會影響已指派 KMS 金鑰的現有集合。

這也適用於政策比對規則。如果您新增、修改或刪除規則，則變更僅適用於新建集合。如果您修改政策的規則，使其不再符合集合的名稱，則現有集合不會遺失其指派的 KMS 金鑰。

若要更新 OpenSearch Serverless 主控台中的加密政策，請選擇 **Encryption policies** (加密政策)，選取要修改的政策，然後選擇 **Edit** (編輯)。進行變更，然後選擇 **Save** (儲存)。

若要使用 OpenSearch Serverless API 更新加密政策，請使用 [UpdateSecurityPolicy](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_UpdateSecurityPolicy.html) 操作。下列請求會使用新的政策 JSON 文件更新加密政策：

```
aws opensearchserverless update-security-policy \
    --name sales-inventory \
    --type encryption \
    --policy-version 2 \
    --policy file://my-new-policy.json
```

### 刪除加密政策
<a name="serverless-encryption-delete"></a>

當您刪除加密政策時，目前使用政策中定義之 KMS 金鑰的任何集合都不會受到影響。若要刪除 OpenSearch Serverless 主控台中的政策，請選取該政策，然後選擇 **Delete** (刪除)。

您也可以使用 [DeleteSecurityPolicy](https://docs.aws.amazon.com/opensearch-service/latest/ServerlessAPIReference/API_DeleteSecurityPolicy.html) 操作：

```
aws opensearchserverless delete-security-policy --name my-policy --type encryption
```

## 傳輸中加密
<a name="serverless-encryption-in-transit"></a>

在 OpenSearch Serverless 中，集合中的所有路徑都會使用 Transport Layer Security 1.2 (TLS) 搭配業界標準 AES-256 密碼在傳輸中加密。也可透過 TLS 1.2 存取 Opensearch 的所有 APIs 和儀表板。TLS 是一組業界標準的密碼編譯通訊協定，用於加密透過網路交換的資訊。