You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
666 lines
26 KiB
Python
666 lines
26 KiB
Python
"""Fabric plan, helper, and coordinator tests."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import base64
|
|
import json
|
|
import subprocess
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.fabric import (
|
|
FABRIC_INTERFACE,
|
|
FABRIC_OWNER_MARKER,
|
|
FabricError,
|
|
FabricHostAdapter,
|
|
FabricCoordinator,
|
|
_BOOT_HELPER,
|
|
_apply_script,
|
|
_identity_script,
|
|
_observe_script,
|
|
parse_fabric_plan,
|
|
validate_wireguard_key,
|
|
)
|
|
|
|
|
|
KEY1 = base64.b64encode(bytes([1]) * 32).decode()
|
|
KEY2 = base64.b64encode(bytes([2]) * 32).decode()
|
|
|
|
|
|
def plan():
|
|
return parse_fabric_plan({
|
|
"fabric_id": "hk-fabric",
|
|
"region": "hk",
|
|
"overlay_cidr": "100.64.1.0/24",
|
|
"revision": 1,
|
|
"listen_port": 51820,
|
|
"nodes": [
|
|
{"host_id": "host-a", "overlay_ip": "100.64.1.1",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
{"host_id": "host-b", "overlay_ip": "100.64.1.2",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
],
|
|
})
|
|
|
|
|
|
def test_plan_is_canonical_and_rejects_wide_or_tenant_routes():
|
|
first = plan()
|
|
reordered = parse_fabric_plan({
|
|
"nodes": [
|
|
{"host_id": "host-b", "overlay_ip": "100.64.1.2",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
{"host_id": "host-a", "overlay_ip": "100.64.1.1",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
],
|
|
"fabric_id": "hk-fabric", "region": "hk",
|
|
"overlay_cidr": "100.64.1.0/24", "revision": 1,
|
|
})
|
|
assert first.digest == reordered.digest
|
|
with pytest.raises(FabricError):
|
|
parse_fabric_plan({
|
|
"fabric_id": "bad", "region": "hk",
|
|
"overlay_cidr": "10.0.0.0/8", "revision": 1,
|
|
"nodes": [
|
|
{"host_id": "a", "overlay_ip": "10.0.0.1",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
{"host_id": "b", "overlay_ip": "10.0.0.2",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
],
|
|
})
|
|
with pytest.raises(FabricError):
|
|
parse_fabric_plan({
|
|
"fabric_id": "bad", "region": "hk",
|
|
"overlay_cidr": "100.64.1.0/24", "revision": 1,
|
|
"nodes": [
|
|
{"host_id": "a", "overlay_ip": "100.64.1.1",
|
|
"endpoint": "100.64.1.2:51820"},
|
|
{"host_id": "b", "overlay_ip": "100.64.1.2",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
],
|
|
})
|
|
with pytest.raises(FabricError):
|
|
parse_fabric_plan({
|
|
"fabric_id": "bad", "region": "hk",
|
|
"overlay_cidr": "100.64.1.0/24", "revision": 1,
|
|
"nodes": [
|
|
{"host_id": "a", "overlay_ip": "100.64.1.1",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
{"host_id": "b", "overlay_ip": "100.64.1.2",
|
|
"endpoint": "0.0.0.0:51820"},
|
|
],
|
|
})
|
|
|
|
|
|
def test_wireguard_key_rejects_the_all_zero_reserved_key():
|
|
with pytest.raises(FabricError):
|
|
validate_wireguard_key(base64.b64encode(bytes(32)).decode())
|
|
|
|
|
|
def test_generated_helpers_are_posix_safe_and_do_not_embed_private_keys():
|
|
current = plan()
|
|
scripts = (
|
|
_BOOT_HELPER,
|
|
_identity_script(),
|
|
_observe_script(("100.64.1.2",), "100.64.1.1",
|
|
("203.0.113.2",), "networkmanager_nmcli",
|
|
"12345678-1234-5678-1234-567812345678"),
|
|
_apply_script(current, current.nodes[0], {
|
|
"host-a": KEY1, "host-b": KEY2,
|
|
}),
|
|
)
|
|
for script in scripts:
|
|
result = subprocess.run(
|
|
["/bin/sh", "-n", "-c", script],
|
|
check=False, capture_output=True, text=True,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert "br0" not in script
|
|
assert "PRIVATE KEY" not in script
|
|
observe = scripts[2]
|
|
assert "__VC_FABRIC_ADMIN_STATE__" in observe
|
|
assert "__VC_FABRIC_SYSCTL__ip_forward" in observe
|
|
assert "__VC_FABRIC_SYSCTL__rp_filter_default" in observe
|
|
assert "__VC_FABRIC_RPF__wg-fabric0" in observe
|
|
assert "__VC_FABRIC_VPC_STATE__" in observe
|
|
assert "list set inet virt_cloud_wg_fabric vpc_tenant_ips" in observe
|
|
assert 'table!="main" && table!="local"' in observe
|
|
apply = scripts[-1]
|
|
assert KEY2 in apply and KEY1 not in apply
|
|
assert 'KEY="$BASE/wg-fabric0.key"' in apply
|
|
assert "wg-fabric0" in apply
|
|
assert "address_conflict" in apply
|
|
assert "existing_route" in apply
|
|
assert "route_overlaps_target" in apply
|
|
assert "supernet" not in apply # shell check is numeric and bounded
|
|
assert "virt-cloud-wg-fabric.nft" in apply
|
|
assert "include \"/etc/nftables.d/virt-cloud-wg-fabric.nft\"" in apply
|
|
assert "trap rollback EXIT HUP INT TERM" in apply
|
|
assert '"$WG" showconf' in apply
|
|
assert "route save table all" in apply
|
|
assert 'chmod 700 "$BASE" /usr/local/libexec' not in apply
|
|
assert "virt-cloud-wg-fabric-v1" in apply
|
|
assert "virt_cloud_wg_fabric" in apply
|
|
assert 'iifname "wg-fabric0"' in apply
|
|
assert 'oifname "wg-fabric0"' in apply
|
|
assert 'ip protocol icmp accept' in apply
|
|
assert "route get" in apply
|
|
assert "virt-cloud-wg-fabric-reconcile.timer" in apply
|
|
assert "Restart=on-failure" not in apply
|
|
assert f"fabric-plan-digest={current.digest}" in apply
|
|
assert "NetworkManager/system-connections" in apply
|
|
assert "nmcli" in apply
|
|
assert "flock" in apply
|
|
assert "vpc_state_load" in apply
|
|
assert "vpc_state_allowed_for_peer" in apply
|
|
assert "vpc_state_refresh_nft" in apply
|
|
assert "vpc_state_refresh_nft" in _BOOT_HELPER
|
|
assert 'vpc_state != "invalid"' in observe
|
|
assert 'vpc[prefix "|" device "|" table "|" via "|" metric]' in observe
|
|
assert "key|wg.conf|address.save|route.save|nft.table" in _BOOT_HELPER
|
|
assert 'journal_file_mode" = 600' in _BOOT_HELPER
|
|
ifup = _apply_script(current, current.nodes[0], {
|
|
"host-a": KEY1, "host-b": KEY2,
|
|
}, "ifupdown", "/usr/bin/ifup")
|
|
result = subprocess.run(
|
|
["/bin/sh", "-n", "-c", ifup],
|
|
check=False, capture_output=True, text=True,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert "/etc/network/interfaces.d/virt-cloud-wg-fabric0" in ifup
|
|
assert "ifup" in ifup and "ifdown" in ifup
|
|
assert "ExecStart=/usr/local/libexec/virt-cloud-wg-fabric --ifupdown-service" in ifup
|
|
# The Debian 13 contract must also work with classic ifupdown; its
|
|
# ifquery implementation does not provide ifupdown2's --check flag.
|
|
assert "ifquery\" --check" not in ifup
|
|
assert "VC_FABRIC_LOCK_HELD=1" in ifup
|
|
assert "VC_FABRIC_ALLOW_TRANSITION=1" in ifup
|
|
assert "br0" not in ifup
|
|
with pytest.raises(FabricError):
|
|
_apply_script(current, current.nodes[0], {
|
|
"host-a": KEY1, "host-b": KEY2,
|
|
}, "ifupdown", "/tmp/ifup")
|
|
|
|
|
|
class FakeManager:
|
|
ssh_binary = "ssh"
|
|
|
|
def __init__(self, public_key=KEY1, extra_address=False, self_route=False,
|
|
config_digest=None, ping_ok=True, endpoint_device="eth0"):
|
|
self.public_key = public_key
|
|
self.extra_address = extra_address
|
|
self.self_route = self_route
|
|
self.config_digest = config_digest
|
|
self.ping_ok = ping_ok
|
|
self.endpoint_device = endpoint_device
|
|
self.scripts = []
|
|
self.reprobe_calls = 0
|
|
self.admin_state = "up"
|
|
self.ip_forward = "1"
|
|
self.rp_filter = "2"
|
|
self.rp_filter_default = "2"
|
|
self.rp_filter_iface = "2"
|
|
self.profile = {
|
|
"ssh_config_file": "/tmp/fake-ssh-config",
|
|
"ssh_target": "vp-managed",
|
|
"key_locked": False,
|
|
"capabilities": {
|
|
"vpc_ready": True,
|
|
"wireguard": {
|
|
"interface": FABRIC_INTERFACE,
|
|
"deployment_mode": "networkmanager_nmcli",
|
|
"ready": True,
|
|
},
|
|
"network": {
|
|
"network_manager": "networkmanager",
|
|
"network_manager_supported": True,
|
|
"ip_probe_valid": True,
|
|
},
|
|
},
|
|
}
|
|
|
|
def current(self):
|
|
return self.profile
|
|
|
|
def reprobe_current(self):
|
|
self.reprobe_calls += 1
|
|
return self.profile["capabilities"]
|
|
|
|
def _exec(self, argv, *, timeout, input_text=None):
|
|
assert argv[-1] == "/bin/sh -s"
|
|
assert timeout == 35
|
|
self.scripts.append(input_text)
|
|
if input_text.startswith("#!/bin/sh\nset -eu") and "__VC_FABRIC_PUBLIC_KEY__=" in input_text:
|
|
output = f"__VC_FABRIC_PUBLIC_KEY__={self.public_key}\n"
|
|
else:
|
|
now = int(time.time())
|
|
peer = KEY2 if self.public_key == KEY1 else KEY1
|
|
address = "100.64.1.1" if self.public_key == KEY1 else "100.64.1.2"
|
|
target = "100.64.1.2" if self.public_key == KEY1 else "100.64.1.1"
|
|
endpoint = "203.0.113.2:51820" if self.public_key == KEY1 else "203.0.113.1:51820"
|
|
extra_address = (
|
|
"__VC_FABRIC_ADDRESS__=100.64.1.99\n"
|
|
if self.extra_address else ""
|
|
)
|
|
self_route = (
|
|
f"__VC_FABRIC_ROUTE__={address}|local|{FABRIC_INTERFACE}\n"
|
|
if self.self_route else ""
|
|
)
|
|
output = (
|
|
f"__VC_FABRIC_OWNER__={FABRIC_OWNER_MARKER}\n"
|
|
"__VC_FABRIC_CONFIG_FABRIC__=hk-fabric\n"
|
|
"__VC_FABRIC_CONFIG_REVISION__=1\n"
|
|
f"__VC_FABRIC_CONFIG_DIGEST__={self.config_digest or plan().digest}\n"
|
|
"__VC_FABRIC_CONFIG_NETWORK__=100.64.1.0/24\n"
|
|
f"__VC_FABRIC_CONFIG_OVERLAY__={address}\n"
|
|
"__VC_FABRIC_CONFIG_OK__=yes\n"
|
|
f"__VC_FABRIC_CONFIG_PEER__={peer}|{endpoint}|{target}/32\n"
|
|
f"__VC_FABRIC_CONFIG_ROUTE__={target}/32\n"
|
|
f"__VC_FABRIC_PUBLIC_KEY__={self.public_key}\n"
|
|
"__VC_FABRIC_INTERFACE__=wireguard\n"
|
|
"__VC_FABRIC_LISTEN__=51820\n"
|
|
f"__VC_FABRIC_ADDRESS__={address}\n"
|
|
f"{extra_address}"
|
|
f"__VC_FABRIC_PEER__={peer}|{endpoint}|{target}/32\n"
|
|
f"__VC_FABRIC_HANDSHAKE__={peer}|{now}\n"
|
|
f"{self_route}"
|
|
f"__VC_FABRIC_ROUTE__={target}/32|main|{FABRIC_INTERFACE}\n"
|
|
f"__VC_FABRIC_PATH__={target}|{FABRIC_INTERFACE}\n"
|
|
f"__VC_FABRIC_ENDPOINT_PATH__={endpoint.split(':', 1)[0]}|{self.endpoint_device}\n"
|
|
f"__VC_FABRIC_PING__={target}|{'ok' if self.ping_ok else 'failed'}\n"
|
|
f"__VC_FABRIC_ROUTE_OVERLAP__={target}|clean\n"
|
|
"__VC_FABRIC_MANAGER_MODE__=networkmanager_nmcli\n"
|
|
"__VC_FABRIC_MANAGER_OWNER__=owned\n"
|
|
"__VC_FABRIC_MANAGER_ACTIVE__=active\n"
|
|
f"__VC_FABRIC_ADMIN_STATE__={self.admin_state}\n"
|
|
f"__VC_FABRIC_SYSCTL__ip_forward={self.ip_forward}\n"
|
|
f"__VC_FABRIC_SYSCTL__rp_filter={self.rp_filter}\n"
|
|
f"__VC_FABRIC_SYSCTL__rp_filter_default={self.rp_filter_default}\n"
|
|
f"__VC_FABRIC_RPF__wg-fabric0={self.rp_filter_iface}\n"
|
|
"__VC_FABRIC_ENABLED__=enabled\n"
|
|
"__VC_FABRIC_ACTIVE__=active\n"
|
|
"__VC_FABRIC_TIMER_ENABLED__=enabled\n"
|
|
"__VC_FABRIC_TIMER_ACTIVE__=active\n"
|
|
"__VC_FABRIC_NFT_GUARD__=owned\n"
|
|
"__VC_FABRIC_NFT_PERSISTENCE__=owned\n"
|
|
"__VC_FABRIC_VPC_NFT_STATE__=owned\n"
|
|
"__VC_FABRIC_KEY_SECURE__=yes\n"
|
|
"__VC_FABRIC_CONFIG_SECURE__=yes\n"
|
|
"__VC_FABRIC_HELPER__=owned\n"
|
|
"__VC_FABRIC_UNIT__=owned\n"
|
|
"__VC_FABRIC_REPAIR__=owned\n"
|
|
"__VC_FABRIC_TIMER__=owned\n"
|
|
)
|
|
return subprocess.CompletedProcess(argv, 0, output, "")
|
|
|
|
|
|
def test_adapter_readback_requires_exact_peer_route_and_persistence():
|
|
current = plan()
|
|
manager = FakeManager(KEY1)
|
|
adapter = FabricHostAdapter(manager)
|
|
assert adapter.identity() == KEY1
|
|
assert adapter.observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)["transport_verified"] is True
|
|
|
|
|
|
def test_adapter_rejects_an_unexpected_overlay_address():
|
|
current = plan()
|
|
manager = FakeManager(KEY1, extra_address=True)
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
assert observed["state"] == "degraded"
|
|
|
|
|
|
def test_adapter_ignores_the_kernel_connected_route_for_its_own_address():
|
|
current = plan()
|
|
manager = FakeManager(KEY1, self_route=True)
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is True
|
|
|
|
|
|
def test_adapter_requires_admin_up_and_fabric_sysctl_readback():
|
|
current = plan()
|
|
manager = FakeManager(KEY1)
|
|
manager.admin_state = "down"
|
|
assert FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)["config_ok"] is False
|
|
|
|
manager.admin_state = "up"
|
|
manager.rp_filter_iface = "1"
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
assert observed["observed"]["admin_state"] == "up"
|
|
assert observed["observed"]["rp_filters"][FABRIC_INTERFACE] == "1"
|
|
|
|
|
|
def test_adapter_rejects_stale_config_digest_and_failed_probe():
|
|
current = plan()
|
|
stale = FakeManager(KEY1, config_digest="0" * 64)
|
|
observed = FabricHostAdapter(stale).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
|
|
|
|
def test_adapter_rejects_endpoint_egress_drift_to_fabric_or_missing_device():
|
|
current = plan()
|
|
for device in (FABRIC_INTERFACE, ""):
|
|
observed = FabricHostAdapter(FakeManager(KEY1, endpoint_device=device)).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
failed_ping = FakeManager(KEY1, ping_ok=False)
|
|
observed = FabricHostAdapter(failed_ping).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
|
|
|
|
class MemoryStore:
|
|
def __init__(self):
|
|
self.values = {}
|
|
|
|
def get(self, kind, key):
|
|
return self.values.get((kind, key))
|
|
|
|
def set(self, kind, key, value):
|
|
self.values[(kind, key)] = value
|
|
|
|
def list(self, kind):
|
|
return {key: value for (row_kind, key), value in self.values.items()
|
|
if row_kind == kind}
|
|
|
|
|
|
class FakeAdapter:
|
|
failures = set()
|
|
|
|
def __init__(self, manager, profile=None):
|
|
self.manager = manager
|
|
|
|
def identity(self):
|
|
return self.manager.public_key
|
|
|
|
def apply(self, *_args):
|
|
if self.manager.host_id in self.failures:
|
|
raise FabricError("offline", 502, "fabric_remote_command_failed")
|
|
|
|
def observe(self, current, host, keys):
|
|
if self.manager.host_id in self.failures:
|
|
raise FabricError("offline", 502, "fabric_remote_command_failed")
|
|
return {
|
|
"state": "active", "transport_verified": True,
|
|
"observed": {"interface": FABRIC_INTERFACE},
|
|
}
|
|
|
|
|
|
class Registry:
|
|
def __init__(self, components):
|
|
self.components = components
|
|
|
|
async def get(self, host_id):
|
|
return self.components[host_id]
|
|
|
|
|
|
def test_coordinator_keeps_successful_hosts_and_blocks_next_stage(monkeypatch):
|
|
store = MemoryStore()
|
|
managers = {}
|
|
components = {}
|
|
for host_id, key in (("host-a", KEY1), ("host-b", KEY2)):
|
|
manager = FakeManager(key)
|
|
manager.host_id = host_id
|
|
manager.profile["host_id"] = host_id
|
|
manager.profile["region"] = "hk"
|
|
manager.profile["capabilities"] = {
|
|
"vpc_ready": True,
|
|
"wireguard": {
|
|
"interface": FABRIC_INTERFACE,
|
|
"deployment_mode": "networkmanager_nmcli",
|
|
"ready": True,
|
|
},
|
|
"network": {
|
|
"network_manager": "networkmanager",
|
|
"network_manager_supported": True,
|
|
"ip_probe_valid": True,
|
|
},
|
|
}
|
|
managers[host_id] = manager
|
|
components[host_id] = SimpleNamespace(manager=manager)
|
|
monkeypatch.setattr("virsh_panel.fabric.FabricHostAdapter", FakeAdapter)
|
|
FakeAdapter.failures = {"host-b"}
|
|
result = asyncio.run(FabricCoordinator(store, Registry(components)).apply({
|
|
"fabric_id": "hk-fabric", "region": "hk",
|
|
"overlay_cidr": "100.64.1.0/24", "revision": 1,
|
|
"nodes": [
|
|
{"host_id": "host-a", "overlay_ip": "100.64.1.1",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
{"host_id": "host-b", "overlay_ip": "100.64.1.2",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
],
|
|
}))
|
|
assert result["state"] == "uncertain"
|
|
assert result["next_stage_allowed"] is False
|
|
states = {node["host_id"]: node["state"] for node in result["nodes"]}
|
|
assert states == {"host-a": "active", "host-b": "uncertain"}
|
|
assert managers["host-a"].reprobe_calls == 1
|
|
assert managers["host-b"].reprobe_calls == 1
|
|
serialized = "\n".join(store.values.values())
|
|
assert "PRIVATE KEY" not in serialized
|
|
assert KEY1 in serialized and KEY2 in serialized
|
|
FakeAdapter.failures = set()
|
|
|
|
|
|
def test_recover_does_not_replay_an_interrupted_write():
|
|
store = MemoryStore()
|
|
desired = {"fabric_id": "hk-fabric", "state": "applying",
|
|
"next_stage_allowed": False,
|
|
"revision": 1, "plan_digest": "x",
|
|
"nodes": [{"host_id": "host-a", "state": "active",
|
|
"transport_verified": True}]}
|
|
store.set("fabric_desired", "hk-fabric", json.dumps(desired))
|
|
coordinator = FabricCoordinator(store, Registry({}))
|
|
coordinator.recover()
|
|
recovered = json.loads(store.get("fabric_desired", "hk-fabric"))
|
|
assert recovered["state"] == "uncertain"
|
|
assert recovered["next_stage_allowed"] is False
|
|
assert recovered["nodes"][0]["transport_verified"] is False
|
|
|
|
desired["state"] = "active"
|
|
desired["next_stage_allowed"] = True
|
|
desired["nodes"][0]["state"] = "active"
|
|
desired["nodes"][0]["transport_verified"] = True
|
|
store.set("fabric_desired", "hk-fabric", json.dumps(desired))
|
|
coordinator.recover()
|
|
restarted = json.loads(store.get("fabric_desired", "hk-fabric"))
|
|
assert restarted["state"] == "uncertain"
|
|
assert restarted["next_stage_allowed"] is False
|
|
|
|
|
|
def test_public_status_does_not_echo_nested_secrets_or_commands():
|
|
public = FabricCoordinator._public({
|
|
"fabric_id": "f", "state": "uncertain",
|
|
"listen_port": 51820,
|
|
"private_key": "secret",
|
|
"command": "rm -rf /",
|
|
"nodes": [{
|
|
"host_id": "host-a", "public_key": KEY1,
|
|
"private_key": "secret",
|
|
"observed": {
|
|
"interface": FABRIC_INTERFACE,
|
|
"command": "wg show",
|
|
"peers": [(KEY2, "203.0.113.2:51820", "100.64.1.2/32")],
|
|
"handshakes": {KEY2: 123},
|
|
},
|
|
}],
|
|
})
|
|
serialized = json.dumps(public)
|
|
assert public["listen_port"] == 51820
|
|
assert "private_key" not in serialized
|
|
assert "command" not in serialized
|
|
assert KEY1 in serialized and KEY2 in serialized
|
|
|
|
|
|
def test_reconcile_rejects_a_tampered_plan_digest():
|
|
store = MemoryStore()
|
|
current = plan()
|
|
store.set("fabric_desired", current.fabric_id, json.dumps({
|
|
"fabric_id": current.fabric_id,
|
|
"region": current.region,
|
|
"overlay_cidr": current.overlay_cidr,
|
|
"revision": current.revision,
|
|
"listen_port": current.listen_port,
|
|
"plan_digest": "0" * 64,
|
|
"public_keys": {node.host_id: key for node, key in zip(
|
|
current.nodes, (KEY1, KEY2))},
|
|
"nodes": [
|
|
{"host_id": node.host_id, "overlay_ip": node.overlay_ip,
|
|
"endpoint": node.endpoint, "listen_port": node.listen_port}
|
|
for node in current.nodes
|
|
],
|
|
}))
|
|
coordinator = FabricCoordinator(store, Registry({}))
|
|
with pytest.raises(FabricError) as error:
|
|
asyncio.run(coordinator.reconcile(current.fabric_id))
|
|
assert error.value.error_code == "fabric_record_corrupt"
|
|
|
|
|
|
def test_new_fabric_cannot_reuse_a_host_or_overlay_catalog_entry():
|
|
store = MemoryStore()
|
|
store.set("fabric_desired", "existing", json.dumps({
|
|
"fabric_id": "existing",
|
|
"overlay_cidr": "100.64.2.0/24",
|
|
"nodes": [{"host_id": "host-a"}, {"host_id": "host-c"}],
|
|
}))
|
|
coordinator = FabricCoordinator(store, Registry({}))
|
|
value = {
|
|
"fabric_id": "new-fabric", "region": "hk",
|
|
"overlay_cidr": "100.64.2.0/24", "revision": 1,
|
|
"nodes": [
|
|
{"host_id": "host-a", "overlay_ip": "100.64.2.10",
|
|
"endpoint": "203.0.113.1:51820"},
|
|
{"host_id": "host-b", "overlay_ip": "100.64.2.11",
|
|
"endpoint": "203.0.113.2:51820"},
|
|
],
|
|
}
|
|
with pytest.raises(FabricError) as error:
|
|
asyncio.run(coordinator.apply(value))
|
|
assert error.value.error_code == "fabric_host_interface_conflict"
|
|
|
|
|
|
class TenantReadbackManager(FakeManager):
|
|
"""Synthetic readback with panel-owned VPC tenant state."""
|
|
|
|
def __init__(self, *args, tenant_ip="10.10.0.2/32", **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.tenant_ip = tenant_ip
|
|
|
|
def _exec(self, argv, *, timeout, input_text=None):
|
|
result = super()._exec(argv, timeout=timeout, input_text=input_text)
|
|
if input_text.startswith("#!/bin/sh\nset -eu"):
|
|
return result
|
|
peer = KEY2 if self.public_key == KEY1 else KEY1
|
|
target = "100.64.1.2" if self.public_key == KEY1 else "100.64.1.1"
|
|
endpoint = "203.0.113.2:51820" if self.public_key == KEY1 else "203.0.113.1:51820"
|
|
marker = f"__VC_FABRIC_PEER__={peer}|{endpoint}|{target}/32"
|
|
output = result.stdout.replace(marker, f"{marker},{self.tenant_ip}")
|
|
output += (
|
|
"__VC_FABRIC_VPC_STATE__=owned\n"
|
|
f"__VC_FABRIC_VPC_TENANT__={peer}|{self.tenant_ip}\n"
|
|
"__VC_FABRIC_VPC_NFT_STATE__=owned\n"
|
|
f"__VC_FABRIC_VPC_NFT_IP__={self.tenant_ip}\n"
|
|
)
|
|
return result.__class__(result.args, result.returncode, output, result.stderr)
|
|
|
|
|
|
def test_fabric_merges_and_reads_panel_owned_vpc_allowed_ips():
|
|
current = plan()
|
|
observed = FabricHostAdapter(TenantReadbackManager(KEY1)).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is True
|
|
assert observed["observed"]["peer_allowed_exact"] is True
|
|
assert f"{KEY2}|10.10.0.2/32" in observed["observed"]["vpc_tenants"]
|
|
assert all("10001" not in route for route in observed["observed"]["routes"])
|
|
|
|
|
|
def test_fabric_rejects_unbacked_vpc_allowed_ip():
|
|
current = plan()
|
|
manager = FakeManager(KEY1)
|
|
original_exec = manager._exec
|
|
|
|
def unbacked(argv, *, timeout, input_text=None):
|
|
result = original_exec(argv, timeout=timeout, input_text=input_text)
|
|
if input_text.startswith("#!/bin/sh\nset -eu"):
|
|
return result
|
|
peer = KEY2
|
|
marker = f"__VC_FABRIC_PEER__={peer}|203.0.113.2:51820|100.64.1.2/32"
|
|
output = result.stdout.replace(marker, f"{marker},10.10.0.2/32")
|
|
return result.__class__(result.args, result.returncode, output, result.stderr)
|
|
|
|
manager._exec = unbacked
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
assert observed["observed"]["peer_allowed_exact"] is False
|
|
|
|
|
|
def test_fabric_rejects_non_rfc1918_vpc_tenant_prefix():
|
|
current = plan()
|
|
observed = FabricHostAdapter(
|
|
TenantReadbackManager(KEY1, tenant_ip="169.254.1.2/32")
|
|
).observe(current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2})
|
|
assert observed["config_ok"] is False
|
|
assert observed["observed"]["peer_allowed_exact"] is False
|
|
|
|
|
|
def test_fabric_rejects_stale_vpc_nft_tenant_set():
|
|
current = plan()
|
|
manager = TenantReadbackManager(KEY1)
|
|
original_exec = manager._exec
|
|
|
|
def stale_nft(argv, *, timeout, input_text=None):
|
|
result = original_exec(argv, timeout=timeout, input_text=input_text)
|
|
if input_text.startswith("#!/bin/sh\nset -eu"):
|
|
return result
|
|
output = result.stdout.replace(
|
|
"__VC_FABRIC_VPC_NFT_IP__=10.10.0.2/32\n", "")
|
|
return result.__class__(result.args, result.returncode, output, result.stderr)
|
|
|
|
manager._exec = stale_nft
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
assert observed["observed"]["vpc_nft_exact"] is False
|
|
|
|
|
|
def test_fabric_does_not_hide_foreign_custom_route():
|
|
current = plan()
|
|
manager = FakeManager(KEY1)
|
|
original_exec = manager._exec
|
|
|
|
def foreign_route(argv, *, timeout, input_text=None):
|
|
result = original_exec(argv, timeout=timeout, input_text=input_text)
|
|
if input_text.startswith("#!/bin/sh\nset -eu"):
|
|
return result
|
|
output = result.stdout + (
|
|
f"__VC_FABRIC_ROUTE__=10.10.0.2/32|10001|{FABRIC_INTERFACE}\n"
|
|
)
|
|
return result.__class__(result.args, result.returncode, output, result.stderr)
|
|
|
|
manager._exec = foreign_route
|
|
observed = FabricHostAdapter(manager).observe(
|
|
current, current.nodes[0], {"host-a": KEY1, "host-b": KEY2}
|
|
)
|
|
assert observed["config_ok"] is False
|
|
assert "10.10.0.2/32|10001|wg-fabric0" in observed["observed"]["routes"]
|