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
Owner

Two fixes, one commit each.

#43 — the image URL in the node's root commands

create_vm_from_cloud_init()'s SSH download path built its node commands (which run as root) from the image URL: the URL wrapped in hand-written single quotes, the cache path containing the URL's basename unquoted in test -f, wget -O, mv, <algo>sum, rm, the unpack step and qm importdisk.

  • Every interpolated value goes through shlex.quote (URL, cache path, .tmp path, packed/unpacked paths, the image storage in qm importdisk).
  • The cache file name is the URL's hash plus its basename with every character outside Proxmox's safe set ([A-Za-z0-9-.+=_], the class _import_volume_name already used) replaced by _. Ordinary image names are unchanged, so existing cache entries stay valid.
  • The checksum algorithm is part of a command name (sha256sum), so it is checked against what Proxmox's download-url accepts (md5, sha1, sha224, sha256, sha384, sha512); anything else is a ValueError before anything runs on the node.
  • The file-name helpers move to napalm_proxmox/image_names.py (strict mypy): vm_provision_mixin.py would otherwise cross the 1000-line hard limit. _download_cloud_image drops below 50 lines, so its baseline entry is removed.

Tests run the real commands through a local sh (the existing _LocalShell seam) with a URL https://example.org/$(reboot)/a b/x';reboot;'.img: nothing runs, wget receives the whole URL as one argument, the checksum is verified, and a packed .xz variant unpacks the same way.

#17 — command execution reads errors as empty output

Already done on main before this PR (f491f9f, 6bb3035): any key type through paramiko's key loading, the key tried before the password even with an API password set, the host key verified through napalm-device-types' HostKeyCheck (ssh_host_keys / trust on first use, reported to the observer) instead of AutoAddPolicy, and one connect path (_node_ssh, which _run_node_command uses too).

This PR does the rest, in the driver:

  • _exec_ssh_command raises ConnectionException (with the cause) when the command cannot run: a refused login, an unreachable node, a timeout. SshHostKeyMismatch passes through as before. It returned "".
  • No SSH password and no usable key (an API-token-only node): _node_ssh raises before connecting, instead of letting paramiko try whatever keys or agent the process has.
  • Output is stdout. Stderr no longer stands in when stdout is empty (a cat: …: No such file was parsed as content); it is logged at debug.
  • The /nodes/{node}/execute call is gone: it runs a batch of API calls, not a shell, and every command paid for it before SSH.
  • The exit status stays unchecked: the device-types transports this feeds (_run_service_command, _run_kernel_facts_command, …) are defined as "return what it printed", and callers that need the status use _rc. Moving to napalm-unix's run_command is not part of this (see NAPALM/napalm-device-types#35).

The test fixtures that faked the execute endpoint now fake the node's SSH session (NodeShell in conftest.py). README lists the SSH arguments and says what needs SSH.

netOrk

  • get_snmp_config() now raises on token-only nodes. netOrk's SNMP check (netork/jobs/tasks/poll/_snmp.py) already reads an exception as "SSH not available": no false snmp_not_working, and snmp_not_configured comes as info without a Fix button. Before, the None produced the alarm and a Fix button that could not work.
  • get_packages() and the other SSH-backed getters raise there too; the poll catches each and logs <getter>() failed: …. lldpd_not_installed needs a non-empty package list, so it was not raised from these nodes before and is not now.
  • cli() and _send_command() raise instead of returning ""; netOrk calls cli() only for OpenWrt (uci).

Checks

Fixtures only. Red first: the four #43 tests and the seven #17 tests in tests/test_node_exec.py, plus the host-key test that expected an empty read, failed against main. pytest (506 passed), ruff format/check, mypy (no errors in the package), bandit, napalm-standard check and napalm-standard ratchet green; coverage 79.4 % (floor 68).

Closes #43
Closes #17

