From fc4f08302c3afd10f505e935a34d95bcf4a84993 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 12:24:14 +0100 Subject: [PATCH] refactor(firewall): SNMP's firewall step lives with the others in OpenPortMixin --- napalm_linux/_open_port.py | 8 +++++++- napalm_linux/linux.py | 4 ---- tests/quality_baseline.json | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/napalm_linux/_open_port.py b/napalm_linux/_open_port.py index dbf9082..0d2e52d 100644 --- a/napalm_linux/_open_port.py +++ b/napalm_linux/_open_port.py @@ -13,6 +13,7 @@ from typing import TYPE_CHECKING, Literal from napalm_device_types import ( OPENED_PORTS_RECORD, + SNMP_PURPOSE, FixStepOutcomeDict, OpenedRuleDict, opened_rule_line, @@ -24,6 +25,7 @@ from napalm_device_types import ( ufw_allow_recorded_command, ufw_delete_command, ) +from napalm_device_types.models import SnmpAgentSettingsDict from napalm_linux._snmp_agent import ( _source, @@ -83,7 +85,7 @@ def _count_command(firewall: Firewall, port: int, proto: str) -> str: class OpenPortMixin: - """``_allow_sources`` and ``_revoke_sources``: the shared firewall steps.""" + """``_allow_sources`` and ``_revoke_sources``: the shared firewall steps, and SNMP's.""" def _firewall(self) -> Firewall: """The firewall this host runs. firewalld first: its hosts have iptables too, and @@ -120,6 +122,10 @@ class OpenPortMixin: done = f"{proto}/{port} open for {', '.join(sources)}" return self._root_outcome(" && ".join(rules), 30, done) + def _allow_snmp_sources(self, settings: SnmpAgentSettingsDict) -> FixStepOutcomeDict: + """SNMP's firewall step (napalm-device-types ``SnmpAgentMixin``): udp/161.""" + return self._allow_sources(settings.get("allowed_sources", []), 161, "udp", SNMP_PURPOSE) + def _revoke_sources(self, purpose: str, port: int, proto: str) -> FixStepOutcomeDict: """Remove the rules recorded under *purpose*, whatever port they opened, and nothing else; report what is left for *proto*/*port*. diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index b0903e4..fe67862 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -30,7 +30,6 @@ from napalm_device_types import ( DNF_INSTALLED_CVES_COMMAND, DNF_SECURITY_COMMAND, LLDPCTL_KEYVALUE_COMMAND, - SNMP_PURPOSE, AptProxyMixin, ByteStream, CommandResult, @@ -1798,9 +1797,6 @@ class LinuxDriver( return {"ok": False, "output": (written.stdout or written.stderr).strip()[-300:]} return {"ok": True, "output": f"wrote {_SNMPD_CONF}"} - def _allow_snmp_sources(self, settings: SnmpAgentSettingsDict) -> FixStepOutcomeDict: - return self._allow_sources(settings.get("allowed_sources", []), 161, "udp", SNMP_PURPOSE) - def _start_snmp_agent(self) -> FixStepOutcomeDict: return self._root_outcome( "systemctl enable snmpd && systemctl restart snmpd && systemctl is-active snmpd", diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 06ea2db..764b710 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -20,7 +20,7 @@ "napalm_linux.linux" ], "python_file_lines": { - "napalm_linux/linux.py": 1993 + "napalm_linux/linux.py": 1990 }, "untyped_public": [] }