From 68775a30cd57fe72fc2d1803f7032dce9d6d9a5a Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 14 Oct 2026 14:58:24 +0800 Subject: [PATCH] fix: scope VPC state route validation per network --- docs/VALIDATION.md | 31 ++++ docs/VPC_DATAPLANE_ACCEPTANCE.md | 19 +++ docs/知识库/Memory/CURRENT.md | 20 ++- engine/tests/test_fabric.py | 3 +- .../tests/test_fabric_libvirt_independence.py | 36 ++++- engine/tests/test_vpc_apply_receipts.py | 29 +++- .../test_vpc_local_neighbor_transaction.py | 14 +- engine/tests/test_vpc_state_overlay.py | 147 +++++++++++++++++- engine/virsh_panel/fabric.py | 14 +- engine/virsh_panel/vpc_dataplane.py | 3 +- 10 files changed, 295 insertions(+), 21 deletions(-) diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index a01fc19b..54812c83 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -1,5 +1,36 @@ # 本版验证记录 +## 2026-10-14:同宿主多 VPC 路由校验修复 + +- 现场新 VPC 的宿主回执为 `vpc_remote_command_failed`,成员网卡证据为空; + 目标实例手动启动后,运行和持久 XML 都只有公网网卡,没有私网配置。 + 本次未发现旧协调 claim 或目标 QGA pending 账本。历史命令未保留退出行, + 因而不能把下面的稳定复现扩写成历史这次唯一失败点的直接证明。 +- 根因复现:共享 loader 只在加载开始清空一次累计 route 文件,却在每份 state + 结束后用当前 CIDR 检查整个累计文件。两个合法、不重叠 VPC 的 state 各自可用, + 同目录加载时第二个会拒绝前一个的 `/32`。线上 helper 也含相同逻辑。 +- 修复只将完整记录解析后的 CIDR 检查限定到当前 state 的 route 行;保留全宿主 + route、tenant、peer 聚合及跨文件重复 peer IP 拒绝。CIDR 后置、外部网段和 + 另一 VPC 网段的恶意 route 仍拒绝,不按“全局任一 VPC CIDR”放宽。 +- Fabric helper 增加 `vpc-state-route-scope-contract-v1`;同摘要旧 helper 的 + 观察结果要求升级,普通观察不派发写入,显式 Fabric 协调可发布当前 helper。 + VPC apply 在网络和 state 变更前检查新契约,防止仅升级 Engine 后继续依赖旧 + 开机恢复脚本。未修改 libvirt 启动项、依赖或公网路径。 +- 新增 28 组双 VPC 行为回归。隔离旧 `54b55e2` 实现为 **8 failed、20 passed**; + 8 个正常场景全部复现错误,修复后完整 state-overlay 模块 **66 passed**。 + 覆盖真实 apply/observe 渲染、文件顺序交换、CIDR 在 route 前后、聚合完整性、 + 越界和重复 peer IP。旧 helper 门禁的 6 组回归也先失败;修复后两套 VPC + 门禁/回执模块 **211 passed**,Fabric helper 观察/显式升级相关用例通过。 +- 最终 Python **3.12.14 / pytest 9.1.1** 分组回归:Fabric 主套件、永久邻居、 + VPC nft、Fabric nft、VPC dataplane、state-overlay 六模块 **621 passed**; + 上述 VPC 门禁/回执两模块 **211 passed**;Fabric libvirt-independence 模块 + **20 passed**,九个不重叠模块合计 **852 passed**。27 条既有 XML truthiness + 弃用警告保留。7 个修改 Python 文件 AST、两种管理器的渲染脚本在 sh/dash/bash + 下共 30 项语法检查、`git diff --check` 及独立复核通过;不是全量引擎验收。 +- 本节记录已完成的源码修复及隔离 Shell/协调器测试;尚未部署本轮修改。 + 部署须先更新并回读相关宿主 helper,再正常停机目标实例,通过原加入任务核对 + 恢复;最终 private0、QGA 地址/路由及流量验收见 `VPC_DATAPLANE_ACCEPTANCE.md`。 + ## 2026-10-14:VPC 加入首次自动暂停与协调恢复边界 - 现场操作顺序已按宿主机日志和任务时间重新核对:revision 10 的加入流程先在 diff --git a/docs/VPC_DATAPLANE_ACCEPTANCE.md b/docs/VPC_DATAPLANE_ACCEPTANCE.md index d4144347..58741a30 100644 --- a/docs/VPC_DATAPLANE_ACCEPTANCE.md +++ b/docs/VPC_DATAPLANE_ACCEPTANCE.md @@ -61,6 +61,25 @@ 后台周期中的桥、tap、路由和邻居稳定性,再执行同机、跨机流量验收。 helper 发布成功本身不代表 VPC 或来宾网络已经可用。 +## 同宿主多 VPC 路由校验与 helper 升级 + +每份受管 state 的 `/32` route 必须在完整解析该文件后,仅按该文件唯一的 CIDR +校验;不能把累计的全宿主路由重新按最后一个 VPC 的 CIDR 校验,也不能按“属于 +任一 VPC 网段”放行。验证完成的 route、tenant IP 和 peer IP 仍需完整聚合, +跨 state 的重复 peer IP 继续拒绝。至少以两个不重叠网段验证正反文件顺序、CIDR +后置,以及跨到另一 VPC 网段的恶意 route 仍被拒绝。 + +宿主 `/usr/local/libexec/virt-cloud-wg-fabric` 还须包含第七行契约 +`# vpc-state-route-scope-contract-v1`。仅更新 Engine 不会替换已安装 helper; +同计划旧 helper 会被 Fabric 观察判为需要升级,普通 GET 和周期观察不执行升级。 +已有邻居契约、状态和在途操作均完整的环境,可通过原 Fabric 的显式“重新核对” +受控发布当前 helper;有旧邻居状态或未完成事务时,仍按上节先核实迁移证据。 +VPC apply 在写事务、state 和修改网桥之前拒绝缺少新契约的 helper。 + +helper 升级并通过完整回读后,正常关闭首次接入尚未完成的目标实例,对原 VPC +任务执行“核对 / 重试”,再按保留的手动电源意图启动并验证 private0、`/32` +和来宾路由。不得重建加入任务或直接写库标记成功;本地回归不代替该现场验收。 + ## 成员接入和回读 对一个跨两台宿主机的 VPC,至少放置两个成员和一个 ACL,逐台检查: diff --git a/docs/知识库/Memory/CURRENT.md b/docs/知识库/Memory/CURRENT.md index 05735e49..4eeb3e77 100644 --- a/docs/知识库/Memory/CURRENT.md +++ b/docs/知识库/Memory/CURRENT.md @@ -2,7 +2,7 @@ type: status status: active project: Virt Cloud -updated: 2026-10-12 +updated: 2026-10-14 tags: - memory/current - project/virt-cloud @@ -10,6 +10,24 @@ tags: # 当前项目交接:Virt Cloud +## 2026-10-14 同宿主多 VPC 路由校验修复 + +- **当前源码**:在 `venix` 的 `54b55e2` 基础上完成本轮修复与回归,尚未部署。 +- **已完成**:每份 state 的路由仅按本文件唯一 CIDR 校验;全宿主 route/tenant/peer + 聚合保留。新增双 VPC、文件顺序、CIDR 后置、越界和重复 peer IP 行为回归; + 旧实现 8 个合法组合全部失败,修复后 state-overlay 模块 66 项通过。 +- **宿主升级边界**:新增 helper 路由契约;Fabric 显式协调可升级同计划旧 helper, + 普通观察只报告需要升级。VPC apply 在 state/网络变更前拒绝旧 helper。 + 最终九个相关模块共 852 项通过,保留 27 条既有警告;AST、30 项渲染 Shell + 语法及独立复核通过,不是全量引擎或现场验收。 +- **现场状态与下一步**:新成员仍只有公网网卡。本轮未再次停机、重试或改数据库; + 部署后须先升级并回读宿主 helper,再正常停机目标并核对原加入任务。 + 历史下发只保存通用错误,不能断言已还原唯一退出行;本地复现不证明现场已恢复。 +- 代码验证与迁移顺序分别见仓库 `docs/VALIDATION.md`、 + `docs/VPC_DATAPLANE_ACCEPTANCE.md`;网络边界沿用 + [[Concepts/Virt Cloud WireGuard Fabric与VPC L3私网]], + 邻居与恢复条件沿用 [[Concepts/Virt Cloud VPC永久邻居与重复协调]]。 + ## 2026-10-12 VPC 实例候选归属修复已交付 - **当前源码**:venix `09801dc6e8de7d208e8bc0494960ca323542bd30` 已提交推送,远端同值,工作区干净。 diff --git a/engine/tests/test_fabric.py b/engine/tests/test_fabric.py index f3770659..ce28c225 100644 --- a/engine/tests/test_fabric.py +++ b/engine/tests/test_fabric.py @@ -340,7 +340,8 @@ def test_observe_artifact_state_accepts_only_kind_specific_header( "# virt-cloud-wg-fabric-v1\n" f"# fabric-plan-digest={current.digest}\n" "# libvirt-startup-independent-v1\n" - "# vpc-local-neighbor-contract-v1\n", + "# vpc-local-neighbor-contract-v1\n" + "# vpc-state-route-scope-contract-v1\n", encoding="utf-8", ) artifact.chmod(int(mode, 8)) diff --git a/engine/tests/test_fabric_libvirt_independence.py b/engine/tests/test_fabric_libvirt_independence.py index f0459e32..18ca2893 100644 --- a/engine/tests/test_fabric_libvirt_independence.py +++ b/engine/tests/test_fabric_libvirt_independence.py @@ -138,7 +138,8 @@ def test_new_scripts_neither_install_gate_nor_control_libvirt_units(mode): assert "nft_plan_owned" in apply -def startup_files(root, current, *, legacy=False, gate=True, legacy_neighbors=False): +def startup_files(root, current, *, legacy=False, gate=True, + legacy_neighbors=False, legacy_routes=False): from virsh_panel.fabric import FABRIC_STARTUP_ISOLATION_MARKER root.mkdir(parents=True, exist_ok=True) for name, content, mode in (("helper", _BOOT_HELPER, 0o700), ("unit", _UNIT, 0o644)): @@ -147,6 +148,8 @@ def startup_files(root, current, *, legacy=False, gate=True, legacy_neighbors=Fa content = content.replace(FABRIC_STARTUP_ISOLATION_MARKER + "\n", "") if legacy_neighbors and name == "helper": content = content.replace("# vpc-local-neighbor-contract-v1\n", "") + if legacy_routes and name == "helper": + content = content.replace("# vpc-state-route-scope-contract-v1\n", "") target = root / name target.write_text(content) target.chmod(mode) @@ -195,13 +198,16 @@ def test_dangling_generator_still_prevents_exact_startup_readback(tmp_path): assert startup_readback(tmp_path, current) == ["foreign", "owned"] -def test_same_plan_legacy_neighbor_helper_requires_upgrade_without_changing_unit(tmp_path): +@pytest.mark.parametrize("upgrade", ["neighbors", "routes"]) +def test_same_plan_legacy_helper_requires_upgrade_without_changing_unit(tmp_path, upgrade): current = plan() - startup_files(tmp_path, current, gate=False, legacy_neighbors=True) + startup_files(tmp_path, current, gate=False, + legacy_neighbors=upgrade == "neighbors", + legacy_routes=upgrade == "routes") assert startup_readback(tmp_path, current) == ["foreign", "owned"] -@pytest.mark.parametrize("upgrade", ["startup", "neighbors"]) +@pytest.mark.parametrize("upgrade", ["startup", "neighbors", "routes"]) def test_explicit_reconcile_upgrades_same_plan_then_skips_current_hosts(tmp_path, monkeypatch, upgrade): import asyncio from virsh_panel.fabric import FabricCoordinator, FABRIC_STARTUP_ISOLATION_MARKER @@ -225,6 +231,7 @@ def test_explicit_reconcile_upgrades_same_plan_then_skips_current_hosts(tmp_path rendered = _apply_script(actual_plan, host, keys) assert FABRIC_STARTUP_ISOLATION_MARKER in rendered assert "# vpc-local-neighbor-contract-v1" in rendered + assert "# vpc-state-route-scope-contract-v1" in rendered assert 'record_desired_absent gate_generator' in rendered startup_files(self.manager.startup_root, actual_plan, gate=False) return super().dispatch_apply(actual_plan, host, keys, operation_id) @@ -246,7 +253,26 @@ def test_explicit_reconcile_upgrades_same_plan_then_skips_current_hosts(tmp_path for component in components.values(): startup_files(component.manager.startup_root, current, legacy=upgrade == "startup", gate=upgrade == "startup", - legacy_neighbors=upgrade == "neighbors") + legacy_neighbors=upgrade == "neighbors", + legacy_routes=upgrade == "routes") + previous_helpers = { + host_id: (component.manager.startup_root / "helper").read_bytes() + for host_id, component in components.items() + } + identity_calls = { + host_id: component.manager.async_identity_calls + for host_id, component in components.items() + } + # The ordinary observation loop reports the stale contract without + # publishing a helper or dispatching an implicit upgrade. + await coordinator.reconcile_all() + observed = coordinator.get_status(current.fabric_id) + assert all(node["state"] == "degraded" for node in observed["nodes"]) + for host_id, component in components.items(): + manager = component.manager + assert manager.async_dispatch_calls == 1 + assert manager.async_identity_calls == identity_calls[host_id] + assert (manager.startup_root / "helper").read_bytes() == previous_helpers[host_id] admitted = await coordinator.enqueue_reconcile(current.fabric_id) assert admitted["operation"]["id"] != initial["operation"]["id"] await wait() diff --git a/engine/tests/test_vpc_apply_receipts.py b/engine/tests/test_vpc_apply_receipts.py index eeadbee5..b5c1a985 100644 --- a/engine/tests/test_vpc_apply_receipts.py +++ b/engine/tests/test_vpc_apply_receipts.py @@ -331,7 +331,9 @@ def test_explicit_nft_absence_repairs_without_losing_public_observation(plan): @pytest.mark.parametrize('shell', SHELLS) -@pytest.mark.parametrize('mutation', ['none', 'nft', 'state', 'ifup', 'missing_state', 'helper']) +@pytest.mark.parametrize('mutation', [ + 'none', 'nft', 'state', 'ifup', 'missing_state', 'helper', 'old_route_scope', +]) def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, mutation, plan): nft_file = tmp_path / 'vpc.nft' state = tmp_path / 'vpc.state' @@ -352,7 +354,20 @@ def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, 'import hashlib,sys,pathlib\n' 'raw=pathlib.Path(sys.argv[1]).read_bytes() if len(sys.argv)>1 else sys.stdin.buffer.read()\n' 'print(hashlib.sha256(raw).hexdigest()+" -")\n') - stat = executable(tmp_path / 'stat', 'print("0:0:600")\n') + helper = tmp_path / 'virt-cloud-wg-fabric' + if mutation != 'helper': + header = ( + '#!/bin/sh\n# virt-cloud-wg-fabric-v1\n' + f'# fabric-plan-digest={plan.fabric_plan_digest}\n' + f'PLAN_DIGEST="{plan.fabric_plan_digest}"\n' + '# libvirt-startup-independent-v1\n' + '# vpc-local-neighbor-contract-v1\n') + if mutation != 'old_route_scope': + header += '# vpc-state-route-scope-contract-v1\n' + helper.write_text(header) + helper.chmod(0o700) + stat = executable(tmp_path / 'stat', 'import sys\n' + f'print("0:0:700" if sys.argv[-1] == {str(helper)!r} else "0:0:600")\n') nft = executable(tmp_path / 'nft', NFT_MOCK) ip = executable(tmp_path / 'ip', 'print("20: vcv-test: mtu 1500 state UP")\n') rendered = dp.render_observe_script(plan) @@ -362,12 +377,15 @@ def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, 'IP': ip, 'NFT': nft, 'SHA256SUM': sha, 'STAT': stat, 'BRIDGE': 'vcv-test', 'TABLE': 'owned', 'NFT_FILE': str(nft_file), 'STATE': str(state), 'IF_FRAGMENT': str(fragment), + 'EXPECTED_DIGEST': plan.fabric_plan_digest, 'nft_file_ok': 'yes', 'bridge_shape': 'yes', 'nm_owned': 'no', 'ifup_owned': 'yes', 'nm_present': 'no', } - script = dp.VPC_APPLY_RECEIPT_SH + '\n' + '\n'.join( + helper_guard = dp.VPC_LOCAL_NEIGHBOR_TRANSACTION_SH.replace( + 'vc_ln_helper=/usr/local/libexec/virt-cloud-wg-fabric', + 'vc_ln_helper=' + shlex.quote(str(helper))) + script = dp.VPC_APPLY_RECEIPT_SH + '\n' + helper_guard + '\n' + '\n'.join( k + '=' + shlex.quote(v) for k, v in settings.items()) + '\n' - script += 'vc_vpc_neighbor_helper_ready() { return ' + ('1' if mutation == 'helper' else '0') + '; }\n' result = subprocess.run([shell, '-eu'], input=script + fragment_script, text=True, capture_output=True) assert result.returncode == 0, result.stderr @@ -376,4 +394,5 @@ def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, assert markers['nft_file_exact'] == ('no' if mutation == 'nft' else 'yes') assert markers['state_file_exact'] == ('no' if mutation in {'state', 'missing_state'} else 'yes') assert markers['persistence_exact'] == ('no' if mutation == 'ifup' else 'yes') - assert markers['helper_contract'] == ('missing' if mutation == 'helper' else 'owned') + assert markers['helper_contract'] == ( + 'missing' if mutation in {'helper', 'old_route_scope'} else 'owned') diff --git a/engine/tests/test_vpc_local_neighbor_transaction.py b/engine/tests/test_vpc_local_neighbor_transaction.py index d358aaeb..f3677135 100644 --- a/engine/tests/test_vpc_local_neighbor_transaction.py +++ b/engine/tests/test_vpc_local_neighbor_transaction.py @@ -198,7 +198,9 @@ def test_rollback_refuses_binding_changed_after_snapshot(tmp_path, shell): @pytest.mark.parametrize("shell", SHELLS) -@pytest.mark.parametrize("case", ["new", "old", "wrong_digest", "wrong_mode", "symlink", "missing"]) +@pytest.mark.parametrize("case", [ + "new", "old", "old_route_scope", "wrong_digest", "wrong_mode", "symlink", "missing", +]) def test_helper_upgrade_gate_runs_before_network_mutation(tmp_path, shell, case): env, prefix, state, actions = boundary(tmp_path) digest = "ab" * 32 @@ -206,6 +208,8 @@ def test_helper_upgrade_gate_runs_before_network_mutation(tmp_path, shell, case) header = ("#!/bin/sh\n# virt-cloud-wg-fabric-v1\n# fabric-plan-digest=" + digest + '\nPLAN_DIGEST="' + digest + '"\n# libvirt-startup-independent-v1\n') if case != "old": header += "# vpc-local-neighbor-contract-v1\n" + if case not in {"old", "old_route_scope"}: + header += "# vpc-state-route-scope-contract-v1\n" if case == "wrong_digest": header = header.replace(digest, "cd" * 32) if case != "missing": helper.write_text(header) @@ -222,10 +226,16 @@ def test_helper_upgrade_gate_runs_before_network_mutation(tmp_path, shell, case) guard = "vc_vpc_neighbor_helper_ready || exit 71" assert script.index(guard) < script.index('mkdir -m 700 "$TXN"') assert script.index(guard) < script.index("MUTATION_STARTED=yes") - result = execute(shell, env, prefix, guard + '\n"$IP" link set dev "$BRIDGE" down\nprintf ready\n') + assert script.index(guard) < script.index('cat > "$STATE_FILE"') + gated_state = tmp_path / "gated.state" + env["GATED_STATE"] = str(gated_state) + result = execute(shell, env, prefix, guard + + '\nprintf configured > "$GATED_STATE"\n' + '"$IP" link set dev "$BRIDGE" down\nprintf ready\n') assert result.returncode == (0 if case == "new" else 71), result.stderr assert result.stdout == ("ready" if case == "new" else "") assert actions.read_text() == ("down\n" if case == "new" else "") + assert gated_state.exists() is (case == "new") @pytest.mark.parametrize("shell", SHELLS) diff --git a/engine/tests/test_vpc_state_overlay.py b/engine/tests/test_vpc_state_overlay.py index dc111480..ee938db7 100644 --- a/engine/tests/test_vpc_state_overlay.py +++ b/engine/tests/test_vpc_state_overlay.py @@ -12,9 +12,152 @@ from virsh_panel.shell_contracts import WG_ALLOWED_IPS_SH -def _loader_from_script(script): +def _loader_from_script(script, *, observe=False): start = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"') - return script[start:script.index('vpc_state_load || exit 74', start)] + end = 'FABRIC_NFT_SNAPSHOT=' if observe else 'vpc_state_load || exit 74' + return script[start:script.index(end, start)] + + +def _two_vpc_plans(): + first = _plan() + payload = first.vpc.canonical() + payload['vpc_id'] = 'vpc-second-network' + payload['operation_id'] = 'vpc-second-network-op-0001' + payload['cidr'] = '10.45.0.0/24' + for subnet in payload['subnets']: + subnet['cidr'] = subnet['cidr'].replace('10.44.', '10.45.') + for member in payload['members']: + member['private_ip_cidr'] = member['private_ip_cidr'].replace( + '10.44.', '10.45.') + payload['fabric_peers'] = [{ + 'host_id': first.peers[0].host_id, + 'public_key': first.peers[0].public_key, + 'overlay_ip': first.peers[0].overlay_cidr, + 'tenant_allowed_ips': ['10.45.0.11/32'], + }] + second = dataplane_module.parse_vpc_dataplane_plan( + payload, first.host_id, fabric_plan_digest=first.fabric_plan_digest) + return first, second + + +def _state_text(plan, *, cidr_after_routes=False): + text = dataplane_module._state(plan, dataplane_module._routes(plan)) + if cidr_after_routes: + # State membership must be checked after parsing the complete record. + # A route can precede its CIDR without inheriting a previous file's CIDR. + lines = text.splitlines() + cidr = next(line for line in lines if line.startswith('cidr|')) + lines.remove(cidr) + lines.append(cidr) + text = '\n'.join(lines) + '\n' + return text + + +def _run_multi_vpc_loader(tmp_path, plan, texts, mode): + state_dir = tmp_path / 'states' + state_dir.mkdir(mode=0o700) + # The production loader sorts valid token filenames through the shell glob. + # Fixed token names let callers exchange the two real rendered records and + # prove that acceptance does not depend on which VPC is read first. + for index, text in enumerate(texts, 1): + state_file = state_dir / f'{index:011x}.state' + state_file.write_text(text) + state_file.chmod(0o600) + observe = mode == 'observe' + script = (dataplane_module.render_observe_script(plan) if observe + else dataplane_module.render_apply_script(plan)) + harness = '\n'.join(( + 'set -eu', + _loader_from_script(script, observe=observe), + 'VPC_STATE_DIR=' + shlex.quote(str(state_dir)), + 'VPC_PEER_FILE=' + shlex.quote(str(tmp_path / 'peers')), + 'VPC_ROUTE_FILE=' + shlex.quote(str(tmp_path / 'routes')), + 'VPC_TENANT_FILE=' + shlex.quote(str(tmp_path / 'tenants')), + 'FIREWALL_MODE=native_nftables', + r''' +vc_fixture_stat() { + [ "$1" = -c ] || return 9 + case "$2" in + '%u:%a') + if [ "$3" = "$VPC_STATE_DIR" ]; then printf '0:700\n' + elif [ "${3%/*}" = "$VPC_STATE_DIR" ] && [ -f "$3" ]; then + printf '0:600\n' + else return 9; fi ;; + '%s') [ "${3%/*}" = "$VPC_STATE_DIR" ] && [ -f "$3" ] || return 9 + wc -c < "$3" | tr -d ' ' ;; + *) return 9 ;; + esac +} +STAT=vc_fixture_stat +vpc_state_load +printf 'state=%s\n' "$VPC_STATE_STATUS" +'''.strip(), + )) + return subprocess.run(['/bin/sh', '-c', harness], text=True, + capture_output=True, check=False) + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('reverse', [False, True]) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +def test_multi_vpc_state_load_keeps_each_route_in_its_own_cidr( + tmp_path, mode, reverse, cidr_after_routes): + first, second = _two_vpc_plans() + texts = [_state_text(plan, cidr_after_routes=cidr_after_routes) + for plan in (first, second)] + result = _run_multi_vpc_loader( + tmp_path, first, texts[::-1] if reverse else texts, mode) + assert result.returncode == 0, result.stderr + assert result.stdout == 'state=owned\n' + expected_routes = [] + for plan, prefix in ((first, '10.44.0'), (second, '10.45.0')): + table = plan.resources.route_table + expected_routes.extend([ + f'{prefix}.10/32|{plan.resources.bridge}|{table}|-|100', + f'{prefix}.11/32|wg-fabric0|{table}|-|100', + ]) + assert sorted((tmp_path / 'routes').read_text().splitlines()) == sorted( + expected_routes) + assert sorted((tmp_path / 'tenants').read_text().splitlines()) == [ + '10.44.0.10/32', '10.44.0.11/32', + '10.45.0.10/32', '10.45.0.11/32', + ] + key = first.peers[0].public_key + assert sorted((tmp_path / 'peers').read_text().splitlines()) == [ + key + '|10.44.0.11/32', key + '|10.45.0.11/32', + ] + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('reverse', [False, True]) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +@pytest.mark.parametrize('foreign_prefix', ['10.45.0.99/32', '10.46.0.99/32']) +def test_multi_vpc_state_load_rejects_routes_outside_the_owning_record( + tmp_path, mode, reverse, cidr_after_routes, foreign_prefix): + first, second = _two_vpc_plans() + bad = _state_text(first, cidr_after_routes=cidr_after_routes).replace( + 'route|10.44.0.11/32|', 'route|' + foreign_prefix + '|') + texts = [bad, _state_text(second)] + result = _run_multi_vpc_loader( + tmp_path, first, texts[::-1] if reverse else texts, mode) + assert result.returncode == (1 if mode == 'observe' else 71), result.stderr + assert 'state=owned' not in result.stdout + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('reverse', [False, True]) +def test_multi_vpc_state_load_rejects_duplicate_peer_ips_across_records( + tmp_path, mode, reverse): + first, second = _two_vpc_plans() + key = first.peers[0].public_key + duplicate = _state_text(second).replace( + 'peer|' + second.peers[0].host_id + '|' + key + '|10.45.0.11/32', + 'peer|' + second.peers[0].host_id + '|' + key + '|10.44.0.11/32') + texts = [_state_text(first), duplicate] + result = _run_multi_vpc_loader( + tmp_path, first, texts[::-1] if reverse else texts, mode) + assert result.returncode == (1 if mode == 'observe' else 71), result.stderr + assert 'state=owned' not in result.stdout @pytest.mark.parametrize('peer_mode', ['none', 'valid', 'duplicate_ip', 'public_ip']) diff --git a/engine/virsh_panel/fabric.py b/engine/virsh_panel/fabric.py index 894f56e1..30787972 100644 --- a/engine/virsh_panel/fabric.py +++ b/engine/virsh_panel/fabric.py @@ -565,14 +565,17 @@ vpc_state_load() { [ "$vpc_snapshot_count" = 0 ] || vpc_state_fail || return 1 vpc_state_local_neighbors_valid "$vpc_state_file" "$vpc_state_bridge" \ "$vpc_state_table" "$vpc_state_cidr_value" || vpc_state_fail || return 1 - # Validate membership after the complete record has been parsed so a - # malicious route cannot precede the cidr line and bypass the check. - while IFS='|' read -r vpc_route_prefix vpc_route_dev vpc_route_tbl vpc_route_via vpc_route_metric vpc_route_extra; do + # Validate only this state's routes against its CIDR after the whole + # record has been parsed. The shared route file also holds other VPCs; + # checking it here would reject their disjoint prefixes. Deferring the + # check still prevents a route before the cidr line from bypassing it. + while IFS='|' read -r vpc_route_kind vpc_route_prefix vpc_route_dev vpc_route_tbl vpc_route_via vpc_route_metric vpc_route_extra; do + [ "$vpc_route_kind" = route ] || continue [ -n "$vpc_route_prefix" ] || continue [ -z "$vpc_route_extra" ] || vpc_state_fail || return 1 vpc_state_cidr_contains "$vpc_route_prefix" "$vpc_state_cidr_value" || vpc_state_fail || return 1 - done < "$VPC_ROUTE_FILE" + done < "$vpc_state_file" done if [ "$vpc_state_files" -gt 0 ]; then VPC_STATE_STATUS=owned; fi return 0 @@ -2656,6 +2659,7 @@ _BOOT_HELPER = r'''#!/bin/sh PLAN_DIGEST="__VC_FABRIC_DIGEST__" # libvirt-startup-independent-v1 # vpc-local-neighbor-contract-v1 +# vpc-state-route-scope-contract-v1 EXPECTED_DIGEST="$PLAN_DIGEST" EXPECTED_NFT_RULES_SHA256="__VC_FABRIC_NFT_RULES_SHA256__" DEPLOYMENT_MODE="__VC_FABRIC_MODE__" @@ -8153,6 +8157,8 @@ artifact_state() {{ if [ "$artifact_kind" = helper ]; then grep -Fqx -- '# vpc-local-neighbor-contract-v1' "$artifact_path" || {{ printf '%s' foreign; return; }} + grep -Fqx -- '# vpc-state-route-scope-contract-v1' "$artifact_path" || + {{ printf '%s' foreign; return; }} fi if [ "$artifact_kind" = helper ] && {{ [ -e "$GATE_GENERATOR" ] || [ -L "$GATE_GENERATOR" ]; }}; then diff --git a/engine/virsh_panel/vpc_dataplane.py b/engine/virsh_panel/vpc_dataplane.py index e6679ef2..025ebb31 100644 --- a/engine/virsh_panel/vpc_dataplane.py +++ b/engine/virsh_panel/vpc_dataplane.py @@ -242,7 +242,8 @@ vc_vpc_neighbor_helper_ready() { [ "$(sed -n '2p' "$vc_ln_helper")" = '# virt-cloud-wg-fabric-v1' ] && [ "$(sed -n '3p' "$vc_ln_helper")" = "# fabric-plan-digest=$EXPECTED_DIGEST" ] && [ "$(sed -n '4p' "$vc_ln_helper")" = "PLAN_DIGEST=\"$EXPECTED_DIGEST\"" ] && - [ "$(sed -n '6p' "$vc_ln_helper")" = '# vpc-local-neighbor-contract-v1' ] + [ "$(sed -n '6p' "$vc_ln_helper")" = '# vpc-local-neighbor-contract-v1' ] && + [ "$(sed -n '7p' "$vc_ln_helper")" = '# vpc-state-route-scope-contract-v1' ] } vc_vpc_neighbors_snapshot() { vc_ln_snapshot=$(vc_local_neighbor_records "$BRIDGE") || return 1