Skip to content

Commit 2d0caf8

Browse files
authored
VED-386: Handle the new format for supplier permissions on Batch. (#638)
Configurable authorization for Batch Processor
1 parent bc67ec5 commit 2d0caf8

26 files changed

+209
-467
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
id: filenameprocessor
4141
continue-on-error: true
4242
run: |
43-
poetry env use 3.10
43+
poetry env use 3.11
4444
poetry install
4545
poetry run coverage run -m unittest discover || echo "filenameprocessor tests failed" >> ../failed_tests.txt
4646
poetry run coverage xml -o ../filenameprocessor-coverage.xml
@@ -50,7 +50,7 @@ jobs:
5050
id: recordprocessor
5151
continue-on-error: true
5252
run: |
53-
poetry env use 3.10
53+
poetry env use 3.11
5454
poetry install
5555
poetry run coverage run -m unittest discover || echo "recordprocessor tests failed" >> ../failed_tests.txt
5656
poetry run coverage xml -o ../recordprocessor-coverage.xml

filenameprocessor/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM public.ecr.aws/lambda/python:3.10 AS base
1+
FROM public.ecr.aws/lambda/python:3.11 AS base
22

33
# Create non-root user
44
RUN mkdir -p /home/appuser && \

filenameprocessor/batch.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM public.ecr.aws/lambda/python:3.10 as base
1+
FROM public.ecr.aws/lambda/python:3.11 as base
22

33
RUN pip install "poetry~=1.5.0"
44

filenameprocessor/poetry.lock

Lines changed: 10 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

filenameprocessor/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Your Name <[email protected]>"]
77
packages = [{ include = "src" }]
88

99
[tool.poetry.dependencies]
10-
python = "~3.10"
10+
python = "~3.11"
1111
"fhir.resources" = "~7.0.2"
1212
boto3 = "~1.38.42"
1313
boto3-stubs-lite = { extras = ["dynamodb"], version = "~1.38.42" }

filenameprocessor/src/constants.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
AUDIT_TABLE_QUEUE_NAME_GSI = "queue_name_index"
1717
AUDIT_TABLE_FILENAME_GSI = "filename_index"
1818

19-
PERMISSIONS_CONFIG_FILE_KEY = "permissions_config.json"
19+
SUPPLIER_PERMISSIONS_HASH_KEY = "supplier_permissions"
2020
VACCINE_TYPE_TO_DISEASES_HASH_KEY = "vacc_to_diseases"
2121

2222
ERROR_TYPE_TO_STATUS_CODE_MAP = {
@@ -60,10 +60,11 @@ class Constants:
6060
"8J1100001": "PINNACLE",
6161
"8HK48": "SONAR",
6262
"YGA": "TPP",
63+
"V0V8L": "MAVIS",
64+
"X8E5B": "RAVS",
6365
"0DE": "AGEM-NIVS",
6466
"0DF": "NIMS",
6567
"8HA94": "EVA",
66-
"X26": "RAVS",
6768
"YGMYH": "MEDICAL_DIRECTOR",
6869
"W00": "WELSH_DA_1",
6970
"W000": "WELSH_DA_2",

filenameprocessor/src/elasticache.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
"Upload the content from a config file in S3 to ElastiCache (Redis)"
2-
31
import json
4-
from clients import s3_client, redis_client
5-
from constants import PERMISSIONS_CONFIG_FILE_KEY, VACCINE_TYPE_TO_DISEASES_HASH_KEY
6-
7-
8-
def upload_to_elasticache(file_key: str, bucket_name: str) -> None:
9-
"""Uploads the config file content from S3 to ElastiCache (Redis)."""
10-
config_file = s3_client.get_object(Bucket=bucket_name, Key=file_key)
11-
config_file_content = config_file["Body"].read().decode("utf-8")
12-
# Use the file_key as the Redis key and file content as the value
13-
redis_client.set(file_key, config_file_content)
2+
from clients import redis_client
3+
from constants import VACCINE_TYPE_TO_DISEASES_HASH_KEY, SUPPLIER_PERMISSIONS_HASH_KEY
144

155

16-
def get_permissions_config_json_from_cache() -> dict:
6+
def get_supplier_permissions_from_cache(supplier_system: str) -> list[str]:
177
"""Gets and returns the permissions config file content from ElastiCache (Redis)."""
18-
return json.loads(redis_client.get(PERMISSIONS_CONFIG_FILE_KEY))
8+
permissions_str = redis_client.hget(SUPPLIER_PERMISSIONS_HASH_KEY, supplier_system)
9+
return json.loads(permissions_str) if permissions_str else []
1910

2011

2112
def get_valid_vaccine_types_from_cache() -> list[str]:

filenameprocessor/src/file_name_processor.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from make_and_upload_ack_file import make_and_upload_the_ack_file
1515
from audit_table import upsert_audit_table, get_next_queued_file_details, ensure_file_is_not_a_duplicate
1616
from clients import logger
17-
from elasticache import upload_to_elasticache
1817
from logging_decorator import logging_decorator
1918
from supplier_permissions import validate_vaccine_type_permissions
2019
from errors import (
@@ -140,17 +139,6 @@ def handle_record(record) -> dict:
140139
"supplier": supplier
141140
}
142141

143-
elif "config" in bucket_name:
144-
try:
145-
upload_to_elasticache(file_key, bucket_name)
146-
logger.info("%s content successfully uploaded to cache", file_key)
147-
message = "File content successfully uploaded to cache"
148-
return {"statusCode": 200, "message": message, "file_key": file_key}
149-
except Exception as error: # pylint: disable=broad-except
150-
logger.error("Error uploading to cache for file '%s': %s", file_key, error)
151-
message = "Failed to upload file content to cache"
152-
return {"statusCode": 500, "message": message, "file_key": file_key, "error": str(error)}
153-
154142
else:
155143
try:
156144
vaccine_type, supplier = validate_file_key(file_key)

filenameprocessor/src/send_sqs_message.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ def send_to_supplier_queue(message_body: dict, vaccine_type: str, supplier: str)
2727

2828

2929
def make_and_send_sqs_message(
30-
file_key: str, message_id: str, permission: str, vaccine_type: str, supplier: str, created_at_formatted_string: str
30+
file_key: str,
31+
message_id: str,
32+
permission: list[str],
33+
vaccine_type: str,
34+
supplier: str,
35+
created_at_formatted_string: str
3136
) -> None:
3237
"""Attempts to send a message to the SQS queue. Raises an exception if the message is not successfully sent."""
3338
message_body = {

filenameprocessor/src/supplier_permissions.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,18 @@
22

33
from clients import logger
44
from errors import VaccineTypePermissionsError
5-
from elasticache import get_permissions_config_json_from_cache
6-
7-
8-
def get_supplier_permissions(supplier: str) -> list:
9-
"""
10-
Returns the permissions for the given supplier.
11-
Defaults return value is an empty list, including when the supplier has no permissions.
12-
"""
13-
permissions_config = get_permissions_config_json_from_cache()
14-
return permissions_config.get("all_permissions", {}).get(supplier, [])
5+
from elasticache import get_supplier_permissions_from_cache
156

167

178
def validate_vaccine_type_permissions(vaccine_type: str, supplier: str) -> list:
189
"""
1910
Returns the list of permissions for the given supplier.
2011
Raises an exception if the supplier does not have at least one permission for the vaccine type.
2112
"""
22-
supplier_permissions = get_supplier_permissions(supplier)
13+
supplier_permissions = get_supplier_permissions_from_cache(supplier)
2314

2415
# Validate that supplier has at least one permissions for the vaccine type
25-
if not any(vaccine_type in permission for permission in supplier_permissions):
16+
if not any(permission.split(".")[0] == vaccine_type for permission in supplier_permissions):
2617
error_message = f"Initial file validation failed: {supplier} does not have permissions for {vaccine_type}"
2718
logger.error(error_message)
2819
raise VaccineTypePermissionsError(error_message)

0 commit comments

Comments
 (0)