与 AWS SDK或RevokeGrant一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或RevokeGrant一起使用 CLI

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

CLI
AWS 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 命令。

有关更多信息,请参阅《AWS Key Management Service 开发人员指南》中的停用和撤消授权

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详细信息,请参阅 “AWS SDK for Java 2.x API参考 RevokeGrant” 中的。

Python
SDK适用于 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 SDKPython (Boto3) API 参考。