選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

PutKeyPolicy 搭配 AWS SDK 或 CLI 使用

焦點模式
PutKeyPolicy 搭配 AWS SDK 或 CLI 使用 - AWS Key Management Service

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

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

下列程式碼範例示範如何使用 PutKeyPolicy

CLI
AWS CLI

變更 KMS 金鑰的金鑰政策

下列put-key-policy範例會變更客戶受管金鑰的金鑰政策。

若要開始,請建立金鑰政策並將其儲存在本機 JSON 檔案中。在此範例中,檔案為 key_policy.json。您也可以指定金鑰政策做為 policy 參數的字串值。

此金鑰政策中的第一個陳述式提供 AWS 帳戶使用 IAM 政策來控制對 KMS 金鑰的存取的許可。第二個陳述式提供test-user使用者在 KMS 金鑰上執行 describe-keylist-keys命令的許可。

key_policy.json 的內容:

{ "Version" : "2012-10-17", "Id" : "key-default-1", "Statement" : [ { "Sid" : "Enable IAM User Permissions", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:root" }, "Action" : "kms:*", "Resource" : "*" }, { "Sid" : "Allow Use of Key", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:user/test-user" }, "Action" : [ "kms:DescribeKey", "kms:ListKeys" ], "Resource" : "*" } ] }

若要識別 KMS 金鑰,此範例會使用金鑰 ID,但您也可以使用金鑰 ARN。若要指定金鑰政策,命令會使用 policy 參數。若要指出政策位於 檔案,會使用必要的file://字首。識別所有支援的作業系統上的檔案時,需要此字首。最後, 命令會使用 值為 的 policy-name 參數default。如果未指定政策名稱,預設值為 default。唯一有效的值為 default

aws kms put-key-policy \ --policy-name default \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab \ --policy file://key_policy.json

此命令不會產生任何輸出。若要驗證命令是否有效,請使用 get-key-policy命令。下列範例命令會取得相同 KMS 金鑰的金鑰政策。值為 的 output 參數會text傳回易於讀取的文字格式。

aws kms get-key-policy \ --policy-name default \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab \ --output text

輸出:

{ "Version" : "2012-10-17", "Id" : "key-default-1", "Statement" : [ { "Sid" : "Enable IAM User Permissions", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:root" }, "Action" : "kms:*", "Resource" : "*" }, { "Sid" : "Allow Use of Key", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:user/test-user" }, "Action" : [ "kms:Describe", "kms:List" ], "Resource" : "*" } ] }

如需詳細資訊,請參閱 金鑰管理服務開發人員指南中的變更金鑰政策AWS

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutKeyPolicy

PHP
SDK for PHP
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/*** * @param string $keyId * @param string $policy * @return void */ public function putKeyPolicy(string $keyId, string $policy) { try { $this->client->putKeyPolicy([ 'KeyId' => $keyId, 'Policy' => $policy, ]); }catch(KmsException $caught){ echo "There was a problem replacing the key policy: {$caught->getAwsErrorMessage()}\n"; throw $caught; } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for PHP API 參考中的 PutKeyPolicy

Python
SDK for Python (Boto3)
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class KeyPolicy: def __init__(self, kms_client): self.kms_client = kms_client @classmethod def from_client(cls) -> "KeyPolicy": """ Creates a KeyPolicy instance with a default KMS client. :return: An instance of KeyPolicy initialized with the default KMS client. """ kms_client = boto3.client("kms") return cls(kms_client) def set_policy(self, key_id: str, policy: dict[str, any]) -> None: """ Sets the policy of a key. Setting a policy entirely overwrites the existing policy, so care is taken to add a statement to the existing list of statements rather than simply writing a new policy. :param key_id: The ARN or ID of the key to set the policy to. :param policy: The existing policy of the key. :return: None """ principal = input( "Enter the ARN of an IAM role to set as the principal on the policy: " ) if key_id != "" and principal != "": # The updated policy replaces the existing policy. Add a new statement to # the list along with the original policy statements. policy["Statement"].append( { "Sid": "Allow access for ExampleRole", "Effect": "Allow", "Principal": {"AWS": principal}, "Action": [ "kms:Encrypt", "kms:GenerateDataKey*", "kms:Decrypt", "kms:DescribeKey", "kms:ReEncrypt*", ], "Resource": "*", } ) try: self.kms_client.put_key_policy(KeyId=key_id, Policy=json.dumps(policy)) except ClientError as err: logger.error( "Couldn't set policy for key %s. Here's why %s", key_id, err.response["Error"]["Message"], ) raise else: print(f"Set policy for key {key_id}.") else: print("Skipping set policy demo.")
  • 如需 API 詳細資訊,請參閱 SDK AWS for Python (Boto3) API 參考中的 PutKeyPolicy

AWS CLI

變更 KMS 金鑰的金鑰政策

下列put-key-policy範例會變更客戶受管金鑰的金鑰政策。

若要開始,請建立金鑰政策並將其儲存在本機 JSON 檔案中。在此範例中,檔案為 key_policy.json。您也可以指定金鑰政策做為 policy 參數的字串值。

此金鑰政策中的第一個陳述式提供 AWS 帳戶使用 IAM 政策來控制對 KMS 金鑰的存取的許可。第二個陳述式提供test-user使用者在 KMS 金鑰上執行 describe-keylist-keys命令的許可。

key_policy.json 的內容:

{ "Version" : "2012-10-17", "Id" : "key-default-1", "Statement" : [ { "Sid" : "Enable IAM User Permissions", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:root" }, "Action" : "kms:*", "Resource" : "*" }, { "Sid" : "Allow Use of Key", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:user/test-user" }, "Action" : [ "kms:DescribeKey", "kms:ListKeys" ], "Resource" : "*" } ] }

若要識別 KMS 金鑰,此範例會使用金鑰 ID,但您也可以使用金鑰 ARN。若要指定金鑰政策,命令會使用 policy 參數。若要指出政策位於 檔案,會使用必要的file://字首。識別所有支援的作業系統上的檔案時,需要此字首。最後, 命令會使用 值為 的 policy-name 參數default。如果未指定政策名稱,預設值為 default。唯一有效的值為 default

aws kms put-key-policy \ --policy-name default \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab \ --policy file://key_policy.json

此命令不會產生任何輸出。若要驗證命令是否有效,請使用 get-key-policy命令。下列範例命令會取得相同 KMS 金鑰的金鑰政策。值為 的 output 參數會text傳回易於讀取的文字格式。

aws kms get-key-policy \ --policy-name default \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab \ --output text

輸出:

{ "Version" : "2012-10-17", "Id" : "key-default-1", "Statement" : [ { "Sid" : "Enable IAM User Permissions", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:root" }, "Action" : "kms:*", "Resource" : "*" }, { "Sid" : "Allow Use of Key", "Effect" : "Allow", "Principal" : { "AWS" : "arn:aws:iam::111122223333:user/test-user" }, "Action" : [ "kms:Describe", "kms:List" ], "Resource" : "*" } ] }

如需詳細資訊,請參閱 金鑰管理服務開發人員指南中的變更金鑰政策AWS

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutKeyPolicy

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 將此服務與 AWS SDK 搭配使用。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。

下一個主題:

ReEncrypt

上一個主題:

ListKeys
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。