Skip to content
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

Fix TTL extraction [RHELDST-20510] #245

Merged
merged 1 commit into from
Oct 13, 2023
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
4 changes: 2 additions & 2 deletions pubtools/_pulp/cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CdnClient(object):
_EXPONENT = float(os.environ.get("CDN_RETRY_EXPONENT", "3.0"))
_MAX_SLEEP = float(os.environ.get("CDN_RETRY_MAX_SLEEP", "120.0"))

TTL_REGEX = re.compile(r".*/(\d+[smhd])/.*")
TTL_REGEX = re.compile(r"/(\d+[smhd])/")
CACHE_KEY_HEADER = HeaderPair("akamai-x-get-cache-key", "X-Cache-Key")

def __init__(self, url, max_retry_sleep=_MAX_SLEEP, **kwargs):
Expand Down Expand Up @@ -104,7 +104,7 @@ def _get_ttl(self, path):
out = self._get_headers_for_path(path, headers)

def _parse_ttl(value):
parsed = re.match(
parsed = re.search(
self.TTL_REGEX, value.get(self.CACHE_KEY_HEADER.response) or ""
)
return parsed.group(1) if parsed else None
Expand Down
8 changes: 7 additions & 1 deletion tests/cdn/test_cdn_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ def test_format_arl_template(requests_mock, caplog):
("https://cdn.example.com/content/foo/test-path-2/other-file.xml", "30m"),
]

# ARLs are generated from the template using the {ttl} placeholder, which is replaced with
# the real TTL value. The real TTL value is extracted from the cache key header of the real
# request for the given path using '/(\d+[smhd])/' regex.
# The /1h/foo in the mocked header here is to test that if the path contains a component
# that also matches the TTL regex ('/1h/'), it will still find the correct value
# ('/10h/' or '/30m/').
for url, ttl in url_ttl:
headers = {"X-Cache-Key": f"/fake/cache-key/{ttl}/something"}
headers = {"X-Cache-Key": f"/fake/cache-key/{ttl}/something/1h/foo"}
requests_mock.register_uri("HEAD", url, headers=headers)

fts = []
Expand Down
Loading