Skip to content

Commit 06bb140

Browse files
committed
add authorization for extended attribute
1 parent a0feb20 commit 06bb140

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lambdas/filenameprocessor/src/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ODS_CODE_TO_SUPPLIER_SYSTEM_HASH_KEY = "ods_code_to_supplier"
2424
EXTENDED_ATTRIBUTES_FILE_PREFIX = "Vaccination_Extended_Attributes"
2525
DPS_DESTINATION_PREFIX = "dps_destination/"
26-
26+
VALID_EA_VERSIONS = "v1_5"
2727
ERROR_TYPE_TO_STATUS_CODE_MAP = {
2828
VaccineTypePermissionsError: 403,
2929
InvalidFileKeyError: 400, # Includes invalid ODS code, therefore unable to identify supplier

lambdas/filenameprocessor/src/file_validation.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from datetime import datetime
44
from re import match
55

6-
from constants import VALID_VERSIONS
6+
from constants import EXTENDED_ATTRIBUTES_FILE_PREFIX, VALID_EA_VERSIONS, VALID_VERSIONS
77
from elasticache import (
88
get_supplier_system_from_cache,
99
get_valid_vaccine_types_from_cache,
@@ -45,8 +45,22 @@ def validate_extended_attributes_file_key(file_key: str) -> str:
4545
if not match(r"^[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*_[^_.]*", file_key):
4646
raise InvalidFileKeyError("Initial file validation failed: invalid extended attributes file key format")
4747

48-
file_key_parts_without_extension, _ = split_file_key(file_key)
48+
file_key_parts_without_extension, extension = split_file_key(file_key)
49+
file_type = "_".join(file_key_parts_without_extension[:3])
50+
version = "_".join(file_key_parts_without_extension[3:5])
4951
organization_code = file_key_parts_without_extension[5]
52+
timestamp = file_key_parts_without_extension[6]
53+
supplier = get_supplier_system_from_cache(organization_code)
54+
55+
if not (
56+
file_type == EXTENDED_ATTRIBUTES_FILE_PREFIX
57+
and version == VALID_EA_VERSIONS
58+
and supplier # Note that if supplier could be identified, this also implies that ODS code is valid
59+
and is_valid_datetime(timestamp)
60+
and ((extension == "CSV") or (extension == "DAT")) # The DAT extension has been added for MESH file processing
61+
):
62+
raise InvalidFileKeyError("Initial file validation failed: invalid file key")
63+
5064
return f"{organization_code}_COVID"
5165

5266

lambdas/filenameprocessor/tests/test_file_key_validation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def test_split_file_key(self, _):
113113
with self.subTest(f"SubTest for file key: {file_key}"):
114114
self.assertEqual(split_file_key(file_key), expected)
115115

116+
@patch("file_name_processor.get_supplier_system_from_cache", return_value="RAVS") # supplier now VALID
117+
@patch("file_validation.get")
116118
def test_validate_extended_attributes_file_key(self, _):
117119
"""Tests that validate_extended_attributes_file_key returns organization code and COVID vaccine type if all
118120
elements pass validation, and raises an exception otherwise"""

0 commit comments

Comments
 (0)