Skip to content

Commit ac926db

Browse files
committed
check extended attributes and move file
1 parent 0d5f3f7 commit ac926db

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lambdas/filenameprocessor/src/file_name_processor.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
FileNotProcessedReason,
2222
FileStatus,
2323
)
24-
from file_validation import is_file_in_directory_root, validate_file_key
24+
from file_validation import is_file_in_directory_root, validate_batch_file_key, validate_extended_attributes_file_key
2525
from make_and_upload_ack_file import make_and_upload_the_ack_file
2626
from models.errors import (
2727
InvalidFileKeyError,
@@ -77,10 +77,12 @@ def handle_record(record) -> dict:
7777
message_id = str(uuid4())
7878
s3_response = get_s3_client().get_object(Bucket=bucket_name, Key=file_key)
7979
created_at_formatted_string, expiry_timestamp = get_creation_and_expiry_times(s3_response)
80+
8081
if file_key.startswith(EXTENDED_ATTRIBUTES_PREFIXES):
81-
pass
82+
validate_extended_attributes_file_key(file_key)
83+
move_file(bucket_name, file_key, f"archive/{file_key}")
8284
else:
83-
vaccine_type, supplier = validate_file_key(file_key)
85+
vaccine_type, supplier = validate_batch_file_key(file_key)
8486

8587
permissions = validate_vaccine_type_permissions(vaccine_type=vaccine_type, supplier=supplier)
8688
queue_name = f"{supplier}_{vaccine_type}"
@@ -166,21 +168,37 @@ def handle_unexpected_bucket_name(bucket_name: str, file_key: str) -> dict:
166168
"""Handles scenario where Lambda was not invoked by the data-sources bucket. Should not occur due to terraform
167169
config and overarching design"""
168170
try:
169-
vaccine_type, supplier = validate_file_key(file_key)
170-
logger.error(
171-
"Unable to process file %s due to unexpected bucket name %s",
172-
file_key,
173-
bucket_name,
174-
)
175-
message = f"Failed to process file due to unexpected bucket name {bucket_name}"
176-
177-
return {
178-
"statusCode": 500,
179-
"message": message,
180-
"file_key": file_key,
181-
"vaccine_type": vaccine_type,
182-
"supplier": supplier,
183-
}
171+
if file_key.startswith(EXTENDED_ATTRIBUTES_PREFIXES):
172+
validate_extended_attributes_file_key(file_key)
173+
logger.error(
174+
"Unable to process file %s due to unexpected bucket name %s",
175+
file_key,
176+
bucket_name,
177+
)
178+
message = f"Failed to process file due to unexpected bucket name {bucket_name}"
179+
return {
180+
"statusCode": 500,
181+
"message": message,
182+
"file_key": file_key,
183+
"vaccine_type": "extended_attributes",
184+
"supplier": "unknown",
185+
}
186+
else:
187+
vaccine_type, supplier = validate_batch_file_key(file_key)
188+
logger.error(
189+
"Unable to process file %s due to unexpected bucket name %s",
190+
file_key,
191+
bucket_name,
192+
)
193+
message = f"Failed to process file due to unexpected bucket name {bucket_name}"
194+
195+
return {
196+
"statusCode": 500,
197+
"message": message,
198+
"file_key": file_key,
199+
"vaccine_type": vaccine_type,
200+
"supplier": supplier,
201+
}
184202

185203
except Exception as error:
186204
logger.error(

lambdas/filenameprocessor/src/file_validation.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def is_valid_datetime(timestamp: str) -> bool:
3737
return True
3838

3939

40-
def validate_file_key(file_key: str) -> tuple[str, str]:
40+
def validate_extended_attributes_file_key(file_key: str) -> tuple[str, str]:
41+
if not match(r"^[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*", file_key):
42+
raise InvalidFileKeyError("Initial file validation failed: invalid extended attributes file key format")
43+
44+
45+
def validate_batch_file_key(file_key: str) -> tuple[str, str]:
4146
"""
4247
Checks that all elements of the file key are valid, raises an exception otherwise.
4348
Returns a tuple containing the vaccine_type and supplier (both converted to upper case).

0 commit comments

Comments
 (0)