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
181 lines
5.6 KiB
Python
181 lines
5.6 KiB
Python
"""set_hostname: what it writes to /etc/hosts, and that it leaves a clustered node alone
|
|
(napalm-proxmox#21)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import re
|
|
import shlex
|
|
|
|
import pytest
|
|
from napalm.base.exceptions import CommandErrorException
|
|
|
|
from napalm_proxmox import ProxmoxDriver
|
|
|
|
HOSTS = """\
|
|
127.0.0.1 localhost.localdomain localhost
|
|
# pve is the node; pve-backup its backup target
|
|
10.0.0.1 pve.old.lan pve
|
|
10.0.0.2 pve-backup.old.lan pve-backup
|
|
"""
|
|
|
|
|
|
class _Node:
|
|
"""The node's shell: /etc/hostname reads "pve", /etc/hosts reads HOSTS.
|
|
|
|
*cluster_check* is what the test for a corosync.conf prints: exit status 1
|
|
(no corosync.conf, a single node) unless a test says otherwise.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self.ran: list[str] = []
|
|
self.cluster_check = "__PVE_RC=1"
|
|
|
|
def __call__(self, command: str) -> str:
|
|
self.ran.append(command)
|
|
if "corosync.conf" in command:
|
|
return self.cluster_check
|
|
if command.startswith("cat /etc/hostname"):
|
|
return "pve\n"
|
|
if command.startswith("cat /etc/hosts"):
|
|
return HOSTS
|
|
return ""
|
|
|
|
def written_hosts(self) -> str:
|
|
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
|
|
def node() -> _Node:
|
|
return _Node()
|
|
|
|
|
|
@pytest.fixture
|
|
def driver(node: _Node) -> ProxmoxDriver:
|
|
d = ProxmoxDriver("pve", "root@pam", "pw")
|
|
d._exec_ssh_command = node
|
|
return d
|
|
|
|
|
|
def test_an_fqdn_renames_the_nodes_names_and_nothing_else(driver, node):
|
|
driver.set_hostname("new.dom.lan")
|
|
|
|
assert node.written_hosts() == (
|
|
"127.0.0.1 localhost.localdomain localhost\n"
|
|
"# pve is the node; pve-backup its backup target\n"
|
|
"10.0.0.1 new.dom.lan new\n"
|
|
"10.0.0.2 pve-backup.old.lan pve-backup\n"
|
|
)
|
|
|
|
|
|
def test_a_short_name_keeps_the_old_domain(driver, node):
|
|
driver.set_hostname("new")
|
|
|
|
assert "10.0.0.1 new.old.lan new\n" in node.written_hosts()
|
|
assert "10.0.0.2 pve-backup.old.lan pve-backup\n" in node.written_hosts()
|
|
|
|
|
|
def test_the_same_name_leaves_hosts_alone(driver, node):
|
|
driver.set_hostname("pve")
|
|
|
|
assert not any("/etc/hosts" in c and "base64 -d" in c for c in node.ran)
|
|
|
|
|
|
def test_a_clustered_node_is_refused_before_anything_changes(driver, node):
|
|
node.cluster_check = "__PVE_RC=0"
|
|
|
|
with pytest.raises(CommandErrorException, match="part of a cluster"):
|
|
driver.set_hostname("new")
|
|
|
|
assert [c for c in node.ran if "corosync.conf" not in c] == []
|
|
|
|
|
|
def test_the_cluster_check_looks_at_both_corosync_confs(driver, node):
|
|
driver.set_hostname("new")
|
|
|
|
check = next(c for c in node.ran if "corosync.conf" in c)
|
|
assert "/etc/pve/corosync.conf" in check
|
|
assert "/etc/corosync/corosync.conf" in check
|
|
|
|
|
|
def test_an_unreadable_cluster_check_refuses_too(driver, node):
|
|
node.cluster_check = ""
|
|
|
|
with pytest.raises(CommandErrorException, match="could not tell"):
|
|
driver.set_hostname("new")
|
|
|
|
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
|
|
# /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()
|