fix: what bandit and the remaining lint rules found

- set_interface: the except Exception: pass around reading the old
  untagged VLANs now logs at debug level (B110), with a module logger.
- nosec for the empty password field of get_users (B105, now a named
  constant); a false positive.
- Two docstrings quoting tabular device output keep their lines (noqa
  E501); a long docstring and an error message are shortened or split;
  the unused duplex column is named _duplex.
- tests: a get_facts test named SHOW_INTERFACE, which does not exist; it
  is SHOW_INTERFACE_STATUS. raise ... from exc.
This commit is contained in:
Christian Manivong committed 2026-10-23 08:46:36 +02:00
1 parent e53a4b4cc6
commit bef2f23ab2
3 files changed
+16 -9

No files matched your search

+14 -7
View File
@@ -18,6 +18,7 @@ Netmiko device_type: ``tplink_jetstream``
from __future__ import annotations
import logging
import re
from typing import Any, ClassVar
@@ -38,6 +39,11 @@ from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch
from netmiko import ConnectHandler
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException
logger = logging.getLogger(__name__)
#: get_users' password field: the switch never shows a password, so it stays empty.
_PASSWORD_NOT_SHOWN = "" # nosec B105
class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
"""NAPALM driver for TP-Link Jetstream managed switches."""
@@ -372,7 +378,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
Port State Speed Duplex FlowCtrl Description
Gi1/0/1 Enable Auto Auto Disable uplink
"""
""" # noqa: E501 - recorded device output keeps its lines
jumbo_out = self._send_command("show jumbo")
global_mtu = 1518
m = re.search(r"(\d+)", jumbo_out)
@@ -446,7 +452,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
)
if not match:
continue
port, status, speed, duplex, _, _, _, _, description = (
port, status, speed, _duplex, _, _, _, _, description = (
match.group(1),
match.group(2),
match.group(3),
@@ -703,7 +709,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
Port Device ID Port ID management address Port Description System Name
---- ------------ -------- ------------------ ---------------- -----------
Gi1/0/9 64:E8:81:E1:29:00 2 10.7.0.10 2 swt-l0-1-10
"""
""" # noqa: E501 - recorded device output keeps its lines
neighbors: dict[str, list[dict]] = {}
for row in self._get_lldp_table():
neighbors.setdefault(row["local_port"], []).append(
@@ -1026,7 +1032,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
@staticmethod
def _to_cli_interface(port: str) -> str:
"""Convert abbreviated port name (e.g. ``Gi1/0/1``) to CLI form (``gigabitEthernet 1/0/1``)."""
"""Convert a short port name (``Gi1/0/1``) to CLI form (``gigabitEthernet 1/0/1``)."""
_PREFIX_MAP = {
"gi": "gigabitEthernet",
"te": "ten-gigabitEthernet",
@@ -1150,7 +1156,8 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
"""
if self._backup_config is None:
raise CommandErrorException(
"No backup configuration available – commit_config has not been called in this session."
"No backup configuration available – "
"commit_config has not been called in this session."
)
current = self._send_command("show running-config")
@@ -1423,7 +1430,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
# Map TP-Link roles to NAPALM privilege levels (1-15)
level = 15 if role == "admin" else 1
users[username] = {"level": level, "password": "", "sshkeys": []}
users[username] = {"level": level, "password": _PASSWORD_NOT_SHOWN, "sshkeys": []}
return users
@@ -1618,7 +1625,7 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver):
if interface in vlan_info.get("untagged", []) and vid_str != new_vid:
old_untagged_vlans.append(vid_str)
except Exception:
pass
logger.debug("reading the old untagged VLANs: ignored", exc_info=True)
self._enter_config_mode()
try:
+1 -1
View File
@@ -96,7 +96,7 @@ class TestGetFacts:
# _get_interface_list needs show interface
driver.device.send_command.side_effect = lambda cmd, **kw: (
SHOW_SYSTEM_INFO if "system-info" in cmd else SHOW_INTERFACE
SHOW_SYSTEM_INFO if "system-info" in cmd else SHOW_INTERFACE_STATUS
)
facts = driver.get_facts()
+1 -1
View File
@@ -76,7 +76,7 @@ def test_a_changed_key_is_refused():
try:
raise paramiko.BadHostKeyException("10.0.0.8", SERVER_KEY, OTHER_KEY)
except paramiko.SSHException as exc: # what netmiko does with it
raise NetmikoTimeoutException(f"A paramiko SSHException occurred: {exc}")
raise NetmikoTimeoutException(f"A paramiko SSHException occurred: {exc}") from exc
driver = _driver(seen, SshHostKey.of(OTHER_KEY))
with patch(HANDLER, side_effect=refuse):