Skip to content

Commit

Permalink
Support sha-256 want-digest request headers [DELIVERY-8988]
Browse files Browse the repository at this point in the history
This commit provides support in origin_response for requests with
want-digest headers for the sha256 algorithm.
  • Loading branch information
negillett committed Mar 30, 2020
1 parent 08954ba commit 03d29f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
6 changes: 6 additions & 0 deletions exodus_lambda/functions/origin_response/origin_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def handler(self, event, context):
request = event["Records"][0]["cf"]["request"]
response = event["Records"][0]["cf"]["response"]

# TODO: Investigate cf-s3 response errors, ensure no digest if error
if "headers" in request and "want-digest" in request["headers"]:
response["headers"]["digest"] = [
{"key": "Digest", "value": f"sha-256={request['uri']}",}
]

max_age = self.conf["headers"]["max_age"]
max_age_pattern_whitelist = [
".+/PULP_MANIFEST",
Expand Down
43 changes: 30 additions & 13 deletions tests/functions/test_origin_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,71 @@


@pytest.mark.parametrize(
"test_input",
"original_uri, want_digest",
[
"/some/repo/listing",
"/some/repo/repodata/repomd.xml",
"/some/repo/ostree/repo/refs/heads/ok/test",
("/some/repo/listing", True),
("/some/repo/repodata/repomd.xml", True),
("/some/repo/ostree/repo/refs/heads/ok/test", False),
],
)
def test_origin_response_valid_headers(
test_input,
expected=[{"key": "Cache-Control", "value": "max-age={}".format(max_age)}],
):
def test_origin_response_valid_headers(original_uri, want_digest):
event = {
"Records": [
{
"cf": {
"request": {
"uri": "be7f3007df3e51fb48fff57da9c01c52e6b8e60eceacab"
"7aaf0e05b57578493a",
"headers": {
"exodus-original-uri": [
{
"key": "exodus-original-uri",
"value": test_input,
"value": original_uri,
}
]
}
},
},
"response": {"headers": {}},
}
}
]
}

expected_headers = {
"cache-control": [
{"key": "Cache-Control", "value": f"max-age={max_age}"}
]
}

if want_digest:
event["Records"][0]["cf"]["request"]["headers"]["want-digest"] = [
{"key": "Want-Digest", "value": "sha-256"}
]
expected_headers["digest"] = [
{
"key": "Digest",
"value": "sha-256=be7f3007df3e51fb48fff57da9c01c52e6b8e60eceac"
"ab7aaf0e05b57578493a",
}
]

response = LambdaClient(conf_file=CONF_PATH).handler(event, context=None)
assert response["headers"]["cache-control"] == expected
assert response["headers"] == expected_headers


@pytest.mark.parametrize(
"test_input",
["/some/repo/some-rpm.rpm", "/some/repo/ostree/repo/refs/heads/nope"],
)
def test_origin_response_empty_headers(test_input, expected={}):
def test_origin_response_empty_headers(test_input):
event = {
"Records": [
{"cf": {"request": {"headers": {}}, "response": {"headers": {}}}}
]
}

response = LambdaClient(conf_file=CONF_PATH).handler(event, context=None)
assert response["headers"] == expected
assert response["headers"] == {}


def test_origin_response_missing_headers():
Expand Down

0 comments on commit 03d29f0

Please sign in to comment.