fix(snmp): open udp/161 in firewalld, not behind it
driver / standard (pull_request) Failing after 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Failing after 1m1s
driver / test (3.13) (pull_request) Failing after 57s
driver / lowest (pull_request) Failing after 30s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m1s
CI / driver (pull_request) Failing after 2m29s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Failing after 14s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Failing after 1m1s
driver / test (3.13) (pull_request) Failing after 57s
driver / lowest (pull_request) Failing after 30s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m1s
CI / driver (pull_request) Failing after 2m29s
driver / publish (pull_request) Skipped
On a host running firewalld, iptables is installed too, so the SNMP fix inserted an iptables ACCEPT. firewalld keeps its own nftables table, whose reject wins over it, and the step reported the port open while SNMP stayed blocked. With firewalld active, the step now adds a rich rule per allowed source to the default zone (which takes interfaces in no zone, such as a WireGuard interface) and to every active zone, at runtime and permanently, each only when missing. No reload, which would drop rules others keep at runtime. Closes #77
This commit is contained in:
1 parent
235c1e9a91
commit
aab25fb049
3 files changed
+134
-4
No files matched your search
@@ -69,17 +69,22 @@ def driver():
|
||||
class TestSnmpAgentSteps:
|
||||
SETTINGS = {"community": "c0mm", "allowed_sources": ["10.0.0.5"]}
|
||||
|
||||
def _record(self, driver, status=0, firewall="ufw"):
|
||||
def _record(self, driver, status=0, firewall="ufw", firewalld=False):
|
||||
ran: list[str] = []
|
||||
|
||||
def sudo_status(command, read_timeout=100):
|
||||
ran.append(command)
|
||||
return ("", status)
|
||||
|
||||
def send(command, read_timeout=100):
|
||||
if command == "systemctl is-active firewalld 2>/dev/null":
|
||||
return "active" if firewalld else "inactive"
|
||||
return (
|
||||
f"/usr/sbin/{firewall}" if command == f"command -v {firewall} 2>/dev/null" else ""
|
||||
)
|
||||
|
||||
driver._sudo_status = sudo_status
|
||||
driver._send = lambda command, read_timeout=100: (
|
||||
f"/usr/sbin/{firewall}" if command == f"command -v {firewall} 2>/dev/null" else ""
|
||||
)
|
||||
driver._send = send
|
||||
driver._install_file_as_root = MagicMock(
|
||||
return_value=MagicMock(exit_code=0, stdout="", stderr="")
|
||||
)
|
||||
@@ -105,6 +110,88 @@ class TestSnmpAgentSteps:
|
||||
|
||||
assert "iptables -I INPUT -s 10.0.0.5/32 -p udp --dport 161 -j ACCEPT" in ran[1]
|
||||
|
||||
def test_a_running_firewalld_gets_a_rich_rule_not_an_iptables_one(self, driver):
|
||||
"""An iptables ACCEPT does not lift firewalld's reject: with nftables both
|
||||
tables see the packet, and the drop in either wins (Oracle Linux, RHEL)."""
|
||||
ran = self._record(driver, firewall="iptables", firewalld=True)
|
||||
|
||||
result = driver.configure_snmp_agent(self.SETTINGS)
|
||||
|
||||
assert result["success"] is True
|
||||
assert "iptables" not in ran[1]
|
||||
assert (
|
||||
'rule family="ipv4" source address="10.0.0.5/32" port port="161" protocol="udp" accept'
|
||||
) in ran[1]
|
||||
assert "--permanent" in ran[1]
|
||||
assert "--reload" not in ran[1]
|
||||
|
||||
def test_the_firewalld_rule_goes_to_the_default_and_every_active_zone(self, tmp_path):
|
||||
"""An interface in no zone (a WireGuard wg0) is the default zone's; one in a
|
||||
zone of its own is that zone's. The rule goes in now and stays after a reload."""
|
||||
from napalm_linux.linux import firewalld_allow_snmp
|
||||
|
||||
log = tmp_path / "calls"
|
||||
fake = tmp_path / "firewall-cmd"
|
||||
fake.write_text(
|
||||
"#!/bin/sh\n"
|
||||
f'echo "$*" >> {log}\n'
|
||||
'case "$*" in\n'
|
||||
" --get-default-zone) echo public ;;\n"
|
||||
' --get-active-zones) printf "docker\\n interfaces: docker0\\n'
|
||||
'public\\n interfaces: enp0s6\\n" ;;\n'
|
||||
" *--query-rich-rule*) exit 1 ;;\n"
|
||||
"esac\n"
|
||||
)
|
||||
fake.chmod(0o755)
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
done = subprocess.run(
|
||||
["sh", "-c", firewalld_allow_snmp(["10.0.0.5/32", "fd00::/64"])],
|
||||
env={**os.environ, "PATH": f"{tmp_path}:{os.environ['PATH']}"},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
assert done.returncode == 0, done.stdout + done.stderr
|
||||
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'
|
||||
for zone in ("public", "docker"):
|
||||
for rule in (v4, v6):
|
||||
assert f"--zone={zone} --add-rich-rule={rule}" in adds
|
||||
assert f"--permanent --zone={zone} --add-rich-rule={rule}" in adds
|
||||
assert len(adds) == 8
|
||||
|
||||
def test_a_rule_firewalld_holds_already_is_not_added_twice(self, tmp_path):
|
||||
from napalm_linux.linux import firewalld_allow_snmp
|
||||
|
||||
log = tmp_path / "calls"
|
||||
fake = tmp_path / "firewall-cmd"
|
||||
fake.write_text(
|
||||
"#!/bin/sh\n"
|
||||
f'echo "$*" >> {log}\n'
|
||||
'case "$*" in\n'
|
||||
" --get-default-zone) echo public ;;\n"
|
||||
" --get-active-zones) ;;\n"
|
||||
"esac\n"
|
||||
)
|
||||
fake.chmod(0o755)
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
done = subprocess.run(
|
||||
["sh", "-c", firewalld_allow_snmp(["10.0.0.5/32"])],
|
||||
env={**os.environ, "PATH": f"{tmp_path}:{os.environ['PATH']}"},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
assert done.returncode == 0
|
||||
assert "--add-rich-rule" not in log.read_text()
|
||||
|
||||
def test_no_sources_change_no_firewall(self, driver):
|
||||
ran = self._record(driver)
|
||||
|
||||
|
||||
Reference in new issue
Block a user