diff --git a/cdn_lambda/functions/map_to_s3/map_to_s3.json b/cdn_lambda/functions/map_to_s3/map_to_s3.json index d9c4fed3..d1b72190 100755 --- a/cdn_lambda/functions/map_to_s3/map_to_s3.json +++ b/cdn_lambda/functions/map_to_s3/map_to_s3.json @@ -2,5 +2,9 @@ "table": { "name": "exodus", "region": "us-east-1" - } + }, + "uri_aliases": [ + ["/content/origin/", "/origin/"], + ["/origin/rpm/", "/origin/rpms/"] + ] } diff --git a/cdn_lambda/functions/map_to_s3/map_to_s3.py b/cdn_lambda/functions/map_to_s3/map_to_s3.py index 96d392aa..b07640b2 100755 --- a/cdn_lambda/functions/map_to_s3/map_to_s3.py +++ b/cdn_lambda/functions/map_to_s3/map_to_s3.py @@ -31,24 +31,29 @@ def db_client(self): return self._db_client + def uri_alias(self, uri): + # NOTE: Aliases are processed in the order they are listed + for alias in self.conf["uri_aliases"]: + if uri.startswith(alias[0]): + uri = uri.replace(alias[0], alias[1]) + return uri + def handler(self, event, context): # pylint: disable=unused-argument request = event["Records"][0]["cf"]["request"] + uri = self.uri_alias(request["uri"]) + table = self.conf["table"]["name"] - LOG.info( - "Querying '%s' table for '%s'...", - self.conf["table"]["name"], - request["uri"], - ) + LOG.info("Querying '%s' table for '%s'...", table, uri) query_result = self.db_client.query( - TableName=self.conf["table"]["name"], + TableName=table, Limit=1, ScanIndexForward=False, KeyConditionExpression="web_uri = :u and from_date <= :d", ExpressionAttributeValues={ - ":u": {"S": request["uri"]}, + ":u": {"S": uri}, ":d": { "S": str( datetime.now(timezone.utc).isoformat( @@ -60,7 +65,7 @@ def handler(self, event, context): ) if query_result["Items"]: - LOG.info("Item found for '%s'", request["uri"]) + LOG.info("Item found for '%s'", uri) try: # Update request uri to point to S3 object key @@ -77,7 +82,7 @@ def handler(self, event, context): raise err else: - LOG.info("No item found for '%s'", request["uri"]) + LOG.info("No item found for '%s'", uri) # Report 404 to prevent attempts on S3 return { @@ -87,4 +92,4 @@ def handler(self, event, context): # Make handler available at module level -lambda_handler = LambdaClient().handler +lambda_handler = LambdaClient().handler # pylint: disable=invalid-name diff --git a/tests/functions/test_map_to_s3.py b/tests/functions/test_map_to_s3.py index 69c14d2d..0a328402 100644 --- a/tests/functions/test_map_to_s3.py +++ b/tests/functions/test_map_to_s3.py @@ -3,26 +3,58 @@ import mock import json -TEST_PATH = "www.example.com/content/file.ext" +TEST_PATH = "/origin/rpms/repo/ver/dir/filename.ext" MOCKED_DT = "2020-02-17T15:38:05.864+00:00" CONF_PATH = "cdn_lambda/functions/map_to_s3/map_to_s3.json" +@pytest.mark.parametrize( + "req_uri, real_uri", + [ + ( + "/origin/rpm/repo/ver/dir/filename.ext", + "/origin/rpms/repo/ver/dir/filename.ext", + ), + ( + "/content/origin/repo/ver/dir/filename.ext", + "/origin/repo/ver/dir/filename.ext", + ), + ( + "/content/origin/rpm/repo/ver/dir/filename.ext", + "/origin/rpms/repo/ver/dir/filename.ext", + ), + ( + "/content/origin/repo/ver/origin/rpm/filename.ext", + "/origin/repo/ver/origin/rpm/filename.ext", + ), + ( + "/origin/rpms/repo/ver/dir/filename.ext", + "/origin/rpms/repo/ver/dir/filename.ext", + ), + ], + ids=[ + "/origin/rpm/", + "content/origin", + "content/origin/rpm", + "multiple alias keywords", + "no alias keywords", + ], +) @mock.patch("boto3.client") @mock.patch("cdn_lambda.functions.map_to_s3.map_to_s3.datetime") -def test_map_to_s3(mocked_datetime, mocked_boto3_client): +def test_map_to_s3(mocked_datetime, mocked_boto3_client, req_uri, real_uri): mocked_datetime.now().isoformat.return_value = MOCKED_DT mocked_boto3_client().query.return_value = { "Items": [ { - "web_uri": {"S": TEST_PATH}, + "web_uri": {"S": real_uri}, "from_date": {"S": "2020-02-17T00:00:00.000+00:00"}, "object_key": {"S": "e4a3f2sum"}, } ] } - event = {"Records": [{"cf": {"request": {"uri": TEST_PATH}}}]} + event = {"Records": [{"cf": {"request": {"uri": req_uri}}}]} request = LambdaClient(conf_file=CONF_PATH).handler(event, context=None)