Skip to content

Commit

Permalink
Fixed a us-east-1 specific bug in S3 bucket creation code (#102)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Helma <[email protected]>
  • Loading branch information
chelma authored Aug 2, 2023
1 parent c8e4619 commit 5dfb396
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 10 additions & 7 deletions manage_arkime/aws_interactions/s3_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ def create_bucket(bucket_name: str, aws_provider: AwsClientProvider):
aws_env = aws_provider.get_aws_env()

try:
s3_client.create_bucket(
ACL="private",
Bucket=bucket_name,
CreateBucketConfiguration={
create_args = {
"ACL": "private",
"Bucket": bucket_name,
"ObjectOwnership": "BucketOwnerPreferred"
}

if aws_env.aws_region != "us-east-1":
create_args["CreateBucketConfiguration"] = {
"LocationConstraint": aws_env.aws_region
},
ObjectOwnership="BucketOwnerPreferred"
)
}
s3_client.create_bucket(**create_args)
except ClientError as ex:
if "BucketAlreadyOwnedByYou" in str(ex):
logger.debug(f"Bucket {bucket_name} already exists and is owned by this account")
Expand Down
13 changes: 12 additions & 1 deletion test_manage_arkime/aws_interactions/test_s3_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ def test_WHEN_create_bucket_called_THEN_as_expected():
# Set up our mock
mock_s3_client = mock.Mock()
test_env = AwsEnvironment("XXXXXXXXXXX", "my-region-1", "profile")
test_env_use1 = AwsEnvironment("XXXXXXXXXXX", "us-east-1", "profile")

mock_aws_provider = mock.Mock()
mock_aws_provider.get_s3.return_value = mock_s3_client
mock_aws_provider.get_aws_env.return_value = test_env

# TEST: Bucket doesn't exist and we create it
mock_aws_provider.get_aws_env.return_value = test_env
s3.create_bucket("bucket-name", mock_aws_provider)

mock_aws_provider.get_aws_env.return_value = test_env_use1
s3.create_bucket("bucket-name", mock_aws_provider)


create_bucket_calls = [
mock.call(
ACL="private",
Expand All @@ -52,6 +58,11 @@ def test_WHEN_create_bucket_called_THEN_as_expected():
"LocationConstraint": "my-region-1"
},
ObjectOwnership="BucketOwnerPreferred"
),
mock.call(
ACL="private",
Bucket="bucket-name",
ObjectOwnership="BucketOwnerPreferred"
)
]
assert create_bucket_calls == mock_s3_client.create_bucket.call_args_list
Expand Down

0 comments on commit 5dfb396

Please sign in to comment.