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:
Christian Manivong committed 2026-11-26 08:23:32 +01:00
1 parent 5c91af6033
commit 36d4802624
3 files changed
+159 -3

No files matched your search

+61 -2
View File
@@ -20,7 +20,8 @@ Zabbix publishes no repository for OpenWrt, so the feed is the only source.
The package index lives in RAM and is gone after every restart; the template loads
it first (``refresh_available_updates``). The firewall step opens tcp/``listen_port``
in the zone that owns the management address, the way the SNMP agent's step opens
udp/161 -- and only where a firewall runs: an access point often runs none.
udp/161 -- and only where a firewall runs: an access point often runs none. For an
active-only agent it deletes that rule again, by its key (NetOrk/netork#2693).
"""
from __future__ import annotations
@@ -50,6 +51,15 @@ _FIREWALL_RUNS = (
)
#: The key of the rule ``_allow_zabbix_agent_sources`` writes, unscoped or per zone.
_RULE_KEY = "allow_zabbix_agent"
def _netork_rule(key: str) -> bool:
"""Whether the firewall section *key* is the rule netOrk writes for the agent."""
return key == _RULE_KEY or key.startswith(f"{_RULE_KEY}_from_")
def _failed(result: CommandResult, what: str) -> FixStepOutcomeDict:
detail = (result.stderr or result.stdout or "").strip()[-300:]
return {"ok": False, "output": f"{what}: {detail or result.exit_code}"}
@@ -92,7 +102,7 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
if self._send_command(_FIREWALL_RUNS).strip() != "on":
return {"ok": True, "output": "no firewall runs; nothing to open"}
outcome = self._allow_in_management_zone(
key="allow_zabbix_agent",
key=_RULE_KEY,
name="Allow-Zabbix-Agent",
proto="tcp",
port=int(settings["listen_port"]),
@@ -101,6 +111,47 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
)
return {"ok": bool(outcome["ok"]), "output": str(outcome["output"])}
def _revoke_zabbix_agent_sources(self, settings: ZabbixAgentSettingsDict) -> FixStepOutcomeDict:
"""For an active-only agent: delete the rules netOrk wrote for the agent on an
earlier passive run, and nothing else (NetOrk/netork#2693).
netOrk's are the named sections ``allow_zabbix_agent`` and
``allow_zabbix_agent_from_<zone>`` (``_allow_zabbix_agent_sources``); a rule
written in LuCI gets an anonymous ``cfgXXXX`` key. Other rules for the port stay
and are named. The firewall reloads where one runs.
"""
port = int(settings["listen_port"])
sections = self._parse_uci_sections(self._send_command("uci show firewall 2>/dev/null"))
rules = {key: opts for key, opts in sections.items() if opts.get("_type") == "rule"}
mine = sorted(key for key in rules if _netork_rule(key))
others = sorted(
key
for key, opts in rules.items()
if key not in mine and self._uci_scalar(opts.get("dest_port", "")) == str(port)
)
lines: list[str] = []
ok = True
if mine:
deletes = "; ".join(f"uci -q delete firewall.{key}" for key in mine)
done = self._rc_step(f"{deletes}; uci commit firewall", "")
if not done["ok"]:
return {"ok": False, "output": str(done["output"])}
lines.append(f"removed the rule(s) netOrk wrote for the agent: {', '.join(mine)}")
if self._send_command(_FIREWALL_RUNS).strip() == "on":
reload_out = self._send_command(
"fw4 reload 2>&1 || /etc/init.d/firewall reload 2>&1"
).strip()
ok = not self._fw_reload_failed(reload_out)
lines.append(f"reload: {reload_out or 'ok'}")
else:
lines.append("netOrk wrote no rule for the agent; nothing removed")
if others:
lines.append(
f"{len(others)} rule(s) for tcp/{port} that netOrk did not write are left in"
f" place: {', '.join(others)}"
)
return {"ok": ok, "output": "; ".join(lines)}
if TYPE_CHECKING: # pragma: no cover - the driver's
def run_command(
@@ -117,3 +168,11 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
def _allow_in_management_zone(
self, *, key: str, name: str, proto: str, port: int, sources: list[str], word: str
) -> dict[str, Any]: ...
def _parse_uci_sections(self, raw: str) -> dict[str, dict[str, str]]: ...
def _uci_scalar(self, value: str) -> str: ...
def _rc_step(self, command: str, done: str) -> dict[str, Any]: ...
def _fw_reload_failed(self, output: str) -> bool: ...