From 66f26c3d8e6f88a92ea9bf88b82133c6aa925133 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 12:23:54 +0100 Subject: [PATCH] refactor(opened-ports): reading a line in parts, within the quality ratchet --- napalm_device_types/opened_ports.py | 64 +++++++++++++++++++---------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/napalm_device_types/opened_ports.py b/napalm_device_types/opened_ports.py index c5569ec..24d095e 100644 --- a/napalm_device_types/opened_ports.py +++ b/napalm_device_types/opened_ports.py @@ -90,38 +90,60 @@ def opened_rule_line(rule: OpenedRuleDict) -> str: return " ".join(fields) +def _network(value: str) -> bool: + """Whether *value* is a network in its normal form, with its prefix.""" + try: + return "/" in value and str(ipaddress.ip_network(value)) == value + except ValueError: + return False + + +def _port(value: str) -> bool: + return value.isdigit() and value == str(int(value)) and 0 < int(value) < 65536 + + +def _firewalld_place(fields: list[str]) -> tuple[str, bool] | None: + """firewalld's zone and whether the configuration is the permanent one, or None.""" + zone, scope = fields + if not _ZONE.match(zone) or scope not in ("runtime", "permanent"): + return None + return zone, scope == "permanent" + + +def _well_formed(fields: list[str]) -> bool: + """Whether *fields* are a rule line's: version, backend and as many as it takes, + a purpose, a protocol, a port and a network.""" + backend = fields[2] if len(fields) > 2 else "" + if fields[0] != "v1" or backend not in _BACKENDS: + return False + if len(fields) != (8 if backend == "firewalld" else 6): + return False + purpose, proto, port, source = fields[1], fields[3], fields[4], fields[5] + return ( + bool(_PURPOSE.match(purpose)) + and proto in ("tcp", "udp") + and _port(port) + and _network(source) + ) + + def _rule(line: str) -> OpenedRuleDict | None: """The rule *line* says, or None where it is not exactly one.""" fields = line.split(" ") - if len(fields) < 6 or fields[0] != "v1" or fields[2] not in _BACKENDS: + if not _well_formed(fields): return None - backend = fields[2] - if len(fields) != (8 if backend == "firewalld" else 6): + purpose, backend, proto, port, source = fields[1:6] + place = _firewalld_place(fields[6:]) if backend == "firewalld" else ("", True) + if place is None: return None - purpose, proto, port, source = fields[1], fields[3], fields[4], fields[5] - if not _PURPOSE.match(purpose) or proto not in ("tcp", "udp"): - return None - if not port.isdigit() or not 0 < int(port) < 65536 or port != str(int(port)): - return None - try: - if str(ipaddress.ip_network(source)) != source or "/" not in source: - return None - except ValueError: - return None - zone, permanent = "", True - if backend == "firewalld": - zone, scope = fields[6], fields[7] - if not _ZONE.match(zone) or scope not in ("runtime", "permanent"): - return None - permanent = scope == "permanent" return { "purpose": purpose, "backend": cast(Backend, backend), "proto": cast(Literal["tcp", "udp"], proto), "port": int(port), "source": source, - "zone": zone, - "permanent": permanent, + "zone": place[0], + "permanent": place[1], }