feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)
_revoke_zabbix_agent_sources deletes the named rule section netOrk wrote for the agent (allow_zabbix_agent, allow_zabbix_agent_from_<zone>) and reloads the firewall where one runs. Rules with other keys stay and are reported.
This commit is contained in:
1 parent
5c91af6033
commit
36d4802624
3 files changed
+159
-3
No files matched your search
@@ -34,6 +34,8 @@ class _Shell:
|
||||
self.installed = installed
|
||||
self.nft_hits = nft_hits
|
||||
self.issued: list[str] = []
|
||||
self.firewall = UCI_FIREWALL_HEALTHY
|
||||
self.reload = ""
|
||||
|
||||
def __call__(self, cmd, **kw):
|
||||
self.issued.append(cmd)
|
||||
@@ -54,7 +56,9 @@ class _Shell:
|
||||
self.installed = True
|
||||
return "ok\n__OWRT_RC=0"
|
||||
if cmd.startswith("uci show firewall"):
|
||||
return UCI_FIREWALL_HEALTHY
|
||||
return self.firewall
|
||||
if cmd.startswith("fw4 reload"):
|
||||
return self.reload
|
||||
if cmd.startswith("uci show network"):
|
||||
return UCI_NETWORK_STATIC
|
||||
if "$SSH_CONNECTION" in cmd:
|
||||
@@ -234,3 +238,88 @@ class _SnmpShell:
|
||||
self.shell.issued.append(cmd)
|
||||
return "1"
|
||||
return self.shell(cmd, **kw)
|
||||
|
||||
|
||||
# --- Active only: closing the rule netOrk wrote (NetOrk/netork#2693) ----------------
|
||||
|
||||
ACTIVE_ONLY = {**SETTINGS, "active_only": True, "allowed_sources": []}
|
||||
NETORK_RULES = """\
|
||||
firewall.allow_zabbix_agent_from_lan=rule
|
||||
firewall.allow_zabbix_agent_from_lan.name='Allow-Zabbix-Agent'
|
||||
firewall.allow_zabbix_agent_from_lan.proto='tcp'
|
||||
firewall.allow_zabbix_agent_from_lan.dest_port='10050'
|
||||
firewall.allow_zabbix_agent_from_lan.src='lan'
|
||||
firewall.allow_zabbix_agent=rule
|
||||
firewall.allow_zabbix_agent.name='Allow-Zabbix-Agent'
|
||||
firewall.allow_zabbix_agent.dest_port='10051'
|
||||
"""
|
||||
OPERATOR_RULE = """\
|
||||
firewall.cfg0e92bd=rule
|
||||
firewall.cfg0e92bd.name='zabbix from the monitoring VLAN'
|
||||
firewall.cfg0e92bd.proto='tcp'
|
||||
firewall.cfg0e92bd.dest_port='10050'
|
||||
firewall.allow_zabbix_agent_backup=rule
|
||||
firewall.allow_zabbix_agent_backup.dest_port='10050'
|
||||
"""
|
||||
|
||||
|
||||
def _deletes(shell) -> list[str]:
|
||||
return [c for c in shell.issued if "uci -q delete firewall." in c]
|
||||
|
||||
|
||||
def test_active_only_removes_the_rules_netork_wrote_and_reloads(owrt, shell):
|
||||
shell.firewall = UCI_FIREWALL_HEALTHY + NETORK_RULES
|
||||
|
||||
result = owrt.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True, result["output"]
|
||||
deletes = " ".join(_deletes(shell))
|
||||
assert "uci -q delete firewall.allow_zabbix_agent_from_lan" in deletes
|
||||
assert "uci -q delete firewall.allow_zabbix_agent;" in deletes + ";"
|
||||
assert "uci commit firewall" in deletes
|
||||
assert any(c.startswith("fw4 reload") for c in shell.issued)
|
||||
assert "allow_zabbix_agent_from_lan" in result["steps"][2]["output"]
|
||||
assert not any("uci set firewall.allow_zabbix_agent" in c for c in shell.issued)
|
||||
|
||||
|
||||
def test_rules_the_operator_wrote_stay_and_are_reported(owrt, shell):
|
||||
shell.firewall = UCI_FIREWALL_HEALTHY + NETORK_RULES + OPERATOR_RULE
|
||||
|
||||
result = owrt.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
deletes = " ".join(_deletes(shell))
|
||||
assert "cfg0e92bd" not in deletes
|
||||
assert "allow_zabbix_agent_backup" not in deletes
|
||||
firewall = result["steps"][2]["output"]
|
||||
assert "2 rule(s) for tcp/10050" in firewall
|
||||
assert "cfg0e92bd" in firewall
|
||||
|
||||
|
||||
def test_without_a_rule_netork_wrote_nothing_changes(owrt, shell):
|
||||
result = owrt.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True
|
||||
assert _deletes(shell) == []
|
||||
assert not any(c.startswith("fw4 reload") for c in shell.issued)
|
||||
assert "netOrk wrote no rule" in result["steps"][2]["output"]
|
||||
|
||||
|
||||
def test_without_a_running_firewall_the_rule_still_goes_from_the_configuration(owrt, shell):
|
||||
shell.firewall = UCI_FIREWALL_HEALTHY + NETORK_RULES
|
||||
shell.firewall_runs = False
|
||||
|
||||
result = owrt.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is True
|
||||
assert _deletes(shell)
|
||||
assert not any(c.startswith("fw4 reload") for c in shell.issued)
|
||||
|
||||
|
||||
def test_a_reload_the_firewall_refuses_fails(owrt, shell):
|
||||
shell.firewall = UCI_FIREWALL_HEALTHY + NETORK_RULES
|
||||
shell.reload = "Section @rule[3] is not a valid rule"
|
||||
|
||||
result = owrt.configure_zabbix_agent(ACTIVE_ONLY)
|
||||
|
||||
assert result["success"] is False
|
||||
assert result["steps"][-1]["step"] == "firewall"
|
||||
Reference in new issue
Block a user