feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)

Every rule the firewall step adds in firewalld, ufw or iptables is recorded in
napalm-device-types' opened_ports record, only when the firewall did not have
it before. For an active-only agent, _revoke_zabbix_agent_sources removes
exactly the agent's recorded rules and reports what is left for the port.
This commit is contained in:
Christian Manivong committed 2026-11-26 08:19:19 +01:00
1 parent 04ff633290
commit a3b9e4a02d
8 files changed
+516 -37

No files matched your search

+21 -3
View File
@@ -15,6 +15,15 @@ from napalm_linux import LinuxDriver
from napalm_linux.linux import render_snmpd_conf
def _local_record(command: str, record) -> str:
"""*command* with the record of opened rules at *record* instead of root's path."""
from napalm_device_types import OPENED_PORTS_RECORD
return command.replace(
"install -d -m 755 -o root -g root /var/lib/netork", f"mkdir -p {record.parent}"
).replace(OPENED_PORTS_RECORD, str(record))
class TestSnmpdConf:
def test_one_read_only_line_per_allowed_source(self):
conf = render_snmpd_conf(
@@ -100,7 +109,7 @@ class TestSnmpAgentSteps:
assert "apt-get install -y snmpd snmp" in ran[0]
path, content = driver._install_file_as_root.call_args.args
assert path == "/etc/snmp/snmpd.conf" and "rocommunity c0mm 10.0.0.5/32" in content
assert ran[1] == "ufw allow from 10.0.0.5/32 to any port 161 proto udp"
assert "LC_ALL=C ufw allow from 10.0.0.5/32 to any port 161 proto udp" in ran[1]
assert "systemctl restart snmpd" in ran[2]
def test_the_firewall_opens_only_for_the_given_sources_with_iptables(self, driver):
@@ -147,14 +156,20 @@ class TestSnmpAgentSteps:
import os
import subprocess
record = tmp_path / "netork" / "opened-ports"
done = subprocess.run(
["sh", "-c", firewalld_allow_snmp(["10.0.0.5/32", "fd00::/64"])],
["sh", "-c", _local_record(firewalld_allow_snmp(["10.0.0.5/32", "fd00::/64"]), record)],
env={**os.environ, "PATH": f"{tmp_path}:{os.environ['PATH']}"},
capture_output=True,
text=True,
)
assert done.returncode == 0, done.stdout + done.stderr
# Each rule it added is recorded with its zone and configuration (#2693).
recorded = record.read_text().splitlines()
assert "v1 snmp firewalld udp 161 10.0.0.5/32 docker permanent" in recorded
assert "v1 snmp firewalld udp 161 fd00::/64 public runtime" in recorded
assert len(recorded) == 8
adds = [line for line in log.read_text().splitlines() if "--add-rich-rule" in line]
v4 = 'rule family="ipv4" source address="10.0.0.5/32" port port="161" protocol="udp" accept'
v6 = 'rule family="ipv6" source address="fd00::/64" port port="161" protocol="udp" accept'
@@ -182,8 +197,9 @@ class TestSnmpAgentSteps:
import os
import subprocess
record = tmp_path / "netork" / "opened-ports"
done = subprocess.run(
["sh", "-c", firewalld_allow_snmp(["10.0.0.5/32"])],
["sh", "-c", _local_record(firewalld_allow_snmp(["10.0.0.5/32"]), record)],
env={**os.environ, "PATH": f"{tmp_path}:{os.environ['PATH']}"},
capture_output=True,
text=True,
@@ -191,6 +207,8 @@ class TestSnmpAgentSteps:
assert done.returncode == 0
assert "--add-rich-rule" not in log.read_text()
# A rule the zone had is someone else's: not recorded (#2693).
assert not record.exists()
def test_no_sources_change_no_firewall(self, driver):
ran = self._record(driver)