Skip to content

Checking for owner of default s3 bucket #6841

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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: 5 additions & 2 deletions cli/src/pcluster/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ def head_object(self, bucket_name, object_name, expected_bucket_owner=None):
error_code=client_error.response["Error"]["Code"],
)

def head_bucket(self, bucket_name):
def head_bucket(self, bucket_name, expected_bucket_owner=None):
"""Retrieve metadata for a bucket without returning the object itself."""
try:
return self._client.head_bucket(Bucket=bucket_name)
if expected_bucket_owner:
return self._client.head_bucket(Bucket=bucket_name, ExpectedBucketOwner=expected_bucket_owner)
else:
return self._client.head_bucket(Bucket=bucket_name)
except ClientError as client_error:
raise AWSClientError(
function_name="head_bucket",
Expand Down
9 changes: 6 additions & 3 deletions cli/src/pcluster/models/s3_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ def generate_s3_bucket_hash_suffix(account_id, region):
"""
return hashlib.sha256((account_id + region).encode()).hexdigest()[0:16]

def check_bucket_exists(self):
def check_bucket_exists(self, default_bucket):
"""Check bucket existence by bucket name."""
AWSApi.instance().s3.head_bucket(bucket_name=self.name)
if default_bucket:
AWSApi.instance().s3.head_bucket(bucket_name=self.name, expected_bucket_owner=self.account_id)
else:
AWSApi.instance().s3.head_bucket(bucket_name=self.name)

def create_bucket(self):
"""Create a new S3 bucket."""
Expand Down Expand Up @@ -457,7 +460,7 @@ def _check_custom_bucket(cls, service_name: str, custom_s3_bucket: str, artifact
def _check_default_bucket(cls, service_name: str, artifact_directory: str, stack_name: str):
bucket = S3Bucket(service_name=service_name, artifact_directory=artifact_directory, stack_name=stack_name)
try:
bucket.check_bucket_exists()
bucket.check_bucket_exists(default_bucket=True)
except AWSClientError as e:
cls._create_bucket(bucket, e)

Expand Down
Loading