Skip to content

Commit ef89683

Browse files
committed
EXPECTED_BUCKET_OWNER_ACCOUNT
1 parent 69a99fb commit ef89683

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

lambdas/filenameprocessor/src/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
SOURCE_BUCKET_NAME = os.getenv("SOURCE_BUCKET_NAME")
1414

15-
# We have used an internal temporary bucket here and an acutal dps bucket will replace this
15+
# We have used an internal temporary bucket here and an actual dps bucket will replace this
1616
DPS_DESTINATION_BUCKET_NAME = os.getenv("ACK_BUCKET_NAME")
1717
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
1818
AUDIT_TABLE_NAME = os.getenv("AUDIT_TABLE_NAME")

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from constants import (
2525
DPS_DESTINATION_BUCKET_NAME,
2626
ERROR_TYPE_TO_STATUS_CODE_MAP,
27+
EXPECTED_BUCKET_OWNER_ACCOUNT,
2728
EXTENDED_ATTRIBUTES_FILE_PREFIX,
2829
EXTENDED_ATTRIBUTES_VACC_TYPE,
2930
SOURCE_BUCKET_NAME,
@@ -277,9 +278,16 @@ def handle_extended_attributes_file(
277278
)
278279

279280
dest_file_key = f"dps_destination/{file_key}"
280-
copy_file_to_external_bucket(bucket_name, file_key, DPS_DESTINATION_BUCKET_NAME, dest_file_key)
281+
copy_file_to_external_bucket(
282+
bucket_name,
283+
file_key,
284+
DPS_DESTINATION_BUCKET_NAME,
285+
dest_file_key,
286+
EXPECTED_BUCKET_OWNER_ACCOUNT,
287+
EXPECTED_BUCKET_OWNER_ACCOUNT,
288+
)
281289
is_file_in_bucket(DPS_DESTINATION_BUCKET_NAME, dest_file_key)
282-
delete_file(bucket_name, dest_file_key)
290+
delete_file(bucket_name, dest_file_key, EXPECTED_BUCKET_OWNER_ACCOUNT)
283291

284292
upsert_audit_table(
285293
message_id,

lambdas/filenameprocessor/tests/test_lambda_handler.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
with patch.dict("os.environ", MOCK_ENVIRONMENT_DICT):
3737
from common.clients import REGION_NAME
3838
from constants import (
39-
AUDIT_TABLE_NAME,
40-
EXTENDED_ATTRIBUTES_VACC_TYPE,
41-
AuditTableKeys,
42-
FileStatus
39+
AUDIT_TABLE_NAME,
40+
EXTENDED_ATTRIBUTES_VACC_TYPE,
41+
AuditTableKeys,
42+
FileStatus,
4343
)
4444
from file_name_processor import handle_record, lambda_handler
4545

@@ -266,12 +266,14 @@ def test_lambda_handler_extended_attributes_success(self):
266266
Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT,
267267
)
268268

269+
# TODO: rewrite the bucket patches to use moto
270+
269271
# Patch uuid4 (message id), the identifier extraction, and prevent external copy issues by simulating move
270272
with (
271273
patch("file_name_processor.uuid4", return_value=test_cases[0].message_id),
272274
patch(
273275
"file_name_processor.copy_file_to_external_bucket",
274-
side_effect=lambda src_bucket, key, dst_bucket, dst_key: (
276+
side_effect=lambda src_bucket, key, dst_bucket, dst_key, exp_owner, exp_src_owner: (
275277
s3_client.put_object(
276278
Bucket=BucketNames.DESTINATION,
277279
Key=dst_key,
@@ -281,7 +283,7 @@ def test_lambda_handler_extended_attributes_success(self):
281283
),
282284
patch(
283285
"file_name_processor.delete_file",
284-
side_effect=lambda src_bucket, key: (
286+
side_effect=lambda src_bucket, key, exp_owner: (
285287
s3_client.delete_object(
286288
Bucket=BucketNames.SOURCE,
287289
Key=key,
@@ -335,12 +337,15 @@ def test_lambda_handler_extended_attributes_failure(self):
335337
Body=MOCK_EXTENDED_ATTRIBUTES_FILE_CONTENT,
336338
)
337339

340+
# TODO: rewrite the bucket patches to use moto
341+
338342
# Patch uuid4 (message id), the identifier extraction, and don't move the file
339343
with (
340344
patch("file_name_processor.uuid4", return_value=test_cases[0].message_id),
341345
patch(
342346
"file_name_processor.copy_file_to_external_bucket",
343-
side_effect=lambda src_bucket, key, dst_bucket, dst_key: ( # effectively do nothing
347+
side_effect=lambda src_bucket, key, dst_bucket, dst_key, exp_owner, exp_src_owner: (
348+
# effectively do nothing
344349
None,
345350
),
346351
),

lambdas/shared/src/common/aws_s3_utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
"""Non-imms Utility Functions"""
22

3-
import os
43

54
from common.clients import get_s3_client, logger
65

7-
EXPECTED_BUCKET_OWNER_ACCOUNT = os.getenv("ACCOUNT_ID")
8-
96

107
def move_file(bucket_name: str, source_file_key: str, destination_file_key: str) -> None:
118
"""Moves a file from one location to another within a single S3 bucket by copying and then deleting the file."""
@@ -20,24 +17,33 @@ def move_file(bucket_name: str, source_file_key: str, destination_file_key: str)
2017

2118

2219
def copy_file_to_external_bucket(
23-
source_bucket: str, source_key: str, destination_bucket: str, destination_key: str
20+
source_bucket: str,
21+
source_key: str,
22+
destination_bucket: str,
23+
destination_key: str,
24+
expected_bucket_owner: str,
25+
expected_source_bucket_owner: str,
2426
) -> None:
2527
s3_client = get_s3_client()
2628
s3_client.copy_object(
2729
CopySource={"Bucket": source_bucket, "Key": source_key},
2830
Bucket=destination_bucket,
2931
Key=destination_key,
30-
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
31-
ExpectedSourceBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
32+
ExpectedBucketOwner=expected_bucket_owner,
33+
ExpectedSourceBucketOwner=expected_source_bucket_owner,
3234
)
3335

3436

35-
def delete_file(source_bucket: str, source_key: str) -> None:
37+
def delete_file(
38+
source_bucket: str,
39+
source_key: str,
40+
expected_bucket_owner: str,
41+
) -> None:
3642
s3_client = get_s3_client()
3743
s3_client.delete_object(
3844
Bucket=source_bucket,
3945
Key=source_key,
40-
ExpectedBucketOwner=EXPECTED_BUCKET_OWNER_ACCOUNT,
46+
ExpectedBucketOwner=expected_bucket_owner,
4147
)
4248

4349

lambdas/shared/tests/test_common/test_s3_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def test_move_file_outside_bucket_copies_then_deletes(self):
9595
source_key=source_key,
9696
destination_bucket=self.destination_bucket,
9797
destination_key=destination_key,
98+
expected_bucket_owner=aws_s3_utils.EXPECTED_BUCKET_OWNER_ACCOUNT,
99+
expected_source_bucket_owner=aws_s3_utils.EXPECTED_BUCKET_OWNER_ACCOUNT,
98100
)
99101

100102
# Assert destination has the object
@@ -109,6 +111,7 @@ def test_move_file_outside_bucket_copies_then_deletes(self):
109111
aws_s3_utils.delete_file(
110112
source_bucket=self.source_bucket,
111113
source_key=source_key,
114+
expected_bucket_owner=aws_s3_utils.EXPECTED_BUCKET_OWNER_ACCOUNT,
112115
)
113116

114117
# Assert source object was deleted

0 commit comments

Comments
 (0)