feat(zabbix): an active-only agent closes the port netOrk opened for it (NetOrk/netork#2693)
Every rule the firewall step adds in firewalld, ufw or iptables is recorded in napalm-device-types' opened_ports record, only when the firewall did not have it before. For an active-only agent, _revoke_zabbix_agent_sources removes exactly the agent's recorded rules and reports what is left for the port.
This commit is contained in:
1 parent
04ff633290
commit
a3b9e4a02d
8 files changed
+516
-37
No files matched your search
@@ -6,6 +6,12 @@ import ipaddress
|
||||
import re
|
||||
from shlex import quote as _shlex_quote
|
||||
|
||||
from napalm_device_types import (
|
||||
SNMP_PURPOSE,
|
||||
OpenedRuleDict,
|
||||
opened_rule_line,
|
||||
record_opened_rule_command,
|
||||
)
|
||||
from napalm_device_types.models import SnmpAgentSettingsDict
|
||||
|
||||
#: Where net-snmp reads its configuration, and how each package manager installs it.
|
||||
@@ -42,29 +48,73 @@ def firewalld_allow_snmp(sources: list[str]) -> str:
|
||||
return firewalld_allow_port(sources, 161, "udp")
|
||||
|
||||
|
||||
def firewalld_allow_port(sources: list[str], port: int, proto: str) -> str:
|
||||
def firewalld_rich_rule(source: str, port: int, proto: str) -> str:
|
||||
"""The rich rule that lets *source* (normalised) reach *proto*/*port*, shell-quoted."""
|
||||
family = f"ipv{ipaddress.ip_network(source).version}"
|
||||
return _shlex_quote(
|
||||
f'rule family="{family}" source address="{source}"'
|
||||
f' port port="{port}" protocol="{proto}" accept'
|
||||
)
|
||||
|
||||
|
||||
def firewalld_allow_port(
|
||||
sources: list[str], port: int, proto: str, purpose: str = SNMP_PURPOSE
|
||||
) -> str:
|
||||
"""The shell that opens *proto*/*port* for *sources* (already normalised) in firewalld.
|
||||
|
||||
A rich rule per source in every zone of ``_FIREWALLD_ZONES``, at runtime and
|
||||
permanently, each only when the zone lacks it -- no ``--reload``, which would drop
|
||||
the runtime rules others (Docker) put there.
|
||||
the runtime rules others (Docker) put there. Each rule it adds is recorded under
|
||||
*purpose* with its zone and configuration; one the zone had is not
|
||||
(napalm-device-types ``opened_ports``, NetOrk/netork#2693).
|
||||
"""
|
||||
steps = []
|
||||
for source in sources:
|
||||
family = f"ipv{ipaddress.ip_network(source).version}"
|
||||
rule = _shlex_quote(
|
||||
f'rule family="{family}" source address="{source}"'
|
||||
f' port port="{port}" protocol="{proto}" accept'
|
||||
)
|
||||
rule = firewalld_rich_rule(source, port, proto)
|
||||
for scope in ("", "--permanent "):
|
||||
zone = f'firewall-cmd {scope}--zone="$z"'
|
||||
line = opened_rule_line(
|
||||
{
|
||||
"purpose": purpose,
|
||||
"backend": "firewalld",
|
||||
"proto": "udp" if proto == "udp" else "tcp",
|
||||
"port": port,
|
||||
"source": source,
|
||||
"zone": "$z",
|
||||
"permanent": bool(scope),
|
||||
}
|
||||
)
|
||||
steps.append(
|
||||
f"{{ {zone} --query-rich-rule={rule} >/dev/null 2>&1"
|
||||
f" || {zone} --add-rich-rule={rule}; }}"
|
||||
f" || {{ {zone} --add-rich-rule={rule}"
|
||||
f" && {record_opened_rule_command(line)}; }}; }}"
|
||||
)
|
||||
return f"for z in {_FIREWALLD_ZONES}; do {' && '.join(steps)} || exit 1; done"
|
||||
|
||||
|
||||
def firewalld_remove_command(rule: OpenedRuleDict) -> str:
|
||||
"""The shell that removes the recorded *rule* from its zone and configuration;
|
||||
done where the zone no longer has it."""
|
||||
scope = "--permanent " if rule["permanent"] else ""
|
||||
zone = f"firewall-cmd {scope}--zone={rule['zone']}"
|
||||
rich = firewalld_rich_rule(rule["source"], rule["port"], rule["proto"])
|
||||
return (
|
||||
f"if {zone} --query-rich-rule={rich} >/dev/null 2>&1;"
|
||||
f" then {zone} --remove-rich-rule={rich}; fi"
|
||||
)
|
||||
|
||||
|
||||
def firewalld_count_command(port: int, proto: str) -> str:
|
||||
"""The shell that counts the rules for *proto*/*port* in firewalld's permanent
|
||||
configuration, in every zone a rule would go to."""
|
||||
return (
|
||||
f"for z in {_FIREWALLD_ZONES}; do"
|
||||
' firewall-cmd --permanent --zone="$z" --list-rich-rules;'
|
||||
" firewall-cmd --permanent --zone=\"$z\" --list-ports | tr ' ' '\\n'; done"
|
||||
f' | grep -cE \'port="{port}" protocol="{proto}"|^{port}/{proto}$\''
|
||||
)
|
||||
|
||||
|
||||
def render_snmpd_conf(settings: SnmpAgentSettingsDict) -> str:
|
||||
"""net-snmp's configuration for *settings*: read-only v2c, per allowed source."""
|
||||
community = settings["community"]
|
||||
|
||||
Reference in new issue
Block a user