Skip to content

fix: modify session and kms_utils to check for S3 bucket before creation #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,34 +343,35 @@ def default_bucket(self):
default_bucket = "sagemaker-{}-{}".format(region, account)

s3 = self.boto_session.resource("s3")
try:
# 'us-east-1' cannot be specified because it is the default region:
# https://github.com/boto/boto3/issues/125
if region == "us-east-1":
s3.create_bucket(Bucket=default_bucket)
else:
s3.create_bucket(
Bucket=default_bucket, CreateBucketConfiguration={"LocationConstraint": region}
)
bucket = self.boto_session.resource("s3").Bucket(name=default_bucket)
if bucket.creation_date is None:
try:
if region == "us-east-1":
# 'us-east-1' cannot be specified because it is the default region:
# https://github.com/boto/boto3/issues/125
s3.create_bucket(Bucket=default_bucket)
else:
s3.create_bucket(
Bucket=default_bucket,
CreateBucketConfiguration={"LocationConstraint": region},
)

LOGGER.info("Created S3 bucket: %s", default_bucket)
except ClientError as e:
error_code = e.response["Error"]["Code"]
message = e.response["Error"]["Message"]
LOGGER.info("Created S3 bucket: %s", default_bucket)
except ClientError as e:
error_code = e.response["Error"]["Code"]
message = e.response["Error"]["Message"]

if error_code == "BucketAlreadyOwnedByYou":
pass
elif (
error_code == "OperationAborted" and "conflicting conditional operation" in message
):
# If this bucket is already being concurrently created, we don't need to create it
# again.
pass
elif error_code == "TooManyBuckets":
# Succeed if the default bucket exists
s3.meta.client.head_bucket(Bucket=default_bucket)
else:
raise
if error_code == "BucketAlreadyOwnedByYou":
pass
elif (
error_code == "OperationAborted"
and "conflicting conditional operation" in message
):
# If this bucket is already being concurrently created, we don't need to create
# it again.
pass
else:
raise

self._default_bucket = default_bucket

Expand Down
38 changes: 25 additions & 13 deletions tests/integ/kms_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,31 @@ def bucket_with_encryption(boto_session, sagemaker_role):
bucket_name = "sagemaker-{}-{}-with-kms".format(region, account)

s3 = boto_session.client("s3")
try:
# 'us-east-1' cannot be specified because it is the default region:
# https://github.com/boto/boto3/issues/125
if region == "us-east-1":
s3.create_bucket(Bucket=bucket_name)
else:
s3.create_bucket(
Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": region}
)

except exceptions.ClientError as e:
if e.response["Error"]["Code"] != "BucketAlreadyOwnedByYou":
raise
bucket = boto_session.resource("s3").Bucket(name=bucket_name)
if bucket.creation_date is None:
try:
if region == "us-east-1":
# 'us-east-1' cannot be specified because it is the default region:
# https://github.com/boto/boto3/issues/125
s3.create_bucket(Bucket=bucket_name)
else:
s3.create_bucket(
Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": region}
)
except exceptions.ClientError as e:
error_code = e.response["Error"]["Code"]
message = e.response["Error"]["Message"]

if error_code == "BucketAlreadyOwnedByYou":
pass
elif (
error_code == "OperationAborted" and "conflicting conditional operation" in message
):
# If this bucket is already being concurrently created, we don't need to create it
# again.
pass
else:
raise

s3.put_bucket_encryption(
Bucket=bucket_name,
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_default_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
def sagemaker_session():
boto_mock = Mock(name="boto_session", region_name=REGION)
boto_mock.client("sts").get_caller_identity.return_value = {"Account": ACCOUNT_ID}
ims = sagemaker.Session(boto_session=boto_mock)
return ims
sagemaker_session = sagemaker.Session(boto_session=boto_mock)
sagemaker_session.boto_session.resource("s3").Bucket().creation_date = None
return sagemaker_session


def test_default_bucket_s3_create_call(sagemaker_session):
Expand Down