fix(system): set_hostname writes /etc/hosts through a mktemp file in /etc (#66)
driver / standard (pull_request) Failing after 18s
driver / lint (pull_request) Successful in 17s
driver / types (pull_request) Successful in 1m1s
driver / test (3.12) (pull_request) Failing after 1m8s
driver / test (3.13) (pull_request) Failing after 57s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m7s
CI / driver (pull_request) Failing after 2m48s
driver / publish (pull_request) Skipped

The new /etc/hosts 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: root's write went into the user's file, and the
user owned /etc/hosts afterwards (NetOrk/netork#2446).

The file is now made with mktemp beside the target (/etc/.hosts.XXXXXX,
root's directory, same filesystem), written, set to 644 and moved into
place; a failed step removes it.
This commit is contained in:
Christian Manivong committed 2026-11-12 22:33:38 +01:00
1 parent 042b21d3d9
commit a67e8492ee
3 files changed
+36 -5

No files matched your search

+5
View File
@@ -48,6 +48,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Requires napalm-device-types 4.35.0.
### Fixed
- `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
@@ -441,9 +441,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 ─────────────────────────────────────────────
+29 -2
View File
@@ -41,8 +41,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
@@ -105,3 +105,30 @@ def test_an_unreadable_cluster_check_refuses_too(driver, node):
driver.set_hostname("new")
assert [c for c in node.ran if "corosync.conf" not in c] == []
# ── /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()