-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
4,646 additions
and
11,470 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
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,36 @@ | ||
import logging | ||
from botocore.exceptions import ClientError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class S3Client: | ||
def __init__(self, session, account, region): | ||
self._client = session.client('s3', region_name=region) | ||
self._control_client = session.client('s3control', region_name=region) | ||
self._resource = session.resource('s3', region_name=region) | ||
self._account = account | ||
self._region = region | ||
|
||
def delete_bucket(self, bucket_name): | ||
""" | ||
Delete an S3 bucket. | ||
:param bucket_name: Name of the S3 bucket to be deleted | ||
:return: None | ||
""" | ||
try: | ||
# Delete all objects in the bucket before deleting the bucket | ||
bucket = self._resource.Bucket(bucket_name) | ||
# Delete all object versions | ||
bucket.object_versions.all().delete() | ||
# Delete any remaining objects (if versioning was not enabled) | ||
bucket.objects.all().delete() | ||
# Delete any remaining access point | ||
access_points = self._control_client.list_access_points(AccountId=self._account, Bucket=bucket_name)[ | ||
'AccessPointList' | ||
] | ||
for access_point in access_points: | ||
self._control_client.delete_access_point(AccountId=self._account, Name=access_point['Name']) | ||
bucket.delete() | ||
except ClientError as e: | ||
log.exception(f'Error deleting S3 bucket: {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,57 @@ | ||
from uuid import uuid4 | ||
import boto3 | ||
from boto3 import Session | ||
from botocore.credentials import RefreshableCredentials | ||
from botocore.session import get_session | ||
|
||
SESSION_EXPIRATION_TIME_IN_SECONDS = 3600 | ||
|
||
|
||
class STSClient: | ||
def __init__(self, role_arn, region, session_name=None): | ||
self.role_arn = role_arn | ||
self.region = region | ||
self.session_name = session_name or uuid4().hex | ||
|
||
def _refresh_credentials(self): | ||
params = { | ||
'RoleArn': self.role_arn, | ||
'RoleSessionName': self.session_name, | ||
'DurationSeconds': SESSION_EXPIRATION_TIME_IN_SECONDS, | ||
} | ||
sts_client = boto3.client('sts', region_name=self.region) | ||
|
||
response = sts_client.assume_role(**params).get('Credentials') | ||
credentials = { | ||
'access_key': response.get('AccessKeyId'), | ||
'secret_key': response.get('SecretAccessKey'), | ||
'token': response.get('SessionToken'), | ||
'expiry_time': response.get('Expiration').isoformat(), | ||
} | ||
return credentials | ||
|
||
def get_refreshable_session(self) -> Session: | ||
""" | ||
Get refreshable boto3 session. | ||
""" | ||
refreshable_credentials = RefreshableCredentials.create_from_metadata( | ||
metadata=self._refresh_credentials(), | ||
refresh_using=self._refresh_credentials, | ||
method='sts-assume-role', | ||
) | ||
|
||
session = get_session() | ||
session._credentials = refreshable_credentials | ||
session.set_config_variable('region', self.region) | ||
return Session(botocore_session=session) | ||
|
||
def get_role_session(self, session) -> Session: | ||
sts_client = session.client('sts', region_name=self.region) | ||
assumed_role_object = sts_client.assume_role(RoleArn=self.role_arn, RoleSessionName=self.session_name) | ||
credentials = assumed_role_object['Credentials'] | ||
|
||
return boto3.Session( | ||
aws_access_key_id=credentials['AccessKeyId'], | ||
aws_secret_access_key=credentials['SecretAccessKey'], | ||
aws_session_token=credentials['SessionToken'], | ||
) |
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.