Skip to content

Commit

Permalink
Add "X-Exodus-Query" header [RHELDST-9321]
Browse files Browse the repository at this point in the history
Our tests sometimes seem to hit an old version of our lambda, for
unclear reasons. Let's make it possible to query the currently deployed
lambda version so we can at least reliably detect when this happens.

The "X-Exodus-Query" header is primarily used in a request, to provide
the version of exodus-lambda, "X-Exodus-Version" in response.
  • Loading branch information
dichn committed Apr 8, 2022
1 parent f78f372 commit 50dbde2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions configuration/exodus-lambda-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Resources:
HeaderBehavior: whitelist
Headers:
- Want-Digest
- X-Exodus-Query
QueryStringsConfig:
QueryStringBehavior: none

Expand Down
1 change: 1 addition & 0 deletions configuration/lambda_config.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"secret_arn": "$EXODUS_SECRET_ARN",
"key_id": "$EXODUS_KEY_ID",
"lambda_version": "$EXODUS_LAMBDA_VERSION",
"logging": {
"version": 1,
"incremental": "True",
Expand Down
4 changes: 4 additions & 0 deletions exodus_lambda/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def logger(self):
def max_age(self):
return self.conf["headers"]["max_age"]

@property
def lambda_version(self):
return self.conf["lambda_version"]

def set_cache_control(self, uri, response):
max_age_pattern_whitelist = [
".+/PULP_MANIFEST",
Expand Down
5 changes: 5 additions & 0 deletions exodus_lambda/functions/origin_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def handler(self, event, context):
{"key": "Digest", "value": f"id-sha-256={sum_b64}"}
]

if "headers" in request and "x-exodus-query" in request["headers"]:
response["headers"]["x-exodus-version"] = [
{"key": "X-Exodus-Version", "value": self.lambda_version}
]

try:
original_uri = request["headers"]["exodus-original-uri"][0][
"value"
Expand Down
3 changes: 3 additions & 0 deletions scripts/mk-config
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export EXODUS_CONFIG_TABLE=${EXODUS_CONFIG_TABLE:-$PROJECT-config-$ENV_TYPE}
export EXODUS_SECRET_ARN=${EXODUS_SECRET_ARN}
export EXODUS_KEY_ID=${EXODUS_KEY_ID}

REVISION="${CODEBUILD_RESOLVED_SOURCE_VERSION:-$(git rev-parse HEAD)}"
export EXODUS_LAMBDA_VERSION="${EXODUS_LAMBDA_VERSION:-$(date -u --iso=s) ${REVISION}}"

envsubst < ./configuration/lambda_config.template
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def mock_conf_file():
test_env["EXODUS_KEY_ID"] = "K1MOU91G3N7WPY"
test_env["ORIGIN_RESPONSE_LOGGER_LEVEL"] = "DEBUG"
test_env["ORIGIN_REQUEST_LOGGER_LEVEL"] = "DEBUG"
test_env["EXODUS_LAMBDA_VERSION"] = "fake version"

cmd = "envsubst < ./configuration/lambda_config.template > {temp_path}"
cmd = cmd.format(temp_path=temp_file.name)
Expand Down
23 changes: 18 additions & 5 deletions tests/functions/test_origin_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@


@pytest.mark.parametrize(
"original_uri, want_digest",
"original_uri, want_digest, x_exodus_query",
[
("/some/repo/listing", True),
("/some/repo/repodata/repomd.xml", True),
("/some/repo/ostree/repo/refs/heads/ok/test", False),
("/some/repo/listing", True, False),
("/some/repo/repodata/repomd.xml", True, False),
("/some/repo/ostree/repo/refs/heads/ok/test", False, True),
],
)
def test_origin_response_valid_headers(original_uri, want_digest):
def test_origin_response_valid_headers(
original_uri, want_digest, x_exodus_query
):
event = {
"Records": [
{
Expand Down Expand Up @@ -59,6 +61,17 @@ def test_origin_response_valid_headers(original_uri, want_digest):
}
]

if x_exodus_query:
event["Records"][0]["cf"]["request"]["headers"]["x-exodus-query"] = [
{"key": "X-Exodus-Query", "value": "true"}
]
expected_headers["x-exodus-version"] = [
{
"key": "X-Exodus-Version",
"value": "fake version",
}
]

response = OriginResponse(conf_file=TEST_CONF).handler(event, context=None)
assert response["headers"] == expected_headers

Expand Down

0 comments on commit 50dbde2

Please sign in to comment.