

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

# IAM 政策的最佳實務
<a name="iam-policies-best-practices"></a>

保護對 的存取對於所有 AWS 資源的安全 AWS KMS keys 至關重要。KMS 金鑰可用來保護您的 AWS 帳戶中許多最敏感的資源。花時間設計控制 KMS 金鑰存取的[金鑰政策](key-policies.md)、IAM 政策、[授權](grants.md)和 VPC 端點政策。

在控制 KMS 金鑰存取的 IAM 政策陳述式中，使用[最低權限政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。僅為 IAM 主體提供他們必須使用或管理之 KMS 金鑰所需的許可。

下列最佳實務適用於控制 AWS KMS 金鑰和別名存取的 IAM 政策。如需一般 IAM 政策最佳實務指南，請參閱《IAM 使用者指南》**中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

**使用金鑰政策**  
盡可能在影響一個 KMS 金鑰的金鑰政策中提供許可，而不是在可套用至許多 KMS 金鑰的 IAM 政策中提供許可，包括其他 AWS 帳戶中的這些政策。這對 [kms:PutKeyPolicy](https://docs.aws.amazon.com/kms/latest/APIReference/API_PutKeyPolicy.html) 和 [kms:ScheduleKeyDeletion](https://docs.aws.amazon.com/kms/latest/APIReference/API_ScheduleKeyDeletion.html) 等敏感許可特別重要，也適用於決定資料如何受到保護的密碼編譯操作。

**限制 CreateKey 許可**  
僅對需要的主體授予建立金鑰 ([kms:CreateKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html)) 的許可。建立 KMS 金鑰的主體也會設定其金鑰政策，讓他們可以授予自己和其他人使用和管理其所建立之 KMS 金鑰的許可。當您允許此許可時，請考慮使用[政策條件](policy-conditions.md)對其進行限制。例如，您可以使用 [kms:KeySpec](conditions-kms.md#conditions-kms-key-spec) 條件來限制對稱加密 KMS 金鑰的許可。

**在 IAM 政策中指定 KMS 金鑰**  
最佳實務是在政策陳述式的 `Resource` 元素中指定許可套用至其中之每個 KMS 金鑰的[金鑰 ARN](concepts.md#key-id-key-ARN)。此實務會限制主體所需之 KMS 金鑰的許可。例如，此 `Resource` 元素只會列出主體需要使用的 KMS 金鑰。  

```
"Resource": [
    "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
    "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"
]
```
指定 KMS 金鑰不切實際時，請使用限制對信任 AWS 帳戶 和 區域中 KMS 金鑰存取`Resource`的值，例如 `arn:aws:kms:region:account:key/*`。或限制對信任之所有區域 (\$1) 中 KMS 金鑰的存取 AWS 帳戶，例如 `arn:aws:kms:*:account:key/*`。  
您無法使用[金鑰 ID](concepts.md#key-id-key-id)、[別名名稱](concepts.md#key-id-alias-name)，或[別名 ARN](concepts.md#key-id-alias-ARN) 來代表 IAM 政策 `Resource` 欄位中的 KMS 金鑰。如果您指定別名 ARN，政策會套用至別名，而不是 KMS 金鑰。如需別名 IAM 政策的資訊，請參閱 [控制對別名的存取](alias-access.md)。

**避免 IAM 政策中的 "Resource": "\$1"**  <a name="avoid-resource-star"></a>
明智地使用萬用字元 (\$1)。在金鑰政策中，`Resource` 元素中的萬用字元代表金鑰政策所連接的 KMS 金鑰。但是在 IAM 政策中， `Resource`元素 (`"Resource": "*"`) 中只有萬用字元會將許可套用至 AWS 帳戶 委託人帳戶有權使用的所有 KMS 金鑰。這可能包括[其他 中的 KMS 金鑰 AWS 帳戶](key-policy-modifying-external-accounts.md)，以及委託人帳戶中的 KMS 金鑰。  
例如，若要在另一個 中使用 KMS 金鑰 AWS 帳戶，委託人需要來自外部帳戶中 KMS 金鑰之金鑰政策的許可，以及來自自己帳戶中 IAM 政策的許可。假設某個任意帳戶提供了針對其 KMS 金鑰的 AWS 帳戶 [kms:Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html) 許可。如果是這樣，您帳戶中為所有 KMS 金鑰 (`"Resource": "*"`) 提供角色 `kms:Decrypt` 許可的 IAM 政策將滿足需求的 IAM 部分。因此，擔任該角色的主體現在可以使用不可信任帳戶中的 KMS 金鑰來解密加密文字。其操作的項目會顯示在兩個帳戶的 CloudTrail 日誌中。  
特別是，避免在允許下列 API 操作的政策陳述式中使用 `"Resource": "*"`。您可以在其他 的 KMS 金鑰上呼叫這些操作 AWS 帳戶。  
+ [DescribeKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_DescribeKey.html)
+ [GetKeyRotationStatus](https://docs.aws.amazon.com/kms/latest/APIReference/API_GetKeyRotationStatus.html)
+ [密碼編譯操作](kms-cryptography.md#cryptographic-operations) ([Encrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Encrypt.html)、[Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html)、[GenerateDataKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html)、[GenerateDataKeyPair](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyPair.html)、[GenerateDataKeyWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyWithoutPlaintext.html)、[GenerateDataKeyPairWithoutPlaintext](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKeyPairWithoutPlaintext.html)、[GetPublicKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_GetPublicKey.html)、[ReEncrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_ReEncrypt.html)、[Sign](https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html)、[Verify](https://docs.aws.amazon.com/kms/latest/APIReference/API_Verify.html))
+ [CreateGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateGrant.html)、[ListGrants](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListGrants.html)、[ListRetirableGrants](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListRetirableGrants.html)、[RetireGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RetireGrant.html)、[RevokeGrant](https://docs.aws.amazon.com/kms/latest/APIReference/API_RevokeGrant.html)

**何時使用 "Resource": "\$1"**  <a name="require-resource-star"></a>
在 IAM 政策中，僅對需要的許可在 `Resource` 元素中使用萬用字元。只有下列許可需要 `"Resource": "*"` 元素。  
+ [kms:CreateKey](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html)
+ [kms:GenerateRandom](https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateRandom.html)
+ [kms:ListAliases](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListAliases.html)
+ [kms:ListKeys](https://docs.aws.amazon.com/kms/latest/APIReference/API_ListKeys.html)
+ 自訂金鑰存放區的許可，例如 [kms:CreateCustomKeyStore](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateCustomKeyStore.html) 和 [kms:ConnectCustomKeyStore](https://docs.aws.amazon.com/kms/latest/APIReference/API_ConnectCustomKeyStore.html)。
別名操作的許可 ([kms:CreateAlias](https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateAlias.html)、[kms:UpdateAlias](https://docs.aws.amazon.com/kms/latest/APIReference/API_UpdateAlias.html)、[kms:DeleteAlias](https://docs.aws.amazon.com/kms/latest/APIReference/API_DeleteAlias.html)) 必須連接至別名和 KMS 金鑰。您可以使用 IAM 政策中的 `"Resource": "*"` 來表示別名和 KMS 金鑰，或在 `Resource` 元素中指定別名和 KMS 金鑰。如需範例，請參閱 [控制對別名的存取](alias-access.md)。

 

本主題中的範例提供有關設計 KMS 金鑰之 IAM 政策的詳細資訊和指引。如需所有 AWS 資源的 IAM 最佳實務，請參閱《[IAM 使用者指南》中的 IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。 **