From e51ddcd151495296f51e51e1b213e77133e6eac3 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 22:24:30 +0100 Subject: [PATCH] fix(rest): no plain-HTTP login for a switch with a pinned certificate The probe falls back to HTTP when HTTPS does not answer. For a switch known by its certificate that is a downgrade anyone who blocks port 443 can force: the login would go out in clear text, unchecked. With a pinned fingerprint the REST API is skipped then and the CLI transports are tried; without one, plain HTTP is used as before. Refs #17 --- CHANGELOG.md | 4 ++++ README.md | 2 +- napalm_procurve/procurve.py | 11 ++++++++-- tests/quality_baseline.json | 2 +- tests/unit/test_rest_certificate.py | 31 +++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bf9e1..02ed381 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ All notable changes to this driver. The format follows does not fall back to SSH or Telnet then. Every connection of the REST session is held to the certificate checked (#17). - A REST login that failed is no longer retried with certificate verification turned off. +- A switch whose certificate is pinned is not logged in to over plain HTTP: when only + HTTP answers, the REST API is skipped and the CLI transports are tried. Blocking port + 443 would otherwise downgrade the login to clear text. Without a pin, plain HTTP is + used as before (#17). ### Changed - `ssl_verify` (`verify_ssl`, `verify`) no longer has any effect: the REST certificate is diff --git a/README.md b/README.md index dba4484..4ba3c75 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ has one (napalm-device-types): A refused host key or certificate ends `open()` with a `ConnectionException` naming both fingerprints. The driver does not try the next transport then: it would reach the same host with the same password, over Telnet in clear text. Plain HTTP REST has no -certificate to check. +certificate to check: a switch with a pinned certificate is not logged in to over it. ## Switch prerequisites diff --git a/napalm_procurve/procurve.py b/napalm_procurve/procurve.py index 76c7533..eddf673 100644 --- a/napalm_procurve/procurve.py +++ b/napalm_procurve/procurve.py @@ -41,7 +41,7 @@ from napalm_device_types.ssh_host_keys import ( HostKeyCheck, SshHostKeyMismatch, ) -from napalm_device_types.tls_certificates import TlsCertificateMismatch +from napalm_device_types.tls_certificates import CertificateCheck, TlsCertificateMismatch from netmiko import ConnectHandler from netmiko.exceptions import ( NetmikoAuthenticationException, @@ -49,7 +49,7 @@ from netmiko.exceptions import ( ) from napalm_procurve import parsers -from napalm_procurve.api_client import ProcurveApiClient +from napalm_procurve.api_client import HTTPS_PORT, ProcurveApiClient logger = logging.getLogger("napalm_procurve") @@ -275,6 +275,10 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver): logger.warning("REST API not detected on %s — falling back to CLI", self.hostname) self._attempts.append("api: not detected") return False + if proto == "http" and self._certificate_pinned(): + # Port 443 blocked would otherwise downgrade the login to clear text (#17). + self._attempts.append("api: only plain HTTP answered, the certificate is pinned") + return False client = ProcurveApiClient( hostname=self.hostname, @@ -297,6 +301,9 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver): logger.info("Connected to %s via REST API (%s %s)", self.hostname, proto, ver) return True + def _certificate_pinned(self) -> bool: + return CertificateCheck(self._optional_args, self.hostname, HTTPS_PORT).expected is not None + def _netmiko_kwargs(self, legacy: bool = False) -> dict: """Return kwargs for ConnectHandler, merging explicit fields with netmiko_optional_args. diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index da90cab..068daeb 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": 1387 + "napalm_procurve/procurve.py": 1394 }, "untyped_public": [ "napalm_procurve/procurve.py::ProcurveDriver.get_snmp_config" diff --git a/tests/unit/test_rest_certificate.py b/tests/unit/test_rest_certificate.py index 7e76c22..3b09778 100644 --- a/tests/unit/test_rest_certificate.py +++ b/tests/unit/test_rest_certificate.py @@ -209,3 +209,34 @@ class TestUnreachable: driver.open() fetch.assert_not_called() assert driver._transport == "api" + + +class TestNoPlainHttpWhenPinned: + """A switch known by its certificate is not logged in to over plain HTTP. + + Blocking port 443 would otherwise downgrade the login to clear text, unchecked. + """ + + def test_a_pinned_switch_that_answers_only_http_gets_no_rest_login(self): + driver = _driver([], known=SWITCH_FP) + cli = MagicMock() + cli.base_prompt = "switch" + with ( + patch(PROBE, return_value=("v7", "http")), + patch.object(requests.Session, "post") as post, + patch.object(ProcurveDriver, "_ssh_connect", return_value=cli), + ): + driver.open() + post.assert_not_called() + assert driver._transport == "ssh" + assert any("plain HTTP" in a for a in driver._attempts) + + def test_without_a_pin_plain_http_is_used_as_before(self): + driver = _driver([]) + with ( + patch(PROBE, return_value=("v7", "http")), + patch.object(requests.Session, "post", return_value=_login_ok()) as post, + ): + driver.open() + assert post.call_count == 1 + assert driver._transport == "api"