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: Make _should_wait_via_user_data() handle non-dict data #5976

Merged
merged 4 commits into from
Jan 15, 2025
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
9 changes: 8 additions & 1 deletion cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,12 @@ def _should_wait_via_user_data(
if not raw_config:
return False, "no configuration found"

# Since this could be some arbitrarily large blob of binary data,
# such as a gzipped file, only grab enough to inspect the header.
# Since we can get a header like #cloud-config-archive, make sure
# we grab enough to not be incorrectly identified as cloud-config.
if (
handlers.type_from_starts_with(raw_config.strip()[:13])
handlers.type_from_starts_with(raw_config.strip()[:42])
!= "text/cloud-config"
):
return True, "non-cloud-config user data found"
Expand All @@ -348,6 +352,9 @@ def _should_wait_via_user_data(
)
return True, "failed to parse user data as yaml"

if not isinstance(parsed_yaml, dict):
Copy link
Member

@holmanb holmanb Jan 14, 2025

Choose a reason for hiding this comment

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

This works, but I think that this works around an existing bug rather than fixing it.

handlers.type_from_starts_with() only returned "text/cloud-config" because of a bug in the slicing on line 334. I'm guessing that slicing by the length of the longest value in INCLUSION_TYPES_MAP should do it.

As it is, either of the following types will incorrectly return text/cloud-config:

#cloud-config-archive
#cloud-config-jsonp

If I revert your change to main.py and then update the slice from 13 to 42, the test passes.

Aside: this logic feels out of place in main.py

Choose a reason for hiding this comment

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

As an alternative, you could also do .split(maxsplit=1)[0] to just get the first word

Copy link
Member Author

Choose a reason for hiding this comment

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

@kageurufu , thanks for the suggestion, but the goal of the slice was to avoid parsing processing the entire blob.

Copy link
Member

Choose a reason for hiding this comment

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

the goal of the slice was to avoid parsing processing the entire blob.

@kageurufu's suggestion would accomplish that: split on the first whitespace and return only the first value

Copy link
Member Author

Choose a reason for hiding this comment

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

@holmanb I was more thinking about non-text binary data like some big gzipped blob.

return True, "parsed config not in cloud-config format"

# These all have the potential to require network access, so we should wait
if "write_files" in parsed_yaml:
for item in parsed_yaml["write_files"]:
Expand Down
31 changes: 31 additions & 0 deletions tests/integration_tests/modules/test_cloud_config_archive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from tests.integration_tests.instances import IntegrationInstance
from tests.integration_tests.util import verify_clean_boot, verify_clean_log

USER_DATA = """\
#cloud-config-archive
- type: "text/cloud-boothook"
content: |
#!/bin/sh
echo "this is from a boothook." > /var/tmp/boothook.txt
- type: "text/cloud-config"
content: |
bootcmd:
- echo "this is from a cloud-config." > /var/tmp/bootcmd.txt
"""


@pytest.mark.ci
@pytest.mark.user_data(USER_DATA)
def test_cloud_config_archive(client: IntegrationInstance):
"""Basic correctness test for #cloud-config-archive."""
log = client.read_from_file("/var/log/cloud-init.log")
assert "this is from a boothook." in client.read_from_file(
"/var/tmp/boothook.txt"
)
assert "this is from a cloud-config." in client.read_from_file(
"/var/tmp/bootcmd.txt"
)
verify_clean_log(log)
verify_clean_boot(client)
15 changes: 15 additions & 0 deletions tests/unittests/cmd/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
)


CLOUD_CONFIG_ARCHIVE = """\
#cloud-config-archive
- type: "text/cloud-boothook"
content: |
#!/bin/sh
echo "this is from a boothook." > /var/tmp/boothook.txt
- type: "text/cloud-config"
content: |
bootcmd:
- echo "this is from a cloud-config." > /var/tmp/bootcmd.txt
"""


EXTRA_CLOUD_CONFIG = """\
#cloud-config
write_files
Expand Down Expand Up @@ -264,6 +277,8 @@ def test_main_sys_argv(
),
# Not parseable as yaml
(mock.Mock(), "#cloud-config\nbootcmd:\necho hello", True),
# Yaml that parses to list
(mock.Mock(), CLOUD_CONFIG_ARCHIVE, True),
# Non-cloud-config
(mock.Mock(), "#!/bin/bash\n - echo hello", True),
# Something that after processing won't decode to utf-8
Expand Down
Loading