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(lxd): Properly handle unicode from LXD socket #5309

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions cloudinit/sources/DataSourceLXD.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def _get_json_response(
"Skipping %s on [HTTP:%d]:%s",
url,
url_response.status_code,
url_response.text,
url_response.content.decode("utf-8"),
)
return {}
try:
Expand All @@ -293,7 +293,7 @@ def _get_json_response(
raise sources.InvalidMetaDataException(
"Unable to process LXD config at {url}."
" Expected JSON but found: {resp}".format(
url=url, resp=url_response.text
url=url, resp=url_response.content.decode("utf-8")
)
) from exc

Expand Down Expand Up @@ -321,7 +321,7 @@ def _do_request(
"Invalid HTTP response [{code}] from {route}: {resp}".format(
code=response.status_code,
route=url,
resp=response.text,
resp=response.content.decode("utf-8"),
)
)
return response
Expand Down Expand Up @@ -364,29 +364,28 @@ def _process_config(self, session: requests.Session) -> dict:
config_route_response = _do_request(
session, config_route_url, do_raise=False
)
response_text = config_route_response.content.decode("utf-8")
if not config_route_response.ok:
LOG.debug(
"Skipping %s on [HTTP:%d]:%s",
config_route_url,
config_route_response.status_code,
config_route_response.text,
response_text,
)
continue

cfg_key = config_route.rpartition("/")[-1]
# Leave raw data values/format unchanged to represent it in
# instance-data.json for cloud-init query or jinja template
# use.
config["config"][cfg_key] = config_route_response.text
config["config"][cfg_key] = response_text
# Promote common CONFIG_KEY_ALIASES to top-level keys.
if cfg_key in CONFIG_KEY_ALIASES:
# Due to sort of config_routes, promote cloud-init.*
# aliases before user.*. This allows user.* keys to act as
# fallback config on old LXD, with new cloud-init images.
if CONFIG_KEY_ALIASES[cfg_key] not in config:
config[
CONFIG_KEY_ALIASES[cfg_key]
] = config_route_response.text
config[CONFIG_KEY_ALIASES[cfg_key]] = response_text
else:
LOG.warning(
"Ignoring LXD config %s in favor of %s value.",
Expand All @@ -404,7 +403,9 @@ def __call__(self, *, metadata_keys: MetaDataKeys) -> dict:
md_route = url_helper.combine_url(
self._version_url, "meta-data"
)
md["meta-data"] = _do_request(session, md_route).text
md["meta-data"] = _do_request(
session, md_route
).content.decode("utf-8")
if MetaDataKeys.CONFIG in metadata_keys:
md.update(self._process_config(session))
if MetaDataKeys.DEVICES in metadata_keys:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests/modules/test_combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
me: "127.0.0.1"
runcmd:
- echo 'hello world' > /var/tmp/runcmd_output
- echo '💩' > /var/tmp/unicode_data

- #
- logger "My test log"
Expand Down Expand Up @@ -506,6 +507,9 @@ def test_instance_cloud_id_across_reboot(
assert client.execute(f"test -f /run/cloud-init/{cloud_file}").ok
assert client.execute("test -f /run/cloud-init/cloud-id").ok

def test_unicode(self, class_client: IntegrationInstance):
client = class_client
assert "💩" == client.read_from_file("/var/tmp/unicode_data")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, who says we don't know how to have fun in this project?


@pytest.mark.user_data(USER_DATA)
class TestCombinedNoCI:
Expand Down