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

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

ScheduleKeyDeletion配使用 AWS SDK或 CLI

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

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

CLI
AWS CLI

排程刪除客戶管理的KMS金鑰。

下列schedule-key-deletion範例會排程在 15 天內刪除指定的客戶管理KMS金鑰。

--key-id參數可識別KMS金鑰。此範例使用索引鍵ARN值,但您可以使用金鑰 ID 或金KMS鑰的ARN。--pending-window-in-days參數指定 7-30 天等待期間的長度。根據預設,等待期為 30 天。這個例子指定一個值 15,它告訴 AWS 在命令完成後 15 天永久刪除KMS密鑰。

aws kms schedule-key-deletion \ --key-id arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab \ --pending-window-in-days 15

響應包括密鑰ARN,密鑰狀態,等待期(PendingWindowInDays)和 Unix 時間中的刪除日期。若要以當地時間檢視刪除日期,請使用 AWS KMS控制台。KMS處於金鑰狀態的金PendingDeletion鑰無法用於密碼編譯作業。

{ "KeyId": "arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab", "DeletionDate": "2022-06-18T23:43:51.272000+00:00", "KeyState": "PendingDeletion", "PendingWindowInDays": 15 }

如需詳細資訊,請參閱刪除 AWS 金鑰管理服務開發人員指南

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

/** * Deletes a KMS key asynchronously. * * <p><strong>Warning:</strong> Deleting a KMS key is a destructive and potentially dangerous operation. * When a KMS key is deleted, all data that was encrypted under the KMS key becomes unrecoverable. * This means that any files, databases, or other data that were encrypted using the deleted KMS key * will become permanently inaccessible. Exercise extreme caution when deleting KMS keys.</p> * * @param keyId the ID of the KMS key to delete * @return a {@link CompletableFuture} that completes when the key deletion is scheduled */ public CompletableFuture<Void> deleteKeyAsync(String keyId) { ScheduleKeyDeletionRequest deletionRequest = ScheduleKeyDeletionRequest.builder() .keyId(keyId) .pendingWindowInDays(7) .build(); return getAsyncClient().scheduleKeyDeletion(deletionRequest) .thenRun(() -> { logger.info("Key {} will be deleted in 7 days", keyId); }) .exceptionally(throwable -> { throw new RuntimeException("Failed to schedule key deletion for key ID: " + keyId, throwable); }); }
Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

class KeyManager: def __init__(self, kms_client): self.kms_client = kms_client self.created_keys = [] def delete_keys(self, keys): """ Deletes a list of keys. Warning: Deleting a KMS key is a destructive and potentially dangerous operation. When a KMS key is deleted, all data that was encrypted under the KMS key is unrecoverable. :param keys: The list of keys to delete. """ print(""" Warning: Deleting a KMS key is a destructive and potentially dangerous operation. When a KMS key is deleted, all data that was encrypted under the KMS key is unrecoverable. """) answer = input("Do you want to delete these keys (y/n)? ") if answer.lower() == "y": window = 7 for key in keys: try: self.kms_client.schedule_key_deletion( KeyId=key["KeyId"], PendingWindowInDays=window ) except ClientError as err: logging.error( "Couldn't delete key %s. Here's why: %s", key["KeyId"], err.response["Error"]["Message"], ) else: print( f"Key {key['KeyId']} scheduled for deletion in {window} days." )
  • 有API關詳細資訊,請參閱 ScheduleKeyDeletionAWS SDK對於 Python(肉毒桿 3)API參考。

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