Skip to content

Sniping fix for S3 bucket #4952

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions src/sagemaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,13 @@ def _create_s3_bucket_if_it_does_not_exist(self, bucket_name, region):
s3 = self.s3_resource

bucket = s3.Bucket(name=bucket_name)

if bucket.creation_date is None:
self.general_bucket_check_if_user_has_permission(bucket_name, s3, bucket, region, True)

self.expected_bucket_owner_id_bucket_check(bucket_name, s3, self.account_id())
elif self._default_bucket_set_by_sdk:
self.general_bucket_check_if_user_has_permission(bucket_name, s3, bucket, region, False)

expected_bucket_owner_id = self.account_id()
self.expected_bucket_owner_id_bucket_check(bucket_name, s3, expected_bucket_owner_id)
self.expected_bucket_owner_id_bucket_check(bucket_name, s3, self.account_id())

def expected_bucket_owner_id_bucket_check(self, bucket_name, s3, expected_bucket_owner_id):
"""Checks if the bucket belongs to a particular owner and throws a Client Error if it is not
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_default_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,31 @@ def test_bucket_creation_other_error(sagemaker_session):

sagemaker_session.default_bucket()
assert sagemaker_session._default_bucket is None


def test_default_bucket_s3_create_call_creation_date(sagemaker_session):
error = ClientError(
error_response={"Error": {"Code": "403", "Message": "Forbidden"}},
operation_name="foo",
)
sagemaker_session.boto_session.resource("s3").meta.client.head_bucket.side_effect = Mock(
side_effect=error
)

with pytest.raises(ClientError):
sagemaker_session.default_bucket()


def test_default_bucket_s3_create_call_default_bucket_set_by_sdk(sagemaker_session):
sagemaker_session._default_bucket_set_by_sdk = True
sagemaker_session.boto_session.resource("s3").Bucket().creation_date = 1733509801
error = ClientError(
error_response={"Error": {"Code": "403", "Message": "Forbidden"}},
operation_name="foo",
)
sagemaker_session.boto_session.resource("s3").meta.client.head_bucket.side_effect = Mock(
side_effect=error
)

with pytest.raises(ClientError):
sagemaker_session.default_bucket()
1 change: 1 addition & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def boto_session(request):
with patch("sagemaker.user_agent.get_user_agent_extra_suffix", return_value=user_agent_suffix):
client_mock._client_config.user_agent = user_agent
boto_mock.client.return_value = client_mock
boto_mock.client("sts").get_caller_identity.return_value = {"Account": "Account-001"}
return boto_mock


Expand Down
Loading