fix(cli): decode the REST CLI answer
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 49s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m25s
driver / publish (pull_request) Skipped

Over the REST API, cli() returned the /cli endpoint's
result_base64_encoded field as it came: base64 instead of the command's
output. It now decodes it and drops the NUL byte the switch ends it with,
in the same parser the SNMP read over REST uses.

New finding, confirmed by the show snmp-server answer recorded from a
ProCurve switch; no issue filed.
This commit is contained in:
Christian Manivong committed 2026-11-01 22:17:59 +01:00
1 parent 03cf40de99
commit 1d5399e51b
5 files changed
+56 -5

No files matched your search

+2
View File
@@ -17,6 +17,8 @@ All notable changes to this driver. The format follows
a session log object, which older releases do not accept.
### Fixed
- `cli()` over the REST API returns the command's output. It returned the API's
base64 encoding of it as it came, NUL byte included.
- `configure_snmp_agent` over the REST API opens its temporary SSH session on port 22.
It used `optional_args["port"]`, which names the REST API's port when REST is the
transport, so the SNMP setup failed on switches managed over REST.
+7
View File
@@ -7,6 +7,7 @@ command output strings. The same parsers are shared by the SSH and Telnet
backends.
"""
import base64
import re
#: get_users' password field: the switch never shows a password, so it stays empty.
@@ -927,3 +928,9 @@ def derive_port_pvids(vlans: dict[int, dict]) -> dict[str, int]:
pvids[port] = 1
return pvids
def parse_cli_answer(answer: dict[str, str]) -> str:
"""The output in a REST ``/cli`` answer: base64, the switch ends it with a NUL byte."""
encoded = answer.get("result_base64_encoded", "")
return base64.b64decode(encoded).decode("utf-8", "replace").rstrip("\x00")
+2 -4
View File
@@ -15,7 +15,6 @@ A specific transport can be forced with ``optional_args={"transport": "ssh"}``.
Netmiko device type: ``hp_procurve`` / ``hp_procurve_telnet``
"""
import base64
import logging
import re
from typing import Any, ClassVar
@@ -1146,7 +1145,7 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver):
for cmd in commands:
resp = self._api.post("cli", json={"cmd": cmd})
if resp.ok:
results[cmd] = resp.json().get("result_base64_encoded", "")
results[cmd] = parsers.parse_cli_answer(resp.json())
else:
results[cmd] = f"Error: HTTP {resp.status_code}"
return results
@@ -1238,8 +1237,7 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver):
resp = self._api.post("cli", json={"cmd": command})
if not resp.ok or resp.json().get("status") == "CCS_FAILURE":
raise CommandErrorException(f"{command!r} over REST: HTTP {resp.status_code}")
encoded = resp.json().get("result_base64_encoded", "")
return base64.b64decode(encoded).decode("utf-8", "replace")
return parsers.parse_cli_answer(resp.json())
# ------------------------------------------------------------------
# SNMP agent (napalm-device-types SnmpAgentMixin): the caller's values
+1 -1
View File
@@ -27,7 +27,7 @@
"napalm_procurve.procurve"
],
"python_file_lines": {
"napalm_procurve/procurve.py": 1392
"napalm_procurve/procurve.py": 1390
},
"untyped_public": []
}
+44
View File
@@ -0,0 +1,44 @@
"""cli() over REST returns the command's output, not the API's base64 of it.
The REST ``/cli`` endpoint answers ``{"result_base64_encoded": ...}``: the output in
base64, ending in a NUL byte. cli() returned that string as it came.
"""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock
from napalm_procurve.procurve import ProcurveDriver
FIXTURES = Path(__file__).parent.parent / "fixtures"
SHOW_SNMP_SERVER = (FIXTURES / "procurve-show-snmp-server.txt").read_text(encoding="utf-8")
SHOW_SNMP_SERVER_REST = (FIXTURES / "procurve-rest-cli-show-snmp-server.b64").read_text().strip()
def _rest_driver(resp: MagicMock) -> ProcurveDriver:
driver = ProcurveDriver("10.0.0.40", "manager", "s3cret")
driver._transport = "api"
driver._api = MagicMock()
driver._api.post.return_value = resp
return driver
def test_over_rest_cli_returns_the_decoded_output():
resp = MagicMock(ok=True, status_code=200)
resp.json.return_value = {
"cmd": "show snmp-server",
"result_base64_encoded": SHOW_SNMP_SERVER_REST,
"status": "CCS_SUCCESS",
}
out = _rest_driver(resp).cli(["show snmp-server"])
assert out == {"show snmp-server": SHOW_SNMP_SERVER}
def test_over_rest_cli_names_a_failed_request():
resp = MagicMock(ok=False, status_code=401)
assert _rest_driver(resp).cli(["show version"]) == {"show version": "Error: HTTP 401"}