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

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

RevokeGrant搭配使用 Amazon SDK或 CLI

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

CLI
Amazon CLI

撤消对客户主密钥的授权

以下revoke-grant示例从KMS密钥中删除授权。以下示例命令指定 grant-idkey-id 参数。key-id参数的值可以是密钥 ID 或密钥ARN的KMS密钥。

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

此命令不生成任何输出。要确认授权是否已撤消,请使用 list-grants 命令。

有关更多信息,请参阅《中止和撤销授权》Amazon 密钥管理服务开发人员指南

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

Java
SDK适用于 Java 2.x
注意

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

/** * 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中的 Amazon SDK for Java 2.x API参考

Python
SDK适用于 Python (Boto3)
注意

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

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中的 Amazon SDK供参考 Python (Boto3) API。

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