From bfba804ec261ac04ff46f5347c5ee6e69a76d0fc Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:46:44 +0100 Subject: [PATCH 1/2] fix(vm-provision): quote the image URL in the node's root commands The cloud-image download put image_url, hand-wrapped in single quotes, and the cache path built from its basename unquoted into commands that run as root on the node: a quote, $(...), ; or space in a URL from a release feed was acted on by the shell. The URL and every path built from it now go through shlex.quote (exists check, wget, mv, checksum, rm, unpack, qm importdisk), and the cache file name keeps only the characters Proxmox keeps in a content file name, as the import volume name already did. The checksum algorithm is checked against the ones Proxmox knows, since it is part of the command name. The file-name helpers move to image_names.py; vm_provision_mixin.py would otherwise cross the 1000-line limit. _download_cloud_image drops below 50 lines, so its baseline entry goes. Closes #43 --- CHANGELOG.md | 7 +++ napalm_proxmox/image_names.py | 70 +++++++++++++++++++++ napalm_proxmox/vm_provision_mixin.py | 93 +++++++++------------------- tests/quality_baseline.json | 1 - tests/test_vm_import_download.py | 6 +- tests/test_vm_provision_mixin.py | 84 +++++++++++++++++++++++++ 6 files changed, 192 insertions(+), 69 deletions(-) create mode 100644 napalm_proxmox/image_names.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f8a18..7ff8e4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 its cause, so the caller can remove it with `destroy_vm()`. Failures there that were a `ValueError` (no snippet storage, a NIC without a bridge) are its cause now. Needs `VMProvisionIncomplete` from napalm-device-types (#28). +- `create_vm_from_cloud_init()` quotes the image URL and every path built from it in + the commands it runs as root on the node (download, checksum, unpack, disk + import). A URL with a quote, `$(…)`, `;` or a space no longer reaches the shell: + the node's cache file name keeps only the characters Proxmox keeps in a file + name, the others become `_`. A checksum algorithm other than md5, sha1, sha224, + sha256, sha384 or sha512 is refused with a `ValueError` before anything runs on + the node (#43). ## [0.8.0] - 2026-10-23 diff --git a/napalm_proxmox/image_names.py b/napalm_proxmox/image_names.py new file mode 100644 index 0000000..5441937 --- /dev/null +++ b/napalm_proxmox/image_names.py @@ -0,0 +1,70 @@ +"""File names and checksums of the cloud images a VM is provisioned from. + +The image URL comes from whoever publishes the image (a release feed), so +nothing taken from it reaches a file name on the node unless Proxmox would keep +it in a content file name too. +""" + +from __future__ import annotations + +import hashlib +import re + +# Proxmox reads an import volume's format off its extension and accepts only +# these. Ubuntu ships its qcow2 cloud images as ".img", so that is stored as +# qcow2: if an .img is really raw, the import fails loudly, whereas guessing +# raw for a qcow2 would attach the qcow2 container as a raw disk silently. +_IMPORT_EXTENSIONS = {"qcow2": "qcow2", "raw": "raw", "vmdk": "vmdk", "img": "qcow2"} + +# Characters Proxmox keeps in a content file name (PVE::Storage's +# SAFE_CHAR_CLASS_RE); anything else it would rewrite behind our back. +_UNSAFE_FILENAME_CHARS = re.compile(r"[^A-Za-z0-9\-.+=_]") + +# The checksum algorithms Proxmox's download-url accepts; each has a coreutils +# "sum" on the node. +_CHECKSUM_ALGORITHMS = frozenset({"md5", "sha1", "sha224", "sha256", "sha384", "sha512"}) + + +def url_key(image_url: str) -> str: + """Short hash of the full URL — Ubuntu and others publish a new build + under the same basename every day, so the basename alone is no cache key.""" + return hashlib.sha256(image_url.encode()).hexdigest()[:12] + + +def _basename(image_url: str) -> str: + return image_url.rstrip("/").rsplit("/", 1)[-1] + + +def split_checksum(image_checksum: str) -> tuple[str, str]: + """``":"`` as ``(algo, hex)``; the algorithm defaults to sha256. + + :raises ValueError: for an algorithm Proxmox does not know. + """ + algo, _, expected = image_checksum.partition(":") + algo = (algo or "sha256").lower() + if algo not in _CHECKSUM_ALGORITHMS: + raise ValueError(f"unknown checksum algorithm {algo!r}") + return algo, expected + + +def cache_name(image_url: str) -> str: + """The file name *image_url* gets in the node's image cache. + + The URL's hash, then its basename with every character Proxmox would not + keep replaced by ``_``. + """ + return f"{url_key(image_url)}-{_UNSAFE_FILENAME_CHARS.sub('_', _basename(image_url))}" + + +def import_volume_name(image_url: str) -> str | None: + """The file name *image_url* gets in an import storage. + + None when Proxmox cannot import the image's type at all (compressed, + ISO, no extension) — provisioning then downloads it over SSH as before. + """ + stem, dot, ext = _basename(image_url).rpartition(".") + extension = _IMPORT_EXTENSIONS.get(ext.lower()) if dot and stem else None + if extension is None: + return None + safe_stem = _UNSAFE_FILENAME_CHARS.sub("_", stem) + return f"netork-{url_key(image_url)}-{safe_stem}.{extension}" diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 87beb7c..9d1d3e8 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -3,10 +3,10 @@ from __future__ import annotations import base64 -import hashlib import ipaddress import logging import re +import shlex import time from typing import TYPE_CHECKING, Any from urllib.parse import quote @@ -26,6 +26,7 @@ from napalm_device_types.provisioning import ( split_compression, ) +from napalm_proxmox.image_names import cache_name, import_volume_name, split_checksum from napalm_proxmox.network_targets import bridge_targets, vnet_target from napalm_proxmox.utils import cpu_flags @@ -81,12 +82,6 @@ _HOST_CPU_DESCRIPTION = ( # download cost once. _IMAGE_CACHE_DIR = "/var/lib/vz/template/netork-images" -# Proxmox reads an import volume's format off its extension and accepts only -# these. Ubuntu ships its qcow2 cloud images as ".img", so that is stored as -# qcow2: if an .img is really raw, the import fails loudly, whereas guessing -# raw for a qcow2 would attach the qcow2 container as a raw disk silently. -_IMPORT_EXTENSIONS = {"qcow2": "qcow2", "raw": "raw", "vmdk": "vmdk", "img": "qcow2"} - # The VM shell each guest needs. OpenBSD's qemu-ga cannot use virtio-serial, # and over ISA serial it answers only as the second port (cua01): the OpenBSD # cloud image keeps its console on com0, which serial0 takes (netork#793). @@ -99,37 +94,6 @@ _GUEST_VM_SETTINGS: dict[str, dict[str, str]] = { # A NIC as Proxmox reports it: "virtio=BC:24:11:AA:BB:02,bridge=vmbr0". _NIC_MAC = re.compile(r"^[a-z0-9]+=([0-9A-Fa-f:]{17})") -# Characters Proxmox keeps in a content file name (PVE::Storage's -# SAFE_CHAR_CLASS_RE); anything else it would rewrite behind our back. -_UNSAFE_FILENAME_CHARS = re.compile(r"[^A-Za-z0-9\-.+=_]") - - -def _url_key(image_url: str) -> str: - """Short hash of the full URL — Ubuntu and others publish a new build - under the same basename every day, so the basename alone is no cache key.""" - return hashlib.sha256(image_url.encode()).hexdigest()[:12] - - -def _split_checksum(image_checksum: str) -> tuple[str, str]: - """``":"`` as ``(algo, hex)``; the algorithm defaults to sha256.""" - algo, _, expected = image_checksum.partition(":") - return (algo or "sha256").lower(), expected - - -def _import_volume_name(image_url: str) -> str | None: - """The file name *image_url* gets in an import storage. - - None when Proxmox cannot import the image's type at all (compressed, - ISO, no extension) — provisioning then downloads it over SSH as before. - """ - basename = image_url.rstrip("/").rsplit("/", 1)[-1] - stem, dot, ext = basename.rpartition(".") - extension = _IMPORT_EXTENSIONS.get(ext.lower()) if dot and stem else None - if extension is None: - return None - safe_stem = _UNSAFE_FILENAME_CHARS.sub("_", stem) - return f"netork-{_url_key(image_url)}-{safe_stem}.{extension}" - def _nic_config_args(nics: list[dict[str, Any]]) -> dict[str, str]: """``{"net": "virtio[=],bridge=[,tag=|,trunks=]"}`` per NIC.""" @@ -197,33 +161,26 @@ class ProxmoxVMProvisionMixin: once (covers a corrupted/partial transfer or a stale same-keyed file) before raising. """ - filename = image_url.rstrip("/").rsplit("/", 1)[-1] - local_path = f"{_IMAGE_CACHE_DIR}/{_url_key(image_url)}-{filename}" - - algo, expected = _split_checksum(image_checksum or "") + local_path = f"{_IMAGE_CACHE_DIR}/{cache_name(image_url)}" + algo, expected = split_checksum(image_checksum or "") max_attempts = 2 for attempt in range(1, max_attempts + 1): - exists = self._run_node_command( - f"mkdir -p {_IMAGE_CACHE_DIR} && test -f {local_path} " - f"&& echo EXISTS || echo MISSING", - timeout=30, - ) - if "EXISTS" not in exists: + if not self._cached_on_node(local_path): self._fetch_image(image_url, local_path, timeout) if not image_checksum: return local_path actual = self._run_node_command( - f"{algo}sum {local_path} | awk '{{print $1}}'", timeout=60 + f"{algo}sum {shlex.quote(local_path)} | awk '{{print $1}}'", timeout=60 ) if actual.lower() == expected.lower(): return local_path # Remove the bad file so the next attempt re-downloads instead of # reusing it. - self._run_node_command(f"rm -f {local_path}", timeout=30) + self._run_node_command(f"rm -f {shlex.quote(local_path)}", timeout=30) if attempt == max_attempts: raise RuntimeError( f"Checksum mismatch for {image_url}: expected {expected}, got {actual}" @@ -235,6 +192,15 @@ class ProxmoxVMProvisionMixin: raise AssertionError("unreachable") # loop always returns or raises above + def _cached_on_node(self, path: str) -> bool: + """Whether *path* is already in the node's image cache (made if missing).""" + found = self._run_node_command( + f"mkdir -p {shlex.quote(_IMAGE_CACHE_DIR)} && test -f {shlex.quote(path)} " + "&& echo EXISTS || echo MISSING", + timeout=30, + ) + return "EXISTS" in found + def _fetch_image(self, image_url: str, local_path: str, timeout: int) -> None: """Download *image_url* to *local_path* on the node, through a ``.tmp``. @@ -242,9 +208,10 @@ class ProxmoxVMProvisionMixin: reaches the error, and a failed download leaves no partial ``.tmp``. """ _logger.info(f"Downloading cloud image {image_url} -> {local_path}") - tmp = f"{local_path}.tmp" + tmp = shlex.quote(f"{local_path}.tmp") + path = shlex.quote(local_path) self._run_node_command( - f"wget -nv -O {tmp} '{image_url}' && mv {tmp} {local_path} " + f"wget -nv -O {tmp} {shlex.quote(image_url)} && mv {tmp} {path} " f"|| {{ s=$?; rm -f {tmp}; exit $s; }}", timeout=timeout, ) @@ -257,22 +224,19 @@ class ProxmoxVMProvisionMixin: the unpacked file is kept. Proxmox's download-url unpacks ISOs only, so a packed image always comes this way. """ - filename = image_url.rstrip("/").rsplit("/", 1)[-1] - unpacked_name, unpack = split_compression(filename) + unpacked_name, unpack = split_compression(cache_name(image_url)) if unpack is None: return self._download_cloud_image(image_url, image_checksum, timeout=timeout) - unpacked = f"{_IMAGE_CACHE_DIR}/{_url_key(image_url)}-{unpacked_name}" - cached = self._run_node_command( - f"mkdir -p {_IMAGE_CACHE_DIR} && test -f {unpacked} && echo EXISTS || echo MISSING", - timeout=30, - ) - if "EXISTS" in cached: + unpacked = f"{_IMAGE_CACHE_DIR}/{unpacked_name}" + if self._cached_on_node(unpacked): return unpacked packed = self._download_cloud_image(image_url, image_checksum, timeout=timeout) _logger.info(f"Unpacking {packed} -> {unpacked}") + tmp = shlex.quote(f"{unpacked}.tmp") self._run_node_command( - f"{unpack} {packed} > {unpacked}.tmp && mv {unpacked}.tmp {unpacked} && rm -f {packed}", + f"{unpack} {shlex.quote(packed)} > {tmp} && mv {tmp} {shlex.quote(unpacked)} " + f"&& rm -f {shlex.quote(packed)}", timeout=timeout, ) return unpacked @@ -344,7 +308,7 @@ class ProxmoxVMProvisionMixin: params: dict[str, Any] = {"url": image_url, "content": "import", "filename": filename} if image_checksum: - algo, expected = _split_checksum(image_checksum) + algo, expected = split_checksum(image_checksum) params["checksum"] = expected params["checksum-algorithm"] = algo _logger.info(f"Downloading cloud image {image_url} into {volid}") @@ -369,7 +333,8 @@ class ProxmoxVMProvisionMixin: local_path = self._cloud_image_on_node(image_url, image_checksum, download_timeout) _logger.info(f"Importing {local_path} into VM {vmid} on storage {image_storage}") self._run_node_command( - f"qm importdisk {vmid} {local_path} {image_storage} --format qcow2", + f"qm importdisk {int(vmid)} {shlex.quote(local_path)} {shlex.quote(image_storage)} " + "--format qcow2", timeout=timeout, ) @@ -401,7 +366,7 @@ class ProxmoxVMProvisionMixin: verifies and copies the image itself — and over SSH otherwise. """ import_storage = self._find_import_storage() - filename = _import_volume_name(image_url) if import_storage else None + filename = import_volume_name(image_url) if import_storage else None if not import_storage or not filename: self._import_over_ssh( vmid, image_storage, image_url, image_checksum, download_timeout, timeout diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 97dd17c..52e528b 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -37,7 +37,6 @@ "napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114, "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._download_cloud_image": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.create_vm_from_cloud_init": 220, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92 diff --git a/tests/test_vm_import_download.py b/tests/test_vm_import_download.py index 1167bff..3479912 100644 --- a/tests/test_vm_import_download.py +++ b/tests/test_vm_import_download.py @@ -10,10 +10,8 @@ from __future__ import annotations from unittest.mock import MagicMock, patch -from napalm_proxmox.vm_provision_mixin import ( - ProxmoxVMProvisionMixin, - _import_volume_name, -) +from napalm_proxmox.image_names import import_volume_name as _import_volume_name +from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin _UBUNTU = ( "https://cloud-images.ubuntu.com/releases/26.04/release/ubuntu-26.04-server-cloudimg-amd64.img" diff --git a/tests/test_vm_provision_mixin.py b/tests/test_vm_provision_mixin.py index 98df5e0..ec5c56c 100644 --- a/tests/test_vm_provision_mixin.py +++ b/tests/test_vm_provision_mixin.py @@ -1417,3 +1417,87 @@ def test_download_cloud_image_does_not_silence_wget(): download = mixin._run_node_command.call_args_list[1][0][0] assert download.startswith("wget -nv ") + + +# --------------------------------------------------------------------------- +# The image URL in root shell commands (#43) +# --------------------------------------------------------------------------- + +# A URL a tampered release feed could publish: a quote that ends hand-written +# quoting, a command substitution and a space, each of which the node's shell +# would act on if the URL or the file name built from it went in unquoted. +_HOSTILE_URL = "https://example.org/$(reboot)/a b/x';reboot;'.img" + +# reboot() records that the shell ran it; wget() saves the URL it was given as +# the "image", so the file's content shows whether the URL arrived as one argument. +_HOSTILE_PRELUDE = ( + 'reboot() { echo injected >> "$MARK"; }; ' + "wget() { " + "while [ $# -gt 0 ]; do " + '[ "$1" = -O ] && o=$2; [ $# -eq 1 ] && u=$1; shift; done; ' + 'printf \'%s\' "$u" > "$o"; }; ' + # The unpack step of a packed image: "xz -dc " as a plain copy. + 'xz() { cat "$2"; }; ' +) + + +def _hostile_shell(tmp_path, monkeypatch) -> ProxmoxVMProvisionMixin: + import napalm_proxmox.vm_provision_mixin as module + + cache = tmp_path / "cache" + monkeypatch.setattr(module, "_IMAGE_CACHE_DIR", str(cache)) + monkeypatch.setenv("MARK", str(tmp_path / "injected")) + mixin = ProxmoxVMProvisionMixin() + mixin._node_ssh = MagicMock(return_value=_LocalShell(_HOSTILE_PRELUDE)) + return mixin + + +def test_a_hostile_image_url_stays_one_argument_in_every_command(tmp_path, monkeypatch): + import hashlib + from pathlib import Path + + mixin = _hostile_shell(tmp_path, monkeypatch) + checksum = "sha256:" + hashlib.sha256(_HOSTILE_URL.encode()).hexdigest() + + path = mixin._download_cloud_image(_HOSTILE_URL, checksum, timeout=10) + + assert not (tmp_path / "injected").exists() + assert Path(path).read_text() == _HOSTILE_URL # wget got the whole URL + assert Path(path).parent == tmp_path / "cache" + + +def test_the_cache_file_name_keeps_only_safe_characters(tmp_path, monkeypatch): + import re + from pathlib import Path + + mixin = _hostile_shell(tmp_path, monkeypatch) + + path = mixin._download_cloud_image(_HOSTILE_URL, None, timeout=10) + + assert re.fullmatch(r"[A-Za-z0-9._+=-]+", Path(path).name) + assert Path(path).name.endswith("-x__reboot__.img") + + +def test_a_packed_hostile_image_is_unpacked_without_running_anything(tmp_path, monkeypatch): + from pathlib import Path + + mixin = _hostile_shell(tmp_path, monkeypatch) + url = _HOSTILE_URL + ".xz" + + path = mixin._cloud_image_on_node(url, None, timeout=10) + + assert not (tmp_path / "injected").exists() + assert Path(path).read_text() == url + assert not path.endswith(".xz") + + +def test_a_checksum_algorithm_outside_the_known_ones_is_refused(): + mixin = ProxmoxVMProvisionMixin() + mixin._run_node_command = MagicMock(return_value="EXISTS") + + with pytest.raises(ValueError, match="checksum algorithm"): + mixin._download_cloud_image( + "https://example.org/img/debian.qcow2", "sha256;reboot;:abc", timeout=10 + ) + + assert not any("reboot" in c.args[0] for c in mixin._run_node_command.call_args_list) From daf1b519eb52874d6c62f4626811b6b035209be7 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:52:32 +0100 Subject: [PATCH 2/2] fix(ssh): raise when a node command cannot run, drop the execute call _exec_ssh_command returned "" on any failure (no SSH credential, a refused login or host key, an unreachable node, a timeout), so a getter that could not look read like one that found nothing; on API-token-only nodes get_snmp_config answered None and the SSH-backed getters empty lists on every poll. It now raises ConnectionException with the cause (SshHostKeyMismatch as it is), and refuses before connecting when there is neither a password nor a usable key. It returned stderr when stdout was empty; output is stdout now. It first posted every command to /nodes/{node}/execute, which runs a batch of API calls rather than a shell; that dead round trip is gone. The key types, the key before the password, the host-key check and the single connect path were done in f491f9f and 6bb3035. The shared transport is NAPALM/napalm-device-types#35. Closes #17 --- CHANGELOG.md | 9 +++ README.md | 19 +++-- napalm_proxmox/config_mixin.py | 6 +- napalm_proxmox/driver.py | 72 ++++++++++-------- napalm_proxmox/interfaces_mixin.py | 6 +- napalm_proxmox/routing_mixin.py | 4 +- napalm_proxmox/system_mixin.py | 3 +- napalm_proxmox/vm_provision_mixin.py | 8 +- tests/conftest.py | 38 +++++++++- tests/test_misc.py | 74 ++++++++---------- tests/test_node_exec.py | 109 +++++++++++++++++++++++++++ tests/test_ovs_and_arp.py | 10 +-- tests/test_ssh_host_key.py | 11 +-- 13 files changed, 261 insertions(+), 108 deletions(-) create mode 100644 tests/test_node_exec.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff8e4c..d0976c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,6 +120,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 name, the others become `_`. A checksum algorithm other than md5, sha1, sha224, sha256, sha384 or sha512 is refused with a `ValueError` before anything runs on the node (#43). +- Commands on the node (ARP, IPv6 neighbours, packages, users, NTP, SNMP config, + services, kernel facts, `cli()`, …) raise `ConnectionException` when they cannot run: + no SSH password or key (a node reached with an API token only), a refused login, an + unreachable node, a timeout; a changed host key raises `SshHostKeyMismatch` as + before. They returned empty output, so "could not look" read as "nothing there": + `get_snmp_config()` answered None on token-only nodes and the getters empty lists. + Only what a command prints on stdout is its output; stderr no longer stands in when + stdout is empty. Each command goes straight to SSH: the driver first posted it to + `/nodes/{node}/execute`, which runs API calls, not a shell, and always failed (#17). ## [0.8.0] - 2026-10-23 diff --git a/README.md b/README.md index ab36f60..683df97 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ device = driver( | `get_interfaces_ip` | IPv4/IPv6 from classic ifaces + SDN subnet gateways | | `get_interfaces_counters` | From `node/netstat` RRD data | | `get_environment` | CPU %, memory (bytes), hardware temperature sensors | -| `get_arp_table` | Reads `/proc/net/arp` via exec endpoint | +| `get_arp_table` | Reads `/proc/net/arp` over SSH | | `get_mac_address_table` | `bridge fdb show` (Linux) + `ovs-appctl fdb/show` (OVS) | | `get_vlans` | SDN VNet tags + `bridge vlan show` | | `get_network_instances` | SDN zones (VLAN→L2VPN, EVPN→L3VRF) + default instance | @@ -116,13 +116,13 @@ device = driver( | `compare_config` | Unified diff | | `commit_config` | Writes `/etc/network/interfaces` + `ifreload -a` | | `discard_config` / `rollback` | Revert candidate | -| `get_lldp_neighbors` | `lldpcli show neighbors` via exec | +| `get_lldp_neighbors` | `lldpcli show neighbors` over SSH | | `get_lldp_neighbors_detail` | Full LLDP detail | | `get_ipv6_neighbors_table` | `ip -6 neigh show` | -| `ping` | Linux `ping` via exec endpoint | -| `traceroute` | `traceroute -n` via exec endpoint | -| `cli` | Arbitrary command execution via exec endpoint | -| `get_route_to` | `ip route show` via exec endpoint | +| `ping` | Linux `ping` over SSH | +| `traceroute` | `traceroute -n` over SSH | +| `cli` | Arbitrary commands over SSH (stdout) | +| `get_route_to` | `ip route show` over SSH | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, @@ -138,6 +138,13 @@ device = driver( | `node` | str | *auto* | Override target node name | | `token_name` | str | `None` | API token identifier (`user@realm!tokenid`) | | `token_value` | str | `None` | API token secret | +| `ssh_username` | str | *username* | Login for the node's SSH (port 22) | +| `ssh_password` | str | *password* | SSH password; the API password when not set | +| `ssh_private_key_str` | str | `None` | SSH private key (Ed25519, ECDSA or RSA), tried before the password | + +What the API does not offer (ARP, LLDP, packages, SNMP config, `cli`, …) runs in the +node's shell over SSH. Without an SSH password or key — a node reached with an API +token only — those calls raise `ConnectionException` instead of reading as empty. ## Development diff --git a/napalm_proxmox/config_mixin.py b/napalm_proxmox/config_mixin.py index fc72eba..980cba7 100644 --- a/napalm_proxmox/config_mixin.py +++ b/napalm_proxmox/config_mixin.py @@ -114,12 +114,12 @@ class ProxmoxConfigMixin: .. note:: Full programmatic apply requires the Proxmox API to accept raw - interface configs. This implementation uses ``pvesh`` via exec - which requires the node exec endpoint to be available. + interface configs. This implementation uses ``pvesh`` over the + node's SSH session. """ if not self._candidate_config: return - # Write via exec endpoint + # Write over the node's SSH session escaped = self._candidate_config.replace("'", "'\\''") self._exec_ssh_command( f"printf '%s' '{escaped}' > /etc/network/interfaces && " diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 3647fdf..5a1c40a 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -48,7 +48,7 @@ from napalm_device_types import ( SystemdServicesMixin, SystemIdentityMixin, ) -from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch +from napalm_device_types.ssh_host_keys import HostKeyCheck try: from proxmoxer import ProxmoxAPI @@ -299,48 +299,40 @@ class ProxmoxDriver( return [] def _exec_ssh_command(self, command: str) -> str: - """Execute a shell command on the Proxmox node and return output. + """Run *command* in the node's shell over SSH and return what it printed. - Tries the Proxmox API exec endpoint first. If that fails, - falls back to a direct paramiko SSH connection. + Stdout only: stderr is not output (a command that wants it says + ``2>&1``). The exit status is not checked; a caller that needs it asks + for it (``_rc``). The node has no API endpoint that runs a shell command + (``/nodes/{node}/execute`` runs a batch of API calls), so this is the + one way, the same session ``_run_node_command`` uses (see + NAPALM/napalm-device-types#35 for the shared transport). + + :raises ConnectionException: when the command could not run: no SSH + credential, a refused login or host key (``SshHostKeyMismatch``), + an unreachable node, a timeout. """ - import base64 as _b64 - - # Try API exec endpoint - try: - encoded = _b64.b64encode(command.encode()).decode() - result = self._node_api().execute.post("command", f"echo {encoded} | base64 -d | sh") - # On PVE 8.x the exec endpoint returns a dict with 'data' key - if isinstance(result, dict): - raw = result.get("data", result.get("output", "")) - else: - raw = result - if raw: - return str(raw).strip() - except Exception as exc: - logger.debug("API exec failed, falling back to SSH: %s", exc) - - # Fallback: direct paramiko SSH try: _, stdout, stderr = self._node_ssh().exec_command(command, timeout=self.timeout) - err = stderr.read().decode().strip() - out = stdout.read().decode().strip() - return out or err - except ImportError: - logger.warning("paramiko not installed — cannot exec SSH commands") - except SshHostKeyMismatch as exc: - # Refused before any login, and already reported to the caller's store. - logger.warning("%s", exc) - except Exception as exc: - logger.debug("SSH exec command failed: %s", exc) - - return "" + out = stdout.read().decode(errors="replace") + err = stderr.read().decode(errors="replace").strip() + except ConnectionException: + raise + except _ssh_errors() as exc: + raise ConnectionException( + f"Cannot run a command on {self.hostname} over SSH: {exc}" + ) from exc + if err: + logger.debug("%s: stderr of a node command: %s", self.hostname, err[-300:]) + return out.strip() def _node_ssh(self) -> Any: # paramiko.SSHClient; paramiko is imported lazily """The SSH session to the node on port 22, opened once. Its host key is the one the caller expects for port 22, or on first use whichever the node presents; either way it is reported (netork#1167). + + :raises ConnectionException: without a password or a usable key. """ import paramiko @@ -356,6 +348,12 @@ class ProxmoxDriver( pkey = _load_private_key(self._ssh_key) if self._ssh_key else None if pkey is not None: connect_kwargs["pkey"] = pkey + if not password and pkey is None: + # An API-token-only node: nothing to log in with, so no connection. + raise ConnectionException( + f"no SSH credential for {self.hostname}: set ssh_password or " + "ssh_private_key_str" + ) client = paramiko.SSHClient() try: HostKeyCheck(self.optional_args, self.hostname, 22).connect( @@ -528,3 +526,11 @@ def _load_private_key(text: str) -> Any: # paramiko.PKey; paramiko is imported continue logger.warning("The SSH private key could not be read; logging in with the password") return None + + +def _ssh_errors() -> tuple[type[Exception], ...]: + """What a command that could not run raises: the socket's errors, timeouts + included, and paramiko's (a refused login, a closed session).""" + import paramiko + + return (OSError, paramiko.SSHException) diff --git a/napalm_proxmox/interfaces_mixin.py b/napalm_proxmox/interfaces_mixin.py index 0868e37..8e0eac7 100644 --- a/napalm_proxmox/interfaces_mixin.py +++ b/napalm_proxmox/interfaces_mixin.py @@ -160,9 +160,9 @@ class ProxmoxInterfaceMixin: def get_arp_table(self, vrf: str = "") -> list[_JsonDict]: """Return ARP table. - Proxmox does not expose ARP via the REST API directly. We attempt - to read it via the node's ``/proc/net/arp`` through the Proxmox - exec endpoint. If that is unavailable, an empty list is returned. + Proxmox does not expose ARP via the REST API, so the node's + ``/proc/net/arp`` is read over SSH; a node that cannot be reached + there raises. """ raw = self._exec_ssh_command("cat /proc/net/arp") if not raw: diff --git a/napalm_proxmox/routing_mixin.py b/napalm_proxmox/routing_mixin.py index df3ede7..e36d336 100644 --- a/napalm_proxmox/routing_mixin.py +++ b/napalm_proxmox/routing_mixin.py @@ -58,7 +58,7 @@ class ProxmoxRoutingMixin: raw = self._exec_ssh_command(f"{cmd} 2>&1 || true") if not raw: - return {"error": "Ping command not available via exec endpoint"} + return {"error": "ping printed nothing on the node"} # Detect common failure strings before parsing statistics _error_patterns = ( @@ -126,7 +126,7 @@ class ProxmoxRoutingMixin: raw = self._exec_ssh_command(" ".join(cmd_parts) + " 2>&1 || true") if not raw: - return {"error": "traceroute not available via exec endpoint"} + return {"error": "traceroute printed nothing on the node"} hops: _JsonDict = {} for line in raw.splitlines(): diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 2a013bf..bf3e23f 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -504,7 +504,8 @@ class ProxmoxSystemMixin: def get_snmp_config(self): """Return SNMP agent config if snmpd is installed and running on the node. - Uses _exec_ssh_command (Proxmox API exec or SSH) to inspect the node. + Uses _exec_ssh_command (the node's SSH session) to inspect the node; + raises when the node cannot be reached there. Returns a SNMPConfigDict or None. """ try: diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 9d1d3e8..051b082 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -129,10 +129,10 @@ class ProxmoxVMProvisionMixin: """ Execute a shell command on the Proxmox node via SSH, raising on failure. - Unlike ``_exec_ssh_command`` (best-effort, fixed timeout, swallows - errors), this is for critical provisioning steps — image download, - disk import — where a non-zero exit or a caller-specific timeout must - surface as a hard failure rather than an empty string. + Unlike ``_exec_ssh_command`` (fixed timeout, exit status not checked), + this is for critical provisioning steps — image download, disk import — + where a non-zero exit or a caller-specific timeout must surface as a + hard failure. """ _, stdout, stderr = self._node_ssh().exec_command(command, timeout=timeout) exit_status = stdout.channel.recv_exit_status() diff --git a/tests/conftest.py b/tests/conftest.py index b927a36..dd90d37 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -120,6 +120,36 @@ PVE_USERS = [ ] +# --------------------------------------------------------------------------- +# The node's shell +# --------------------------------------------------------------------------- + + +class _Stream: + def __init__(self, data: str) -> None: + self._data = data.encode() + + def read(self) -> bytes: + return self._data + + +class NodeShell: + """The node's SSH session for the driver fixtures. + + Every command prints ``output`` on stdout, or ``outputs[command]`` when the + command is there. A test sets either through ``driver._node_ssh()``. + """ + + def __init__(self, output: str = "") -> None: + self.output = output + self.outputs: dict[str, str] = {} + self.commands: list[str] = [] + + def exec_command(self, command: str, timeout: int | None = None): + self.commands.append(command) + return None, _Stream(self.outputs.get(command, self.output)), _Stream("") + + # --------------------------------------------------------------------------- # Driver fixture # --------------------------------------------------------------------------- @@ -137,7 +167,6 @@ def _build_mock_api( ntp=None, netstat=None, pve_users=None, - exec_return="", sensors=None, cluster_status=None, ): @@ -159,7 +188,6 @@ def _build_mock_api( node_res.dns.get.return_value = dns or DNS_INFO node_res.ntp.get.return_value = ntp or NTP_INFO node_res.netstat.get.return_value = netstat or NODE_NETSTAT - node_res.execute.post.return_value = {"data": exec_return} node_res.hardware.sensors.get.return_value = sensors or [] node_res.time.get.return_value = {} node_res.subscription.get.return_value = {} @@ -204,12 +232,13 @@ def driver(): mock_api = _build_mock_api() with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api): drv.open() + drv._node_ssh = MagicMock(return_value=NodeShell()) return drv @pytest.fixture def driver_with_exec(request): - """Return a driver whose exec endpoint returns a parametrised string.""" + """Return a driver whose node shell prints a parametrised string.""" exec_output = getattr(request, "param", "") drv = ProxmoxDriver( hostname="pve1.example.com", @@ -217,7 +246,8 @@ def driver_with_exec(request): password="secret", optional_args={"verify_ssl": False}, ) - mock_api = _build_mock_api(exec_return=exec_output) + mock_api = _build_mock_api() with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api): drv.open() + drv._node_ssh = MagicMock(return_value=NodeShell(exec_output)) return drv diff --git a/tests/test_misc.py b/tests/test_misc.py index c374cac..aeec986 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -26,19 +26,19 @@ class TestGetNTPStats: ) def test_synchronized_server(self, driver): - driver._node_api().execute.post.return_value = {"data": self.NTP_OUTPUT} + driver._node_ssh().output = self.NTP_OUTPUT stats = driver.get_ntp_stats() synced = [s for s in stats if s["synchronized"]] assert len(synced) == 1 assert synced[0]["remote"] == "192.168.1.1" def test_stratum(self, driver): - driver._node_api().execute.post.return_value = {"data": self.NTP_OUTPUT} + driver._node_ssh().output = self.NTP_OUTPUT stats = driver.get_ntp_stats() assert stats[0]["stratum"] == 4 def test_empty_when_no_ntp_command(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" stats = driver.get_ntp_stats() assert stats == [] @@ -53,7 +53,7 @@ class TestGetSNMPInformation: ) def test_communities(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert "public" in result["community"] assert result["community"]["public"]["mode"] == "ro" @@ -61,30 +61,30 @@ class TestGetSNMPInformation: assert result["community"]["private"]["mode"] == "rw" def test_location(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert result["location"] == "Server Room A" def test_contact(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert result["contact"] == "admin@example.com" def test_chassis_id_is_node_name(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_snmp_information() assert result["chassis_id"] == "pve1" class TestGetUsers: def test_pve_users_returned(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_users() assert "root@pam" in result assert "napalm@pam" in result def test_user_has_required_keys(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_users() for _user, data in result.items(): assert "level" in data @@ -93,7 +93,7 @@ class TestGetUsers: def test_root_local_user_from_passwd(self, driver): passwd_output = "root:x:0:0:root:/root:/bin/bash\n" - driver._node_api().execute.post.return_value = {"data": passwd_output} + driver._node_ssh().output = passwd_output result = driver.get_users() assert "root" in result assert result["root"]["level"] == 15 @@ -136,14 +136,14 @@ class TestConfigManagement: assert diff == "" def test_get_config_running(self, driver): - driver._node_api().execute.post.return_value = {"data": self.RUNNING} + driver._node_ssh().output = self.RUNNING result = driver.get_config(retrieve="running") assert self.RUNNING in result["running"] assert result["candidate"] == "" def test_get_config_sanitized_redacts_password(self, driver): raw = "password mysecret\nauto eth0\n" - driver._node_api().execute.post.return_value = {"data": raw} + driver._node_ssh().output = raw result = driver.get_config(retrieve="running", sanitized=True) assert "mysecret" not in result["running"] assert "****" in result["running"] @@ -168,7 +168,7 @@ class TestPing: ) def test_success(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PING_SUCCESS} + driver._node_ssh().output = self.PING_SUCCESS result = driver.ping("8.8.8.8") assert "success" in result assert result["success"]["probes_sent"] == 2 @@ -177,7 +177,7 @@ class TestPing: def test_every_reply_is_a_result(self, driver): """iputils puts "from " before icmp_seq (napalm-proxmox#12).""" - driver._node_api().execute.post.return_value = {"data": self.PING_SUCCESS} + driver._node_ssh().output = self.PING_SUCCESS result = driver.ping("8.8.8.8") assert result["success"]["results"] == [ {"ip_address": "8.8.8.8", "rtt": 10.5}, @@ -193,7 +193,7 @@ class TestPing: "2 packets transmitted, 2 received, 0% packet loss, time 1001ms\n" "rtt min/avg/max/mdev = 0.045/61.5/123.0/61.4 ms\n" ) - driver._node_api().execute.post.return_value = {"data": output} + driver._node_ssh().output = output result = driver.ping("dns.google") assert result["success"]["results"] == [ {"ip_address": "8.8.4.4", "rtt": 123.0}, @@ -201,12 +201,12 @@ class TestPing: ] def test_failure(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PING_FAIL} + driver._node_ssh().output = self.PING_FAIL result = driver.ping("badhost") assert "error" in result - def test_exec_unavailable(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + def test_no_output(self, driver): + driver._node_ssh().output = "" result = driver.ping("8.8.8.8") assert "error" in result @@ -220,44 +220,34 @@ class TestTraceroute: ) def test_success_hops(self, driver): - driver._node_api().execute.post.return_value = {"data": self.TR_OUTPUT} + driver._node_ssh().output = self.TR_OUTPUT result = driver.traceroute("8.8.8.8") assert "success" in result assert 1 in result["success"] assert 3 in result["success"] def test_hop_rtt(self, driver): - driver._node_api().execute.post.return_value = {"data": self.TR_OUTPUT} + driver._node_ssh().output = self.TR_OUTPUT result = driver.traceroute("8.8.8.8") hop1 = result["success"][1]["probes"] assert hop1[1]["ip_address"] == "10.0.0.254" def test_empty_returns_error(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.traceroute("8.8.8.8") assert "error" in result class TestCLI: def test_cli_single_command(self, driver): - driver._node_api().execute.post.return_value = {"data": "hello world"} + driver._node_ssh().output = "hello world" result = driver.cli(["echo hello world"]) assert result["echo hello world"] == "hello world" def test_cli_multiple_commands(self, driver): - responses = {"uptime": "up 2 days", "hostname": "pve1"} - call_count = 0 - - def _exec(command): - nonlocal call_count - cmd = command - call_count += 1 - return {"data": responses.get(cmd, "")} - - driver._node_api().execute.post.side_effect = _exec + driver._node_ssh().outputs = {"uptime": "up 2 days", "hostname": "pve1"} result = driver.cli(["uptime", "hostname"]) - assert "uptime" in result - assert "hostname" in result + assert result == {"uptime": "up 2 days", "hostname": "pve1"} class TestGetRouteTo: @@ -267,12 +257,12 @@ class TestGetRouteTo: ) def test_returns_routes(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to(destination="10.0.0.0/24") assert "10.0.0.0/24" in result def test_route_fields(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to() for _prefix, routes in result.items(): for route in routes: @@ -281,7 +271,7 @@ class TestGetRouteTo: assert "outgoing_interface" in route def test_protocol_filter(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to(protocol="dhcp") for _prefix, routes in result.items(): for route in routes: @@ -316,7 +306,7 @@ class TestLLDPNeighbors: assert result["eth0"][0]["hostname"] == "sw01.example.com" def test_empty_when_no_lldp(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_lldp_neighbors() assert result == {} @@ -329,18 +319,18 @@ class TestIPv6Neighbors: ) def test_neighbors_parsed(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() assert len(result) == 2 # FAILED entry excluded def test_mac_normalized(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() macs = {e["mac"] for e in result} assert "aa:bb:cc:dd:ee:ff" in macs def test_state_present(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() states = {e["state"] for e in result} assert "REACHABLE" in states @@ -348,7 +338,7 @@ class TestIPv6Neighbors: def test_age_is_unknown(self, driver): # `ip neigh` reports no age, as for the IPv4 table; 0.0 would read as # "confirmed just now" (NetOrk/netork#1327). - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() assert {e["age"] for e in result} == {-1.0} diff --git a/tests/test_node_exec.py b/tests/test_node_exec.py new file mode 100644 index 0000000..173e1b3 --- /dev/null +++ b/tests/test_node_exec.py @@ -0,0 +1,109 @@ +"""Commands on the node run over its SSH session, and a failure to run one raises (#17). + +``_exec_ssh_command`` used to post to ``/nodes/{node}/execute`` first (a batch of API +calls, not a shell), to return stderr as output, and to return ``""`` whenever the +command could not run at all: no credential, a refused login, a changed host key, a +timeout. "Nothing there" and "could not look" read the same. +""" + +from __future__ import annotations + +from unittest.mock import MagicMock + +import paramiko +import pytest +from napalm.base.exceptions import ConnectionException + +from napalm_proxmox.driver import ProxmoxDriver +from tests.test_ssh_host_key import client # noqa: F401 - the fake paramiko client + + +class _Stream: + def __init__(self, data: bytes | Exception) -> None: + self._data = data + + def read(self) -> bytes: + if isinstance(self._data, Exception): + raise self._data + return self._data + + +class _Shell: + """The node's SSH session: prints *out* on stdout and *err* on stderr.""" + + def __init__(self, out: bytes | Exception = b"", err: bytes = b"") -> None: + self.out, self.err = out, err + self.commands: list[str] = [] + + def exec_command(self, command: str, timeout: int | None = None): + self.commands.append(command) + return None, _Stream(self.out), _Stream(self.err) + + +def _driver(shell: _Shell | None = None, **kwargs) -> ProxmoxDriver: + driver = ProxmoxDriver("10.0.0.9", "root", kwargs.pop("password", "s3cret"), **kwargs) + driver._node_name = "pve1" + driver._api = MagicMock() + if shell is not None: + driver._node_ssh = MagicMock(return_value=shell) # type: ignore[method-assign] + return driver + + +def test_a_command_goes_straight_to_the_ssh_session(): + shell = _Shell(b"6.8.12-4-pve\n") + driver = _driver(shell) + + assert driver._exec_ssh_command("uname -r") == "6.8.12-4-pve" + assert shell.commands == ["uname -r"] + driver._api.nodes.return_value.execute.post.assert_not_called() + + +def test_stderr_is_not_output(): + shell = _Shell(b"", b"cat: /etc/snmp/snmpd.conf: No such file or directory\n") + + assert _driver(shell)._exec_ssh_command("cat /etc/snmp/snmpd.conf") == "" + + +def test_a_refused_login_raises(): + driver = _driver() + driver._node_ssh = MagicMock( # type: ignore[method-assign] + side_effect=paramiko.AuthenticationException("Authentication failed.") + ) + + with pytest.raises(ConnectionException, match="Authentication failed") as err: + driver._exec_ssh_command("uptime") + assert isinstance(err.value.__cause__, paramiko.AuthenticationException) + + +def test_an_unreachable_node_raises(): + driver = _driver() + driver._node_ssh = MagicMock( # type: ignore[method-assign] + side_effect=OSError(113, "No route to host") + ) + + with pytest.raises(ConnectionException, match="No route to host"): + driver._exec_ssh_command("uptime") + + +def test_a_timeout_raises(): + shell = _Shell(TimeoutError("timed out")) + + with pytest.raises(ConnectionException, match="timed out"): + _driver(shell)._exec_ssh_command("apt list --upgradable") + + +def test_without_an_ssh_credential_nothing_connects(client): # noqa: F811 + # An API-token-only node: no API password, no SSH password, no key. + driver = _driver(password="", optional_args={"token_name": "root@pam!t", "token_value": "x"}) + + with pytest.raises(ConnectionException, match="no SSH credential"): + driver._exec_ssh_command("command -v lldpd") + assert client.connected_with is None + + +def test_get_snmp_config_raises_where_it_cannot_look(client): # noqa: F811 + """netOrk reads an exception as "SSH not available" and raises no SNMP alarm.""" + driver = _driver(password="", optional_args={"token_name": "root@pam!t", "token_value": "x"}) + + with pytest.raises(ConnectionException): + driver.get_snmp_config() diff --git a/tests/test_ovs_and_arp.py b/tests/test_ovs_and_arp.py index 464f84c..091e3a7 100644 --- a/tests/test_ovs_and_arp.py +++ b/tests/test_ovs_and_arp.py @@ -103,23 +103,23 @@ class TestGetARPTable: ) def test_arp_entries_returned(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table() assert len(result) == 2 # 00:00:00:00:00:00 excluded def test_mac_normalized(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table() macs = {e["mac"] for e in result} assert "aa:bb:cc:dd:ee:ff" in macs def test_vrf_filter(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table(vrf="vmbr0") assert all(e["interface"] == "vmbr0" for e in result) - def test_empty_when_exec_unavailable(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + def test_empty_when_the_node_prints_nothing(self, driver): + driver._node_ssh().output = "" result = driver.get_arp_table() assert result == [] diff --git a/tests/test_ssh_host_key.py b/tests/test_ssh_host_key.py index e977084..e858d7d 100644 --- a/tests/test_ssh_host_key.py +++ b/tests/test_ssh_host_key.py @@ -1,7 +1,7 @@ """The node's SSH session verifies its host key, and reports the one it sees (NetOrk/netork#1167). -Both ways onto the node's shell — the best-effort ``_exec_ssh_command`` and the -provisioning steps' ``_run_node_command`` — share one session, opened on port 22. +Both ways onto the node's shell — ``_exec_ssh_command`` and the provisioning +steps' ``_run_node_command`` — share one session, opened on port 22. """ from __future__ import annotations @@ -87,7 +87,6 @@ def _driver(seen: list, known: dict | None = None) -> ProxmoxDriver: if known is not None: args[KNOWN_KEYS_ARG] = known driver = ProxmoxDriver("10.0.0.9", "root", "s3cret", optional_args=args) - driver._node_api = MagicMock(side_effect=RuntimeError("no exec endpoint")) # type: ignore[method-assign] return driver @@ -116,10 +115,12 @@ def test_a_changed_key_stops_a_provisioning_step(client): assert seen[0]["accepted"] is False -def test_a_changed_key_leaves_the_best_effort_read_empty(client): +def test_a_changed_key_stops_a_read_too(client): + """It used to read as empty output, "nothing there" (#17).""" seen: list = [] driver = _driver(seen, {"22": SshHostKey.of(OTHER_KEY).as_dict()}) - assert driver._exec_ssh_command("cat /etc/os-release") == "" + with pytest.raises(SshHostKeyMismatch): + driver._exec_ssh_command("cat /etc/os-release") assert client.commands == [] assert seen[0]["accepted"] is False