fix(rest): no plain-HTTP login for a switch with a pinned certificate
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Failing after 1m9s
driver / test (3.13) (pull_request) Failing after 1m0s
driver / lowest (pull_request) Failing after 29s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m17s
CI / driver (pull_request) Failing after 3m1s
driver / publish (pull_request) Skipped

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
This commit is contained in:
Christian Manivong committed 2026-11-01 22:24:30 +01:00
1 parent cb7268b89c
commit e51ddcd151
5 files changed
+46 -4

No files matched your search

+4
View File
@@ -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 does not fall back to SSH or Telnet then. Every connection of the REST session is held
to the certificate checked (#17). to the certificate checked (#17).
- A REST login that failed is no longer retried with certificate verification turned off. - 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 ### Changed
- `ssl_verify` (`verify_ssl`, `verify`) no longer has any effect: the REST certificate is - `ssl_verify` (`verify_ssl`, `verify`) no longer has any effect: the REST certificate is
+1 -1
View File
@@ -33,7 +33,7 @@ has one (napalm-device-types):
A refused host key or certificate ends `open()` with a `ConnectionException` naming both 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 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 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 ## Switch prerequisites
+9 -2
View File
@@ -41,7 +41,7 @@ from napalm_device_types.ssh_host_keys import (
HostKeyCheck, HostKeyCheck,
SshHostKeyMismatch, SshHostKeyMismatch,
) )
from napalm_device_types.tls_certificates import TlsCertificateMismatch from napalm_device_types.tls_certificates import CertificateCheck, TlsCertificateMismatch
from netmiko import ConnectHandler from netmiko import ConnectHandler
from netmiko.exceptions import ( from netmiko.exceptions import (
NetmikoAuthenticationException, NetmikoAuthenticationException,
@@ -49,7 +49,7 @@ from netmiko.exceptions import (
) )
from napalm_procurve import parsers 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") 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) logger.warning("REST API not detected on %s — falling back to CLI", self.hostname)
self._attempts.append("api: not detected") self._attempts.append("api: not detected")
return False 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( client = ProcurveApiClient(
hostname=self.hostname, 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) logger.info("Connected to %s via REST API (%s %s)", self.hostname, proto, ver)
return True 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: def _netmiko_kwargs(self, legacy: bool = False) -> dict:
"""Return kwargs for ConnectHandler, merging explicit fields with netmiko_optional_args. """Return kwargs for ConnectHandler, merging explicit fields with netmiko_optional_args.
+1 -1
View File
@@ -27,7 +27,7 @@
"napalm_procurve.procurve" "napalm_procurve.procurve"
], ],
"python_file_lines": { "python_file_lines": {
"napalm_procurve/procurve.py": 1387 "napalm_procurve/procurve.py": 1394
}, },
"untyped_public": [ "untyped_public": [
"napalm_procurve/procurve.py::ProcurveDriver.get_snmp_config" "napalm_procurve/procurve.py::ProcurveDriver.get_snmp_config"
+31
View File
@@ -209,3 +209,34 @@ class TestUnreachable:
driver.open() driver.open()
fetch.assert_not_called() fetch.assert_not_called()
assert driver._transport == "api" 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"