Skip to content

Commit 83a9491

Browse files
authored
VED-814 Resolve sonarcloud warnings on S3 bucket ownership (#882)
1 parent 92b59bb commit 83a9491

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

mesh_processor/src/converter.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import boto3
66
from smart_open import open
77

8+
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
89
DESTINATION_BUCKET_NAME = os.getenv("DESTINATION_BUCKET_NAME")
910

1011
logging.basicConfig(level=logging.INFO)
@@ -80,9 +81,15 @@ def move_file(source_bucket: str, source_key: str, destination_bucket: str, dest
8081
s3_client.copy_object(
8182
CopySource={"Bucket": source_bucket, "Key": source_key},
8283
Bucket=destination_bucket,
83-
Key=destination_key
84+
Key=destination_key,
85+
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
86+
ExpectedSourceBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT
87+
)
88+
s3_client.delete_object(
89+
Bucket=source_bucket,
90+
Key=source_key,
91+
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT
8492
)
85-
s3_client.delete_object(Bucket=source_bucket, Key=source_key)
8693

8794

8895
def transfer_multipart_content(bucket_name: str, file_key: str, boundary: bytes, filename: str) -> None:
@@ -122,7 +129,11 @@ def process_record(record: dict) -> None:
122129
file_key = record["s3"]["object"]["key"]
123130
logger.info(f"Processing {file_key}")
124131

125-
response = s3_client.head_object(Bucket=bucket_name, Key=file_key)
132+
response = s3_client.head_object(
133+
Bucket=bucket_name,
134+
Key=file_key,
135+
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT
136+
)
126137
content_type = response['ContentType']
127138
media_type, content_type_params = parse_header_value(content_type)
128139
filename = response["Metadata"].get("mex-filename") or file_key
@@ -136,7 +147,9 @@ def process_record(record: dict) -> None:
136147
s3_client.copy_object(
137148
Bucket=DESTINATION_BUCKET_NAME,
138149
CopySource={"Bucket": bucket_name, "Key": file_key},
139-
Key=filename
150+
Key=filename,
151+
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
152+
ExpectedSourceBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT
140153
)
141154

142155
logger.info(f"Transfer complete for {file_key}")

mesh_processor/tests/test_converter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from botocore.exceptions import ClientError
77
from moto import mock_aws
88

9+
MOCK_MOTO_ACCOUNT_ID = "123456789012"
10+
911

1012
def invoke_lambda(file_key: str):
1113
# Local import so that globals can be mocked
@@ -26,7 +28,7 @@ def invoke_lambda(file_key: str):
2628

2729

2830
@mock_aws
29-
@patch.dict(os.environ, {"DESTINATION_BUCKET_NAME": "destination-bucket"})
31+
@patch.dict(os.environ, {"DESTINATION_BUCKET_NAME": "destination-bucket", "ACCOUNT_ID": MOCK_MOTO_ACCOUNT_ID})
3032
class TestLambdaHandler(TestCase):
3133
def setUp(self):
3234
s3 = boto3.client("s3", region_name="eu-west-2")

terraform/mesh_processor.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ resource "aws_lambda_function" "mesh_file_converter_lambda" {
217217

218218
environment {
219219
variables = {
220+
ACCOUNT_ID = var.immunisation_account_id
220221
DESTINATION_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
221222
}
222223
}

0 commit comments

Comments
 (0)