Skip to content

Commit

Permalink
feat: make some interface changes to client class
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed May 1, 2024
1 parent d17f4a3 commit 9ff7a91
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
15 changes: 4 additions & 11 deletions ape_aws/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
from ape.utils import cached_property

from .utils import AliasResponse, _convert_der_to_rsv
from .client import client
from .client import kms_client


class AwsAccountContainer(AccountContainerAPI):
@cached_property
def kms_client(self):
return client.kms_client

@cached_property
def raw_aliases(self) -> List[AliasResponse]:
paginator = self.kms_client.get_paginator('list_aliases')
paginator = kms_client.client.get_paginator('list_aliases')
pages = paginator.paginate()
return [
AliasResponse(**page)
Expand Down Expand Up @@ -55,13 +52,9 @@ class KmsAccount(AccountAPI):
key_id: str
key_arn: str

@cached_property
def kms_client(self):
return client.kms_client

@cached_property
def public_key(self):
return self.kms_client.get_public_key(KeyId=self.key_id)["PublicKey"]
return kms_client.client.get_public_key(KeyId=self.key_id)["PublicKey"]

@cached_property
def address(self) -> AddressType:
Expand All @@ -70,7 +63,7 @@ def address(self) -> AddressType:
)

def _sign_raw_hash(self, msghash: HexBytes) -> Optional[bytes]:
response = self.kms_client.sign(
response = kms_client.client.sign(
KeyId=self.key_id,
Message=msghash,
MessageType='DIGEST',
Expand Down
18 changes: 9 additions & 9 deletions ape_aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

from pydantic import BaseModel

from ape.utils import cached_property


class Client(BaseModel):
@cached_property
def kms_client(self):
return boto3.client('kms')
client_name: str
_client: boto3.client = None

@cached_property
def iam_client(self):
return boto3.client('iam')
@property
def client(self):
if not self._client:
self._client = boto3.client(self.client_name)
return self._client


client = Client()
kms_client = Client(client_name='kms')
iam_client = Client(client_name='iam')
11 changes: 5 additions & 6 deletions ape_aws/iam/_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boto3
import click

from ape_aws.client import iam_client


@click.group("iam")
def iam():
Expand All @@ -9,12 +10,11 @@ def iam():

@iam.command()
def list_admins():
iam_client = boto3.client('iam')
response = iam_client.list_users()
response = iam_client.client.list_users()
admins = []
for user in response['Users']:
user_name = user['UserName']
user_policies = iam_client.list_attached_user_policies(UserName=user_name)
user_policies = iam_client.client.list_attached_user_policies(UserName=user_name)
for policy in user_policies['AttachedPolicies']:
if policy['PolicyName'] == 'AdministratorAccess':
admins.append(user_name)
Expand All @@ -24,6 +24,5 @@ def list_admins():

@iam.command()
def list_users():
iam_client = boto3.client('iam')
response = iam_client.list_users()
response = iam_client.client.list_users()
click.echo(f'Users: {response.get("Users")}')
16 changes: 8 additions & 8 deletions ape_aws/kms/_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click

from ape_aws.accounts import KmsAccount, AwsAccountContainer
from ape_aws.client import client
from ape_aws.client import kms_client
from ape.cli import ape_cli_context


Expand Down Expand Up @@ -79,15 +79,15 @@ def create_key(
alias_name str: The alias of the key you intend to create
description str: The description of the key you intend to create.
"""
response = client.kms_client.create_key(
response = kms_client.client.create_key(
Description=description,
KeyUsage='SIGN_VERIFY',
KeySpec='ECC_SECG_P256K1',
Origin='AWS_KMS',
MultiRegion=False,
)
key_id = response['KeyMetadata']['KeyId']
client.kms_client.create_alias(
kms_client.client.create_alias(
AliasName=f'alias/{alias_name}',
TargetKeyId=key_id,
)
Expand All @@ -96,18 +96,18 @@ def create_key(
for k_v in tags:
k, v = k_v.split('=')
tags_list.append(dict(k=v))
client.kms_client.tag_resource(
kms_client.client.tag_resource(
KeyId=key_id,
Tags=tags_list,
)
for arn in administrators:
client.kms_client.put_key_policy(
kms_client.client.put_key_policy(
KeyId=key_id,
PolicyName='default',
Policy=ADMIN_KEY_POLICY.format(arn=arn)
)
for arn in users:
client.kms_client.put_key_policy(
kms_client.client.put_key_policy(
KeyId=key_id,
PolicyName='default',
Policy=USER_KEY_POLICY.format(arn=arn)
Expand All @@ -132,8 +132,8 @@ def schedule_delete_key(cli_ctx, alias_name, days):
if not kms_account:
cli_ctx.abort(f"No KMS Key with alias name: {alias_name}")

kms_account.kms_client.delete_alias(AliasName=alias_name)
kms_account.kms_client.schedule_key_deletion(
kms_client.client.delete_alias(AliasName=alias_name)
kms_client.client.schedule_key_deletion(
KeyId=kms_account.key_id, PendingWindowInDays=days
)
cli_ctx.logger.success(f"Key {kms_account.key_alias} scheduled for deletion")

0 comments on commit 9ff7a91

Please sign in to comment.