Optimize firewalld capability probing

venix
admin 1 month ago
parent a45a0da682
commit 9f32fe6217

@ -153,18 +153,184 @@ def test_capability_probe_command_is_parseable_by_posix_sh():
assert "FirewallBackend" in _CAPABILITY_PROBE_COMMAND
assert "NftablesTableOwner" in _CAPABILITY_PROBE_COMMAND
assert "__VCAP_FIREWALL_CONF__source_zones=" in _CAPABILITY_PROBE_COMMAND
assert _CAPABILITY_PROBE_COMMAND.count("--list-all-zones") == 2
assert "--list-sources" not in _CAPABILITY_PROBE_COMMAND
assert "--list-interfaces" not in _CAPABILITY_PROBE_COMMAND
assert "--get-zones" not in _CAPABILITY_PROBE_COMMAND
assert "--permanent --get-default-zone" not in _CAPABILITY_PROBE_COMMAND
# firewall-cmd is a D-Bus client and can activate an inactive unit. Every
# use must therefore be dominated by the read-only systemd active check.
assert _CAPABILITY_PROBE_COMMAND.count(
'test "$firewall_service_state" = active') == 4
'test "$firewall_service_state" = active') == 3
assert _CAPABILITY_PROBE_COMMAND.count(
'$firewall_cmd --state') == 4
'$firewall_cmd --state') == 3
assert _CAPABILITY_PROBE_COMMAND.index(
'firewall_service_state="$status"') < _CAPABILITY_PROBE_COMMAND.index(
'$firewall_cmd --state')
def run_probe_with_firewalld_snapshots(
tmp_path: Path,
*,
runtime: str,
permanent: str,
) -> tuple[subprocess.CompletedProcess[str], list[str]]:
runtime_path = tmp_path / "runtime.zones"
runtime_path.write_text(runtime, encoding="utf-8")
permanent_path = tmp_path / "permanent.zones"
permanent_path.write_text(permanent, encoding="utf-8")
calls_path = tmp_path / "firewall.calls"
firewall_cmd = tmp_path / "firewall-cmd"
firewall_cmd.write_text(
"#!/bin/sh\n"
f'printf "%s\\n" "$*" >> "{calls_path}"\n'
'case "$*" in\n'
' "--state") printf "%s\\n" running; exit 0 ;;\n'
' "--get-default-zone") printf "%s\\n" public; exit 0 ;;\n'
' "--list-all-zones") '
f'/bin/cat "{runtime_path}"; exit 0 ;;\n'
' "--permanent --list-all-zones") '
f'/bin/cat "{permanent_path}"; exit 0 ;;\n'
"esac\n"
"exit 64\n",
encoding="utf-8",
)
firewall_cmd.chmod(0o700)
systemctl = tmp_path / "systemctl"
systemctl.write_text(
"#!/bin/sh\n"
'case "$*" in\n'
' "is-active firewalld") printf "%s\\n" active; exit 0 ;;\n'
' "is-active "*) printf "%s\\n" inactive; exit 3 ;;\n'
' "is-enabled firewalld") printf "%s\\n" enabled; exit 0 ;;\n'
' "is-enabled "*) printf "%s\\n" disabled; exit 1 ;;\n'
' "show -p LoadState --value firewalld") '
'printf "%s\\n" loaded; exit 0 ;;\n'
' "show -p LoadState --value "*) '
'printf "%s\\n" not-found; exit 0 ;;\n'
"esac\n"
"exit 64\n",
encoding="utf-8",
)
systemctl.chmod(0o700)
command = _CAPABILITY_PROBE_COMMAND
for candidate in (
"/usr/bin/firewall-cmd", "/usr/sbin/firewall-cmd",
"/bin/firewall-cmd", "/sbin/firewall-cmd",
):
command = command.replace(candidate, str(firewall_cmd))
for candidate in (
"/usr/bin/systemctl", "/usr/sbin/systemctl",
"/bin/systemctl", "/sbin/systemctl",
):
command = command.replace(candidate, str(systemctl))
result = subprocess.run(
shlex.split(command),
capture_output=True,
text=True,
check=False,
timeout=15,
)
calls = calls_path.read_text(encoding="utf-8").splitlines()
return result, calls
def test_capability_probe_uses_two_bounded_firewalld_zone_snapshots(tmp_path):
result, calls = run_probe_with_firewalld_snapshots(
tmp_path,
runtime=(
"public (default, active)\n"
" target: default\n"
" interfaces: br0 bond0\n"
" sources:\n"
" services: ssh\n"
"trusted\n"
" target: ACCEPT\n"
" interfaces:\n"
" sources:\n"
),
permanent=(
"public (default)\n"
" target: default\n"
" interfaces: br0\n"
" sources:\n"
"trusted\n"
" target: ACCEPT\n"
" interfaces:\n"
" sources:\n"
),
)
assert result.returncode == 0, result.stderr
assert (
"__VCAP_FIREWALL_IFACE_BEGIN__runtime\n"
"br0|public\n"
"bond0|public\n"
"__VCAP_FIREWALL_IFACE_END__runtime=0"
) in result.stdout
assert (
"__VCAP_FIREWALL_IFACE_BEGIN__permanent\n"
"br0|public\n"
"__VCAP_FIREWALL_IFACE_END__permanent=0"
) in result.stdout
assert "__VCAP_FIREWALL_CONF__source_zones=absent" in result.stdout
assert calls.count("--list-all-zones") == 1
assert calls.count("--permanent --list-all-zones") == 1
assert not any("--list-sources" in call for call in calls)
assert not any("--list-interfaces" in call for call in calls)
@pytest.mark.parametrize(
("runtime", "expected_source_zones", "expected_status"),
[
(
"public (active)\n"
" interfaces: br0\n"
" sources: 192.0.2.0/24\n",
"present",
"0",
),
(
"public (active)\n"
" interfaces: br0\n"
" sources:\n"
" sources:\n",
"unknown",
"1",
),
],
)
def test_capability_probe_firewalld_snapshot_is_fail_closed(
tmp_path,
runtime,
expected_source_zones,
expected_status,
):
result, _ = run_probe_with_firewalld_snapshots(
tmp_path,
runtime=runtime,
permanent=(
"public\n"
" interfaces: br0\n"
" sources:\n"
),
)
assert result.returncode == 0, result.stderr
assert (
f"__VCAP_FIREWALL_IFACE_END__runtime={expected_status}"
in result.stdout
)
assert (
f"__VCAP_FIREWALL_CONF__source_zones={expected_source_zones}"
in result.stdout
)
def test_capability_probe_executes_the_discovered_ip_binary():
assert (
"probe_binary ip /usr/sbin/ip /usr/bin/ip /sbin/ip /bin/ip"

@ -255,39 +255,133 @@ case "$firewall_backend" in nftables|iptables) : ;; *) firewall_backend=unknown
case "$firewall_table_owner" in yes|no) : ;; *) firewall_table_owner=unknown ;; esac
printf "__VCAP_FIREWALL_CONF__backend=%s\n" "$firewall_backend"
printf "__VCAP_FIREWALL_CONF__table_owner=%s\n" "$firewall_table_owner"
firewall_source_zones=unknown
firewall_cmd=
for firewall_candidate in /usr/bin/firewall-cmd /usr/sbin/firewall-cmd /bin/firewall-cmd /sbin/firewall-cmd; do
if test -x "$firewall_candidate"; then firewall_cmd="$firewall_candidate"; break; fi
done
firewall_inventory_ready=no
firewall_source_zones=unknown
if test "$firewall_service_state" = active && test -n "$firewall_cmd" &&
test "$($firewall_cmd --state 2>/dev/null)" = running; then
firewall_inventory_ready=yes
firewall_source_zones=absent
for firewall_scope in runtime permanent; do
if test "$firewall_scope" = permanent; then
firewall_zones=$($firewall_cmd --permanent --get-zones 2>/dev/null)
fi
# One bounded list-all-zones snapshot per scope replaces four per-zone D-Bus
# query loops. Only zone names, interface bindings and the presence of source
# bindings are projected. Missing fields, duplicate fields, malformed zone
# headers or oversized output fail closed instead of becoming an empty zone.
probe_firewall_zone_snapshot() {
firewall_snapshot_scope="$1"
printf "__VCAP_FIREWALL_IFACE_BEGIN__%s\n" "$firewall_snapshot_scope"
firewall_snapshot_status=1
firewall_snapshot=
if test "$firewall_inventory_ready" = yes; then
if test "$firewall_snapshot_scope" = permanent; then
firewall_snapshot=$(LC_ALL=C "$firewall_cmd" --permanent --list-all-zones 2>/dev/null)
else
firewall_zones=$($firewall_cmd --get-zones 2>/dev/null)
firewall_snapshot=$(LC_ALL=C "$firewall_cmd" --list-all-zones 2>/dev/null)
fi
if test $? != 0; then firewall_source_zones=unknown; break; fi
firewall_zone_count=0
for firewall_zone in $firewall_zones; do
case "$firewall_zone" in ""|*[!A-Za-z0-9_-]*) firewall_source_zones=unknown; break 2 ;; esac
if test "${#firewall_zone}" -gt 64; then firewall_source_zones=unknown; break 2; fi
firewall_zone_count=$((firewall_zone_count + 1))
if test "$firewall_zone_count" -gt 64; then firewall_source_zones=unknown; break 2; fi
if test "$firewall_scope" = permanent; then
firewall_sources=$($firewall_cmd --permanent --zone="$firewall_zone" --list-sources 2>/dev/null)
else
firewall_sources=$($firewall_cmd --zone="$firewall_zone" --list-sources 2>/dev/null)
firewall_snapshot_command_status=$?
if test "$firewall_snapshot_command_status" = 0 &&
test "${#firewall_snapshot}" -le 262144; then
firewall_snapshot_status=0
firewall_snapshot_zone=
firewall_snapshot_zone_count=0
firewall_snapshot_binding_count=0
firewall_snapshot_interface_fields=0
firewall_snapshot_source_fields=0
firewall_snapshot_seen_zones="|"
firewall_snapshot_seen_fields="|"
firewall_snapshot_has_sources=no
while IFS= read -r firewall_snapshot_line || test -n "$firewall_snapshot_line"; do
case "$firewall_snapshot_line" in
"") continue ;;
[![:space:]]*)
firewall_snapshot_zone=${firewall_snapshot_line%% *}
firewall_snapshot_suffix=${firewall_snapshot_line#"$firewall_snapshot_zone"}
case "$firewall_snapshot_zone" in
""|*[!A-Za-z0-9_-]*) firewall_snapshot_status=1; break ;;
esac
if test "${#firewall_snapshot_zone}" -gt 64; then
firewall_snapshot_status=1
break
fi
case "$firewall_snapshot_suffix" in
""|" (active)"|" (default)"|" (default, active)"|" (active, default)") : ;;
*) firewall_snapshot_status=1; break ;;
esac
case "$firewall_snapshot_seen_zones" in
*"|${firewall_snapshot_zone}|"*) firewall_snapshot_status=1; break ;;
esac
firewall_snapshot_seen_zones="${firewall_snapshot_seen_zones}${firewall_snapshot_zone}|"
firewall_snapshot_zone_count=$((firewall_snapshot_zone_count + 1))
if test "$firewall_snapshot_zone_count" -gt 64; then
firewall_snapshot_status=1
break
fi
;;
" interfaces:"*)
if test -z "$firewall_snapshot_zone"; then firewall_snapshot_status=1; break; fi
case "$firewall_snapshot_seen_fields" in
*"|${firewall_snapshot_zone}:interfaces|"*) firewall_snapshot_status=1; break ;;
esac
firewall_snapshot_seen_fields="${firewall_snapshot_seen_fields}${firewall_snapshot_zone}:interfaces|"
firewall_snapshot_interface_fields=$((firewall_snapshot_interface_fields + 1))
firewall_snapshot_ifaces=${firewall_snapshot_line#" interfaces:"}
for firewall_snapshot_iface in $firewall_snapshot_ifaces; do
case "$firewall_snapshot_iface" in
""|*[!A-Za-z0-9_.:-]*) firewall_snapshot_status=1; break 2 ;;
esac
if test "${#firewall_snapshot_iface}" -gt 15; then
firewall_snapshot_status=1
break 2
fi
firewall_snapshot_binding_count=$((firewall_snapshot_binding_count + 1))
if test "$firewall_snapshot_binding_count" -gt 4096; then
firewall_snapshot_status=1
break 2
fi
printf "%s|%s\n" "$firewall_snapshot_iface" "$firewall_snapshot_zone"
done
;;
" sources:"*)
if test -z "$firewall_snapshot_zone"; then firewall_snapshot_status=1; break; fi
case "$firewall_snapshot_seen_fields" in
*"|${firewall_snapshot_zone}:sources|"*) firewall_snapshot_status=1; break ;;
esac
firewall_snapshot_seen_fields="${firewall_snapshot_seen_fields}${firewall_snapshot_zone}:sources|"
firewall_snapshot_source_fields=$((firewall_snapshot_source_fields + 1))
firewall_snapshot_sources=${firewall_snapshot_line#" sources:"}
if test -n "$firewall_snapshot_sources"; then firewall_snapshot_has_sources=yes; fi
;;
" "*)
if test -z "$firewall_snapshot_zone"; then firewall_snapshot_status=1; break; fi
;;
*) firewall_snapshot_status=1; break ;;
esac
done <<__VCAP_FIREWALL_SNAPSHOT__
$firewall_snapshot
__VCAP_FIREWALL_SNAPSHOT__
if test "$firewall_snapshot_zone_count" -eq 0 ||
test "$firewall_snapshot_interface_fields" -ne "$firewall_snapshot_zone_count" ||
test "$firewall_snapshot_source_fields" -ne "$firewall_snapshot_zone_count"; then
firewall_snapshot_status=1
fi
if test $? != 0; then firewall_source_zones=unknown; break 2; fi
if test -n "$firewall_sources"; then firewall_source_zones=present; break 2; fi
done
if test "$firewall_zone_count" -eq 0; then firewall_source_zones=unknown; break; fi
done
fi
fi
fi
if test "$firewall_snapshot_status" != 0; then
firewall_source_zones=unknown
elif test "$firewall_snapshot_has_sources" = yes &&
test "$firewall_source_zones" != unknown; then
firewall_source_zones=present
fi
printf "__VCAP_FIREWALL_IFACE_END__%s=%s\n" "$firewall_snapshot_scope" "$firewall_snapshot_status"
}
probe_firewall_zone_snapshot runtime
probe_firewall_zone_snapshot permanent
printf "__VCAP_FIREWALL_CONF__source_zones=%s\n" "$firewall_source_zones"
firewall_default_runtime=unknown
firewall_default_permanent=unknown
if test "$firewall_service_state" = active && test -n "$firewall_cmd" &&
@ -307,51 +401,6 @@ fi
printf "__VCAP_FIREWALL_CONF__default_runtime=%s\n" "$firewall_default_runtime"
printf "__VCAP_FIREWALL_CONF__default_permanent=%s\n" "$firewall_default_permanent"
# Keep the interface-to-zone inventory bounded and syntactically closed. It
# is consumed only as structured evidence for the selected local Fabric
# endpoint; arbitrary firewalld output is never returned by the API.
probe_firewall_interfaces() {
firewall_iface_scope="$1"
printf "__VCAP_FIREWALL_IFACE_BEGIN__%s\n" "$firewall_iface_scope"
firewall_iface_status=1
if test "$firewall_service_state" = active && test -n "$firewall_cmd" &&
test "$($firewall_cmd --state 2>/dev/null)" = running; then
if test "$firewall_iface_scope" = permanent; then
firewall_iface_zones=$($firewall_cmd --permanent --get-zones 2>/dev/null)
else
firewall_iface_zones=$($firewall_cmd --get-zones 2>/dev/null)
fi
if test $? = 0; then
firewall_iface_status=0
firewall_iface_zone_count=0
firewall_iface_binding_count=0
for firewall_iface_zone in $firewall_iface_zones; do
case "$firewall_iface_zone" in ""|*[!A-Za-z0-9_-]*) firewall_iface_status=1; break ;; esac
if test "${#firewall_iface_zone}" -gt 64; then firewall_iface_status=1; break; fi
firewall_iface_zone_count=$((firewall_iface_zone_count + 1))
if test "$firewall_iface_zone_count" -gt 64; then firewall_iface_status=1; break; fi
if test "$firewall_iface_scope" = permanent; then
firewall_ifaces=$($firewall_cmd --permanent --zone="$firewall_iface_zone" --list-interfaces 2>/dev/null)
else
firewall_ifaces=$($firewall_cmd --zone="$firewall_iface_zone" --list-interfaces 2>/dev/null)
fi
if test $? != 0; then firewall_iface_status=1; break; fi
for firewall_iface in $firewall_ifaces; do
case "$firewall_iface" in ""|*[!A-Za-z0-9_.:-]*) firewall_iface_status=1; break 2 ;; esac
if test "${#firewall_iface}" -gt 15; then firewall_iface_status=1; break 2; fi
firewall_iface_binding_count=$((firewall_iface_binding_count + 1))
if test "$firewall_iface_binding_count" -gt 4096; then firewall_iface_status=1; break 2; fi
printf "%s|%s\n" "$firewall_iface" "$firewall_iface_zone"
done
done
if test "$firewall_iface_zone_count" -eq 0; then firewall_iface_status=1; fi
fi
fi
printf "__VCAP_FIREWALL_IFACE_END__%s=%s\n" "$firewall_iface_scope" "$firewall_iface_status"
}
probe_firewall_interfaces runtime
probe_firewall_interfaces permanent
# For NetworkManager, permanent interface ownership lives in the active
# connection keyfile rather than in firewalld zone XML. Resolve every active
# interface through one UUID and parse only the exact [connection] zone key

Loading…
Cancel
Save