From d14c11e9911acddf69d9a4e8554e544eb0030da9 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Fri, 30 Oct 2026 09:17:48 +0100 Subject: [PATCH] fix(ssh): keep the key exchange off DH group16 The switch offers diffie-hellman-group14-sha256 and group16-sha512 only, and paramiko picks group16. On an SG2210P with firmware 5.30 that 4096-bit exchange takes 9 to 16 seconds, against netmiko's 10-second conn_timeout: paramiko then tries to log in on a transport whose key exchange has not finished and fails with "No existing session", so every other poll failed and netOrk flapped the switch between online and offline. group14 takes 1.5 seconds. group16 is now disabled by default; a disabled_algorithms passed in optional_args still wins. The netmiko arguments move into _ssh.py, so tplink_jetstream.py keeps its recorded length. --- CHANGELOG.md | 7 ++++++ napalm_tplink_jetstream/_ssh.py | 19 +++++++++++++++ napalm_tplink_jetstream/tplink_jetstream.py | 8 +++---- tests/unit/test_ssh_kex.py | 26 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 napalm_tplink_jetstream/_ssh.py create mode 100644 tests/unit/test_ssh_kex.py 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 -- 2.54.1