Utilizzare ListGrants con un AWS SDKo CLI - AWS Key Management Service

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzare ListGrants con un AWS SDKo CLI

I seguenti esempi di codice mostrano come utilizzareListGrants.

.NET
AWS SDK for .NET
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

using System; using System.Threading.Tasks; using Amazon.KeyManagementService; using Amazon.KeyManagementService.Model; /// <summary> /// List the AWS Key Management Service (AWS KMS) grants that are associated with /// a specific key. /// </summary> public class ListGrants { public static async Task Main() { // The identifier of the AWS KMS key to disable. You can use the // key Id or the Amazon Resource Name (ARN) of the AWS KMS key. var keyId = "1234abcd-12ab-34cd-56ef-1234567890ab"; var client = new AmazonKeyManagementServiceClient(); var request = new ListGrantsRequest { KeyId = keyId, }; var response = new ListGrantsResponse(); do { response = await client.ListGrantsAsync(request); response.Grants.ForEach(grant => { Console.WriteLine($"{grant.GrantId}"); }); request.Marker = response.NextMarker; } while (response.Truncated); } }
  • Per API i dettagli, vedere ListGrantsin AWS SDK for .NET APIRiferimento.

CLI
AWS CLI

Per visualizzare le sovvenzioni su un AWS KMSchiave

L'list-grantsesempio seguente visualizza tutte le sovvenzioni sul valore specificato AWS KMSchiave gestita per Amazon DynamoDB nel tuo account. Questa concessione consente a DynamoDB di utilizzare KMS la chiave per conto dell'utente per crittografare una tabella DynamoDB prima di scriverla su disco. È possibile utilizzare un comando come questo per visualizzare le sovvenzioni relative a AWS KMSchiavi gestite e KMS chiavi gestite dal cliente in AWS account e regione.

Questo comando utilizza il key-id parametro con un ID chiave per identificare la KMS chiave. È possibile utilizzare un ID o una chiave ARN per identificare la KMS chiave. Per ottenere l'ID della chiave o la chiave ARN di un AWS KMSchiave gestita, usa il list-aliases comando list-keys or.

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

L'output mostra che la concessione autorizza Amazon DynamoDB a utilizzare KMS la chiave per operazioni crittografiche e consente di visualizzare i dettagli sulla chiave DescribeKey () e di ritirare KMS le sovvenzioni (). RetireGrant Il EncryptionContextSubset vincolo limita queste autorizzazioni alle richieste che includono le coppie di contesto di crittografia specificate. Di conseguenza, le autorizzazioni incluse nella concessione sono valide solo per l'account e la tabella DynamoDB specificati.

{ "Grants": [ { "Constraints": { "EncryptionContextSubset": { "aws:dynamodb:subscriberId": "123456789012", "aws:dynamodb:tableName": "Services" } }, "IssuingAccount": "arn:aws:iam::123456789012:root", "Name": "8276b9a6-6cf0-46f1-b2f0-7993a7f8c89a", "Operations": [ "Decrypt", "Encrypt", "GenerateDataKey", "ReEncryptFrom", "ReEncryptTo", "RetireGrant", "DescribeKey" ], "GrantId": "1667b97d27cf748cf05b487217dd4179526c949d14fb3903858e25193253fe59", "KeyId": "arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab", "RetiringPrincipal": "dynamodb.us-west-2.amazonaws.com", "GranteePrincipal": "dynamodb.us-west-2.amazonaws.com", "CreationDate": "2021-05-13T18:32:45.144000+00:00" } ] }

Per ulteriori informazioni, consulta Sovvenzioni in AWS KMSnel AWS Guida per gli sviluppatori del servizio di gestione delle chiavi.

  • Per API i dettagli, vedere ListGrantsin AWS CLI Riferimento ai comandi.

Java
SDKper Java 2.x
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

/** * Asynchronously displays the grant IDs for the specified key ID. * * @param keyId the ID of the AWS KMS key for which to list the grants * @return a {@link CompletableFuture} that, when completed, will be null if the operation succeeded, or will throw a {@link RuntimeException} if the operation failed * @throws RuntimeException if there was an error listing the grants, either due to an {@link KmsException} or an unexpected error */ public CompletableFuture<Object> displayGrantIdsAsync(String keyId) { ListGrantsRequest grantsRequest = ListGrantsRequest.builder() .keyId(keyId) .limit(15) .build(); ListGrantsPublisher paginator = getAsyncClient().listGrantsPaginator(grantsRequest); return paginator.subscribe(response -> { response.grants().forEach(grant -> { logger.info("The grant Id is: " + grant.grantId()); }); }) .thenApply(v -> null) .exceptionally(ex -> { Throwable cause = ex.getCause(); if (cause instanceof KmsException) { throw new RuntimeException("Failed to list grants: " + cause.getMessage(), cause); } else { throw new RuntimeException("An unexpected error occurred: " + cause.getMessage(), cause); } }); }
  • Per API i dettagli, vedere ListGrantsin AWS SDK for Java 2.x APIRiferimento.

Kotlin
SDKper Kotlin
Nota

c'è altro da fare. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

suspend fun displayGrantIds(keyIdVal: String?) { val request = ListGrantsRequest { keyId = keyIdVal limit = 15 } KmsClient { region = "us-west-2" }.use { kmsClient -> val response = kmsClient.listGrants(request) response.grants?.forEach { grant -> println("The grant Id is ${grant.grantId}") } } }
  • Per API i dettagli, vedere ListGrantsin AWS SDKper riferimento a KotlinAPI.

Python
SDKper Python (Boto3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurare ed eseguire in AWS Repository di esempi di codice.

class GrantManager: def __init__(self, kms_client): self.kms_client = kms_client def list_grants(self, key_id): """ Lists grants for a key. :param key_id: The ARN or ID of the key to query. :return: The grants for the key. """ answer = input(f"Ready to list grants on key {key_id} (y/n)? ") if answer.lower() == "y": try: grants = self.kms_client.list_grants(KeyId=key_id)["Grants"] except ClientError as err: logger.error( "Couldn't list grants for key %s. Here's why: %s", key_id, err.response["Error"]["Message"], ) else: print(f"Grants for key {key_id}:") pprint(grants) return grants
  • Per API i dettagli, vedere ListGrantsin AWS SDKper Python (Boto3) Reference. API

Per un elenco completo di AWS SDKguide per sviluppatori ed esempi di codice, vediUtilizzo AWS KMS con un AWS SDK. Questo argomento include anche informazioni su come iniziare e dettagli sulle SDK versioni precedenti.