fix: quote the cloud image URL, raise when a node command cannot run (#43, #17) #45

Merged
christianmanivong merged 2 commits from fix/bugs-2026-11-02 into main 2026-11-02 08:23:52 +00:00
17 changed files with 453 additions and 177 deletions

No files matched your search

+16
View File
@@ -113,6 +113,22 @@ 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 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. were a `ValueError` (no snippet storage, a NIC without a bridge) are its cause now.
Needs `VMProvisionIncomplete` from napalm-device-types (#28). 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).
- 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 ## [0.8.0] - 2026-10-23
+13 -6
View File
@@ -103,7 +103,7 @@ device = driver(
| `get_interfaces_ip` | IPv4/IPv6 from classic ifaces + SDN subnet gateways | | `get_interfaces_ip` | IPv4/IPv6 from classic ifaces + SDN subnet gateways |
| `get_interfaces_counters` | From `node/netstat` RRD data | | `get_interfaces_counters` | From `node/netstat` RRD data |
| `get_environment` | CPU %, memory (bytes), hardware temperature sensors | | `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_mac_address_table` | `bridge fdb show` (Linux) + `ovs-appctl fdb/show` (OVS) |
| `get_vlans` | SDN VNet tags + `bridge vlan show` | | `get_vlans` | SDN VNet tags + `bridge vlan show` |
| `get_network_instances` | SDN zones (VLAN→L2VPN, EVPN→L3VRF) + default instance | | `get_network_instances` | SDN zones (VLAN→L2VPN, EVPN→L3VRF) + default instance |
@@ -116,13 +116,13 @@ device = driver(
| `compare_config` | Unified diff | | `compare_config` | Unified diff |
| `commit_config` | Writes `/etc/network/interfaces` + `ifreload -a` | | `commit_config` | Writes `/etc/network/interfaces` + `ifreload -a` |
| `discard_config` / `rollback` | Revert candidate | | `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_lldp_neighbors_detail` | Full LLDP detail |
| `get_ipv6_neighbors_table` | `ip -6 neigh show` | | `get_ipv6_neighbors_table` | `ip -6 neigh show` |
| `ping` | Linux `ping` via exec endpoint | | `ping` | Linux `ping` over SSH |
| `traceroute` | `traceroute -n` via exec endpoint | | `traceroute` | `traceroute -n` over SSH |
| `cli` | Arbitrary command execution via exec endpoint | | `cli` | Arbitrary commands over SSH (stdout) |
| `get_route_to` | `ip route show` via exec endpoint | | `get_route_to` | `ip route show` over SSH |
**Not implemented** (raise `NotImplementedError`): **Not implemented** (raise `NotImplementedError`):
`get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`,
@@ -138,6 +138,13 @@ device = driver(
| `node` | str | *auto* | Override target node name | | `node` | str | *auto* | Override target node name |
| `token_name` | str | `None` | API token identifier (`user@realm!tokenid`) | | `token_name` | str | `None` | API token identifier (`user@realm!tokenid`) |
| `token_value` | str | `None` | API token secret | | `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 ## Development
+3 -3
View File
@@ -114,12 +114,12 @@ class ProxmoxConfigMixin:
.. note:: .. note::
Full programmatic apply requires the Proxmox API to accept raw Full programmatic apply requires the Proxmox API to accept raw
interface configs. This implementation uses ``pvesh`` via exec interface configs. This implementation uses ``pvesh`` over the
which requires the node exec endpoint to be available. node's SSH session.
""" """
if not self._candidate_config: if not self._candidate_config:
return return
# Write via exec endpoint # Write over the node's SSH session
escaped = self._candidate_config.replace("'", "'\\''") escaped = self._candidate_config.replace("'", "'\\''")
self._exec_ssh_command( self._exec_ssh_command(
f"printf '%s' '{escaped}' > /etc/network/interfaces && " f"printf '%s' '{escaped}' > /etc/network/interfaces && "
+39 -33
View File
@@ -48,7 +48,7 @@ from napalm_device_types import (
SystemdServicesMixin, SystemdServicesMixin,
SystemIdentityMixin, SystemIdentityMixin,
) )
from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from napalm_device_types.ssh_host_keys import HostKeyCheck
try: try:
from proxmoxer import ProxmoxAPI from proxmoxer import ProxmoxAPI
@@ -299,48 +299,40 @@ class ProxmoxDriver(
return [] return []
def _exec_ssh_command(self, command: str) -> str: 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, Stdout only: stderr is not output (a command that wants it says
falls back to a direct paramiko SSH connection. ``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: try:
_, stdout, stderr = self._node_ssh().exec_command(command, timeout=self.timeout) _, stdout, stderr = self._node_ssh().exec_command(command, timeout=self.timeout)
err = stderr.read().decode().strip() out = stdout.read().decode(errors="replace")
out = stdout.read().decode().strip() err = stderr.read().decode(errors="replace").strip()
return out or err except ConnectionException:
except ImportError: raise
logger.warning("paramiko not installed — cannot exec SSH commands") except _ssh_errors() as exc:
except SshHostKeyMismatch as exc: raise ConnectionException(
# Refused before any login, and already reported to the caller's store. f"Cannot run a command on {self.hostname} over SSH: {exc}"
logger.warning("%s", exc) ) from exc
except Exception as exc: if err:
logger.debug("SSH exec command failed: %s", exc) logger.debug("%s: stderr of a node command: %s", self.hostname, err[-300:])
return out.strip()
return ""
def _node_ssh(self) -> Any: # paramiko.SSHClient; paramiko is imported lazily def _node_ssh(self) -> Any: # paramiko.SSHClient; paramiko is imported lazily
"""The SSH session to the node on port 22, opened once. """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 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). whichever the node presents; either way it is reported (netork#1167).
:raises ConnectionException: without a password or a usable key.
""" """
import paramiko import paramiko
@@ -356,6 +348,12 @@ class ProxmoxDriver(
pkey = _load_private_key(self._ssh_key) if self._ssh_key else None pkey = _load_private_key(self._ssh_key) if self._ssh_key else None
if pkey is not None: if pkey is not None:
connect_kwargs["pkey"] = pkey 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() client = paramiko.SSHClient()
try: try:
HostKeyCheck(self.optional_args, self.hostname, 22).connect( 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 continue
logger.warning("The SSH private key could not be read; logging in with the password") logger.warning("The SSH private key could not be read; logging in with the password")
return None 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)
+70
View File
@@ -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
# "<algo>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]:
"""``"<algo>:<hex>"`` 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}"
+3 -3
View File
@@ -160,9 +160,9 @@ class ProxmoxInterfaceMixin:
def get_arp_table(self, vrf: str = "") -> list[_JsonDict]: def get_arp_table(self, vrf: str = "") -> list[_JsonDict]:
"""Return ARP table. """Return ARP table.
Proxmox does not expose ARP via the REST API directly. We attempt Proxmox does not expose ARP via the REST API, so the node's
to read it via the node's ``/proc/net/arp`` through the Proxmox ``/proc/net/arp`` is read over SSH; a node that cannot be reached
exec endpoint. If that is unavailable, an empty list is returned. there raises.
""" """
raw = self._exec_ssh_command("cat /proc/net/arp") raw = self._exec_ssh_command("cat /proc/net/arp")
if not raw: if not raw:
+2 -2
View File
@@ -58,7 +58,7 @@ class ProxmoxRoutingMixin:
raw = self._exec_ssh_command(f"{cmd} 2>&1 || true") raw = self._exec_ssh_command(f"{cmd} 2>&1 || true")
if not raw: 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 # Detect common failure strings before parsing statistics
_error_patterns = ( _error_patterns = (
@@ -126,7 +126,7 @@ class ProxmoxRoutingMixin:
raw = self._exec_ssh_command(" ".join(cmd_parts) + " 2>&1 || true") raw = self._exec_ssh_command(" ".join(cmd_parts) + " 2>&1 || true")
if not raw: if not raw:
return {"error": "traceroute not available via exec endpoint"} return {"error": "traceroute printed nothing on the node"}
hops: _JsonDict = {} hops: _JsonDict = {}
for line in raw.splitlines(): for line in raw.splitlines():
+2 -1
View File
@@ -504,7 +504,8 @@ class ProxmoxSystemMixin:
def get_snmp_config(self): def get_snmp_config(self):
"""Return SNMP agent config if snmpd is installed and running on the node. """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. Returns a SNMPConfigDict or None.
""" """
try: try:
+33 -68
View File
@@ -3,10 +3,10 @@
from __future__ import annotations from __future__ import annotations
import base64 import base64
import hashlib
import ipaddress import ipaddress
import logging import logging
import re import re
import shlex
import time import time
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from urllib.parse import quote from urllib.parse import quote
@@ -26,6 +26,7 @@ from napalm_device_types.provisioning import (
split_compression, 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.network_targets import bridge_targets, vnet_target
from napalm_proxmox.utils import cpu_flags from napalm_proxmox.utils import cpu_flags
@@ -81,12 +82,6 @@ _HOST_CPU_DESCRIPTION = (
# download cost once. # download cost once.
_IMAGE_CACHE_DIR = "/var/lib/vz/template/netork-images" _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, # 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 # 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). # 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". # 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})") _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]:
"""``"<algo>:<hex>"`` 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]: def _nic_config_args(nics: list[dict[str, Any]]) -> dict[str, str]:
"""``{"net<i>": "virtio[=<mac>],bridge=<bridge>[,tag=|,trunks=]"}`` per NIC.""" """``{"net<i>": "virtio[=<mac>],bridge=<bridge>[,tag=|,trunks=]"}`` per NIC."""
@@ -165,10 +129,10 @@ class ProxmoxVMProvisionMixin:
""" """
Execute a shell command on the Proxmox node via SSH, raising on failure. Execute a shell command on the Proxmox node via SSH, raising on failure.
Unlike ``_exec_ssh_command`` (best-effort, fixed timeout, swallows Unlike ``_exec_ssh_command`` (fixed timeout, exit status not checked),
errors), this is for critical provisioning steps — image download, this is for critical provisioning steps — image download, disk import —
disk import — where a non-zero exit or a caller-specific timeout must where a non-zero exit or a caller-specific timeout must surface as a
surface as a hard failure rather than an empty string. hard failure.
""" """
_, stdout, stderr = self._node_ssh().exec_command(command, timeout=timeout) _, stdout, stderr = self._node_ssh().exec_command(command, timeout=timeout)
exit_status = stdout.channel.recv_exit_status() exit_status = stdout.channel.recv_exit_status()
@@ -197,33 +161,26 @@ class ProxmoxVMProvisionMixin:
once (covers a corrupted/partial transfer or a stale same-keyed file) once (covers a corrupted/partial transfer or a stale same-keyed file)
before raising. before raising.
""" """
filename = image_url.rstrip("/").rsplit("/", 1)[-1] local_path = f"{_IMAGE_CACHE_DIR}/{cache_name(image_url)}"
local_path = f"{_IMAGE_CACHE_DIR}/{_url_key(image_url)}-{filename}" algo, expected = split_checksum(image_checksum or "")
algo, expected = _split_checksum(image_checksum or "")
max_attempts = 2 max_attempts = 2
for attempt in range(1, max_attempts + 1): for attempt in range(1, max_attempts + 1):
exists = self._run_node_command( if not self._cached_on_node(local_path):
f"mkdir -p {_IMAGE_CACHE_DIR} && test -f {local_path} "
f"&& echo EXISTS || echo MISSING",
timeout=30,
)
if "EXISTS" not in exists:
self._fetch_image(image_url, local_path, timeout) self._fetch_image(image_url, local_path, timeout)
if not image_checksum: if not image_checksum:
return local_path return local_path
actual = self._run_node_command( 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(): if actual.lower() == expected.lower():
return local_path return local_path
# Remove the bad file so the next attempt re-downloads instead of # Remove the bad file so the next attempt re-downloads instead of
# reusing it. # 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: if attempt == max_attempts:
raise RuntimeError( raise RuntimeError(
f"Checksum mismatch for {image_url}: expected {expected}, got {actual}" 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 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: 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``. """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``. reaches the error, and a failed download leaves no partial ``.tmp``.
""" """
_logger.info(f"Downloading cloud image {image_url} -> {local_path}") _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( 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; }}", f"|| {{ s=$?; rm -f {tmp}; exit $s; }}",
timeout=timeout, timeout=timeout,
) )
@@ -257,22 +224,19 @@ class ProxmoxVMProvisionMixin:
the unpacked file is kept. Proxmox's download-url unpacks ISOs only, the unpacked file is kept. Proxmox's download-url unpacks ISOs only,
so a packed image always comes this way. so a packed image always comes this way.
""" """
filename = image_url.rstrip("/").rsplit("/", 1)[-1] unpacked_name, unpack = split_compression(cache_name(image_url))
unpacked_name, unpack = split_compression(filename)
if unpack is None: if unpack is None:
return self._download_cloud_image(image_url, image_checksum, timeout=timeout) return self._download_cloud_image(image_url, image_checksum, timeout=timeout)
unpacked = f"{_IMAGE_CACHE_DIR}/{_url_key(image_url)}-{unpacked_name}" unpacked = f"{_IMAGE_CACHE_DIR}/{unpacked_name}"
cached = self._run_node_command( if self._cached_on_node(unpacked):
f"mkdir -p {_IMAGE_CACHE_DIR} && test -f {unpacked} && echo EXISTS || echo MISSING",
timeout=30,
)
if "EXISTS" in cached:
return unpacked return unpacked
packed = self._download_cloud_image(image_url, image_checksum, timeout=timeout) packed = self._download_cloud_image(image_url, image_checksum, timeout=timeout)
_logger.info(f"Unpacking {packed} -> {unpacked}") _logger.info(f"Unpacking {packed} -> {unpacked}")
tmp = shlex.quote(f"{unpacked}.tmp")
self._run_node_command( 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, timeout=timeout,
) )
return unpacked return unpacked
@@ -344,7 +308,7 @@ class ProxmoxVMProvisionMixin:
params: dict[str, Any] = {"url": image_url, "content": "import", "filename": filename} params: dict[str, Any] = {"url": image_url, "content": "import", "filename": filename}
if image_checksum: if image_checksum:
algo, expected = _split_checksum(image_checksum) algo, expected = split_checksum(image_checksum)
params["checksum"] = expected params["checksum"] = expected
params["checksum-algorithm"] = algo params["checksum-algorithm"] = algo
_logger.info(f"Downloading cloud image {image_url} into {volid}") _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) 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}") _logger.info(f"Importing {local_path} into VM {vmid} on storage {image_storage}")
self._run_node_command( 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, timeout=timeout,
) )
@@ -401,7 +366,7 @@ class ProxmoxVMProvisionMixin:
verifies and copies the image itself — and over SSH otherwise. verifies and copies the image itself — and over SSH otherwise.
""" """
import_storage = self._find_import_storage() 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: if not import_storage or not filename:
self._import_over_ssh( self._import_over_ssh(
vmid, image_storage, image_url, image_checksum, download_timeout, timeout vmid, image_storage, image_url, image_checksum, download_timeout, timeout
+34 -4
View File
@@ -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 # Driver fixture
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -137,7 +167,6 @@ def _build_mock_api(
ntp=None, ntp=None,
netstat=None, netstat=None,
pve_users=None, pve_users=None,
exec_return="",
sensors=None, sensors=None,
cluster_status=None, cluster_status=None,
): ):
@@ -159,7 +188,6 @@ def _build_mock_api(
node_res.dns.get.return_value = dns or DNS_INFO node_res.dns.get.return_value = dns or DNS_INFO
node_res.ntp.get.return_value = ntp or NTP_INFO node_res.ntp.get.return_value = ntp or NTP_INFO
node_res.netstat.get.return_value = netstat or NODE_NETSTAT 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.hardware.sensors.get.return_value = sensors or []
node_res.time.get.return_value = {} node_res.time.get.return_value = {}
node_res.subscription.get.return_value = {} node_res.subscription.get.return_value = {}
@@ -204,12 +232,13 @@ def driver():
mock_api = _build_mock_api() mock_api = _build_mock_api()
with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api): with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api):
drv.open() drv.open()
drv._node_ssh = MagicMock(return_value=NodeShell())
return drv return drv
@pytest.fixture @pytest.fixture
def driver_with_exec(request): 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", "") exec_output = getattr(request, "param", "")
drv = ProxmoxDriver( drv = ProxmoxDriver(
hostname="pve1.example.com", hostname="pve1.example.com",
@@ -217,7 +246,8 @@ def driver_with_exec(request):
password="secret", password="secret",
optional_args={"verify_ssl": False}, 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): with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api):
drv.open() drv.open()
drv._node_ssh = MagicMock(return_value=NodeShell(exec_output))
return drv return drv
-1
View File
@@ -37,7 +37,6 @@
"napalm_proxmox/system_mixin.py::ProxmoxSystemMixin.set_hostname": 114, "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_vm_interfaces": 179,
"napalm_proxmox/vm_mixin.py::ProxmoxVMMixin.get_vms": 114, "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.create_vm_from_cloud_init": 220,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 56, "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.destroy_vm": 56,
"napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92 "napalm_proxmox/vm_provision_mixin.py::ProxmoxVMProvisionMixin.get_vm_status": 92
+32 -42
View File
@@ -26,19 +26,19 @@ class TestGetNTPStats:
) )
def test_synchronized_server(self, driver): 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() stats = driver.get_ntp_stats()
synced = [s for s in stats if s["synchronized"]] synced = [s for s in stats if s["synchronized"]]
assert len(synced) == 1 assert len(synced) == 1
assert synced[0]["remote"] == "192.168.1.1" assert synced[0]["remote"] == "192.168.1.1"
def test_stratum(self, driver): 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() stats = driver.get_ntp_stats()
assert stats[0]["stratum"] == 4 assert stats[0]["stratum"] == 4
def test_empty_when_no_ntp_command(self, driver): 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() stats = driver.get_ntp_stats()
assert stats == [] assert stats == []
@@ -53,7 +53,7 @@ class TestGetSNMPInformation:
) )
def test_communities(self, driver): 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() result = driver.get_snmp_information()
assert "public" in result["community"] assert "public" in result["community"]
assert result["community"]["public"]["mode"] == "ro" assert result["community"]["public"]["mode"] == "ro"
@@ -61,30 +61,30 @@ class TestGetSNMPInformation:
assert result["community"]["private"]["mode"] == "rw" assert result["community"]["private"]["mode"] == "rw"
def test_location(self, driver): 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() result = driver.get_snmp_information()
assert result["location"] == "Server Room A" assert result["location"] == "Server Room A"
def test_contact(self, driver): 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() result = driver.get_snmp_information()
assert result["contact"] == "admin@example.com" assert result["contact"] == "admin@example.com"
def test_chassis_id_is_node_name(self, driver): 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() result = driver.get_snmp_information()
assert result["chassis_id"] == "pve1" assert result["chassis_id"] == "pve1"
class TestGetUsers: class TestGetUsers:
def test_pve_users_returned(self, driver): def test_pve_users_returned(self, driver):
driver._node_api().execute.post.return_value = {"data": ""} driver._node_ssh().output = ""
result = driver.get_users() result = driver.get_users()
assert "root@pam" in result assert "root@pam" in result
assert "napalm@pam" in result assert "napalm@pam" in result
def test_user_has_required_keys(self, driver): def test_user_has_required_keys(self, driver):
driver._node_api().execute.post.return_value = {"data": ""} driver._node_ssh().output = ""
result = driver.get_users() result = driver.get_users()
for _user, data in result.items(): for _user, data in result.items():
assert "level" in data assert "level" in data
@@ -93,7 +93,7 @@ class TestGetUsers:
def test_root_local_user_from_passwd(self, driver): def test_root_local_user_from_passwd(self, driver):
passwd_output = "root:x:0:0:root:/root:/bin/bash\n" 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() result = driver.get_users()
assert "root" in result assert "root" in result
assert result["root"]["level"] == 15 assert result["root"]["level"] == 15
@@ -136,14 +136,14 @@ class TestConfigManagement:
assert diff == "" assert diff == ""
def test_get_config_running(self, driver): 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") result = driver.get_config(retrieve="running")
assert self.RUNNING in result["running"] assert self.RUNNING in result["running"]
assert result["candidate"] == "" assert result["candidate"] == ""
def test_get_config_sanitized_redacts_password(self, driver): def test_get_config_sanitized_redacts_password(self, driver):
raw = "password mysecret\nauto eth0\n" 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) result = driver.get_config(retrieve="running", sanitized=True)
assert "mysecret" not in result["running"] assert "mysecret" not in result["running"]
assert "****" in result["running"] assert "****" in result["running"]
@@ -168,7 +168,7 @@ class TestPing:
) )
def test_success(self, driver): 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") result = driver.ping("8.8.8.8")
assert "success" in result assert "success" in result
assert result["success"]["probes_sent"] == 2 assert result["success"]["probes_sent"] == 2
@@ -177,7 +177,7 @@ class TestPing:
def test_every_reply_is_a_result(self, driver): def test_every_reply_is_a_result(self, driver):
"""iputils puts "from <ip>" before icmp_seq (napalm-proxmox#12).""" """iputils puts "from <ip>" 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") result = driver.ping("8.8.8.8")
assert result["success"]["results"] == [ assert result["success"]["results"] == [
{"ip_address": "8.8.8.8", "rtt": 10.5}, {"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" "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" "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") result = driver.ping("dns.google")
assert result["success"]["results"] == [ assert result["success"]["results"] == [
{"ip_address": "8.8.4.4", "rtt": 123.0}, {"ip_address": "8.8.4.4", "rtt": 123.0},
@@ -201,12 +201,12 @@ class TestPing:
] ]
def test_failure(self, driver): 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") result = driver.ping("badhost")
assert "error" in result assert "error" in result
def test_exec_unavailable(self, driver): def test_no_output(self, driver):
driver._node_api().execute.post.return_value = {"data": ""} driver._node_ssh().output = ""
result = driver.ping("8.8.8.8") result = driver.ping("8.8.8.8")
assert "error" in result assert "error" in result
@@ -220,44 +220,34 @@ class TestTraceroute:
) )
def test_success_hops(self, driver): 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") result = driver.traceroute("8.8.8.8")
assert "success" in result assert "success" in result
assert 1 in result["success"] assert 1 in result["success"]
assert 3 in result["success"] assert 3 in result["success"]
def test_hop_rtt(self, driver): 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") result = driver.traceroute("8.8.8.8")
hop1 = result["success"][1]["probes"] hop1 = result["success"][1]["probes"]
assert hop1[1]["ip_address"] == "10.0.0.254" assert hop1[1]["ip_address"] == "10.0.0.254"
def test_empty_returns_error(self, driver): 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") result = driver.traceroute("8.8.8.8")
assert "error" in result assert "error" in result
class TestCLI: class TestCLI:
def test_cli_single_command(self, driver): 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"]) result = driver.cli(["echo hello world"])
assert result["echo hello world"] == "hello world" assert result["echo hello world"] == "hello world"
def test_cli_multiple_commands(self, driver): def test_cli_multiple_commands(self, driver):
responses = {"uptime": "up 2 days", "hostname": "pve1"} driver._node_ssh().outputs = {"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
result = driver.cli(["uptime", "hostname"]) result = driver.cli(["uptime", "hostname"])
assert "uptime" in result assert result == {"uptime": "up 2 days", "hostname": "pve1"}
assert "hostname" in result
class TestGetRouteTo: class TestGetRouteTo:
@@ -267,12 +257,12 @@ class TestGetRouteTo:
) )
def test_returns_routes(self, driver): 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") result = driver.get_route_to(destination="10.0.0.0/24")
assert "10.0.0.0/24" in result assert "10.0.0.0/24" in result
def test_route_fields(self, driver): 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() result = driver.get_route_to()
for _prefix, routes in result.items(): for _prefix, routes in result.items():
for route in routes: for route in routes:
@@ -281,7 +271,7 @@ class TestGetRouteTo:
assert "outgoing_interface" in route assert "outgoing_interface" in route
def test_protocol_filter(self, driver): 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") result = driver.get_route_to(protocol="dhcp")
for _prefix, routes in result.items(): for _prefix, routes in result.items():
for route in routes: for route in routes:
@@ -316,7 +306,7 @@ class TestLLDPNeighbors:
assert result["eth0"][0]["hostname"] == "sw01.example.com" assert result["eth0"][0]["hostname"] == "sw01.example.com"
def test_empty_when_no_lldp(self, driver): 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() result = driver.get_lldp_neighbors()
assert result == {} assert result == {}
@@ -329,18 +319,18 @@ class TestIPv6Neighbors:
) )
def test_neighbors_parsed(self, driver): 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() result = driver.get_ipv6_neighbors_table()
assert len(result) == 2 # FAILED entry excluded assert len(result) == 2 # FAILED entry excluded
def test_mac_normalized(self, driver): 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() result = driver.get_ipv6_neighbors_table()
macs = {e["mac"] for e in result} macs = {e["mac"] for e in result}
assert "aa:bb:cc:dd:ee:ff" in macs assert "aa:bb:cc:dd:ee:ff" in macs
def test_state_present(self, driver): 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() result = driver.get_ipv6_neighbors_table()
states = {e["state"] for e in result} states = {e["state"] for e in result}
assert "REACHABLE" in states assert "REACHABLE" in states
@@ -348,7 +338,7 @@ class TestIPv6Neighbors:
def test_age_is_unknown(self, driver): def test_age_is_unknown(self, driver):
# `ip neigh` reports no age, as for the IPv4 table; 0.0 would read as # `ip neigh` reports no age, as for the IPv4 table; 0.0 would read as
# "confirmed just now" (NetOrk/netork#1327). # "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() result = driver.get_ipv6_neighbors_table()
assert {e["age"] for e in result} == {-1.0} assert {e["age"] for e in result} == {-1.0}
+109
View File
@@ -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()
+5 -5
View File
@@ -103,23 +103,23 @@ class TestGetARPTable:
) )
def test_arp_entries_returned(self, driver): 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() result = driver.get_arp_table()
assert len(result) == 2 # 00:00:00:00:00:00 excluded assert len(result) == 2 # 00:00:00:00:00:00 excluded
def test_mac_normalized(self, driver): 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() result = driver.get_arp_table()
macs = {e["mac"] for e in result} macs = {e["mac"] for e in result}
assert "aa:bb:cc:dd:ee:ff" in macs assert "aa:bb:cc:dd:ee:ff" in macs
def test_vrf_filter(self, driver): 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") result = driver.get_arp_table(vrf="vmbr0")
assert all(e["interface"] == "vmbr0" for e in result) assert all(e["interface"] == "vmbr0" for e in result)
def test_empty_when_exec_unavailable(self, driver): def test_empty_when_the_node_prints_nothing(self, driver):
driver._node_api().execute.post.return_value = {"data": ""} driver._node_ssh().output = ""
result = driver.get_arp_table() result = driver.get_arp_table()
assert result == [] assert result == []
+6 -5
View File
@@ -1,7 +1,7 @@
"""The node's SSH session verifies its host key, and reports the one it sees (NetOrk/netork#1167). """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 Both ways onto the node's shell — ``_exec_ssh_command`` and the provisioning
provisioning steps' ``_run_node_command`` — share one session, opened on port 22. steps' ``_run_node_command`` — share one session, opened on port 22.
""" """
from __future__ import annotations from __future__ import annotations
@@ -87,7 +87,6 @@ def _driver(seen: list, known: dict | None = None) -> ProxmoxDriver:
if known is not None: if known is not None:
args[KNOWN_KEYS_ARG] = known args[KNOWN_KEYS_ARG] = known
driver = ProxmoxDriver("10.0.0.9", "root", "s3cret", optional_args=args) 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 return driver
@@ -116,10 +115,12 @@ def test_a_changed_key_stops_a_provisioning_step(client):
assert seen[0]["accepted"] is False 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 = [] seen: list = []
driver = _driver(seen, {"22": SshHostKey.of(OTHER_KEY).as_dict()}) 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 client.commands == []
assert seen[0]["accepted"] is False assert seen[0]["accepted"] is False
+2 -4
View File
@@ -10,10 +10,8 @@ from __future__ import annotations
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
from napalm_proxmox.vm_provision_mixin import ( from napalm_proxmox.image_names import import_volume_name as _import_volume_name
ProxmoxVMProvisionMixin, from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin
_import_volume_name,
)
_UBUNTU = ( _UBUNTU = (
"https://cloud-images.ubuntu.com/releases/26.04/release/ubuntu-26.04-server-cloudimg-amd64.img" "https://cloud-images.ubuntu.com/releases/26.04/release/ubuntu-26.04-server-cloudimg-amd64.img"
+84
View File
@@ -1417,3 +1417,87 @@ def test_download_cloud_image_does_not_silence_wget():
download = mixin._run_node_command.call_args_list[1][0][0] download = mixin._run_node_command.call_args_list[1][0][0]
assert download.startswith("wget -nv ") 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 <file>" 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)