Two fixes, one commit each. ## #43 — the image URL in the node's root commands `create_vm_from_cloud_init()`'s SSH download path built its node commands (which run as root) from the image URL: the URL wrapped in hand-written single quotes, the cache path containing the URL's basename unquoted in `test -f`, `wget -O`, `mv`, `<algo>sum`, `rm`, the unpack step and `qm importdisk`. - Every interpolated value goes through `shlex.quote` (URL, cache path, `.tmp` path, packed/unpacked paths, the image storage in `qm importdisk`). - The cache file name is the URL's hash plus its basename with every character outside Proxmox's safe set (`[A-Za-z0-9-.+=_]`, the class `_import_volume_name` already used) replaced by `_`. Ordinary image names are unchanged, so existing cache entries stay valid. - The checksum algorithm is part of a command name (`sha256sum`), so it is checked against what Proxmox's `download-url` accepts (md5, sha1, sha224, sha256, sha384, sha512); anything else is a `ValueError` before anything runs on the node. - The file-name helpers move to `napalm_proxmox/image_names.py` (strict mypy): `vm_provision_mixin.py` would otherwise cross the 1000-line hard limit. `_download_cloud_image` drops below 50 lines, so its baseline entry is removed. Tests run the real commands through a local `sh` (the existing `_LocalShell` seam) with a URL `https://example.org/$(reboot)/a b/x';reboot;'.img`: nothing runs, wget receives the whole URL as one argument, the checksum is verified, and a packed `.xz` variant unpacks the same way. ## #17 — command execution reads errors as empty output Already done on main before this PR (f491f9f, 6bb3035): any key type through paramiko's key loading, the key tried before the password even with an API password set, the host key verified through napalm-device-types' `HostKeyCheck` (`ssh_host_keys` / trust on first use, reported to the observer) instead of `AutoAddPolicy`, and one connect path (`_node_ssh`, which `_run_node_command` uses too). This PR does the rest, in the driver: - `_exec_ssh_command` raises `ConnectionException` (with the cause) when the command cannot run: a refused login, an unreachable node, a timeout. `SshHostKeyMismatch` passes through as before. It returned `""`. - No SSH password and no usable key (an API-token-only node): `_node_ssh` raises before connecting, instead of letting paramiko try whatever keys or agent the process has. - Output is stdout. Stderr no longer stands in when stdout is empty (a `cat: …: No such file` was parsed as content); it is logged at debug. - The `/nodes/{node}/execute` call is gone: it runs a batch of API calls, not a shell, and every command paid for it before SSH. - The exit status stays unchecked: the device-types transports this feeds (`_run_service_command`, `_run_kernel_facts_command`, …) are defined as "return what it printed", and callers that need the status use `_rc`. Moving to napalm-unix's `run_command` is not part of this (see NAPALM/napalm-device-types#35). The test fixtures that faked the execute endpoint now fake the node's SSH session (`NodeShell` in `conftest.py`). README lists the SSH arguments and says what needs SSH. ### netOrk - `get_snmp_config()` now raises on token-only nodes. netOrk's SNMP check (`netork/jobs/tasks/poll/_snmp.py`) already reads an exception as "SSH not available": no false `snmp_not_working`, and `snmp_not_configured` comes as info without a Fix button. Before, the `None` produced the alarm and a Fix button that could not work. - `get_packages()` and the other SSH-backed getters raise there too; the poll catches each and logs `<getter>() failed: …`. `lldpd_not_installed` needs a non-empty package list, so it was not raised from these nodes before and is not now. - `cli()` and `_send_command()` raise instead of returning `""`; netOrk calls `cli()` only for OpenWrt (uci). ## Checks Fixtures only. Red first: the four #43 tests and the seven #17 tests in `tests/test_node_exec.py`, plus the host-key test that expected an empty read, failed against main. pytest (506 passed), ruff format/check, mypy (no errors in the package), bandit, `napalm-standard check` and `napalm-standard ratchet` green; coverage 79.4 % (floor 68). Closes #43 Closes #17
christianmanivong added 2 commits 2026-11-02 07:53:02 +00:00
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
fix(ssh): raise when a node command cannot run, drop the execute call
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 40s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 2m53s
driver / publish (pull_request) Skipped
daf1b519eb
_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
christianmanivong merged commit b368ad9856 into main 2026-11-02 08:23:52 +00:00
christianmanivong deleted branch fix/bugs-2026-11-02 2026-11-02 08:23:52 +00:00
Sign in to join this conversation.