搭RevokeGrant配使用 AWS SDK或 CLI - AWS Key Management Service

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

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 金鑰管理服務開發人員指南

  • 有API關詳細資訊,請參閱 RevokeGrantAWS CLI 指令參考

Java
SDK對於爪哇 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關詳細資訊,請參閱 RevokeGrantAWS SDK for Java 2.x API參考

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 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關詳細資訊,請參閱 RevokeGrantAWS SDK對於 Python(肉毒桿 3)API參考。

有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱使用 AWS KMS 用一個 AWS SDK。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。