fix(system): set_hostname writes /etc/hosts through a mktemp file in /etc (#66) #67

Merged
christianmanivong merged 4 commits from fix/hosts-tempfile-2446 into main 2026-11-13 09:09:28 +00:00
4 changed files with 37 additions and 6 deletions

No files matched your search

+5
View File
@@ -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.
+2 -3
View File
@@ -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 ─────────────────────────────────────────────
+1 -1
View File
@@ -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,
+29 -2
View File
@@ -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()