feat(opened-ports): revoke_recorded_rules, the loop napalm-linux and napalm-proxmox share
This commit is contained in:
1 parent
cbd1cd3d11
commit
4df932e0ca
4 files changed
+85
-1
No files matched your search
+3
-1
@@ -15,7 +15,9 @@ recorded here; their history is the git log.
|
||||
the operator wrote is never recorded. `parse_opened_ports_record` reads it strictly: not at
|
||||
all where the directory or the file is not root's alone or is a symbolic link, and a line
|
||||
that is not exactly a rule is reported, never acted on. `ufw_allow_recorded_command` and
|
||||
`ufw_delete_command` are ufw's commands, which napalm-linux and napalm-proxmox share;
|
||||
`ufw_delete_command` are ufw's commands, which napalm-linux and napalm-proxmox share,
|
||||
`revoke_recorded_rules` removes the rules recorded under a purpose with the driver's own
|
||||
command and keeps every other line;
|
||||
`revoked_output` says what closing removed and what it left for the port.
|
||||
- `ZabbixAgentMixin._revoke_zabbix_agent_sources(settings)`, optional: for an `active_only`
|
||||
agent, `configure_zabbix_agent`'s firewall step has the driver close what netOrk opened
|
||||
|
||||
@@ -235,11 +235,13 @@ from napalm_device_types.opened_ports import (
|
||||
ZABBIX_AGENT_PURPOSE,
|
||||
OpenedPortsRecordDict,
|
||||
OpenedRuleDict,
|
||||
RevokedRulesDict,
|
||||
opened_rule_line,
|
||||
opened_rules_text,
|
||||
parse_opened_ports_record,
|
||||
read_opened_ports_command,
|
||||
record_opened_rule_command,
|
||||
revoke_recorded_rules,
|
||||
revoked_output,
|
||||
ufw_allow_recorded_command,
|
||||
ufw_delete_command,
|
||||
@@ -445,6 +447,8 @@ __all__ = [
|
||||
"parse_opened_ports_record",
|
||||
"read_opened_ports_command",
|
||||
"record_opened_rule_command",
|
||||
"RevokedRulesDict",
|
||||
"revoke_recorded_rules",
|
||||
"revoked_output",
|
||||
"ufw_allow_recorded_command",
|
||||
"ufw_delete_command",
|
||||
|
||||
@@ -33,8 +33,11 @@ from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
from typing import Literal, TypedDict, cast
|
||||
|
||||
from napalm_device_types.models import FixStepOutcomeDict
|
||||
|
||||
#: Where a host keeps the rules netOrk opened, root's and outside every account's home.
|
||||
OPENED_PORTS_DIRECTORY = "/var/lib/netork"
|
||||
OPENED_PORTS_RECORD = f"{OPENED_PORTS_DIRECTORY}/opened-ports"
|
||||
@@ -176,6 +179,41 @@ def parse_opened_ports_record(output: str) -> OpenedPortsRecordDict:
|
||||
return {"rules": rules, "unknown": unknown, "problem": ""}
|
||||
|
||||
|
||||
class RevokedRulesDict(TypedDict):
|
||||
"""What :func:`revoke_recorded_rules` did."""
|
||||
|
||||
removed: list[OpenedRuleDict]
|
||||
#: The record after it: every other line, in order, as the driver writes it back.
|
||||
record: str
|
||||
#: A rule it could not remove, with why; it stays in ``record``.
|
||||
failed: list[str]
|
||||
|
||||
|
||||
def revoke_recorded_rules(
|
||||
record: OpenedPortsRecordDict,
|
||||
purpose: str,
|
||||
remove: Callable[[OpenedRuleDict], FixStepOutcomeDict],
|
||||
) -> RevokedRulesDict:
|
||||
"""Remove each rule of *record* recorded under *purpose*, whatever port it opened,
|
||||
with the driver's *remove*; keep every other line, the unknown ones too."""
|
||||
removed: list[OpenedRuleDict] = []
|
||||
kept: list[str] = []
|
||||
failed: list[str] = []
|
||||
for rule in record["rules"]:
|
||||
if rule["purpose"] == purpose:
|
||||
outcome = remove(rule)
|
||||
if outcome["ok"]:
|
||||
removed.append(rule)
|
||||
continue
|
||||
failed.append(f"{opened_rule_line(rule)}: {outcome.get('output', '')}")
|
||||
kept.append(opened_rule_line(rule))
|
||||
return {
|
||||
"removed": removed,
|
||||
"record": opened_rules_text(kept + record["unknown"]),
|
||||
"failed": failed,
|
||||
}
|
||||
|
||||
|
||||
def opened_rules_text(lines: list[str]) -> str:
|
||||
"""The record holding *lines*, as a driver writes it back after closing rules."""
|
||||
return "".join(f"{line}\n" for line in lines)
|
||||
@@ -273,6 +311,8 @@ __all__ = [
|
||||
"parse_opened_ports_record",
|
||||
"read_opened_ports_command",
|
||||
"record_opened_rule_command",
|
||||
"RevokedRulesDict",
|
||||
"revoke_recorded_rules",
|
||||
"revoked_output",
|
||||
"ufw_allow_recorded_command",
|
||||
"ufw_delete_command",
|
||||
|
||||
@@ -15,6 +15,7 @@ from napalm_device_types.opened_ports import (
|
||||
parse_opened_ports_record,
|
||||
read_opened_ports_command,
|
||||
record_opened_rule_command,
|
||||
revoke_recorded_rules,
|
||||
revoked_output,
|
||||
ufw_allow_recorded_command,
|
||||
ufw_delete_command,
|
||||
@@ -246,3 +247,40 @@ class TestReport:
|
||||
output = revoked_output([], unknown=["v9 x"], left=0, proto="tcp", port=10050)
|
||||
|
||||
assert "v9 x" in output
|
||||
|
||||
|
||||
class TestRevoking:
|
||||
SNMP: OpenedRuleDict = {**UFW, "purpose": "snmp", "proto": "udp", "port": 161}
|
||||
EARLIER_PORT: OpenedRuleDict = {**UFW, "port": 10051}
|
||||
|
||||
def _record(self, *lines):
|
||||
return parse_opened_ports_record(_read(*lines))
|
||||
|
||||
def test_only_the_purpose_s_rules_go_whatever_their_port(self):
|
||||
record = self._record(
|
||||
opened_rule_line(UFW),
|
||||
opened_rule_line(self.SNMP),
|
||||
"v9 not a rule",
|
||||
opened_rule_line(self.EARLIER_PORT),
|
||||
)
|
||||
asked: list[OpenedRuleDict] = []
|
||||
|
||||
done = revoke_recorded_rules(
|
||||
record, "zabbix-agent", lambda r: asked.append(r) or {"ok": True, "output": ""}
|
||||
)
|
||||
|
||||
assert asked == [UFW, self.EARLIER_PORT]
|
||||
assert done["removed"] == [UFW, self.EARLIER_PORT]
|
||||
assert done["record"] == f"{opened_rule_line(self.SNMP)}\nv9 not a rule\n"
|
||||
assert done["failed"] == []
|
||||
|
||||
def test_a_rule_that_will_not_go_stays_and_says_why(self):
|
||||
record = self._record(opened_rule_line(UFW))
|
||||
|
||||
done = revoke_recorded_rules(
|
||||
record, "zabbix-agent", lambda r: {"ok": False, "output": "ufw: locked"}
|
||||
)
|
||||
|
||||
assert done["removed"] == []
|
||||
assert done["record"] == f"{opened_rule_line(UFW)}\n"
|
||||
assert done["failed"] == [f"{opened_rule_line(UFW)}: ufw: locked"]
|
||||
Reference in new issue
Block a user