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:
1 parent
04ff633290
commit
a3b9e4a02d
8 files changed
+516
-37
No files matched your search
@@ -0,0 +1,114 @@
|
||||
"""The firewall step's shell, run with fake firewalls (NetOrk/netork#2693)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_linux._open_port import _count_command, _iptables_allow, _remove_command
|
||||
from tests.test_fix_templates import _local_record
|
||||
|
||||
|
||||
def _run(tmp_path, command: str, tools: dict[str, str]):
|
||||
"""Run *command* with each of *tools* a fake answering its shell body."""
|
||||
bin_dir = tmp_path / "bin"
|
||||
bin_dir.mkdir(exist_ok=True)
|
||||
for name, body in tools.items():
|
||||
fake = bin_dir / name
|
||||
fake.write_text(f"#!/bin/sh\n{body}\n")
|
||||
fake.chmod(0o755)
|
||||
record = tmp_path / "netork" / "opened-ports"
|
||||
done = subprocess.run(
|
||||
["sh", "-c", _local_record(command, record)],
|
||||
env={**os.environ, "PATH": f"{bin_dir}:{os.environ['PATH']}"},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return done, (record.read_text() if record.exists() else "")
|
||||
|
||||
|
||||
class TestIptables:
|
||||
def test_a_rule_the_check_did_not_find_is_added_and_recorded(self, tmp_path):
|
||||
done, record = _run(
|
||||
tmp_path,
|
||||
_iptables_allow("10.0.0.5/32", 10050, "tcp", "zabbix-agent"),
|
||||
{"iptables": 'case "$1" in -C) exit 1 ;; esac'},
|
||||
)
|
||||
|
||||
assert done.returncode == 0, done.stderr
|
||||
assert record == "v1 zabbix-agent iptables tcp 10050 10.0.0.5/32\n"
|
||||
|
||||
def test_a_rule_iptables_had_is_not_recorded(self, tmp_path):
|
||||
done, record = _run(
|
||||
tmp_path,
|
||||
_iptables_allow("10.0.0.5/32", 10050, "tcp", "zabbix-agent"),
|
||||
{"iptables": "exit 0"},
|
||||
)
|
||||
|
||||
assert done.returncode == 0
|
||||
assert record == ""
|
||||
|
||||
def test_a_rule_that_is_gone_already_removes_as_done(self, tmp_path):
|
||||
rule = {
|
||||
"purpose": "zabbix-agent",
|
||||
"backend": "iptables",
|
||||
"proto": "tcp",
|
||||
"port": 10050,
|
||||
"source": "10.0.0.5/32",
|
||||
"zone": "",
|
||||
"permanent": True,
|
||||
}
|
||||
done, _ = _run(
|
||||
tmp_path, _remove_command(rule), {"iptables": 'case "$1" in -C) exit 1 ;; esac'}
|
||||
)
|
||||
|
||||
assert done.returncode == 0
|
||||
|
||||
|
||||
class TestCountingWhatIsLeft:
|
||||
@pytest.mark.parametrize(
|
||||
("added", "count"),
|
||||
[
|
||||
("ufw allow from 10.0.0.9 to any port 10050 proto tcp", "1"),
|
||||
("ufw allow 10050/tcp", "1"),
|
||||
("ufw allow from 10.0.0.9 to any port 100500 proto tcp", "0"),
|
||||
("ufw allow from 10.0.0.9 to any port 10050 proto udp", "0"),
|
||||
],
|
||||
)
|
||||
def test_ufw(self, tmp_path, added, count):
|
||||
ufw = f"printf 'Added user rules (see ufw status for running firewall):\\n{added}\\n'"
|
||||
done, _ = _run(tmp_path, _count_command("ufw", 10050, "tcp"), {"ufw": ufw})
|
||||
|
||||
assert done.stdout.strip() == count
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("rule", "count"),
|
||||
[
|
||||
("-A INPUT -s 10.0.0.9/32 -p tcp -m tcp --dport 10050 -j ACCEPT", "1"),
|
||||
("-A INPUT -s 10.0.0.9/32 -p tcp -m tcp --dport 100500 -j ACCEPT", "0"),
|
||||
("-A INPUT -s 10.0.0.9/32 -p udp -m udp --dport 10050 -j ACCEPT", "0"),
|
||||
],
|
||||
)
|
||||
def test_iptables(self, tmp_path, rule, count):
|
||||
iptables = f"printf -- '-P INPUT ACCEPT\\n{rule}\\n'"
|
||||
done, _ = _run(tmp_path, _count_command("iptables", 10050, "tcp"), {"iptables": iptables})
|
||||
|
||||
assert done.stdout.strip() == count
|
||||
|
||||
def test_firewalld_counts_rich_rules_and_ports_in_each_zone(self, tmp_path):
|
||||
firewall_cmd = (
|
||||
'case "$*" in\n'
|
||||
" --get-default-zone) echo public ;;\n"
|
||||
" --get-active-zones) ;;\n"
|
||||
' *--list-rich-rules*) echo \'rule family="ipv4" source address="10.0.0.9/32"'
|
||||
' port port="10050" protocol="tcp" accept\' ;;\n'
|
||||
" *--list-ports*) echo '22/tcp 10050/tcp 100500/tcp' ;;\n"
|
||||
"esac"
|
||||
)
|
||||
done, _ = _run(
|
||||
tmp_path, _count_command("firewalld", 10050, "tcp"), {"firewall-cmd": firewall_cmd}
|
||||
)
|
||||
|
||||
assert done.stdout.strip() == "2"
|
||||
Reference in new issue
Block a user