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.
252 lines
9.4 KiB
Python
252 lines
9.4 KiB
Python
"""Pure VPC private data-plane contract tests.
|
|
|
|
These tests render the host transaction only; real nft/ip/wg and libvirt
|
|
acceptance remains a host-matrix test because this checkout has no network
|
|
namespace or libvirt daemon.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from dataclasses import replace
|
|
|
|
from virsh_panel.vpc import VPC_SCHEMA_VERSION
|
|
from virsh_panel.vpc_dataplane import (
|
|
_markers,
|
|
parse_vpc_dataplane_plan,
|
|
patch_private_interface_xml,
|
|
render_apply_script,
|
|
render_observe_script,
|
|
render_remove_script,
|
|
render_vpc_nft_rules,
|
|
)
|
|
|
|
|
|
WG_KEY = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
|
|
WG_KEY_2 = "AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI="
|
|
|
|
|
|
def _plan():
|
|
return parse_vpc_dataplane_plan({
|
|
"schema_version": VPC_SCHEMA_VERSION,
|
|
"vpc_id": "vpc-hk-dp",
|
|
"region": "hk",
|
|
"fabric_id": "fabric-hk",
|
|
"fabric_revision": 3,
|
|
"cidr": "10.42.0.0/24",
|
|
"desired_revision": 1,
|
|
"operation_id": "vpc-op-dp-0001",
|
|
"subnets": [{"subnet_id": "subnet-a", "cidr": "10.42.0.0/25"}],
|
|
"hosts": [
|
|
{"host_id": "host-a", "members": [{
|
|
"member_id": "member-a",
|
|
"instance_uuid": "11111111-1111-4111-8111-111111111111",
|
|
"private_ip_cidr": "10.42.0.10/32",
|
|
"subnet_id": "subnet-a",
|
|
}]},
|
|
{"host_id": "host-b", "members": [{
|
|
"member_id": "member-b",
|
|
"instance_uuid": "22222222-2222-4222-8222-222222222222",
|
|
"private_ip_cidr": "10.42.0.11/32",
|
|
"subnet_id": "subnet-a",
|
|
}]},
|
|
],
|
|
"routes": [{
|
|
"route_id": "service-a", "destination_cidr": "10.42.0.100/32",
|
|
"next_hop_member_id": "member-b", "metric": 20,
|
|
}],
|
|
"acls": [{
|
|
"acl_id": "allow-https", "direction": "ingress", "action": "allow",
|
|
"protocol": "tcp", "source_cidr": "10.42.0.0/24",
|
|
"destination_cidr": "10.42.0.10/32", "port_from": 443,
|
|
"port_to": 443, "priority": 10,
|
|
}],
|
|
"fabric_peers": [{
|
|
"host_id": "host-b", "public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.42.0.11/32", "10.42.0.100/32"],
|
|
}],
|
|
}, "host-a")
|
|
|
|
|
|
def test_rendered_data_plane_has_exact_routes_proxy_arp_and_atomic_nft_batch():
|
|
plan = _plan()
|
|
apply = render_apply_script(plan)
|
|
nft = render_vpc_nft_rules(plan)
|
|
assert "10.42.0.11/32|wg-fabric0" in apply
|
|
assert "10.42.0.100/32|wg-fabric0" in apply
|
|
assert 'route replace "$destination" table "$table" via "$via" dev "$device" onlink' in apply
|
|
assert 'neigh replace proxy "$proxy_ip"' in apply
|
|
assert "table arp" in nft
|
|
assert "arp-proxy-output" in nft
|
|
assert 'proxy_arp = 0' in apply
|
|
assert 'proxy_arp_pvlan = 1' in apply
|
|
assert 'NFT_BATCH="$TXN/apply.nft"' in apply
|
|
assert '"$NFT" -f "$NFT_BATCH"' in apply
|
|
assert '"$NFT" delete table inet "$TABLE"' not in apply
|
|
assert "anti-spoof-public-to-bridge" in nft
|
|
assert "anti-spoof-public-to-fabric" in nft
|
|
assert "anti-spoof-bridge-to-foreign" in nft
|
|
assert "anti-spoof-fabric-to-foreign" in nft
|
|
assert 'oifname "wg-fabric0" ip daddr @vpc_dest_ips drop' in nft
|
|
|
|
|
|
def test_rendered_scripts_are_posix_shell():
|
|
plan = _plan()
|
|
for script in (render_apply_script(plan), render_observe_script(plan)):
|
|
result = subprocess.run(
|
|
["/bin/sh", "-n"], input=script, text=True,
|
|
capture_output=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
|
def test_remove_script_checks_ownership_before_destructive_cleanup():
|
|
plan = _plan()
|
|
empty_host = replace(
|
|
plan,
|
|
members=tuple(member for member in plan.members
|
|
if member.host_id != plan.host_id),
|
|
)
|
|
for mode in ("networkmanager_nmcli", "ifupdown"):
|
|
script = render_remove_script(empty_host, deployment_mode=mode)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-n"], input=script, text=True,
|
|
capture_output=True, check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert 'connection.stable-id' in script
|
|
assert 'nm_empty ipv4.routes' in script
|
|
assert "virt-cloud-vpc-dataplane-v1 vpc=" in script
|
|
assert 'nft_table_count' in script
|
|
assert '[ "$name" = "$TABLE" ]' in script
|
|
assert 'BRIDGE_PRESENT=no' in script
|
|
assert 'if "$IP" link show dev "$BRIDGE"' in script
|
|
assert 'bridge_routes6' in script
|
|
assert 'proxy_left' in script
|
|
for phase in (
|
|
"routes_removed", "manager_removed", "bridge_removed",
|
|
"nft_removed", "files_removed", "wg_merged", "committed",
|
|
):
|
|
assert f"set_phase {phase}" in script
|
|
assert '[ "$RESUME" = yes ]' in script
|
|
assert 'committed)' in script
|
|
assert 'files_removed|wg_merged)' in script
|
|
|
|
|
|
def test_proxy_markers_are_bounded_and_typed():
|
|
parsed = _markers(
|
|
"__VC_VPC_PROXY_ARP__=0\n"
|
|
"__VC_VPC_PROXY_ARP_PVLAN__=1\n"
|
|
"__VC_VPC_PROXY_STATE__=owned\n"
|
|
"__VC_VPC_PROXY_IP__=10.42.0.11\n"
|
|
)
|
|
assert parsed["proxy_arp"] == "0"
|
|
assert parsed["proxy_arp_pvlan"] == "1"
|
|
assert parsed["proxy_state"] == "owned"
|
|
assert parsed["proxy_ips"] == ["10.42.0.11"]
|
|
|
|
|
|
def test_peer_allowed_ips_are_closed_over_vpc_or_trusted_fabric_set():
|
|
plan = _plan()
|
|
payload = plan.vpc.canonical()
|
|
payload["fabric_peers"] = [{
|
|
"host_id": "host-b", "public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.42.0.11/32", "10.42.0.100/32",
|
|
"10.42.0.200/32"],
|
|
}]
|
|
try:
|
|
parse_vpc_dataplane_plan(payload, "host-a")
|
|
except Exception as exc:
|
|
assert getattr(exc, "error_code", None) == "vpc_tenant_allowed_ip_unowned"
|
|
else:
|
|
raise AssertionError("unowned peer AllowedIP was accepted")
|
|
|
|
parsed = parse_vpc_dataplane_plan(
|
|
payload, "host-a",
|
|
trusted_tenant_allowed_ips={
|
|
"host-b": ["10.42.0.11/32", "10.42.0.100/32",
|
|
"10.42.0.200/32"],
|
|
})
|
|
assert parsed.peers[0].tenant_allowed_ips[-1] == "10.42.0.200/32"
|
|
|
|
|
|
def test_observe_can_require_the_complete_authoritative_fabric_peer_set():
|
|
plan = _plan()
|
|
strict = parse_vpc_dataplane_plan(
|
|
plan.vpc.canonical(), "host-a",
|
|
fabric_peers=[{
|
|
"host_id": "host-b", "public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.42.0.11/32", "10.42.0.100/32"],
|
|
}],
|
|
trusted_tenant_allowed_ips={
|
|
"host-b": ["10.42.0.11/32", "10.42.0.100/32"],
|
|
},
|
|
fabric_peer_keys=[WG_KEY, WG_KEY_2],
|
|
)
|
|
assert strict.fabric_peer_keys == frozenset({WG_KEY, WG_KEY_2})
|
|
observe = render_observe_script(strict)
|
|
assert "__VC_VPC_WG_PEER_SET__" in observe
|
|
assert "live_fabric_peer_keys" in observe
|
|
try:
|
|
parse_vpc_dataplane_plan(
|
|
plan.vpc.canonical(), "host-a",
|
|
fabric_peers=[{
|
|
"host_id": "host-b", "public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.42.0.11/32", "10.42.0.100/32"],
|
|
}],
|
|
trusted_tenant_allowed_ips={
|
|
"host-b": ["10.42.0.11/32", "10.42.0.100/32"],
|
|
},
|
|
fabric_peer_keys=[WG_KEY_2],
|
|
)
|
|
except Exception as exc:
|
|
assert getattr(exc, "error_code", None) == "vpc_fabric_peer_set_invalid"
|
|
else:
|
|
raise AssertionError("VPC peer outside authoritative Fabric set was accepted")
|
|
|
|
|
|
def test_private_xml_patch_keeps_public_role_and_leaves_private_filter_free():
|
|
plan = _plan()
|
|
member = plan.members[0]
|
|
xml = """<domain><metadata/><devices>
|
|
<interface type='bridge'><mac address='52:54:00:00:00:01'/>
|
|
<alias name='public0'/><source bridge='br0'/></interface>
|
|
</devices></domain>"""
|
|
patched = patch_private_interface_xml(
|
|
xml, member=member, resources=plan.resources,
|
|
vpc_id=plan.vpc.vpc_id, desired_revision=plan.desired_revision,
|
|
desired_digest=plan.desired_digest,
|
|
)
|
|
assert "alias name=\"public0\"" in patched
|
|
assert 'alias name="private0"' in patched
|
|
assert f'bridge="{plan.resources.bridge}"' in patched
|
|
assert 'filter="clean-traffic"' not in patched
|
|
assert f'private-network-digest="{plan.desired_digest}"' in patched
|
|
|
|
|
|
def test_private_xml_patch_promotes_legacy_single_nic_to_public_role():
|
|
plan = _plan()
|
|
member = plan.members[0]
|
|
legacy = """<domain><devices>
|
|
<interface type='bridge'><mac address='52:54:00:00:00:01'/>
|
|
<source bridge='br0'/></interface>
|
|
</devices></domain>"""
|
|
patched = patch_private_interface_xml(
|
|
legacy, member=member, resources=plan.resources,
|
|
vpc_id=plan.vpc.vpc_id, desired_revision=plan.desired_revision,
|
|
desired_digest=plan.desired_digest,
|
|
)
|
|
assert 'alias name="public0"' in patched
|
|
assert 'alias name="private0"' in patched
|
|
# A second reconciliation must still identify the original NIC as public.
|
|
reparsed = patch_private_interface_xml(
|
|
patched, member=member, resources=plan.resources,
|
|
vpc_id=plan.vpc.vpc_id, desired_revision=plan.desired_revision,
|
|
desired_digest=plan.desired_digest,
|
|
)
|
|
assert reparsed.count('alias name="public0"') == 1
|
|
assert reparsed.count('alias name="private0"') == 1
|