From a67e8492ee9e04327afd045c07b2d0eea50ad1e3 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 22:33:38 +0100 Subject: [PATCH 1/3] fix(system): set_hostname writes /etc/hosts through a mktemp file in /etc (#66) 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. --- CHANGELOG.md | 5 +++++ napalm_proxmox/system_mixin.py | 5 ++--- tests/test_set_hostname.py | 31 +++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acc6f36..9aeb309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 7f1c2a8..34ec20c 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -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 ───────────────────────────────────────────── diff --git a/tests/test_set_hostname.py b/tests/test_set_hostname.py index b5f86ab..e245795 100644 --- a/tests/test_set_hostname.py +++ b/tests/test_set_hostname.py @@ -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() -- 2.54.0 From 83d1ef780e2f6c885d6f4a34d92eadd15c914fc8 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Fri, 13 Nov 2026 08:55:37 +0100 Subject: [PATCH 2/3] test(time-sync): follow napalm-device-types 4.36.0, which keeps DHCP's servers napalm-device-types 4.36.0 (released after main's last CI run) changed PosixTimeSyncMixin: get_time_sync() reports DHCP-provided servers apart (dhcp_ntp_servers), and configure_time_sync() no longer comments out the DHCP client's chrony sourcedir; it gives back one that 4.35.0 took out (NetOrk/netork#2380). CI installs the newest release, so five time-sync tests failed on every branch. The tests now expect that behaviour, read DHCP's servers apart, and check that a node 4.35.0 aligned gets its DHCP sourcedir back. The floor moves to 4.36.0, so the lowest-versions job tests the same behaviour; README, CHANGELOG and the module docstring say what a write leaves alone. --- CHANGELOG.md | 15 ++++++----- README.md | 4 +-- napalm_proxmox/time_sync.py | 5 ++-- pyproject.toml | 2 +- tests/test_time_sync.py | 51 ++++++++++++++++++++++++++++++++----- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aeb309..134962d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,13 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Time sync on both drivers (NetOrk/netork#2365), through napalm-device-types' - `TimeSyncMixin` (4.35.0); both write the servers and the zone. Neither API keeps time + `TimeSyncMixin` (4.36.0); both write the servers and the zone. Neither API keeps time servers -- both products run chrony on Debian -- so `get_time_sync()` reads chrony's - sources, whether it is synchronised and the zone (`timedatectl`) in one round trip over - the driver's SSH session (`PosixTimeSyncMixin`), and the zone from `GET - /nodes/{node}/time` only where the host names none. `configure_time_sync()` writes the - servers to `/etc/chrony/sources.d/netork.sources` with every other source commented out - (Debian's vendor pool, the DHCP `sourcedir`) and reloads or restarts chrony; the zone + sources, the ones DHCP gave apart (`dhcp_ntp_servers`), whether it is synchronised and + the zone (`timedatectl`) in one round trip over the driver's SSH session + (`PosixTimeSyncMixin`), and the zone from `GET /nodes/{node}/time` only where the host + names none. `configure_time_sync()` writes the servers to + `/etc/chrony/sources.d/netork.sources` with every other configured source commented out + (Debian's vendor pool; the DHCP `sourcedir` stays) and reloads or restarts chrony; the zone goes through `PUT /nodes/{node}/time`, Proxmox's own call, so `/etc/timezone` and the web UI follow `/etc/localtime`. The token needs `Sys.Modify` for it (PVE on `/nodes/{node}`, PBS on `/system/time`). Only the node of the connection is read and @@ -45,7 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 in its own daemons; `get_clusters()` and `get_backup_jobs()` keep them. ### Changed -- Requires napalm-device-types 4.35.0. +- Requires napalm-device-types 4.36.0. ### Fixed - `set_hostname()` writes the new `/etc/hosts` to a `mktemp` file beside it diff --git a/README.md b/README.md index 53d7826..6c6a1c3 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,8 @@ device = driver( | `get_scheduled_jobs` | cron, `/etc/cron.d`, user crontabs, run-parts, anacron, systemd timers, as root over SSH (napalm-device-types' `PosixScheduledJobsMixin`); `pve-*` units, `pvesr.timer` and `/etc/cron.d/vzdump` are `platform` | | `manage_scheduled_job(key, action, digest=)` | enable, disable, run now; a `/etc/cron.d/vzdump` line only runs | | `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | an application's own job, as a systemd timer | -| `get_time_sync` | chrony's sources and whether it is synchronised, the zone from `timedatectl`, in one round trip over SSH (napalm-device-types' `PosixTimeSyncMixin`); the zone from `GET /nodes/{node}/time` only where the host names none | -| `configure_time_sync(ntp_servers=, timezone=)` | servers to `/etc/chrony/sources.d/netork.sources`, every other source commented out, then a reload or a restart of chrony; the zone through `PUT /nodes/{node}/time` (the token needs `Sys.Modify` on `/nodes/{node}`), so `/etc/timezone` and the web UI follow. Only this node: in a cluster each node keeps its own clock | +| `get_time_sync` | chrony's sources, the ones DHCP gave apart (`dhcp_ntp_servers`), whether it is synchronised, the zone from `timedatectl`, in one round trip over SSH (napalm-device-types' `PosixTimeSyncMixin`); the zone from `GET /nodes/{node}/time` only where the host names none | +| `configure_time_sync(ntp_servers=, timezone=)` | servers to `/etc/chrony/sources.d/netork.sources`, every other configured source commented out (DHCP's `sourcedir` stays), then a reload or a restart of chrony; the zone through `PUT /nodes/{node}/time` (the token needs `Sys.Modify` on `/nodes/{node}`), so `/etc/timezone` and the web UI follow. Only this node: in a cluster each node keeps its own clock | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, diff --git a/napalm_proxmox/time_sync.py b/napalm_proxmox/time_sync.py index efaab2c..35d3dac 100644 --- a/napalm_proxmox/time_sync.py +++ b/napalm_proxmox/time_sync.py @@ -3,8 +3,9 @@ Neither API keeps time servers: ``/nodes/{node}/time`` knows only the zone, and both products run chrony on Debian. So the servers are :class:`~napalm_device_types.PosixTimeSyncMixin`'s, over the driver's SSH session -(``run_command``, root): read from chrony's configuration, written to -``/etc/chrony/sources.d/netork.sources`` with every other source commented out. +(``run_command``, root): read from chrony's configuration, DHCP's apart, written to +``/etc/chrony/sources.d/netork.sources`` with every other configured source commented +out; the DHCP client's ``sourcedir`` stays. The zone is read with them, from the host (``timedatectl``, the zone the node's clock really runs in), and the API's ``GET /nodes/{node}/time`` answers only when diff --git a/pyproject.toml b/pyproject.toml index 572c7b9..2f763cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ requires-python = ">=3.12" dependencies = [ "napalm>=5.0.0", - "napalm_device_types>=4.35.0", # TimeSyncMixin, PosixTimeSyncMixin + "napalm_device_types>=4.36.0", # TimeSyncMixin, PosixTimeSyncMixin (DHCP's servers) "paramiko>=5.0.0", # CVE-2026-44405; imported directly for the node's SSH session (_base.py) "proxmoxer>=2.0.0", "netaddr>=0.9.0", diff --git a/tests/test_time_sync.py b/tests/test_time_sync.py index bfaebfc..2268d62 100644 --- a/tests/test_time_sync.py +++ b/tests/test_time_sync.py @@ -69,12 +69,19 @@ _MARK = "#napalm-time-sync: " def _reading( - zone: str = "Europe/Berlin", *, servers: tuple[str, ...] = (), timedatectl: bool = True + zone: str = "Europe/Berlin", + *, + servers: tuple[str, ...] = (), + dhcp: tuple[str, ...] = (), + deaf_to_dhcp: bool = False, + timedatectl: bool = True, ) -> str: """What the reading prints; *servers* in netOrk's sources file, the vendor's pool - commented out with it (as a write leaves the host).""" + commented out with it (as a write leaves the host), *dhcp* in the DHCP client's + sources file. *deaf_to_dhcp* is a host napalm-device-types 4.35.0 aligned: its write + commented out the DHCP ``sourcedir`` too.""" written = bool(servers) - conf = _CHRONY_CONF.format(pool=_MARK if written else "", dhcp=_MARK if written else "") + conf = _CHRONY_CONF.format(pool=_MARK if written else "", dhcp=_MARK if deaf_to_dhcp else "") parts = ["@@ts timedatectl"] if timedatectl: parts += [ @@ -89,6 +96,10 @@ def _reading( parts += ["@@ts zone", zone if timedatectl else ""] parts += ["@@ts active", _ACTIVE_CHRONY, "@@ts timesync", "@@ts chronyc", _CHRONYC_SYNCED] parts += ["@@ts chrony 0 main /etc/chrony/chrony.conf", conf, ""] + if dhcp: + parts += ["@@ts chrony 11 sourcedir /run/chrony-dhcp/vmbr0.sources"] + parts += [f"server {s} iburst" for s in dhcp] + parts += [""] if written: parts += ["@@ts chrony 14 sourcedir /etc/chrony/sources.d/netork.sources"] parts += [f"{'pool' if '.pool.' in s else 'server'} {s} iburst" for s in servers] @@ -169,6 +180,7 @@ def test_pve_reads_chrony_and_the_zone_in_one_round_trip() -> None: assert driver.get_time_sync() == { "ntp_servers": ["2.debian.pool.ntp.org"], + "dhcp_ntp_servers": [], "timezone": "Europe/Berlin", "synchronized": True, "daemon": "chrony", @@ -183,6 +195,7 @@ def test_pbs_reads_chrony_and_the_zone() -> None: assert driver.get_time_sync() == { "ntp_servers": ["10.0.0.1"], + "dhcp_ntp_servers": [], "timezone": "Europe/Vienna", "synchronized": True, "daemon": "chrony", @@ -191,6 +204,17 @@ def test_pbs_reads_chrony_and_the_zone() -> None: assert not [c for c in api.calls if c[0] != "GET"] +def test_servers_from_dhcp_are_read_apart_from_the_configured_ones() -> None: + """A node that gets its time giver by DHCP (option 42): chrony reads the DHCP + client's sources file, and those servers are not the node's own (NetOrk/netork#2380).""" + driver, _, _ = _pve(_reading(dhcp=("192.168.1.1",))) + + reading = driver.get_time_sync() + + assert reading["ntp_servers"] == ["2.debian.pool.ntp.org"] + assert reading["dhcp_ntp_servers"] == ["192.168.1.1"] + + def test_a_host_without_a_zone_of_its_own_reads_the_apis() -> None: """No timedatectl and no /etc/timezone or /etc/localtime link: the API's word.""" driver, api, _ = _pve(_reading(timedatectl=False)) @@ -315,10 +339,25 @@ def test_servers_go_to_chronys_sources_d_and_the_vendor_pool_stops_counting(make assert any( "/etc/chrony/sources.d/netork.sources" in c and c.startswith("install") for c in root ) - # line 8 is the vendor's pool, line 11 the DHCP sourcedir: both in the main file, - # which chronyd reads only when it starts. + # line 8 is the vendor's pool, in the main file, which chronyd reads only when it + # starts. Line 11, the DHCP client's sourcedir, stays: DHCP's servers count. sed = next(c for c in root if c.startswith("sed -i")) - assert "8{" in sed and "11{" in sed and sed.endswith("/etc/chrony/chrony.conf") + assert "8{" in sed and "11{" not in sed and sed.endswith("/etc/chrony/chrony.conf") + assert root[-1] == "systemctl restart chrony.service" + + +def test_a_write_gives_back_the_dhcp_sourcedir_an_earlier_write_took() -> None: + """A node napalm-device-types 4.35.0 aligned hears DHCP again (NetOrk/netork#2380).""" + servers = ("10.0.0.1",) + driver, _, shell = _pve(_reading(servers=servers, deaf_to_dhcp=True), _reading(servers=servers)) + + result = driver.configure_time_sync(ntp_servers=list(servers)) + + assert result["success"], result["output"] + root = shell.root() + sed = next(c for c in root if c.startswith("sed -i")) + assert "11{s/^#napalm-time-sync: (sourcedir" in sed + assert sed.endswith("/etc/chrony/chrony.conf") assert root[-1] == "systemctl restart chrony.service" -- 2.54.0 From 610d6f1093dc0ff413116c5e9358a8fa2c8f6f1e Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Fri, 13 Nov 2026 08:55:48 +0100 Subject: [PATCH 3/3] chore(ratchet): set_hostname is a line shorter (#66) The mktemp write of /etc/hosts made set_hostname one line shorter, and the quality ratchet fails on a baseline entry that is no longer reached (stale). Lowered with napalm-standard ratchet --write. --- tests/quality_baseline.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, -- 2.54.0