-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from LEUNGUU/feat/replace-s3-bucketpolicy-wit…
…h-accesspoints Feature: Change to use s3 access point while sharing folders
- Loading branch information
Showing
11 changed files
with
1,037 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
|
||
from .sts import SessionHelper | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class IAM: | ||
@staticmethod | ||
def client(account_id: str): | ||
session = SessionHelper.remote_session(account_id) | ||
return session.client('iam') | ||
|
||
@staticmethod | ||
def update_role_policy( | ||
account_id: str, | ||
role_name: str, | ||
policy_name: str, | ||
policy: str, | ||
): | ||
try: | ||
iamcli = IAM.client(account_id) | ||
iamcli.put_role_policy( | ||
RoleName=role_name, | ||
PolicyName=policy_name, | ||
PolicyDocument=policy, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to add S3 bucket access to target role {account_id}/{role_name} : {e}' | ||
) | ||
raise e | ||
|
||
@staticmethod | ||
def get_role_policy( | ||
account_id: str, | ||
role_name: str, | ||
policy_name: str, | ||
): | ||
try: | ||
iamcli = IAM.client(account_id) | ||
response = iamcli.get_role_policy( | ||
RoleName=role_name, | ||
PolicyName=policy_name, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to get policy {policy_name} of role {role_name} : {e}' | ||
) | ||
return None | ||
else: | ||
return response["PolicyDocument"] | ||
|
||
@staticmethod | ||
def delete_role_policy( | ||
account_id: str, | ||
role_name: str, | ||
policy_name: str, | ||
): | ||
try: | ||
iamcli = IAM.client(account_id) | ||
iamcli.delete_role_policy( | ||
RoleName=role_name, | ||
PolicyName=policy_name, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to delete policy {policy_name} of role {role_name} : {e}' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
|
||
from .sts import SessionHelper | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class KMS: | ||
|
||
@staticmethod | ||
def client(account_id: str): | ||
session = SessionHelper.remote_session(accountid=account_id) | ||
return session.client('kms') | ||
|
||
@staticmethod | ||
def put_key_policy( | ||
account_id: str, | ||
key_id: str, | ||
policy_name: str, | ||
policy: str, | ||
): | ||
try: | ||
kms_client = KMS.client(account_id) | ||
kms_client.put_key_policy( | ||
KeyId=key_id, | ||
PolicyName=policy_name, | ||
Policy=policy, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to attach policy to KMS key {key_id} on {account_id} : {e} ' | ||
) | ||
raise e | ||
|
||
@staticmethod | ||
def get_key_policy( | ||
account_id: str, | ||
key_id: str, | ||
policy_name: str, | ||
): | ||
try: | ||
kms_client = KMS.client(account_id) | ||
response = kms_client.get_key_policy( | ||
KeyId=key_id, | ||
PolicyName=policy_name, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to get kms key policy of key {key_id} : {e}' | ||
) | ||
return None | ||
else: | ||
return response['Policy'] | ||
|
||
@staticmethod | ||
def get_key_id( | ||
account_id: str, | ||
key_alias: str, | ||
): | ||
try: | ||
kms_client = KMS.client(account_id) | ||
response = kms_client.describe_key( | ||
KeyId=key_alias, | ||
) | ||
except Exception as e: | ||
log.error( | ||
f'Failed to get kms key id of {key_alias} : {e}' | ||
) | ||
return None | ||
else: | ||
return response['KeyMetadata']['KeyId'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.