Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cdn_lambda/functions/map_to_s3/map_to_s3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"table": {
"name": "exodus",
"region": "us-east-1"
}
},
"uri_aliases": [
["/content/origin/", "/origin/"],
["/origin/rpm/", "/origin/rpms/"]
]
}
25 changes: 15 additions & 10 deletions cdn_lambda/functions/map_to_s3/map_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
40 changes: 36 additions & 4 deletions tests/functions/test_map_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down