fix(rest): verify the API certificate by its fingerprint

The REST login never verified the switch in practice: with ssl_verify
off nothing was checked, and with it on a self-signed certificate (the
one every ProCurve ships) failed the CA check and the login was retried
with verification off. Either way the password went to whatever answered
on port 443.

The certificate is now checked by its SHA-256 fingerprint, trust on
first use (napalm-device-types tls_certificates): the caller passes the
fingerprint to expect in optional_args["tls_fingerprints"] and receives
every one seen through optional_args["tls_certificate_observer"]. A
different certificate ends open() with TlsCertificateMismatch before the
password is sent, without trying SSH or Telnet, which would reach the
same host with the same password. The session is pinned to the checked
certificate for every later connection. ssl_verify no longer has an
effect, and the unverified retry is gone.

Closes #17
This commit is contained in:
Christian Manivong committed 2026-11-01 22:18:33 +01:00
1 parent 58428042f1
commit cb7268b89c
6 files changed
+320 -54

No files matched your search

+19 -1
View File
@@ -17,6 +17,24 @@ The driver probes transports in order and uses the first one that succeeds:
Override the transport with `optional_args={"transport": "ssh_legacy"}` to skip
auto-detection.
## Host verification
The switch's identity is checked before any password is sent, on every transport that
has one (napalm-device-types):
- **SSH:** the host key, trust on first use: `optional_args["ssh_host_keys"]` holds the
key to expect, `optional_args["ssh_host_key_observer"]` receives the one seen.
- **REST API over HTTPS:** the certificate, by its SHA-256 fingerprint and not by a CA
(ProCurves ship self-signed certificates), trust on first use:
`optional_args["tls_fingerprints"]` = `{"443": "SHA256:AB:CD:…"}` holds the fingerprint
to expect, `optional_args["tls_certificate_observer"]` receives the one seen. Without a
fingerprint the first certificate is accepted.
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.
## Switch prerequisites
### REST API (newer switches, e.g. 2530, 2540)
@@ -63,7 +81,7 @@ with Driver(
optional_args={
# "transport": "ssh", # force transport (api/ssh/ssh_legacy/telnet)
# "port": 22,
# "ssl_verify": False, # disable SSL cert check for API
# "tls_fingerprints": {"443": "SHA256:AB:CD:…"}, # REST certificate to expect
# "api_version": "v7", # API version hint (v3/v6/v7)
# "secret": "enablepassword", # enable password for CLI
},