Merge remote-tracking branch 'origin/main' into fix/hosts-tempfile-2446
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 50s
driver / test (3.12) (pull_request) Successful in 58s
driver / test (3.13) (pull_request) Successful in 58s
driver / lowest (pull_request) Successful in 31s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 57s
CI / driver (pull_request) Successful in 2m23s
driver / publish (pull_request) Skipped

This commit is contained in:
Christian Manivong committed 2026-11-13 09:56:57 +01:00
commit a421a595d4
3 files changed
+60 -9

No files matched your search

+4
View File
@@ -49,6 +49,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Requires napalm-device-types 4.36.0.
### Fixed
- `set_hostname()` hands the name to the shell quoted (`shlex.quote`): to `hostname`, to
`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
+10 -9
View File
@@ -23,6 +23,7 @@ from __future__ import annotations
import ipaddress
import logging
import re
import shlex
from typing import Any
from napalm.base.exceptions import CommandErrorException
@@ -452,12 +453,13 @@ class ProxmoxSystemMixin:
)
# ── 4. Postfix myhostname ─────────────────────────────────────────
postfix = shlex.quote(f"myhostname = {fqdn}")
self._exec_ssh_command(
f"command -v postconf >/dev/null 2>&1 && postconf -e 'myhostname = {fqdn}' || true"
f"command -v postconf >/dev/null 2>&1 && postconf -e {postfix} || true"
)
# ── 5. Apply hostname at runtime ─────────────────────────────────
self._exec_ssh_command(f"hostname '{short_hostname}'")
self._exec_ssh_command(f"hostname {shlex.quote(short_hostname)}")
# ── 6. Migrate /etc/pve/nodes/<old> → /etc/pve/nodes/<new> ──────
# Proxmox stores VM and CT configs under /etc/pve/nodes/<nodename>/.
@@ -466,16 +468,15 @@ class ProxmoxSystemMixin:
# Without this step the node boots under the new hostname, finds an
# empty /etc/pve/nodes/<new>/ and all VMs appear missing.
if old_short and old_short != short_hostname:
pve_old = f"/etc/pve/nodes/{old_short}"
pve_new = f"/etc/pve/nodes/{short_hostname}"
pve_old = shlex.quote(f"/etc/pve/nodes/{old_short}")
pve_new = shlex.quote(f"/etc/pve/nodes/{short_hostname}")
migrate_out = self._exec_ssh_command(
f"if [ -d '{pve_old}' ] && [ ! -e '{pve_new}' ]; then"
f" mv '{pve_old}' '{pve_new}'"
f" && echo 'migrated {pve_old} -> {pve_new}';"
f"elif [ -d '{pve_new}' ]; then"
f"if [ -d {pve_old} ] && [ ! -e {pve_new} ]; then"
f" mv {pve_old} {pve_new} && echo migrated {pve_old} '->' {pve_new};"
f"elif [ -d {pve_new} ]; then"
f" echo 'target already exists, skipping migration';"
f"else"
f" echo 'source {pve_old} not found, skipping migration';"
f" echo source {pve_old} 'not found, skipping migration';"
f"fi"
)
logger.info("Proxmox node dir migration: %s", migrate_out)
+46
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import base64
import re
import shlex
import pytest
from napalm.base.exceptions import CommandErrorException
@@ -107,6 +108,51 @@ def test_an_unreadable_cluster_check_refuses_too(driver, node):
assert [c for c in node.ran if "corosync.conf" not in c] == []
# ── the name reaches the shell quoted (NetOrk/netork#2456) ──────────────────
#
# The name went into the root commands between single quotes of their own, so a name
# with a single quote ended them: ``x';reboot;'`` ran ``reboot``. Each command now
# hands it on as one shell word, whatever it holds.
INJECTED = "x';reboot;'"
def _run(node: _Node, start: str) -> str:
return next(c for c in node.ran if c.startswith(start))
def test_the_runtime_hostname_is_one_word(driver, node):
driver.set_hostname(f"{INJECTED}.dom.lan")
assert shlex.split(_run(node, "hostname ")) == ["hostname", INJECTED]
def test_postfix_gets_the_fqdn_as_one_word(driver, node):
driver.set_hostname(f"{INJECTED}.dom.lan")
words = shlex.split(_run(node, "command -v postconf"))
assert words[words.index("-e") + 1] == f"myhostname = {INJECTED}.dom.lan"
def test_the_node_directory_is_moved_by_quoted_paths(driver, node):
driver.set_hostname(INJECTED)
words = shlex.split(_run(node, "if [ -d"))
assert words[words.index("mv") + 1 : words.index("mv") + 3] == [
"/etc/pve/nodes/pve",
f"/etc/pve/nodes/{INJECTED}",
]
@pytest.mark.parametrize("name", [INJECTED, "$(reboot)", "`reboot`", "a b"])
def test_no_command_runs_what_the_name_holds(driver, node, name):
driver.set_hostname(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