From d50e6d529e3970fbc4efcc9fa0bce4678e797512 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 14 Oct 2026 16:28:31 +0800 Subject: [PATCH] fix: align Fabric tenant sets with validated peer state --- docs/VALIDATION.md | 36 +++ docs/VPC_DATAPLANE_ACCEPTANCE.md | 14 +- docs/知识库/Memory/CURRENT.md | 18 ++ engine/tests/test_fabric.py | 2 +- .../tests/test_fabric_libvirt_independence.py | 4 +- engine/tests/test_fabric_local_neighbors.py | 13 +- engine/tests/test_vpc_apply_receipts.py | 2 +- .../test_vpc_local_neighbor_transaction.py | 2 +- engine/tests/test_vpc_state_overlay.py | 225 +++++++++++++++++- engine/virsh_panel/fabric.py | 19 +- engine/virsh_panel/vpc_dataplane.py | 2 +- 11 files changed, 324 insertions(+), 13 deletions(-) diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index 2fc874ab..ded00072 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -1,5 +1,41 @@ # 本版验证记录 +## 2026-10-14:Fabric tenant 集合写入与观察合同对齐 + +- `8172a3f` 已在现场部署并重启 Engine;新核对 + `791e7fd68d0d41fca5ec91e7d6ce053d` 中 Debian 成功,Alma 为 + `uncertain/observe/final_readback/readback_failed`,底层 + `fabric_readback_mismatch`。两宿主 helper/服务/管理器、ACL 归属、sysctl、 + 路由与 peer AllowedIPs 检查均正常;Alma 的 `vpc_nft_exact=false`。 +- 实际 xx state 的三个 route 目的为 `10.0.0.1/.3/.4`,peer 还保存新私网的 + `10.0.1.1/32`。共享 loader 仅用 route 填充 `VPC_TENANT_FILE`,刷新后的 + nft 集合因此只有三个地址;Fabric observer 按 route 与 peer 的并集核对, + 必须得到四个地址。不是上次桥 DOWN/路由恢复错误复发,也不是旧 paused 标签。 +- loader 现在将已验证 peer 地址并入 tenant 文件,最后仅对派生 tenant 集合去重; + route/peer 原始冲突检查独立保留,额外提前拒绝重复 route 目的,防止去重掩盖 + 原来在 nft refresh 才拒绝的重复记录。route 仍只按本 state 唯一 CIDR 校验, + 不会因全局 peer 已授权某地址就允许本 VPC 写入该外部网段的 route。 +- helper 契约升级为 `vpc-state-route-scope-contract-v3`,确保另一台当时正常的 + 宿主也发布相同集合合同;普通 GET 仍不派发写入。仅调整 Fabric 共享传输集合, + 不创建 IP 路由、VPC 成员或放宽租户 ACL,事务快照、锁与回滚边界不变。 +- 旧 `8172a3f` 隔离回归的 16 个正常 union/peer-only 场景全部复现地址遗漏; + 重复 peer 和已授权 peer 地址仍不能用于越界 route 的 12 个负向场景通过。 + 新增两个重复 route 场景在旧 loader 可进入 owned,本次在来源校验阶段拒绝。 +- 另有 6 个真实 loader → nft batch → 集合回读 → Fabric adapter 闭环用例: + 完整集合通过,缺地址、多地址均拒绝。旧版两个正常闭环精准复现刷新成功、 + shell 集合核对通过但 adapter 最终核对失败;修复后 state-overlay 完整模块 + **102 passed**。相关九个不重叠模块累计 **888 passed**,保留 27 条既有 XML + truthiness 弃用警告;8 个修改 Python 文件 AST、两种管理器渲染脚本在 + sh/dash/bash 下共 33 项语法检查和独立复核通过,`git diff --check` 通过。 +- 回归期间修复了既有邻居异常测试的夹具漂移:上轮 mock 已改为遍历状态,测试 + 却仍替换旧单行 `pass`,导致 8 个异常场景没有注入。隔离 `8172a3f` 同样 + 8 failed;本次精确替换当前 mock 并断言唯一匹配,8 个安全场景恢复通过, + 没有为此修改生产邻居或紧急隔离逻辑。 +- 本节尚未部署 v3 或再次重试现场网络。现有 state 的全局 peer 快照仍可能在 + 多 state 间重复,或在删除后残留;本轮不声称已完成历史地址归属/撤销迁移。 + 后续若改为每 state 仅保存本 VPC 的 peer 贡献,须有明确的旧记录受控迁移, + 不能取消 peer 冲突检查或静默过滤历史文件来伪造收敛。 + ## 2026-10-14:NM 保留私网桥的路由恢复顺序 - 现场显式 Fabric 核对 `cfb4e6913f764049bcbf507be55d8b40` 中,Debian 节点 diff --git a/docs/VPC_DATAPLANE_ACCEPTANCE.md b/docs/VPC_DATAPLANE_ACCEPTANCE.md index 309b5a9a..ab4cfa09 100644 --- a/docs/VPC_DATAPLANE_ACCEPTANCE.md +++ b/docs/VPC_DATAPLANE_ACCEPTANCE.md @@ -70,7 +70,7 @@ 后置,以及跨到另一 VPC 网段的恶意 route 仍被拒绝。 宿主 `/usr/local/libexec/virt-cloud-wg-fabric` 还须包含第七行契约 -`# vpc-state-route-scope-contract-v2`。仅更新 Engine 不会替换已安装 helper; +`# vpc-state-route-scope-contract-v3`。仅更新 Engine 不会替换已安装 helper; 同计划旧 helper 会被 Fabric 观察判为需要升级,普通 GET 和周期观察不执行升级。 已有邻居契约、状态和在途操作均完整的环境,可通过原 Fabric 的显式“重新核对” 受控发布当前 helper;有旧邻居状态或未完成事务时,仍按上节先核实迁移证据。 @@ -80,6 +80,18 @@ helper 升级并通过完整回读后,正常关闭首次接入尚未完成的 任务执行“核对 / 重试”,再按保留的手动电源意图启动并验证 private0、`/32` 和来宾路由。不得重建加入任务或直接写库标记成功;本地回归不代替该现场验收。 +## Fabric tenant 集合的统一口径 + +`VPC_TENANT_FILE`、Fabric nft `vpc_tenant_ips` 和观察期望必须统一为全部受管 +state 的已验证 route 与 peer `/32` 的去重并集。peer 行可携带认证计划中的全局 +Fabric 地址,即使该地址不在当前 state 的 CIDR 中;这不授权当前 VPC 创建该 +地址的 route。仅派生 tenant 集合可去重,原始重复 route 或 peer 仍必须拒绝。 + +至少验证另一个 VPC 的 peer-only 地址、route/peer 自然重叠、多个 state 与文件 +顺序、CIDR 后置、nft refresh 后完整回读;故意漏掉任一 peer 地址时观察不得 +active。集合对齐不等于每份旧全局 peer 快照已完成归属/删除收敛;涉及历史快照 +清理时保留原操作与严格迁移边界,不通过删除旧 VPC 或改写成功状态绕过。 + ## NetworkManager 带运行网卡的桥恢复 对带运行 tap 的受管桥,Fabric 隔离只关闭桥的行政状态,保留 NM active profile、 diff --git a/docs/知识库/Memory/CURRENT.md b/docs/知识库/Memory/CURRENT.md index 2c13ac77..20d25146 100644 --- a/docs/知识库/Memory/CURRENT.md +++ b/docs/知识库/Memory/CURRENT.md @@ -10,6 +10,24 @@ tags: # 当前项目交接:Virt Cloud +## 2026-10-14 Fabric tenant 集合回读不一致 + +- **现场新事实**:`8172a3f` 已部署,宿主 helper v2 已发布。新操作中 Debian + 成功,Alma 的配置/服务/管理器/路由均正常,仅 `vpc_nft_exact=false`,进入 + uncertain/observe/final_readback;不是上次桥恢复顺序错误复发。 +- **根因与源码修复**:写入仅收集 route 地址,观察却核对 route∪peer;旧 VPC + peer 已携带另一私网地址,因而少写一个 `/32`。共享 loader 改为同一并集, + 仅派生 tenant 去重,来源 peer/route 冲突仍拒绝,route 继续严格按本 state CIDR。 + helper 契约升 v3,普通观察只读,显式核对发布。 +- **验证**:相关九模块不重复 888 passed,含真实 nft batch 与 Fabric adapter + 的完整/漏地址/多地址闭环;旧版两例闭环精准复现本次错误。既有邻居异常测试 + 的失效注入同时修正;独立复核通过。源码回归不代替宿主或来宾验收。 +- **边界**:本轮尚未部署 v3 或重试现场;不声称历史全局 peer 快照已完成删除或 + 多 state 归属迁移。部署后先完成宿主回读和原 Fabric 核对,再推进原加入任务。 +- 证据与验收见仓库 `docs/VALIDATION.md`、`docs/VPC_DATAPLANE_ACCEPTANCE.md`; + 关系沿用 [[Concepts/Virt Cloud WireGuard Fabric与VPC L3私网]] 与 + [[Concepts/Virt Cloud VPC永久邻居与重复协调]]。 + ## 2026-10-14 Fabric 核对失败与保留桥恢复顺序 - **现场新事实**:`68775a3` 已部署,显式核对中 Debian 节点成功,Alma 节点在 diff --git a/engine/tests/test_fabric.py b/engine/tests/test_fabric.py index 5570edac..af914a0f 100644 --- a/engine/tests/test_fabric.py +++ b/engine/tests/test_fabric.py @@ -341,7 +341,7 @@ def test_observe_artifact_state_accepts_only_kind_specific_header( f"# fabric-plan-digest={current.digest}\n" "# libvirt-startup-independent-v1\n" "# vpc-local-neighbor-contract-v1\n" - "# vpc-state-route-scope-contract-v2\n", + "# vpc-state-route-scope-contract-v3\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 1b25f57c..bed488f9 100644 --- a/engine/tests/test_fabric_libvirt_independence.py +++ b/engine/tests/test_fabric_libvirt_independence.py @@ -149,7 +149,7 @@ def startup_files(root, current, *, legacy=False, gate=True, 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-v2\n", "") + content = content.replace("# vpc-state-route-scope-contract-v3\n", "") target = root / name target.write_text(content) target.chmod(mode) @@ -231,7 +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-v2" in rendered + assert "# vpc-state-route-scope-contract-v3" 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) diff --git a/engine/tests/test_fabric_local_neighbors.py b/engine/tests/test_fabric_local_neighbors.py index a442f1ba..cd67c46a 100644 --- a/engine/tests/test_fabric_local_neighbors.py +++ b/engine/tests/test_fabric_local_neighbors.py @@ -185,9 +185,16 @@ def test_emergency_owned_bridge_stays_isolated_after_neighbor_drift(tmp_path, sh body, env = _fixture(tmp_path, 'healthy') tool = tmp_path / 'bin' / 'ip' - tool.write_text(tool.read_text().replace( - "elif args==['-o','-4','neigh','show','dev',b]:pass", - "elif args==['-o','-4','neigh','show','dev',b]:print(" + repr(raw) + ")")) + neighbor_read = ( + "elif args==['-o','-4','neigh','show','dev',b]:\n" + " for address,mac in sorted(s.get('neighbors',{}).items()):" + "print(address+' dev '+b+' lladdr '+mac+' PERMANENT')" + ) + source = tool.read_text() + assert source.count(neighbor_read) == 1 + tool.write_text(source.replace( + neighbor_read, + "elif args==['-o','-4','neigh','show','dev',b]:print(" + repr(raw) + ")", 1)) if not scenario.startswith('legacy_'): state = tmp_path / 'state' / (TOKEN + '.state') state.write_text(state.read_text() + 'local_neighbor|' + PAIR + '\n') diff --git a/engine/tests/test_vpc_apply_receipts.py b/engine/tests/test_vpc_apply_receipts.py index 96196948..9d7fcd15 100644 --- a/engine/tests/test_vpc_apply_receipts.py +++ b/engine/tests/test_vpc_apply_receipts.py @@ -363,7 +363,7 @@ def test_rendered_host_receipt_requires_exact_persistent_bytes(tmp_path, shell, '# libvirt-startup-independent-v1\n' '# vpc-local-neighbor-contract-v1\n') if mutation != 'old_route_scope': - header += '# vpc-state-route-scope-contract-v2\n' + header += '# vpc-state-route-scope-contract-v3\n' helper.write_text(header) helper.chmod(0o700) stat = executable(tmp_path / 'stat', 'import sys\n' diff --git a/engine/tests/test_vpc_local_neighbor_transaction.py b/engine/tests/test_vpc_local_neighbor_transaction.py index afa56a7e..7a63dc0f 100644 --- a/engine/tests/test_vpc_local_neighbor_transaction.py +++ b/engine/tests/test_vpc_local_neighbor_transaction.py @@ -209,7 +209,7 @@ def test_helper_upgrade_gate_runs_before_network_mutation(tmp_path, shell, case) '\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-v2\n" + header += "# vpc-state-route-scope-contract-v3\n" if case == "wrong_digest": header = header.replace(digest, "cd" * 32) if case != "missing": helper.write_text(header) diff --git a/engine/tests/test_vpc_state_overlay.py b/engine/tests/test_vpc_state_overlay.py index ee938db7..81a20f62 100644 --- a/engine/tests/test_vpc_state_overlay.py +++ b/engine/tests/test_vpc_state_overlay.py @@ -2,8 +2,10 @@ from __future__ import annotations from dataclasses import replace +import json import shlex import subprocess +import sys import pytest from test_vpc_nft_dataplane import _plan @@ -53,7 +55,21 @@ def _state_text(plan, *, cidr_after_routes=False): return text -def _run_multi_vpc_loader(tmp_path, plan, texts, mode): +def _with_global_peer_tenants(plan, *extra_ips): + """Use the same trusted peer closure that the coordinator supplies.""" + payload = plan.vpc.canonical() + tenants = tuple(sorted(set(plan.peers[0].tenant_allowed_ips) | set(extra_ips))) + peer = plan.peers[0] + payload['fabric_peers'] = [{ + 'host_id': peer.host_id, 'public_key': peer.public_key, + 'overlay_ip': peer.overlay_cidr, 'tenant_allowed_ips': list(tenants), + }] + return dataplane_module.parse_vpc_dataplane_plan( + payload, plan.host_id, fabric_plan_digest=plan.fabric_plan_digest, + trusted_tenant_allowed_ips={peer.host_id: tenants}) + + +def _run_multi_vpc_loader(tmp_path, plan, texts, mode, *, extra_script=''): state_dir = tmp_path / 'states' state_dir.mkdir(mode=0o700) # The production loader sorts valid token filenames through the shell glob. @@ -92,6 +108,7 @@ STAT=vc_fixture_stat vpc_state_load printf 'state=%s\n' "$VPC_STATE_STATUS" '''.strip(), + extra_script, )) return subprocess.run(['/bin/sh', '-c', harness], text=True, capture_output=True, check=False) @@ -128,6 +145,212 @@ def test_multi_vpc_state_load_keeps_each_route_in_its_own_cidr( ] +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('records', ['single', 'forward', 'reverse']) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +def test_global_peer_tenants_join_routes_once_in_the_fabric_address_universe( + tmp_path, mode, records, cidr_after_routes): + first, second = _two_vpc_plans() + extra = '10.46.0.21/32' + first = _with_global_peer_tenants(first, extra) + plans = [first] if records == 'single' else [first, second] + if records == 'reverse': + plans.reverse() + texts = [_state_text(plan, cidr_after_routes=cidr_after_routes) + for plan in plans] + result = _run_multi_vpc_loader(tmp_path, first, texts, mode) + assert result.returncode == 0, result.stderr + assert result.stdout == 'state=owned\n' + route_ips = {row['destination_cidr'] for plan in plans + for row in dataplane_module._routes(plan)} + expected = sorted(route_ips | {extra}) + actual = (tmp_path / 'tenants').read_text().splitlines() + assert sorted(actual) == expected + assert len(actual) == len(set(actual)) + expected_peers = sorted( + peer.public_key + '|' + address + for plan in plans for peer in plan.peers + for address in peer.tenant_allowed_ips) + assert sorted((tmp_path / 'peers').read_text().splitlines()) == expected_peers + # A global peer authorization contributes no route in this VPC's table. + # It must not weaken the independent per-record route/CIDR contract. + routes = (tmp_path / 'routes').read_text().splitlines() + assert {row.split('|', 1)[0] for row in routes} == route_ips + assert extra not in route_ips + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +def test_peer_only_state_keeps_its_global_tenants_without_local_routes( + tmp_path, mode, cidr_after_routes): + carrier = _with_global_peer_tenants(_plan(), '10.46.0.21/32') + # The loader supports a retained peer-only record after local members and + # routes disappear. Render that record with the real state producer; + # extract the loader from an ordinary participating-host transaction. + peer_only = replace(carrier, members=()) + text = _state_text(peer_only, cidr_after_routes=cidr_after_routes) + assert '\nroute|' not in text and '\nlocal_neighbor|' not in text + result = _run_multi_vpc_loader(tmp_path, carrier, [text], mode) + assert result.returncode == 0, result.stderr + assert result.stdout == 'state=owned\n' + assert (tmp_path / 'routes').read_text() == '' + actual = (tmp_path / 'tenants').read_text().splitlines() + assert sorted(actual) == ['10.44.0.11/32', '10.46.0.21/32'] + assert len(actual) == len(set(actual)) + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('reverse', [False, True]) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +def test_global_tenant_union_still_rejects_duplicate_peer_ownership( + tmp_path, mode, reverse, cidr_after_routes): + first, second = _two_vpc_plans() + first = _with_global_peer_tenants(first, '10.46.0.21/32') + second = _with_global_peer_tenants(second, '10.46.0.21/32') + 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 == (1 if mode == 'observe' else 71), result.stderr + assert 'state=owned' not in result.stdout + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('cidr_after_routes', [False, True]) +def test_global_peer_authorization_cannot_authorize_a_foreign_state_route( + tmp_path, mode, cidr_after_routes): + extra = '10.46.0.21/32' + plan = _with_global_peer_tenants(_plan(), extra) + text = _state_text(plan, cidr_after_routes=cidr_after_routes).replace( + 'route|10.44.0.11/32|', 'route|' + extra + '|') + result = _run_multi_vpc_loader(tmp_path, plan, [text], 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']) +def test_tenant_union_deduplication_cannot_hide_duplicate_route_ownership( + tmp_path, mode): + plan = _with_global_peer_tenants(_plan(), '10.46.0.21/32') + text = _state_text(plan) + # A remote route bypasses the local-neighbor/direct-route check. Its + # duplicate must still fail before tenant-set de-duplication can hide it. + text += next(line for line in text.splitlines() + if line.startswith('route|') and '|wg-fabric0|' in line) + '\n' + result = _run_multi_vpc_loader(tmp_path, plan, [text], mode) + assert result.returncode == (1 if mode == 'observe' else 71), result.stderr + assert 'state=owned' not in result.stdout + + +def _stateful_tenant_nft(tmp_path, mutation): + tool = tmp_path / 'nft' + tool.write_text( + '#!' + sys.executable + '\nROOT=' + repr(str(tmp_path)) + + '\nMUTATION=' + repr(mutation) + '\n' + r''' +import ipaddress,json,re,sys +from pathlib import Path +root=Path(ROOT);args=sys.argv[1:] +if args==['-s','list','table','inet','virt_cloud_wg_fabric']: + print('fixture-owned-fabric-table') +elif args[:2]==['-c','-f'] or args[:1]==['-f']: + batch=Path(args[-1]).read_text() + lines=batch.splitlines() + assert lines[0]=='flush set inet virt_cloud_wg_fabric vpc_tenant_ips',lines + match=re.fullmatch(r'add element inet virt_cloud_wg_fabric vpc_tenant_ips \{ (.*) \}',lines[1]) + assert len(lines)==2 and match,batch + values=[str(ipaddress.ip_network(value.strip(),strict=True)) + for value in match[1].split(',')] + assert all(value.endswith('/32') for value in values),values + assert len(values)==len(set(values)),values + (root/('checked.batch' if args[0]=='-c' else 'applied.batch')).write_text(batch) + if args[0]!='-c': + if MUTATION=='missing':values.remove('10.46.0.21/32') + elif MUTATION=='extra':values.append('10.47.0.99/32') + (root/'live-set.json').write_text(json.dumps(sorted(values))) +elif args==['list','set','inet','virt_cloud_wg_fabric','vpc_tenant_ips']: + values=json.loads((root/'live-set.json').read_text()) + print('table inet virt_cloud_wg_fabric {\n set vpc_tenant_ips {\n type ipv4_addr\n flags interval') + print(' elements = { '+', '.join(value.removesuffix('/32') for value in values)+' }') + print(' }\n}') +else:raise SystemExit(90) +''') + tool.chmod(0o700) + return tool + + +@pytest.mark.parametrize('mode', ['apply', 'observe']) +@pytest.mark.parametrize('mutation', ['none', 'missing', 'extra']) +def test_global_tenant_refresh_and_fabric_observer_share_the_exact_universe( + tmp_path, mode, mutation): + from test_fabric import FakeManager, KEY1, KEY2, plan as fabric_plan + from virsh_panel.fabric import FabricHostAdapter, _VPC_NFT_OBSERVE_FRAGMENT + + base = _plan() + carrier = replace(base, peers=(replace(base.peers[0], public_key=KEY2),)) + carrier = _with_global_peer_tenants(carrier, '10.46.0.21/32') + nft = _stateful_tenant_nft(tmp_path, mutation) + script = '\n'.join(( + 'NFT=' + shlex.quote(str(nft)), + 'EXPECTED_DIGEST=' + shlex.quote(carrier.fabric_plan_digest), + # Ownership is independent of the tenant-universe contract exercised + # here. Both exact-state reads must see this same owned table. + 'nft_plan_owned() { [ "$1" = fixture-owned-fabric-table ] && ' + '[ "$2" = "$EXPECTED_DIGEST" ]; }', + 'vpc_state_refresh_nft', + 'if vpc_state_nft_set_matches; then printf "nft_matches=yes\\n"; ' + 'else printf "nft_matches=no\\n"; fi', + _VPC_NFT_OBSERVE_FRAGMENT, + )) + result = _run_multi_vpc_loader( + tmp_path, carrier, [_state_text(carrier)], mode, extra_script=script) + assert result.returncode == 0, result.stderr + wanted = ['10.44.0.10/32', '10.44.0.11/32', '10.46.0.21/32'] + assert (tmp_path / 'checked.batch').read_text() == ( + tmp_path / 'applied.batch').read_text() + matches = mutation == 'none' + assert ('nft_matches=yes\n' in result.stdout) == matches + + # Use the real loader output and real nft observation fragment for every + # VPC marker. The existing manager supplies only the unrelated healthy + # Fabric background; the adapter's strict union comparison is unchanged. + peer_rows = (tmp_path / 'peers').read_text().splitlines() + tenant_ips = (tmp_path / 'tenants').read_text().splitlines() + nft_markers = '\n'.join(line for line in result.stdout.splitlines() + if line.startswith('__VC_FABRIC_VPC_NFT_')) + '\n' + current = fabric_plan() + manager = FakeManager(KEY1) + original_exec = manager._exec + + def readback(argv, *, timeout, input_text=None): + background = original_exec(argv, timeout=timeout, input_text=input_text) + output = '\n'.join(line for line in background.stdout.splitlines() + if not line.startswith('__VC_FABRIC_VPC_')) + '\n' + peer = current.nodes[1] + marker = f'__VC_FABRIC_PEER__={KEY2}|{peer.endpoint}|{peer.allowed_ip}' + extras = ','.join(row.split('|', 1)[1] for row in peer_rows) + output = output.replace(marker + '\n', marker + ',' + extras + '\n') + output += '__VC_FABRIC_VPC_STATE__=owned\n' + output += ''.join('__VC_FABRIC_VPC_TENANT__=' + row + '\n' + for row in peer_rows) + output += ''.join('__VC_FABRIC_VPC_TENANT_IP__=' + ip + '\n' + for ip in tenant_ips) + output += nft_markers + return background.__class__(background.args, background.returncode, + output, background.stderr) + + manager._exec = readback + observed = FabricHostAdapter(manager).observe( + current, current.nodes[0], {'host-a': KEY1, 'host-b': KEY2}) + assert observed['observed']['peer_allowed_exact'] is True + assert observed['observed']['vpc_nft_state'] == 'owned' + assert observed['observed']['vpc_nft_exact'] is matches + assert observed['config_ok'] is matches + assert 'add element inet virt_cloud_wg_fabric vpc_tenant_ips { ' + ( + ', '.join(wanted)) + ' }' in (tmp_path / 'applied.batch').read_text() + assert observed['observed']['vpc_nft_ips'] == json.loads( + (tmp_path / 'live-set.json').read_text()) + + @pytest.mark.parametrize('mode', ['apply', 'observe']) @pytest.mark.parametrize('reverse', [False, True]) @pytest.mark.parametrize('cidr_after_routes', [False, True]) diff --git a/engine/virsh_panel/fabric.py b/engine/virsh_panel/fabric.py index 41e7ba54..12390048 100644 --- a/engine/virsh_panel/fabric.py +++ b/engine/virsh_panel/fabric.py @@ -392,6 +392,7 @@ vpc_state_load() { chmod 600 "$VPC_TENANT_FILE" || vpc_state_fail || return 1 vpc_state_files=0 vpc_state_seen_ips= + vpc_state_seen_route_ips= for vpc_state_file in "$VPC_STATE_DIR"/*.state; do [ -e "$vpc_state_file" ] || [ -L "$vpc_state_file" ] || continue vpc_state_files=$((vpc_state_files + 1)) @@ -510,6 +511,10 @@ vpc_state_load() { [ "$vpc_route_metric" -gt 65535 ] 2>/dev/null; then vpc_state_fail || return 1 fi + case " $vpc_state_seen_route_ips " in + *" $vpc_a "*) vpc_state_fail || return 1 ;; + esac + vpc_state_seen_route_ips="$vpc_state_seen_route_ips $vpc_a" printf '%s|%s|%s|%s|%s\n' "$vpc_a" "$vpc_b" "$vpc_c" "$vpc_route_via" "$vpc_route_metric" >> "$VPC_ROUTE_FILE" || vpc_state_fail || return 1 # The Fabric quarantine set must cover both sides of an @@ -538,6 +543,12 @@ vpc_state_load() { printf '%s|%s\n' "$vpc_b" "$vpc_ip" >> "$VPC_PEER_FILE" || { IFS=$vpc_old_ifs; vpc_state_fail || return 1; } + # A trusted peer snapshot may include another VPC on + # this Fabric. The shared nft set must cover the same + # route/peer universe that the observer verifies. + printf '%s\n' "$vpc_ip" >> "$VPC_TENANT_FILE" || { + IFS=$vpc_old_ifs; vpc_state_fail || return 1; + } done IFS=$vpc_old_ifs ;; '') : ;; @@ -577,6 +588,10 @@ vpc_state_load() { vpc_state_fail || return 1 done < "$vpc_state_file" done + # Route and peer records naturally overlap. Deduplicate only the derived + # tenant universe; the independent route/peer conflict checks above must + # still reject ambiguous source records before any nft refresh. + LC_ALL=C sort -u -o "$VPC_TENANT_FILE" "$VPC_TENANT_FILE" || vpc_state_fail || return 1 if [ "$vpc_state_files" -gt 0 ]; then VPC_STATE_STATUS=owned; fi return 0 } @@ -2659,7 +2674,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-v2 +# vpc-state-route-scope-contract-v3 EXPECTED_DIGEST="$PLAN_DIGEST" EXPECTED_NFT_RULES_SHA256="__VC_FABRIC_NFT_RULES_SHA256__" DEPLOYMENT_MODE="__VC_FABRIC_MODE__" @@ -8315,7 +8330,7 @@ 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-v2' "$artifact_path" || + grep -Fqx -- '# vpc-state-route-scope-contract-v3' "$artifact_path" || {{ printf '%s' foreign; return; }} fi if [ "$artifact_kind" = helper ] && diff --git a/engine/virsh_panel/vpc_dataplane.py b/engine/virsh_panel/vpc_dataplane.py index add68581..b2ef9c7f 100644 --- a/engine/virsh_panel/vpc_dataplane.py +++ b/engine/virsh_panel/vpc_dataplane.py @@ -243,7 +243,7 @@ vc_vpc_neighbor_helper_ready() { [ "$(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 '7p' "$vc_ln_helper")" = '# vpc-state-route-scope-contract-v2' ] + [ "$(sed -n '7p' "$vc_ln_helper")" = '# vpc-state-route-scope-contract-v3' ] } vc_vpc_neighbors_snapshot() { vc_ln_snapshot=$(vc_local_neighbor_records "$BRIDGE") || return 1