Skip to content
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
2 changes: 1 addition & 1 deletion cloudinit/net/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def parse_dhcp_lease_file(lease_file):
@raises: InvalidDHCPLeaseFileError on empty of unparseable leasefile
content.
"""
lease_regex = re.compile(r"lease {(?P<lease>[^}]*)}\n")
lease_regex = re.compile(r"lease {(?P<lease>.*?)}\n", re.DOTALL)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

From man dchp.client I see now that lease declarations such as filename, server-name, script-name can all contain arbitrary "string" values, so trying to limit our regex and stop matching at the first the "}" it encounters will cause some significant issue, as you mentioned.

The description of a lease declaration from man dhclient.conf is

       A  lease  statement  consists  of the lease keyword, followed by a left
       curly brace, followed by one or more lease declaration statements, fol‐
       lowed  by  a  right  curly brace.  The following lease declarations are
       possible:

I can see by the vague text it could be possible that a multiple DHCP lease declarations could be emitted all on one line as in lease { ... } lease { ... }, in that case our regex ending with a curly brace newline "}\n" would continue to break there,... But, I'd say since we have gotten this far on multiple distributions we probably don't need to solve that issue in this PR. We can deal with that if it comes up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just adding to this for future reference when/if it comes up. I saw that also but I have never personally seen it resolve like that, might need a special set of very intentional circumstances. However if it ever does come up I do not think regex is the correct answer. It took me quite some time and fiddling to get this to work, and that would be excessively difficult. I would sooner take a customized algorithmic approach to handling this if it comes up by counting the brackets. Every { adds 1 to a var, every } reduces by one, concluding the parsing only when that var == 0. Again just adding this for the next unfortunate sap left to stare this issue down! If anyone wants me to implement this now however I wouldnt mind doing so and adding the test case for it.

dhcp_leases = []
lease_content = util.load_file(lease_file)
if len(lease_content) == 0:
Expand Down
6 changes: 5 additions & 1 deletion cloudinit/net/tests/test_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_parse_multiple_leases(self):
lease {
interface "wlp3s0";
fixed-address 192.168.2.74;
filename "http://192.168.2.50/boot.php?mac=${netX}";
option subnet-mask 255.255.255.0;
option routers 192.168.2.1;
renew 4 2017/07/27 18:02:30;
Expand All @@ -50,6 +51,7 @@ def test_parse_multiple_leases(self):
lease {
interface "wlp3s0";
fixed-address 192.168.2.74;
filename "http://192.168.2.50/boot.php?mac=${netX}";
option subnet-mask 255.255.255.0;
option routers 192.168.2.1;
}
Expand All @@ -58,8 +60,10 @@ def test_parse_multiple_leases(self):
{'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1',
'renew': '4 2017/07/27 18:02:30',
'expire': '5 2017/07/28 07:08:15'},
'expire': '5 2017/07/28 07:08:15',
'filename': 'http://192.168.2.50/boot.php?mac=${netX}'},
{'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
'filename': 'http://192.168.2.50/boot.php?mac=${netX}',
'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1'}]
write_file(lease_file, content)
self.assertCountEqual(expected, parse_dhcp_lease_file(lease_file))
Expand Down