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
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user