fix(ssh): verify the host key netOrk expects, report the one seen (NetOrk/netork#1167)
CI / test (3.10) (push) Successful in 33s
CI / test (3.11) (push) Successful in 32s
CI / test (3.12) (push) Successful in 31s
CI / test (3.10) (pull_request) Successful in 27s
CI / test (3.11) (pull_request) Successful in 24s
CI / test (3.12) (pull_request) Successful in 26s
CI / test (3.10) (push) Successful in 33s
CI / test (3.11) (push) Successful in 32s
CI / test (3.12) (push) Successful in 31s
CI / test (3.10) (pull_request) Successful in 27s
CI / test (3.11) (pull_request) Successful in 24s
CI / test (3.12) (pull_request) Successful in 26s
The driver accepted any SSH host key. It now verifies through napalm-device-types' HostKeyCheck (>= 4.19.0): with a known key in optional_args["ssh_host_keys"] any other key is refused before login, and every key seen is reported to optional_args["ssh_host_key_observer"]. Without either argument it connects as before (first use).
This commit is contained in:
1 parent
8062188b12
commit
2de5844f92
3 files changed
+101
-10
No files matched your search
@@ -46,6 +46,7 @@ from napalm.base.exceptions import (
|
||||
ReplaceConfigException,
|
||||
)
|
||||
from napalm.base.netmiko_helpers import netmiko_args
|
||||
from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch
|
||||
import napalm.base.constants as C
|
||||
|
||||
|
||||
@@ -126,6 +127,8 @@ class NetgearSmartDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
|
||||
optional_args.setdefault("banner_timeout", 20)
|
||||
|
||||
self.netmiko_optional_args = netmiko_args(optional_args)
|
||||
# The host key to expect, and where the one seen is reported (netork#1167).
|
||||
self._host_key = HostKeyCheck(optional_args, hostname, self.port)
|
||||
|
||||
# Config management state
|
||||
self._candidate_config: Optional[str] = None
|
||||
@@ -146,16 +149,20 @@ class NetgearSmartDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
|
||||
def open(self) -> None:
|
||||
"""Open an SSH connection to the device."""
|
||||
try:
|
||||
self.device = ConnectHandler(
|
||||
device_type=self.NETMIKO_DEVICE_TYPE,
|
||||
host=self.hostname,
|
||||
username=self.username,
|
||||
password=self.password,
|
||||
timeout=self.timeout,
|
||||
**self.netmiko_optional_args,
|
||||
)
|
||||
with self._host_key.netmiko(self.netmiko_optional_args) as netmiko_kwargs:
|
||||
self.device = ConnectHandler(
|
||||
device_type=self.NETMIKO_DEVICE_TYPE,
|
||||
host=self.hostname,
|
||||
username=self.username,
|
||||
password=self.password,
|
||||
timeout=self.timeout,
|
||||
**netmiko_kwargs,
|
||||
)
|
||||
self._host_key.observe(self.device.remote_conn_pre.get_transport())
|
||||
if not self.force_no_enable:
|
||||
self.device.enable()
|
||||
except SshHostKeyMismatch:
|
||||
raise # refused before the login; nothing was sent
|
||||
except NetmikoTimeoutException as exc:
|
||||
raise ConnectionException(f"Cannot connect to {self.hostname}: {exc}") from exc
|
||||
except NetmikoAuthenticationException as exc:
|
||||
|
||||
Reference in new issue
Block a user