RevokeGrant 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

RevokeGrant 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 RevokeGrant

CLI
AWS CLI

撤銷客戶主金鑰的授予

下列revoke-grant範例會從KMS金鑰刪除授予。下列範例命令會指定 grant-idkey-id 參數。key-id 參數的值可以是金鑰 ID 或ARNKMS金鑰。

aws kms revoke-grant \ --grant-id 1234a2345b8a4e350500d432bccf8ecd6506710e1391880c4f7f7140160c9af3 \ --key-id 1234abcd-12ab-34cd-56ef-1234567890ab

此命令不會產生輸出。若要確認授予已撤銷,請使用 list-grants命令。

如需詳細資訊,請參閱 AWS Key Management Service 開發人員指南中的淘汰和撤銷授予

  • 如需API詳細資訊,請參閱 命令參考 RevokeGrant中的 。 AWS CLI

Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 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; }
  • 如需API詳細資訊,請參閱 參考 RevokeGrant中的 。 AWS SDK for Java 2.x API

Python
SDK for Python (Boto3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class GrantManager: def __init__(self, kms_client): self.kms_client = kms_client def revoke_grant(self, key_id, grant): """ 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: The grant to revoke. """ try: self.kms_client.revoke_grant(KeyId=key_id, GrantId=grant["GrantId"]) except ClientError as err: logger.error( "Couldn't revoke grant %s. Here's why: %s", grant["GrantId"], err.response["Error"]["Message"], ) else: print(f"Grant {grant['GrantId']} revoked.")
  • 如需API詳細資訊,請參閱 RevokeGrant 中的 AWS SDK for Python (Boto3) API參考