diff --git a/CHANGELOG.md b/CHANGELOG.md index e448205..4bc2cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `postconf -e` and in the paths of the `/etc/pve/nodes` move (#64, NetOrk/netork#2456). They stood between single quotes of their own, so a name with a single quote ended them and the rest ran as root (`x';reboot;'`). +- `set_hostname()` writes the new `/etc/hosts` to a `mktemp` file beside it + (`/etc/.hosts.XXXXXX`), sets its mode and moves it into place; a failed step removes + it (#66, NetOrk/netork#2446). It wrote as root to the fixed `/tmp/_netork_hosts` and + moved that over `/etc/hosts`, so a local user who created the file first owned + `/etc/hosts` afterwards. - `get_ntp_servers()` read `GET /nodes/{node}/ntp`, a path Proxmox VE does not have, and so always reported no servers. It now reports chrony's configured sources, the ones `get_time_sync()` reads, and the backup server has it too. diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 2ee9241..908a8de 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -442,9 +442,8 @@ class ProxmoxSystemMixin: hosts_new = _rename_in_hosts(hosts_raw, old_short, short_hostname, domain) hosts_b64 = _b64.b64encode(hosts_new.encode()).decode() self._exec_ssh_command( - f"echo {hosts_b64} | base64 -d > /tmp/_netork_hosts" - f" && mv /tmp/_netork_hosts /etc/hosts" - f" && chmod 644 /etc/hosts" + f't=$(mktemp /etc/.hosts.XXXXXX) && echo {hosts_b64} | base64 -d > "$t"' + ' && chmod 644 "$t" && mv "$t" /etc/hosts || { rm -f "$t"; false; }' ) # ── 3. /etc/mailname ───────────────────────────────────────────── diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 416529d..7d45159 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -29,7 +29,7 @@ "napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.get_vlans": 66, "napalm_proxmox/sdn_mixin.py::ProxmoxSDNMixin.set_vlan": 70, "napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.get_disk_smart": 74, - "napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114, + "napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 113, "napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vm_interfaces": 179, "napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 220, diff --git a/tests/test_set_hostname.py b/tests/test_set_hostname.py index 3911d85..bf6dc36 100644 --- a/tests/test_set_hostname.py +++ b/tests/test_set_hostname.py @@ -42,8 +42,8 @@ class _Node: return "" def written_hosts(self) -> str: - write = next(c for c in self.ran if c.rstrip().endswith("chmod 644 /etc/hosts")) - return base64.b64decode(re.match(r"echo (\S+) \| base64 -d", write).group(1)).decode() + write = next(c for c in self.ran if "/etc/hosts" in c and "base64 -d" in c) + return base64.b64decode(re.search(r"echo (\S+) \| base64 -d", write).group(1)).decode() @pytest.fixture @@ -151,3 +151,30 @@ def test_no_command_runs_what_the_name_holds(driver, node, name): for command in node.ran: words = list(shlex.shlex(command, posix=True, punctuation_chars=True)) assert "reboot" not in words, command + + +# ── /etc/hosts is written through a private temporary file (#66, NetOrk/netork#2446) ── +# +# The new file was written as root to the fixed /tmp/_netork_hosts and moved over +# /etc/hosts: a local user could create that file (or a symlink) beforehand and own +# /etc/hosts afterwards. It is now a mktemp file beside the target, in root's /etc. + + +def _hosts_write(node: _Node) -> str: + return next(c for c in node.ran if "/etc/hosts" in c and "base64 -d" in c) + + +def test_hosts_goes_through_a_mktemp_file_in_etc(driver, node): + driver.set_hostname("new") + + write = _hosts_write(node) + assert "/tmp/" not in write + assert write.startswith("t=$(mktemp /etc/.hosts.XXXXXX) && ") + assert ' | base64 -d > "$t" && chmod 644 "$t" && mv "$t" /etc/hosts' in write + assert write.endswith(' || { rm -f "$t"; false; }') + + +def test_what_is_written_is_still_the_new_hosts(driver, node): + driver.set_hostname("new") + + assert "10.0.0.1 new.old.lan new\n" in node.written_hosts()