AWS SDK または CLI ListKeyPoliciesで を使用する - AWS SDK コード例

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK または CLI ListKeyPoliciesで を使用する

以下のコード例は、ListKeyPolicies の使用方法を示しています。

CLI
AWS CLI

KMS キーのキーポリシーの名前を取得するには

次の list-key-policies の例は、サンプルアカウントとリージョンのカスタマーマネージドキーのキーポリシーの名前を取得します。このコマンドを使用して、 AWS マネージドキーとカスタマーマネージドキーのキーポリシーの名前を検索できます。

有効なキーポリシー名は default のみであるため、このコマンドは有用ではありません。

KMS キーを指定するには、key-id パラメータを使用します。この例では、キー ID 値を使用していますが、このコマンドでは、キー ID またはキー ARN を使用できます。

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

出力:

{ "PolicyNames": [ "default" ] }

KMS キーポリシーの詳細については、 AWS 「 Key Management Service デベロッパーガイド」の「KMS AWS でのキーポリシーの使用」を参照してください。 AWS

  • API の詳細については、AWS CLI コマンドリファレンスの「ListKeyPolicies」を参照してください。

Java
SDK for Java 2.x
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

/** * 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); }); }
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 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"], ) raise else: print(f"The policies for key {key_id} are:") pprint(policy_names)
  • API の詳細については、「AWS SDK for Python (Boto3) APIリファレンス」の「ListKeyPolicies」を参照してください。