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

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

ListKeyPolicies搭配使用 Amazon SDK或 CLI

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

CLI
Amazon CLI

获取密钥的密钥策略名称 KMS

以下 list-key-policies 示例获取示例账户和区域中客户托管密钥的密钥策略名称。您可以使用此命令查找的密钥策略的名称 Amazon 托管密钥和客户管理的密钥。

由于唯一有效的密钥策略名称是 default,因此,此命令没有用。

要指定KMS密钥,请使用key-id参数。此示例使用密钥 ID 值,但您可以在此命令ARN中使用密钥 ID 或密钥。

aws kms list-key-policies \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab

输出:

{ "PolicyNames": [ "default" ] }

有关 Amazon KMS密钥策略,请参阅中的使用密钥策略 Amazon KMSAmazon 密钥管理服务开发人员指南

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

Java
SDK适用于 Java 2.x
注意

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

/** * Asynchronously retrieves the key policy for the specified key ID and policy name. * * @param keyId the ID of the AWS KMS key for which to retrieve the policy * @param policyName the name of the key policy to retrieve * @return a {@link CompletableFuture} that, when completed, contains the key policy as a {@link String} */ public CompletableFuture<String> getKeyPolicyAsync(String keyId, String policyName) { GetKeyPolicyRequest policyRequest = GetKeyPolicyRequest.builder() .keyId(keyId) .policyName(policyName) .build(); return getAsyncClient().getKeyPolicy(policyRequest) .thenApply(response -> { String policy = response.policy(); logger.info("The response is: " + policy); return policy; }) .exceptionally(ex -> { throw new RuntimeException("Failed to get key policy", ex); }); }
  • 有关API详细信息,请参阅ListKeyPolicies中的 Amazon SDK for Java 2.x API参考

Python
SDK适用于 Python (Boto3)
注意

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

class KeyPolicy: def __init__(self, kms_client): self.kms_client = kms_client def list_policies(self, key_id): """ Lists the names of the policies for a key. :param key_id: The ARN or ID of the key to query. """ try: policy_names = self.kms_client.list_key_policies(KeyId=key_id)[ "PolicyNames" ] except ClientError as err: logging.error( "Couldn't list your policies. Here's why: %s", err.response["Error"]["Message"], ) else: print(f"The policies for key {key_id} are:") pprint(policy_names)
  • 有关API详细信息,请参阅ListKeyPolicies中的 Amazon SDK供参考 Python (Boto3) API。

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