refactor(opened-ports): reading a line in parts, within the quality ratchet
driver / lint (pull_request) Successful in 19s
driver / standard (pull_request) Successful in 22s
driver / types (pull_request) Successful in 1m1s
driver / lowest (pull_request) Successful in 37s
driver / test (3.12) (pull_request) Successful in 1m23s
driver / test (3.13) (pull_request) Successful in 1m22s
driver / build (pull_request) Successful in 34s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m44s
driver / publish (pull_request) Skipped

This commit is contained in:
christianmanivong committed 2026-11-26 12:23:54 +01:00
1 parent 507e9a8728
commit 66f26c3d8e
1 file changed
+43 -21
+43 -21
View File
@@ -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],
}