From 1d5399e51ba50c43b92099e463252212f7888b43 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 22:17:59 +0100 Subject: [PATCH] fix(cli): decode the REST CLI answer 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. --- CHANGELOG.md | 2 ++ napalm_procurve/parsers.py | 7 ++++++ napalm_procurve/procurve.py | 6 ++--- tests/quality_baseline.json | 2 +- tests/unit/test_cli.py | 44 +++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/unit/test_cli.py diff --git a/CHANGELOG.md b/CHANGELOG.md index db407ff..5c3c0dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/napalm_procurve/parsers.py b/napalm_procurve/parsers.py index 0ac1a3a..df42e47 100644 --- a/napalm_procurve/parsers.py +++ b/napalm_procurve/parsers.py @@ -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") diff --git a/napalm_procurve/procurve.py b/napalm_procurve/procurve.py index 4c7dbfc..e652673 100644 --- a/napalm_procurve/procurve.py +++ b/napalm_procurve/procurve.py @@ -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 diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 2185629..5afa848 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -27,7 +27,7 @@ "napalm_procurve.procurve" ], "python_file_lines": { - "napalm_procurve/procurve.py": 1392 + "napalm_procurve/procurve.py": 1390 }, "untyped_public": [] } diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py new file mode 100644 index 0000000..e860fa7 --- /dev/null +++ b/tests/unit/test_cli.py @@ -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"}