Skip to content

Commit

Permalink
feat: add purge to delete key from hidden ape folder
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed Jun 27, 2024
1 parent 0cc8bb9 commit a4a83cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions ape_aws/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ape_aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 13 additions & 3 deletions ape_aws/kms/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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")

0 comments on commit a4a83cd

Please sign in to comment.