Skip to content

Introduce mirrored reads [RHELDST-28332] #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
1 change: 1 addition & 0 deletions configuration/lambda_config.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"headers": {
"max_age": $EXODUS_HEADERS_MAX_AGE
},
"mirror_reads": "$EXODUS_MIRROR_READS",
"lambda_version": "$EXODUS_LAMBDA_VERSION",
"index_filename": "$EXODUS_INDEX_FILENAME",
"logging": {
Expand Down
9 changes: 9 additions & 0 deletions exodus_lambda/functions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def lambda_version(self):
def index(self):
return self.conf["index_filename"]

@property
def mirror_reads(self):
if str(self.conf.get("mirror_reads", "true")).lower() in (
"0",
"false",
):
return False
return True

def set_lambda_version(self, response):
response.setdefault("headers", {})["x-exodus-version"] = [
{"key": "X-Exodus-Version", "value": self.lambda_version}
Expand Down
32 changes: 27 additions & 5 deletions exodus_lambda/functions/origin_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ def uri_alias(self, uri, aliases, ignore_exclusions=False):

return uri

def resolve_aliases(self, uri, ignore_exclusions=False):
def resolve_aliases(
self, uri, ignore_exclusions=False, ignore_releasever=False
):
# aliases relating to origin, e.g. content/origin <=> origin

uri = self.uri_alias(
uri, self.definitions.get("origin_alias"), ignore_exclusions
)

# aliases relating to rhui; listing files are a special exemption
# because they must be allowed to differ for rhui vs non-rhui.
if not uri.endswith("/listing"):
Expand All @@ -140,9 +142,12 @@ def resolve_aliases(self, uri, ignore_exclusions=False):
)

# aliases relating to releasever; e.g. /content/dist/rhel8/8 <=> /content/dist/rhel8/8.5
uri = self.uri_alias(
uri, self.definitions.get("releasever_alias"), ignore_exclusions
)
if not ignore_releasever:
uri = self.uri_alias(
uri,
self.definitions.get("releasever_alias"),
ignore_exclusions,
)

self.logger.debug("Resolved request URI: %s", uri)

Expand Down Expand Up @@ -393,6 +398,23 @@ def handler(self, event, context):
if preferred_uri != fallback_uri:
uris.append(fallback_uri)

# When exodus-cdn is looking up content to be served for a path having a
# $releasever alias in effect, it should attempt to look up content on
# both sides of the alias.
if self.mirror_reads:
# Attempt to look up content on the other side of the alias (the original
# path.)
# Note: Only the releasever alias is left unresolved. Other alias types
# (rhui and origin aliases) are resolved.
for ignore_exclusions in (False, True):
mirrored_uri = self.resolve_aliases(
request["uri"],
ignore_exclusions=ignore_exclusions,
ignore_releasever=True,
)
if mirrored_uri not in uris:
uris.append(mirrored_uri)

for uri in uris:
if listing_response := self.handle_listing_request(uri):
self.set_cache_control(uri, listing_response)
Expand Down
1 change: 1 addition & 0 deletions scripts/mk-config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export ENV_TYPE=${ENV_TYPE:-dev}
export EXODUS_TABLE=${EXODUS_TABLE:-$PROJECT-cdn-$ENV_TYPE}
export EXODUS_CONFIG_TABLE=${EXODUS_CONFIG_TABLE:-$PROJECT-config-$ENV_TYPE}
export EXODUS_INDEX_FILENAME=${EXODUS_INDEX_FILENAME:-.__exodus_autoindex}
export EXODUS_MIRROR_READS=${EXODUS_MIRROR_READS:-true}

REVISION="${CODEBUILD_RESOLVED_SOURCE_VERSION:-$(git rev-parse HEAD)}"
export EXODUS_LAMBDA_VERSION="${EXODUS_LAMBDA_VERSION:-$(date -u --iso=s) ${REVISION}}"
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def mock_conf_file():
test_env["ORIGIN_REQUEST_LOGGER_LEVEL"] = "DEBUG"
test_env["EXODUS_LAMBDA_VERSION"] = "fake version"
test_env["EXODUS_INDEX_FILENAME"] = ".__exodus_autoindex"
test_env["EXODUS_MIRROR_READS"] = ""

subprocess.run(
["envsubst"],
Expand Down
31 changes: 31 additions & 0 deletions tests/functions/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,34 @@ def test_json_logger_configurable_datefmt(caplog):
"response": None,
},
]


@pytest.mark.parametrize(
"config_value,enabled",
[
("", True),
("True", True),
("true", True),
(1, True),
("1", True),
("false", False),
("0", False),
(0, False),
],
ids=[
"empty string, unset",
"mixed case true",
"lowercase true",
"int 1",
"string 1",
"false string",
"string 0",
"int 0",
],
)
def test_mirrored_reads(config_value, enabled):
"""Verify that the mirror_reads config value produces the expected
mirror_reads property."""
conf = copy.deepcopy(TEST_CONF)
conf["mirror_reads"] = config_value
assert LambdaBase(conf_file=conf).mirror_reads == enabled
Loading