Fix Fabric explicit-source route parsing

venix
admin 1 month ago
parent 0c5bfb1d0c
commit d1c0ac6749

@ -17,8 +17,9 @@
时才整体迁移;修改 endpoint 后旧 plan 必须显示 `migration_required`,不能原地冒充
active。
- 记录 endpoint 地址所属接口,并逐个执行普通 `ip -4 route get <peer>` 与
`ip -4 route get <peer> from <本机 endpoint>`。两次结果都必须只有一个 `dev` 和 `src`,
`src` 必须等于本机 endpoint,地址 owner 接口也必须与出口接口一致;loopback、dummy、
`ip -4 route get <peer> from <本机 endpoint>`。普通查询必须只有一个 `dev` 和 `src`;
显式源查询必须只有一个相同的 `dev` 和 `from`,可选的 `src` 若存在也必须唯一且相同。
两种来源字段都必须等于本机 endpoint,地址 owner 接口也必须与出口接口一致;loopback、dummy、
`wg-fabric0`、VPC bridge、多路径、错误源地址或不同出口均应阻断自动放行。
## 防火墙所有权模式

@ -2136,14 +2136,22 @@ def test_native_runtime_guard_rejects_inactive_unmasked_firewalld(tmp_path):
assert result.returncode != 0
def test_underlay_route_path_requires_exact_selected_source(tmp_path):
@pytest.mark.parametrize(("distribution", "target", "source"), [
("debian-13-iproute2-6.15", "203.0.113.123", "203.0.113.126"),
("almalinux-10-iproute-6.17", "198.51.100.126", "198.51.100.123"),
])
def test_underlay_route_path_accepts_real_explicit_from_output(
tmp_path, distribution, target, source,
):
fake_ip = tmp_path / "ip"
fake_ip.write_text(
"#!/bin/sh\n"
"if [ \"${5:-}\" = from ]; then\n"
" printf '%s dev %s src %s\\n' \"$4\" \"$VC_FROM_DEV\" \"$VC_FROM_SRC\"\n"
" printf '%s from %s dev %s uid 0\\n cache\\n' "
"\"$4\" \"$VC_FROM_SRC\" \"$VC_FROM_DEV\"\n"
"else\n"
" printf '%s dev %s src %s\\n' \"$4\" \"$VC_PLAIN_DEV\" \"$VC_PLAIN_SRC\"\n"
" printf '%s dev %s src %s uid 0\\n cache\\n' "
"\"$4\" \"$VC_PLAIN_DEV\" \"$VC_PLAIN_SRC\"\n"
"fi\n",
encoding="utf-8",
)
@ -2153,29 +2161,123 @@ def test_underlay_route_path_requires_exact_selected_source(tmp_path):
f"IP='{fake_ip}'",
"find_tool() { for p in \"$@\"; do [ -x \"$p\" ] && { printf '%s' \"$p\"; return 0; }; done; return 1; }",
_FIREWALL_MODE_GUARD_SH,
"vc_underlay_route_path 203.0.113.2 203.0.113.1",
f"vc_underlay_route_path {target} {source}",
))
base_env = {
"PATH": "/usr/bin:/bin",
"VC_PLAIN_DEV": "eth0",
"VC_FROM_DEV": "eth0",
"VC_FROM_SRC": "203.0.113.1",
"VC_PLAIN_DEV": "br0",
"VC_FROM_DEV": "br0",
"VC_FROM_SRC": source,
}
exact = subprocess.run(
["/bin/sh", "-c", script],
env={**base_env, "VC_PLAIN_SRC": source},
check=False, capture_output=True, text=True,
)
assert exact.returncode == 0, f"{distribution}: {exact.stderr}"
assert exact.stdout == f"br0|{source}"
@pytest.mark.parametrize(("plain_source", "explicit_source"), [
("10.20.30.40", "203.0.113.1"),
("203.0.113.1", "10.20.30.40"),
])
def test_underlay_route_path_rejects_inexact_plain_or_explicit_source(
tmp_path, plain_source, explicit_source,
):
fake_ip = tmp_path / "ip"
fake_ip.write_text(
"#!/bin/sh\n"
"if [ \"${5:-}\" = from ]; then\n"
" printf '%s from %s dev eth0 uid 0\\n cache\\n' "
"\"$4\" \"$VC_FROM_SRC\"\n"
"else\n"
" printf '%s dev eth0 src %s uid 0\\n cache\\n' "
"\"$4\" \"$VC_PLAIN_SRC\"\n"
"fi\n",
encoding="utf-8",
)
fake_ip.chmod(0o700)
script = "\n".join((
"set -eu",
f"IP='{fake_ip}'",
"find_tool() { for p in \"$@\"; do [ -x \"$p\" ] && { printf '%s' \"$p\"; return 0; }; done; return 1; }",
_FIREWALL_MODE_GUARD_SH,
"vc_underlay_route_path 203.0.113.2 203.0.113.1",
))
mismatch = subprocess.run(
["/bin/sh", "-c", script],
env={**base_env, "VC_PLAIN_SRC": "10.20.30.40"},
env={
"PATH": "/usr/bin:/bin",
"VC_PLAIN_SRC": plain_source,
"VC_FROM_SRC": explicit_source,
},
check=False, capture_output=True, text=True,
)
assert mismatch.returncode != 0
exact = subprocess.run(
def _run_underlay_explicit_evidence(
tmp_path: Path, explicit_output: str,
) -> subprocess.CompletedProcess[str]:
fake_ip = tmp_path / "ip"
fake_ip.write_text(
"#!/bin/sh\n"
"if [ \"${5:-}\" = from ]; then\n"
" printf '%s\\n' \"$VC_EXPLICIT_OUTPUT\"\n"
"else\n"
" printf '%s\\n' '203.0.113.2 dev br0 src 203.0.113.1 uid 0'\n"
"fi\n",
encoding="utf-8",
)
fake_ip.chmod(0o700)
script = "\n".join((
"set -eu",
f"IP='{fake_ip}'",
"find_tool() { for p in \"$@\"; do [ -x \"$p\" ] && { printf '%s' \"$p\"; return 0; }; done; return 1; }",
_FIREWALL_MODE_GUARD_SH,
"vc_underlay_route_path 203.0.113.2 203.0.113.1",
))
return subprocess.run(
["/bin/sh", "-c", script],
env={**base_env, "VC_PLAIN_SRC": "203.0.113.1"},
env={
"PATH": "/usr/bin:/bin",
"VC_EXPLICIT_OUTPUT": explicit_output,
},
check=False, capture_output=True, text=True,
)
assert exact.returncode == 0, exact.stderr
assert exact.stdout == "eth0|203.0.113.1"
def test_underlay_route_path_accepts_matching_optional_selected_source(
tmp_path,
):
result = _run_underlay_explicit_evidence(
tmp_path,
"203.0.113.2 from 203.0.113.1 dev br0 "
"src 203.0.113.1 uid 0",
)
assert result.returncode == 0, result.stderr
assert result.stdout == "br0|203.0.113.1"
@pytest.mark.parametrize("explicit_output", [
"203.0.113.2 dev br0 uid 0",
"203.0.113.2 dev br0 src 203.0.113.1 uid 0",
"203.0.113.2 from 203.0.113.1 from 203.0.113.1 dev br0 uid 0",
(
"203.0.113.2 from 203.0.113.1 dev br0 "
"src 10.20.30.40 uid 0"
),
"203.0.113.2 from 203.0.113.1 dev br0 dev br0 uid 0",
"203.0.113.2 dev br0 src 203.0.113.1 from",
])
def test_underlay_route_path_rejects_missing_or_ambiguous_explicit_evidence(
tmp_path, explicit_output,
):
result = _run_underlay_explicit_evidence(tmp_path, explicit_output)
assert result.returncode != 0
def _run_native_apply_underlay_preflight(
@ -2213,8 +2315,8 @@ def _run_native_apply_underlay_preflight(
" printf '%s\\n' \"203.0.113.2 dev $VC_PLAIN_DEV "
"src $VC_PLAIN_SRC\" ;;\n"
" '-4 route get 203.0.113.2 from 203.0.113.1')\n"
" printf '%s\\n' \"203.0.113.2 dev $VC_FROM_DEV "
"src $VC_FROM_SRC\" ;;\n"
" printf '%s\\n' \"203.0.113.2 from $VC_FROM_SRC "
"dev $VC_FROM_DEV uid 0\" \" cache\" ;;\n"
" *) exit 91 ;;\n"
"esac\n",
encoding="utf-8",
@ -2268,6 +2370,7 @@ def test_native_apply_uses_local_endpoint_without_firewalld_metadata(tmp_path):
@pytest.mark.parametrize(("kwargs", "expected_status"), [
({"local_address": "192.0.2.99"}, 71),
({"plain_source": "192.0.2.99"}, 77),
({"from_source": "192.0.2.99"}, 77),
({"from_device": "eth1"}, 77),
])
def test_native_apply_rejects_nonlocal_or_inexact_underlay_path(

@ -1510,16 +1510,41 @@ vc_firewalld_live_nft_ready() {
printf '%s\n' "$vc_firewalld_table" | grep -Eq '"flags":\[[^]]*"persist"'
}
vc_underlay_route_fields() {
awk '
vc_route_expected_from=${1:-}
awk -v expected_from="$vc_route_expected_from" '
{
for (i=1; i<=NF; i++) {
if ($i=="dev" && i<NF) {device=$(i+1); devices++}
if ($i=="src" && i<NF) {source=$(i+1); sources++}
if ($i=="dev") {
devices++
if (i>=NF) invalid=1; else device=$(i+1)
}
if ($i=="src") {
sources++
if (i>=NF) invalid=1; else source=$(i+1)
}
if ($i=="from") {
explicit_froms++
if (i>=NF) invalid=1; else explicit_from=$(i+1)
}
}
}
END {
if (devices != 1 || sources != 1) exit 1
printf "%s|%s", device, source
if (invalid || devices != 1) exit 1
if (expected_from == "") {
if (sources != 1 || explicit_froms != 0) exit 1
selected_source=source
} else {
# `ip route get TARGET from SOURCE` reports the constrained
# source as `from SOURCE` on current Debian and AlmaLinux. It
# may omit `src` because SOURCE was supplied by the caller.
if (explicit_froms != 1 || explicit_from != expected_from ||
sources > 1 ||
(sources == 1 && source != expected_from)) {
exit 1
}
selected_source=expected_from
}
printf "%s|%s", device, selected_source
}
'
}
@ -1542,7 +1567,7 @@ vc_underlay_route_path() {
# must resolve to the same exact path or the underlay is ambiguous.
vc_route_from=$($IP -4 route get "$vc_route_target" from "$vc_route_source" 2>/dev/null) || return 1
vc_route_from_fields=$(printf '%s\n' "$vc_route_from" |
vc_underlay_route_fields) || return 1
vc_underlay_route_fields "$vc_route_source") || return 1
[ "$vc_route_from_fields" = "$vc_route_plain_fields" ] || return 1
printf '%s' "$vc_route_plain_fields"
}

Loading…
Cancel
Save