文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
PutKeyPolicy
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 PutKeyPolicy
。
- CLI
-
- AWS CLI
-
變更 KMS 金鑰的金鑰政策
下列
put-key-policy
範例會變更客戶受管金鑰的金鑰政策。若要開始,請建立金鑰政策並將其儲存在本機 JSON 檔案中。在此範例中,檔案為
key_policy.json
。您也可以將金鑰政策指定為policy
參數的字串值。此金鑰政策中的第一個陳述式授予 AWS 帳戶使用 IAM 政策控制 KMS 金鑰存取權的許可。第二個陳述式提供
test-user
使用者在 KMS 金鑰上執行describe-key
和list-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-id1234abcd-12ab-34cd-56ef-1234567890ab
\ --policyfile://key_policy.json
此命令不會產生任何輸出。若要驗證命令是否有效,請使用
get-key-policy
命令。下列範例命令會取得相同 KMS 金鑰的金鑰政策。值為 的output
參數會text
傳回易於讀取的文字格式。aws kms get-key-policy \ --policy-name
default
\ --key-id1234abcd-12ab-34cd-56ef-1234567890ab
\ --outputtext
輸出:
{ "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
-
- 適用於 PHP 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 PutKeyPolicy AWS SDK for PHP 參考中的 API。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 on 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 詳細資訊,請參閱 PutKeyPolicy AWS SDK for Python (Boto3) Word 參考中的 API。
-