diff options
author | Ryan Harper <ryan.harper@canonical.com> | 2019-09-26 19:52:02 +0000 |
---|---|---|
committer | Server Team CI Bot <josh.powers+server-team-bot@canonical.com> | 2019-09-26 19:52:02 +0000 |
commit | f80f7f0d82eaf05e68419c7ac87bf5d4c934b796 (patch) | |
tree | 5b2e1c1d39ea82d34f3ebfb2462efbfa630360e5 | |
parent | 0948cdfbef2052cdf839f24d6a17d457aa9fd4d3 (diff) | |
download | vyos-cloud-init-f80f7f0d82eaf05e68419c7ac87bf5d4c934b796.tar.gz vyos-cloud-init-f80f7f0d82eaf05e68419c7ac87bf5d4c934b796.zip |
sysconfig: only write resolv.conf if network_state has DNS values
If an OS image provided an /etc/resolv.conf file that was not empty
cloud-init would read and re-write it with a cloud-init header even
if no DNS network configuration was provided (e.g. DHCP only).
This can cause problems for some network services which don't
ignore cloud-init's header.
LP: #1843634
-rw-r--r-- | cloudinit/net/sysconfig.py | 6 | ||||
-rw-r--r-- | tests/unittests/test_net.py | 12 |
2 files changed, 16 insertions, 2 deletions
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py index 416c1c97..87b548e5 100644 --- a/cloudinit/net/sysconfig.py +++ b/cloudinit/net/sysconfig.py @@ -577,6 +577,10 @@ class Renderer(renderer.Renderer): @staticmethod def _render_dns(network_state, existing_dns_path=None): + # skip writing resolv.conf if network_state doesn't include any input. + if not any([len(network_state.dns_nameservers), + len(network_state.dns_searchdomains)]): + return None content = resolv_conf.ResolvConf("") if existing_dns_path and os.path.isfile(existing_dns_path): content = resolv_conf.ResolvConf(util.load_file(existing_dns_path)) @@ -584,8 +588,6 @@ class Renderer(renderer.Renderer): content.add_nameserver(nameserver) for searchdomain in network_state.dns_searchdomains: content.add_search_domain(searchdomain) - if not str(content): - return None header = _make_header(';') content_str = str(content) if not content_str.startswith(header): diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py index a093cf10..b6597412 100644 --- a/tests/unittests/test_net.py +++ b/tests/unittests/test_net.py @@ -2701,6 +2701,10 @@ USERCTL=no ns = network_state.parse_net_config_data(CONFIG_V1_EXPLICIT_LOOPBACK) render_dir = self.tmp_path("render") os.makedirs(render_dir) + # write an etc/resolv.conf and expect it to not be modified + resolvconf = os.path.join(render_dir, 'etc/resolv.conf') + resolvconf_content = "# Original Content" + util.write_file(resolvconf, resolvconf_content) renderer = self._get_renderer() renderer.render_network_state(ns, target=render_dir) found = dir2dict(render_dir) @@ -2718,6 +2722,8 @@ TYPE=Ethernet USERCTL=no """ self.assertEqual(expected, found[nspath + 'ifcfg-eth0']) + # a dhcp only config should not modify resolv.conf + self.assertEqual(resolvconf_content, found['/etc/resolv.conf']) def test_bond_config(self): expected_name = 'expected_sysconfig_rhel' @@ -3202,6 +3208,10 @@ USERCTL=no ns = network_state.parse_net_config_data(CONFIG_V1_EXPLICIT_LOOPBACK) render_dir = self.tmp_path("render") os.makedirs(render_dir) + # write an etc/resolv.conf and expect it to not be modified + resolvconf = os.path.join(render_dir, 'etc/resolv.conf') + resolvconf_content = "# Original Content" + util.write_file(resolvconf, resolvconf_content) renderer = self._get_renderer() renderer.render_network_state(ns, target=render_dir) found = dir2dict(render_dir) @@ -3219,6 +3229,8 @@ TYPE=Ethernet USERCTL=no """ self.assertEqual(expected, found[nspath + 'ifcfg-eth0']) + # a dhcp only config should not modify resolv.conf + self.assertEqual(resolvconf_content, found['/etc/resolv.conf']) def test_bond_config(self): expected_name = 'expected_sysconfig_opensuse' |