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

Support compressed configuration [RHELDST-25461] #586

Merged
merged 1 commit into from
Jul 29, 2024
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
11 changes: 10 additions & 1 deletion exodus_lambda/functions/origin_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import binascii
import functools
import gzip
import json
import os
import time
Expand Down Expand Up @@ -65,7 +66,15 @@ def definitions(self):
)
if query_result["Items"]:
item = query_result["Items"][0]
out = json.loads(item["config"]["S"])
if item_encoded := item["config"].get("B"):
# new-style: config is compressed and stored as bytes
item_bytes = b64decode(item_encoded)
item_json = gzip.decompress(item_bytes).decode()
else:
# old-style, config was stored as JSON string.
# Consider deleting this code path in 2025
item_json = item["config"]["S"]
out = json.loads(item_json)
else:
# Provide dict with expected keys when no config is found.
out = {
Expand Down
16 changes: 14 additions & 2 deletions tests/functions/test_origin_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gzip
import json
import logging
from base64 import b64encode
from urllib.parse import unquote, urlencode

import mock
Expand Down Expand Up @@ -342,15 +344,25 @@ def test_origin_request_invalid_item(
)


@pytest.mark.parametrize("binary_config", (True, False))
@mock.patch("boto3.client")
def test_origin_request_definitions(mocked_boto3_client):
def test_origin_request_definitions(mocked_boto3_client, binary_config: bool):
mocked_defs = mock_definitions()
json_defs = json.dumps(mocked_defs)

if binary_config:
# Config in the style exodus-gw writes from late 2024 onwards
config = {"B": b64encode(gzip.compress(json_defs.encode())).decode()}
else:
# Older-style config
config = {"S": json_defs}

mocked_boto3_client().query.return_value = {
"Items": [
{
"from_date": {"S": "2020-02-17T00:00:00.000+00:00"},
"config_id": {"S": "exodus-config"},
"config": {"S": json.dumps(mocked_defs)},
"config": config,
}
]
}
Expand Down