From a4a83cd18e6489951093349e528dc4c8e824bcdd Mon Sep 17 00:00:00 2001 From: johnson2427 Date: Thu, 27 Jun 2024 09:37:03 -0500 Subject: [PATCH] feat: add purge to delete key from hidden ape folder --- ape_aws/accounts.py | 10 ++++++++++ ape_aws/client.py | 2 +- ape_aws/kms/_cli.py | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ape_aws/accounts.py b/ape_aws/accounts.py index 33dd256..6e842b0 100644 --- a/ape_aws/accounts.py +++ b/ape_aws/accounts.py @@ -74,6 +74,16 @@ def add_private_key(self, alias, passphrase, private_key): print("Key cached successfully") return + def delete_account(self, alias): + alias = alias.replace("alias/", "") + keyfile = self.data_folder.joinpath(f"{alias}.json") + if keyfile.exists(): + keyfile.unlink() + print(f"Key {alias} deleted successfully") + else: + print(f"Key {alias} not found") + + class KmsAccount(AccountAPI): key_alias: str key_id: str diff --git a/ape_aws/client.py b/ape_aws/client.py index fccdb12..36c0f6b 100644 --- a/ape_aws/client.py +++ b/ape_aws/client.py @@ -28,7 +28,7 @@ class KeyBaseModel(BaseModel): class CreateKeyModel(KeyBaseModel): - description: str = Field(alias="Description") + description: str | None = Field(default=None, alias="Description") policy: str | None = Field(default=None, alias="Policy") key_usage: str = Field(default="SIGN_VERIFY", alias="KeyUsage") key_spec: str = Field(default="ECC_SECG_P256K1", alias="KeySpec") diff --git a/ape_aws/kms/_cli.py b/ape_aws/kms/_cli.py index 33a9f61..2877385 100644 --- a/ape_aws/kms/_cli.py +++ b/ape_aws/kms/_cli.py @@ -86,15 +86,21 @@ def create_key( help="Apply key policy to a list of users if applicable, ex. -u ARN1, -u ARN2", metavar="list[ARN]", ) +@click.option( + "-d", + "--description", + "description", + help="The description of the key you intend to create.", + metavar="str", +) @click.argument("alias_name") -@click.argument("description") def import_key( cli_ctx, alias_name: str, - description: str, private_key: bytes, administrators: list[str], users: list[str], + description: str, ): def ask_for_passphrase(): return click.prompt( @@ -136,8 +142,9 @@ def ask_for_passphrase(): @kms.command(name="delete") @ape_cli_context() @click.argument("alias_name") +@click.option("-p", "--purge", is_flag=True, help="Purge the key from the system") @click.option("-d", "--days", default=30, help="Number of days until key is deactivated") -def schedule_delete_key(cli_ctx, alias_name, days): +def schedule_delete_key(cli_ctx, alias_name, purge, days): if "alias" not in alias_name: alias_name = f"alias/{alias_name}" kms_account = None @@ -150,4 +157,7 @@ def schedule_delete_key(cli_ctx, alias_name, days): delete_key_spec = DeleteKey(alias=alias_name, key_id=kms_account.key_id, days=days) key_alias = kms_client.delete_key(delete_key_spec) + if purge: + aws_account_container = AwsAccountContainer(name="aws", account_type=KmsAccount) + aws_account_container.delete_account(key_alias) cli_ctx.logger.success(f"Key {key_alias} scheduled for deletion in {days} days")