View a markdown version of this page

Utiliser RevokeGrant avec un AWS SDK ou CLI - AWS Key Management Service

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utiliser RevokeGrant avec un AWS SDK ou CLI

Les exemples de code suivants illustrent comment utiliser RevokeGrant.

Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :

CLI
AWS CLI

Pour révoquer un octroi sur la clé principale d’un client

L’exemple revoke-grant suivant supprime un octroi d’une clé KMS. L’exemple de commande suivant spécifie les paramètres grant-id et key-id. La valeur du paramètre key-id peut être l’ID de clé ou l’ARN de clé de la clé KMS.

aws kms revoke-grant \ --grant-id 1234a2345b8a4e350500d432bccf8ecd6506710e1391880c4f7f7140160c9af3 \ --key-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111

Cette commande ne produit aucune sortie. Pour confirmer que l’octroi a été révoqué, utilisez la commande list-grants.

Pour plus d’informations, consultez Retrait et révocation d’octrois dans le Guide du développeur AWS  Key Management Service.

  • Pour plus de détails sur l'API, voir RevokeGrant dans AWS CLI Référence de commande.

Java
SDK pour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/** * Revokes a grant for the specified AWS KMS key asynchronously. * * @param keyId The ID or key ARN of the AWS KMS key. * @param grantId The identifier of the grant to be revoked. * @return A {@link CompletableFuture} representing the asynchronous operation of revoking the grant. * The {@link CompletableFuture} will complete with a {@link RevokeGrantResponse} object * if the operation is successful, or with a {@code null} value if an error occurs. */ public CompletableFuture<RevokeGrantResponse> revokeKeyGrantAsync(String keyId, String grantId) { RevokeGrantRequest grantRequest = RevokeGrantRequest.builder() .keyId(keyId) .grantId(grantId) .build(); CompletableFuture<RevokeGrantResponse> responseFuture = getAsyncClient().revokeGrant(grantRequest); responseFuture.whenComplete((response, exception) -> { if (exception == null) { logger.info("Grant ID: [" + grantId + "] was successfully revoked!"); } else { if (exception instanceof KmsException kmsEx) { if (kmsEx.getMessage().contains("Grant does not exist")) { logger.info("The grant ID '" + grantId + "' does not exist. Moving on..."); } else { throw new RuntimeException("KMS error occurred: " + kmsEx.getMessage(), kmsEx); } } else { throw new RuntimeException("An unexpected error occurred: " + exception.getMessage(), exception); } } }); return responseFuture; }
  • Pour plus de détails sur l'API, voir RevokeGrant dans AWS SDK for Java 2.x Référence de l'API.

PHP
Kit SDK pour PHP
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

/*** * @param string $grantId * @param string $keyId * @return void */ public function revokeGrant(string $grantId, string $keyId) { try{ $this->client->revokeGrant([ 'GrantId' => $grantId, 'KeyId' => $keyId, ]); }catch(KmsException $caught){ echo "There was a problem with revoking the grant: {$caught->getAwsErrorMessage()}.\n"; throw $caught; } }
  • Pour plus de détails sur l'API, voir RevokeGrant dans AWS SDK pour PHP Référence de l'API.

Python
Kit SDK for Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

class GrantManager: def __init__(self, kms_client): self.kms_client = kms_client @classmethod def from_client(cls) -> "GrantManager": """ Creates a GrantManager instance with a default KMS client. :return: An instance of GrantManager initialized with the default KMS client. """ kms_client = boto3.client("kms") return cls(kms_client) def revoke_grant(self, key_id: str, grant_id: str) -> None: """ Revokes a grant so that it can no longer be used. :param key_id: The ARN or ID of the key associated with the grant. :param grant_id: The ID of the grant to revoke. """ try: self.kms_client.revoke_grant(KeyId=key_id, GrantId=grant_id) except ClientError as err: logger.error( "Couldn't revoke grant %s. Here's why: %s", grant_id, err.response["Error"]["Message"], ) raise
  • Pour plus de détails sur l'API, voir RevokeGrant dans AWS Référence de l'API du SDK pour Python (Boto3).

SAP ABAP
Kit SDK pour SAP ABAP
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

TRY. " iv_key_id = 'arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab' " iv_grant_id = '1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p' lo_kms->revokegrant( iv_keyid = iv_key_id iv_grantid = iv_grant_id ). MESSAGE 'Grant revoked successfully.' TYPE 'I'. CATCH /aws1/cx_kmsnotfoundexception. MESSAGE 'Grant or key not found.' TYPE 'E'. CATCH /aws1/cx_kmsinvalidgrantidex. MESSAGE 'Invalid grant ID.' TYPE 'E'. CATCH /aws1/cx_kmskmsinternalex. MESSAGE 'An internal error occurred.' TYPE 'E'. ENDTRY.
  • Pour plus de détails sur l'API, voir RevokeGrant dans AWS Référence du SDK pour l'API SAP ABAP.

Pour une liste complète des AWS Guides du développeur du SDK et exemples de code, voir L'utilisation de ce service avec un AWS Kit SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes du kit SDK.