PutKeyPolicy搭配使用 Amazon SDK或 CLI - Amazon Key Management Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

PutKeyPolicy搭配使用 Amazon SDK或 CLI

以下代码示例演示如何使用 PutKeyPolicy

CLI
Amazon CLI

更改密钥的密KMS钥策略

以下 put-key-policy 示例更改客户托管密钥的密钥策略。

首先,创建密钥策略并将其保存在本地JSON文件中。在本示例中,该文件为 key_policy.json。您也可以将密钥策略指定为 policy 参数的字符串值。

此关键政策的第一条声明给出了 Amazon 使用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:// 前缀。需要使用此前缀来识别所有受支持操作系统上的文件。最后,该命令使用值为 defaultpolicy-name 参数。如果未指定策略名称,则默认值为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密钥策略。值为 textoutput 参数返回一种易于读取的文本格式。

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" : "*" } ] }

有关更多信息,请参阅中的更改密钥策略 Amazon 密钥管理服务开发人员指南

  • 有关API详细信息,请参阅PutKeyPolicy中的 Amazon CLI 命令参考

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。在中查找完整的示例,学习如何设置和运行 Amazon 代码示例存储库

class KeyPolicy: def __init__(self, kms_client): self.kms_client = kms_client def set_policy(self, key_id, policy): """ 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. """ 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, PolicyName="default", 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"], ) else: print(f"Set policy for key {key_id}.") else: print("Skipping set policy demo.")
  • 有关API详细信息,请参阅PutKeyPolicy中的 Amazon SDK供参考 Python (Boto3) API。

有关完整列表 Amazon SDK开发者指南和代码示例,请参阅使用 Amazon KMS 用一个 Amazon SDK。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。