feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)
ufw, iptables and firewalld do not say who wrote a rule. opened_ports keeps a root-owned record of the rules a driver opened, written only when the firewall did not have the rule before, and read strictly. ZabbixAgentMixin's firewall step calls the driver's optional _revoke_zabbix_agent_sources for an active-only agent; a driver without it changes nothing and says why.
This commit is contained in:
1 parent
fb927e7255
commit
ebd3255e5a
6 files changed
+570
-8
No files matched your search
@@ -0,0 +1,187 @@
|
||||
"""The record of the firewall rules netOrk opened on a host (NetOrk/netork#2693)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_device_types.opened_ports import (
|
||||
OPENED_PORTS_RECORD,
|
||||
OpenedRuleDict,
|
||||
opened_rule_line,
|
||||
opened_rules_text,
|
||||
parse_opened_ports_record,
|
||||
read_opened_ports_command,
|
||||
record_opened_rule_command,
|
||||
revoked_output,
|
||||
ufw_allow_recorded_command,
|
||||
ufw_delete_command,
|
||||
)
|
||||
|
||||
UFW: OpenedRuleDict = {
|
||||
"purpose": "zabbix-agent",
|
||||
"backend": "ufw",
|
||||
"proto": "tcp",
|
||||
"port": 10050,
|
||||
"source": "10.40.100.254/32",
|
||||
"zone": "",
|
||||
"permanent": True,
|
||||
}
|
||||
FIREWALLD: OpenedRuleDict = {**UFW, "backend": "firewalld", "zone": "public", "permanent": False}
|
||||
|
||||
ROOT_DIR = "0 755 directory"
|
||||
ROOT_FILE = "0 644 regular file"
|
||||
|
||||
|
||||
def _read(*lines: str, directory: str = ROOT_DIR, record: str = ROOT_FILE) -> str:
|
||||
return "\n".join([directory, record, *lines]) + "\n"
|
||||
|
||||
|
||||
class TestTheLine:
|
||||
def test_ufw(self):
|
||||
assert opened_rule_line(UFW) == "v1 zabbix-agent ufw tcp 10050 10.40.100.254/32"
|
||||
|
||||
def test_firewalld_names_zone_and_configuration(self):
|
||||
assert opened_rule_line(FIREWALLD) == (
|
||||
"v1 zabbix-agent firewalld tcp 10050 10.40.100.254/32 public runtime"
|
||||
)
|
||||
|
||||
def test_a_line_reads_back_as_the_rule(self):
|
||||
for rule in (UFW, FIREWALLD, {**UFW, "backend": "iptables", "source": "2001:db8::/64"}):
|
||||
record = parse_opened_ports_record(_read(opened_rule_line(rule)))
|
||||
assert record["rules"] == [rule]
|
||||
assert record["unknown"] == []
|
||||
assert record["problem"] == ""
|
||||
|
||||
|
||||
class TestStrictReading:
|
||||
@pytest.mark.parametrize(
|
||||
"line",
|
||||
[
|
||||
"v2 zabbix-agent ufw tcp 10050 10.0.0.1/32",
|
||||
"v1 zabbix-agent nft tcp 10050 10.0.0.1/32",
|
||||
"v1 zabbix-agent ufw icmp 10050 10.0.0.1/32",
|
||||
"v1 zabbix-agent ufw tcp 0 10.0.0.1/32",
|
||||
"v1 zabbix-agent ufw tcp 70000 10.0.0.1/32",
|
||||
"v1 zabbix-agent ufw tcp 10050 10.0.0.1",
|
||||
"v1 zabbix-agent ufw tcp 10050 10.0.0.0/8;reboot",
|
||||
"v1 zabbix-agent ufw tcp 10050 any",
|
||||
"v1 Zabbix_Agent ufw tcp 10050 10.0.0.1/32",
|
||||
"v1 zabbix-agent ufw tcp 10050 10.0.0.1/32 public runtime",
|
||||
"v1 zabbix-agent firewalld tcp 10050 10.0.0.1/32",
|
||||
"v1 zabbix-agent firewalld tcp 10050 10.0.0.1/32 $(id) runtime",
|
||||
"v1 zabbix-agent firewalld tcp 10050 10.0.0.1/32 public sometimes",
|
||||
"v1 zabbix-agent ufw tcp 10050 10.0.0.1/32",
|
||||
],
|
||||
)
|
||||
def test_a_line_that_is_not_exactly_a_rule_is_not_acted_on(self, line):
|
||||
record = parse_opened_ports_record(_read(line))
|
||||
|
||||
assert record["rules"] == []
|
||||
assert record["unknown"] == [line]
|
||||
|
||||
def test_blank_lines_are_neither(self):
|
||||
record = parse_opened_ports_record(_read("", opened_rule_line(UFW), " "))
|
||||
|
||||
assert record["rules"] == [UFW]
|
||||
assert record["unknown"] == []
|
||||
|
||||
def test_no_record_is_no_rules(self):
|
||||
record = parse_opened_ports_record("")
|
||||
|
||||
assert record == {"rules": [], "unknown": [], "problem": ""}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("directory", "record", "why"),
|
||||
[
|
||||
("1000 755 directory", ROOT_FILE, "not root's"),
|
||||
("0 775 directory", ROOT_FILE, "writable"),
|
||||
(ROOT_DIR, "1000 644 regular file", "not root's"),
|
||||
(ROOT_DIR, "0 666 regular file", "writable"),
|
||||
(ROOT_DIR, "0 777 symbolic link", "not a regular file"),
|
||||
("0 755 symbolic link", ROOT_FILE, "not a directory"),
|
||||
],
|
||||
)
|
||||
def test_a_record_others_could_have_written_is_not_read(self, directory, record, why):
|
||||
read = parse_opened_ports_record(
|
||||
_read(opened_rule_line(UFW), directory=directory, record=record)
|
||||
)
|
||||
|
||||
assert read["rules"] == []
|
||||
assert why in read["problem"]
|
||||
|
||||
def test_a_read_that_says_nothing_about_owner_and_mode_is_not_trusted(self):
|
||||
read = parse_opened_ports_record("v1 zabbix-agent ufw tcp 10050 10.0.0.1/32\n")
|
||||
|
||||
assert read["rules"] == []
|
||||
assert read["problem"]
|
||||
|
||||
|
||||
class TestCommands:
|
||||
def test_reading_says_owner_mode_and_kind_of_the_directory_and_the_file(self):
|
||||
command = read_opened_ports_command()
|
||||
|
||||
assert "stat -c '%u %a %F' /var/lib/netork " + OPENED_PORTS_RECORD in command
|
||||
assert f"cat {OPENED_PORTS_RECORD}" in command
|
||||
assert "exit" not in command
|
||||
|
||||
def test_recording_creates_root_s_directory_and_appends_the_line(self):
|
||||
command = record_opened_rule_command(opened_rule_line(UFW))
|
||||
|
||||
assert command.startswith("install -d -m 755 -o root -g root /var/lib/netork")
|
||||
assert f"printf '%s\\n' \"{opened_rule_line(UFW)}\" >> {OPENED_PORTS_RECORD}" in command
|
||||
assert f"chmod 644 {OPENED_PORTS_RECORD}" in command
|
||||
|
||||
def test_recording_takes_a_firewalld_zone_from_the_shell(self):
|
||||
line = opened_rule_line({**FIREWALLD, "zone": "$z"})
|
||||
|
||||
assert '"v1 zabbix-agent firewalld tcp 10050 10.40.100.254/32 $z runtime"' in (
|
||||
record_opened_rule_command(line)
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("line", ['v1 "; rm -rf /', "v1 `id`", "v1 a\nb", "v1 $(id)"])
|
||||
def test_recording_refuses_what_is_not_a_line(self, line):
|
||||
with pytest.raises(ValueError):
|
||||
record_opened_rule_command(line)
|
||||
|
||||
def test_ufw_records_a_rule_only_when_ufw_added_it(self):
|
||||
command = ufw_allow_recorded_command("10.40.100.254/32", 10050, "tcp", "zabbix-agent")
|
||||
|
||||
assert "LC_ALL=C ufw allow from 10.40.100.254/32 to any port 10050 proto tcp" in command
|
||||
# ufw says "Skipping adding existing rule" for a rule it had: someone else's.
|
||||
assert "*Skipping*) ;;" in command
|
||||
assert "*'Rule added'*|*'Rules updated'*)" in command
|
||||
assert opened_rule_line(UFW) in command
|
||||
assert "exit" not in command
|
||||
|
||||
def test_ufw_delete(self):
|
||||
assert ufw_delete_command(UFW) == (
|
||||
"LC_ALL=C ufw delete allow from 10.40.100.254/32 to any port 10050 proto tcp"
|
||||
)
|
||||
|
||||
def test_rules_text_keeps_what_it_is_given_one_per_line(self):
|
||||
assert opened_rules_text([]) == ""
|
||||
assert opened_rules_text(["a", "b"]) == "a\nb\n"
|
||||
|
||||
|
||||
class TestReport:
|
||||
def test_removed_rules_are_named(self):
|
||||
output = revoked_output([UFW], unknown=[], left=0, proto="tcp", port=10050)
|
||||
|
||||
assert "removed" in output
|
||||
assert "10.40.100.254/32" in output
|
||||
|
||||
def test_without_a_recorded_rule_it_says_so(self):
|
||||
assert "netOrk recorded no rule" in revoked_output(
|
||||
[], unknown=[], left=0, proto="tcp", port=10050
|
||||
)
|
||||
|
||||
def test_rules_netork_did_not_record_are_reported_not_removed(self):
|
||||
output = revoked_output([], unknown=[], left=2, proto="tcp", port=10050)
|
||||
|
||||
assert "2 rule(s) for tcp/10050" in output
|
||||
assert "left in place" in output
|
||||
|
||||
def test_lines_it_could_not_read_are_reported(self):
|
||||
output = revoked_output([], unknown=["v9 x"], left=0, proto="tcp", port=10050)
|
||||
|
||||
assert "v9 x" in output
|
||||
@@ -407,6 +407,41 @@ class TestZabbixAgentMixin:
|
||||
assert [s["step"] for s in result["steps"]] == ["install", "configure", "firewall", "start"]
|
||||
assert "active only" in result["steps"][2]["output"]
|
||||
|
||||
def test_an_active_only_agent_has_the_driver_revoke_what_netork_opened(self):
|
||||
"""NetOrk/netork#2693: a rule an earlier passive install opened is closed."""
|
||||
agent = _Agent()
|
||||
agent._revoke_zabbix_agent_sources = lambda settings: ( # type: ignore[attr-defined]
|
||||
agent.calls.append(("revoke", settings["listen_port"]))
|
||||
or {"ok": True, "output": "removed tcp/10050"}
|
||||
)
|
||||
|
||||
result = agent.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True
|
||||
assert ("revoke", ACTIVE_ONLY["listen_port"]) in agent.calls
|
||||
assert not [c for c in agent.calls if c[0] == "firewall"]
|
||||
assert result["steps"][2]["output"] == "removed tcp/10050"
|
||||
|
||||
def test_a_driver_that_cannot_tell_its_rules_apart_changes_nothing_and_says_so(self):
|
||||
agent = _Agent()
|
||||
|
||||
result = agent.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True
|
||||
assert "cannot tell" in result["steps"][2]["output"]
|
||||
|
||||
def test_a_revoke_that_fails_starts_nothing(self):
|
||||
agent = _Agent()
|
||||
agent._revoke_zabbix_agent_sources = lambda settings: { # type: ignore[attr-defined]
|
||||
"ok": False,
|
||||
"output": "ufw: permission denied",
|
||||
}
|
||||
|
||||
result = agent.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is False
|
||||
assert ("restart", "zabbix-agent") not in agent.calls
|
||||
|
||||
def test_a_closed_firewall_starts_nothing(self):
|
||||
agent = _Agent(refuse=frozenset({"firewall"}))
|
||||
|
||||
|
||||
Reference in new issue
Block a user