diff --git a/CHANGELOG.md b/CHANGELOG.md index ca8f58a..77a221b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ All notable changes to this driver. The format follows ## [Unreleased] +### Fixed +- The SSH key exchange no longer uses `diffie-hellman-group16-sha512`. On an SG2210P + with firmware 5.30 its 4096-bit exchange took 9 to 16 seconds, against netmiko's + 10-second `conn_timeout`, so every other connection failed and the switch flapped + between online and offline. `diffie-hellman-group14-sha256` takes 1.5 seconds. A + `disabled_algorithms` in `optional_args` still takes precedence. + ### Changed - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). diff --git a/napalm_tplink_jetstream/_ssh.py b/napalm_tplink_jetstream/_ssh.py new file mode 100644 index 0000000..fd8d84a --- /dev/null +++ b/napalm_tplink_jetstream/_ssh.py @@ -0,0 +1,19 @@ +"""What the driver hands netmiko for the SSH connection.""" + +from __future__ import annotations + +from typing import Any + +from napalm.base.netmiko_helpers import netmiko_args + +#: The switch offers DH group14 and group16 only, and paramiko picks group16. Its +#: 4096-bit exchange takes 9-16 s on an SG2210P with firmware 5.30, against +#: netmiko's 10 s conn_timeout; group14 takes 1.5 s. +SLOW_KEX: dict[str, list[str]] = {"kex": ["diffie-hellman-group16-sha512"]} + + +def netmiko_connect_args(optional_args: dict[str, Any]) -> dict[str, Any]: + """netmiko's share of ``optional_args``, without group16 unless the caller says otherwise.""" + args: dict[str, Any] = netmiko_args(optional_args) + args.setdefault("disabled_algorithms", SLOW_KEX) + return args diff --git a/napalm_tplink_jetstream/tplink_jetstream.py b/napalm_tplink_jetstream/tplink_jetstream.py index 402ab8a..c20ccd9 100644 --- a/napalm_tplink_jetstream/tplink_jetstream.py +++ b/napalm_tplink_jetstream/tplink_jetstream.py @@ -32,13 +32,14 @@ from napalm.base.exceptions import ( MergeConfigException, ReplaceConfigException, ) -from napalm.base.netmiko_helpers import netmiko_args from napalm_device_types import ConfigLifecycleMixin, FingerprintRule, SnmpAgentMixin, SwitchDriver from napalm_device_types.models import InterfaceConfigDict, VlanConfigDict from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException +from napalm_tplink_jetstream._ssh import netmiko_connect_args + logger = logging.getLogger(__name__) #: get_users' password field: the switch never shows a password, so it stays empty. @@ -96,13 +97,12 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): self.timeout = timeout self.device: ConnectHandler | None = None - if optional_args is None: - optional_args = {} + optional_args = optional_args or {} self.force_no_enable = optional_args.get("force_no_enable", False) self.port = optional_args.get("port", 22) self.use_canonical_interface = optional_args.get("canonical_int_fmt", False) - self.netmiko_optional_args = netmiko_args(optional_args) + self.netmiko_optional_args = netmiko_connect_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) diff --git a/tests/unit/test_ssh_kex.py b/tests/unit/test_ssh_kex.py new file mode 100644 index 0000000..44ee7b4 --- /dev/null +++ b/tests/unit/test_ssh_kex.py @@ -0,0 +1,26 @@ +"""open() keeps the switch off the 4096-bit DH group, whose key exchange outlasts conn_timeout.""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver + +HANDLER = "napalm_tplink_jetstream.tplink_jetstream.ConnectHandler" + + +def _connect_kwargs(optional_args: dict | None = None) -> dict: + with patch(HANDLER, return_value=MagicMock()) as handler: + TPLinkJetstreamDriver("10.0.0.8", "admin", "s3cret", optional_args=optional_args).open() + return handler.call_args.kwargs + + +def test_group16_is_disabled_by_default(): + kwargs = _connect_kwargs() + assert kwargs["disabled_algorithms"] == {"kex": ["diffie-hellman-group16-sha512"]} + + +def test_disabled_algorithms_from_the_caller_win(): + own = {"kex": [], "ciphers": ["3des-cbc"]} + kwargs = _connect_kwargs({"disabled_algorithms": own}) + assert kwargs["disabled_algorithms"] == own