refactor(zabbix): closing the agent's rule in steps, within the quality ratchet
driver / lint (pull_request) Successful in 13s
driver / standard (pull_request) Successful in 17s
driver / types (pull_request) Successful in 48s
driver / test (3.13) (pull_request) Successful in 45s
driver / test (3.12) (pull_request) Successful in 52s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m0s
driver / publish (pull_request) Skipped

This commit is contained in:
Christian Manivong committed 2026-11-26 12:24:34 +01:00
1 parent dfd9b70278
commit fa5ed09b4c
1 file changed
+38 -19
+38 -19
View File
@@ -121,28 +121,15 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
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)
)
mine, others = self._agent_rules(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'}")
removed = self._delete_rules(mine)
if not removed["ok"]:
return removed
lines.append(str(removed["output"]))
ok = self._reload_if_running(lines)
else:
lines.append("netOrk wrote no rule for the agent; nothing removed")
if others:
@@ -152,6 +139,38 @@ class OpenWrtZabbixAgentMixin(ZabbixAgentMixin):
)
return {"ok": ok, "output": "; ".join(lines)}
def _agent_rules(self, port: int) -> tuple[list[str], list[str]]:
"""The rule sections netOrk wrote for the agent, and the others for *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)
)
return mine, others
def _delete_rules(self, keys: list[str]) -> FixStepOutcomeDict:
deletes = "; ".join(f"uci -q delete firewall.{key}" for key in keys)
done = self._rc_step(f"{deletes}; uci commit firewall", "")
if not done["ok"]:
return {"ok": False, "output": str(done["output"])}
return {
"ok": True,
"output": f"removed the rule(s) netOrk wrote for the agent: {', '.join(keys)}",
}
def _reload_if_running(self, lines: list[str]) -> bool:
"""Reload the firewall where one runs; False where it refused the ruleset."""
if self._send_command(_FIREWALL_RUNS).strip() != "on":
return True
reload_out = self._send_command(
"fw4 reload 2>&1 || /etc/init.d/firewall reload 2>&1"
).strip()
lines.append(f"reload: {reload_out or 'ok'}")
return not self._fw_reload_failed(reload_out)
if TYPE_CHECKING: # pragma: no cover - the driver's
def run_command(