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

net/dhcp: raise InvalidDHCPLeaseFileError on error parsing dhcpcd lease #5128

Merged
merged 1 commit into from
Apr 2, 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
19 changes: 13 additions & 6 deletions cloudinit/net/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,21 @@ def parse_dhcpcd_lease(lease_dump: str, interface: str) -> Dict:
subnet_cidr='20'
subnet_mask='255.255.240.0'
"""
LOG.debug(
"Parsing dhcpcd lease for interface %s: %r", interface, lease_dump
)

# create a dict from dhcpcd dump output - remove single quotes
lease = dict(
[
a.split("=")
for a in lease_dump.strip().replace("'", "").split("\n")
]
)
try:
lease = dict(
[
a.split("=")
for a in lease_dump.strip().replace("'", "").split("\n")
]
)
except ValueError as error:
LOG.error("Error parsing dhcpcd lease: %r", error)
raise InvalidDHCPLeaseFileError from error

# this is expected by cloud-init's code
lease["interface"] = interface
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/net/test_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,17 @@ def test_parse_lease_dump(self):
assert "255.255.240.0" == parsed_lease["subnet-mask"]
assert "192.168.0.1" == parsed_lease["routers"]

def test_parse_lease_dump_fails(self):
lease = dedent(
"""
fail
"""
)

with pytest.raises(InvalidDHCPLeaseFileError):
with mock.patch("cloudinit.net.dhcp.util.load_binary_file"):
Dhcpcd.parse_dhcpcd_lease(lease, "eth0")

@pytest.mark.parametrize(
"lease_file, option_245",
(
Expand Down
Loading