Skip to content

Commit

Permalink
Rename service values
Browse files Browse the repository at this point in the history
  • Loading branch information
RioKnightleyNHS committed Oct 28, 2024
1 parent b9f7d34 commit e329783
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions lambdas/handlers/edge_presign_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def lambda_handler(event, context):
logger.info("Edge received S3 request")

edge_presign_service = EdgePresignService()
request_values: dict = edge_presign_service.extract_request_values(request)
edge_presign_service.presign_request(request_values)
request_values: dict = edge_presign_service.filter_request_values(request)
edge_presign_service.use_presign(request_values)

request: dict = edge_presign_service.prepare_s3_response(request, request_values)
request: dict = edge_presign_service.create_s3_response(request, request_values)

logger.info("Edge returning S3 response")
logger.info("Edge forwarding S3 request")
return request
36 changes: 18 additions & 18 deletions lambdas/services/edge_presign_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ def __init__(self):
self.ssm_service = SSMService()
self.table_name_ssm_param = "EDGE_REFERENCE_TABLE"

def use_presign(self, request_values):
uri = request_values["uri"]
querystring = request_values["querystring"]
domain_name = request_values["domain_name"]

presign_string = f"{uri}?{querystring}"
encoded_presign_string = presign_string.encode("utf-8")
presign_credentials_hash = hashlib.md5(encoded_presign_string).hexdigest()

self.attempt_url_update(
uri_hash=presign_credentials_hash,
domain_name=domain_name,
)

def attempt_url_update(self, uri_hash, domain_name) -> None:
try:
environment = self.extract_environment_from_domain(domain_name)
environment = self.filter_domain_for_env(domain_name)
logger.info(f"Environment found: {environment}")
base_table_name: str = self.ssm_service.get_ssm_parameter(
self.table_name_ssm_param
Expand All @@ -41,22 +55,8 @@ def attempt_url_update(self, uri_hash, domain_name) -> None:
logger.error(f"{str(e)}", {"Result": LambdaError.EdgeNoClient.to_str()})
raise CloudFrontEdgeException(400, LambdaError.EdgeNoClient)

def presign_request(self, request_values):
uri = request_values["uri"]
querystring = request_values["querystring"]
domain_name = request_values["domain_name"]

presign_string = f"{uri}?{querystring}"
encoded_presign_string = presign_string.encode("utf-8")
presign_credentials_hash = hashlib.md5(encoded_presign_string).hexdigest()

self.attempt_url_update(
uri_hash=presign_credentials_hash,
domain_name=domain_name,
)

@staticmethod
def prepare_s3_response(request, request_values):
def create_s3_response(request, request_values):
domain_name = request_values["domain_name"]
if "authorization" in request["headers"]:
del request["headers"]["authorization"]
Expand All @@ -65,7 +65,7 @@ def prepare_s3_response(request, request_values):
return request

@staticmethod
def extract_request_values(request) -> dict:
def filter_request_values(request) -> dict:
try:
uri = request["uri"]
querystring = request["querystring"]
Expand All @@ -84,7 +84,7 @@ def extract_request_values(request) -> dict:
}

@staticmethod
def extract_environment_from_domain(domain_name: str) -> str:
def filter_domain_for_env(domain_name: str) -> str:
match = re.match(r"^[^-]+(?:-[^-]+)?(?=-lloyd)", domain_name)
if match:
return match.group(0)
Expand Down
16 changes: 8 additions & 8 deletions lambdas/tests/unit/handlers/test_edge_presign_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ def mock_edge_presign_service(mocker):

mock_edge_service = mocker.patch("handlers.edge_presign_handler.EdgePresignService")
mock_edge_service_instance = mock_edge_service.return_value
mock_edge_service_instance.extract_request_values.return_value = {
mock_edge_service_instance.filter_request_values.return_value = {
"uri": "/some/path",
"querystring": MOCKED_AUTH_QUERY,
"headers": {"host": [{"key": "Host", "value": MOCKED_DOMAIN}]},
"domain_name": MOCKED_DOMAIN,
}
mock_edge_service_instance.presign_request.return_value = None
mock_edge_service_instance.prepare_s3_response.return_value = {
mock_edge_service_instance.use_presign.return_value = None
mock_edge_service_instance.create_s3_response.return_value = {
"headers": {
"host": [{"key": "Host", "value": MOCKED_DOMAIN}],
}
Expand All @@ -63,13 +63,13 @@ def test_lambda_handler_success(valid_event, mock_edge_presign_service):
context = mock_context()
response = lambda_handler(valid_event, context)

mock_edge_presign_service.extract_request_values.assert_called_once()
mock_edge_presign_service.presign_request.assert_called_once_with(
mock_edge_presign_service.extract_request_values.return_value
mock_edge_presign_service.filter_request_values.assert_called_once()
mock_edge_presign_service.use_presign.assert_called_once_with(
mock_edge_presign_service.filter_request_values.return_value
)
mock_edge_presign_service.prepare_s3_response.assert_called_once_with(
mock_edge_presign_service.create_s3_response.assert_called_once_with(
valid_event["Records"][0]["cf"]["request"],
mock_edge_presign_service.extract_request_values.return_value,
mock_edge_presign_service.filter_request_values.return_value,
)

assert response["headers"]["host"][0]["value"] == MOCKED_DOMAIN
Expand Down
4 changes: 2 additions & 2 deletions lambdas/tests/unit/services/test_edge_presign_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@


# def test_extract_environment_from_valid_domain():
# result = EdgePresignService.extract_environment_from_domain(VALID_DOMAIN)
# result = EdgePresignService.filter_domain_for_env(VALID_DOMAIN)
# assert result == EXPECTED_ENVIRONMENT


# def test_extract_environment_from_invalid_domain():
# result = EdgePresignService.extract_environment_from_domain(INVALID_DOMAIN)
# result = EdgePresignService.filter_domain_for_env(INVALID_DOMAIN)
# assert result == ""


Expand Down

0 comments on commit e329783

Please sign in to comment.