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.
1513 lines
52 KiB
Python
1513 lines
52 KiB
Python
"""Native nftables ownership contracts for the VPC host transaction."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import shlex
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.vpc import VPC_SCHEMA_VERSION
|
|
from virsh_panel.fabric import _NMCLI_FILENAME_RESOLVER_SH, _nft_rules_text
|
|
from virsh_panel.vpc_dataplane import (
|
|
FIREWALL_MODE_NATIVE,
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
VPC_NATIVE_TRANSACTION_GUARD_SH,
|
|
VpcDataplaneError,
|
|
VpcPrivateHostAdapter,
|
|
parse_vpc_dataplane_plan,
|
|
render_apply_script,
|
|
render_observe_script,
|
|
render_remove_script,
|
|
)
|
|
|
|
|
|
WG_KEY = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
|
|
FABRIC_DIGEST = "cd" * 32
|
|
|
|
|
|
def _fabric_guard_functions(script: str) -> str:
|
|
start = script.index("vc_fabric_nft_strict_shape() {")
|
|
end = script.index('[ -n "$IP" ]', start)
|
|
return script[start:end]
|
|
|
|
|
|
def _vpc_state_loader_functions(script: str) -> str:
|
|
start = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
end = script.index("vpc_state_load || exit 74", start)
|
|
return script[start:end]
|
|
|
|
|
|
def _plan():
|
|
return parse_vpc_dataplane_plan({
|
|
"schema_version": VPC_SCHEMA_VERSION,
|
|
"vpc_id": "vpc-native-nft",
|
|
"region": "hk",
|
|
"fabric_id": "fabric-hk",
|
|
"fabric_revision": 1,
|
|
"cidr": "10.44.0.0/24",
|
|
"desired_revision": 1,
|
|
"operation_id": "vpc-native-nft-op-0001",
|
|
"subnets": [{"subnet_id": "subnet-a", "cidr": "10.44.0.0/25"}],
|
|
"hosts": [
|
|
{"host_id": "host-a", "members": [{
|
|
"member_id": "member-a",
|
|
"instance_uuid": "11111111-1111-4111-8111-111111111111",
|
|
"private_ip_cidr": "10.44.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.44.0.11/32",
|
|
"subnet_id": "subnet-a",
|
|
}]},
|
|
],
|
|
"routes": [],
|
|
"acls": [],
|
|
"fabric_peers": [{
|
|
"host_id": "host-b",
|
|
"public_key": WG_KEY,
|
|
"overlay_ip": "100.64.0.2/32",
|
|
"tenant_allowed_ips": ["10.44.0.11/32"],
|
|
}],
|
|
}, "host-a", fabric_plan_digest=FABRIC_DIGEST)
|
|
|
|
|
|
def _empty_local_plan():
|
|
plan = _plan()
|
|
return replace(
|
|
plan,
|
|
members=tuple(member for member in plan.members
|
|
if member.host_id != plan.host_id),
|
|
)
|
|
|
|
|
|
def test_apply_refuses_a_host_without_local_members():
|
|
with pytest.raises(VpcDataplaneError) as rejected:
|
|
render_apply_script(
|
|
_empty_local_plan(), firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert rejected.value.status == 409
|
|
assert rejected.value.error_code == "vpc_apply_no_local_members"
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("transaction", ["apply", "remove"])
|
|
def test_vpc_transactions_strictly_bind_fabric_digest_and_tenant_set(
|
|
tmp_path, transaction):
|
|
plan = _plan()
|
|
script = (
|
|
render_apply_script(
|
|
plan, deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
if transaction == "apply" else
|
|
render_remove_script(
|
|
_empty_local_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
)
|
|
guard = _fabric_guard_functions(script)
|
|
loader = _vpc_state_loader_functions(script)
|
|
wrapper_only = guard[guard.index("nft_plan_owned() {"):]
|
|
expected = tmp_path / f"{transaction}.expected"
|
|
live_table = tmp_path / f"{transaction}.table"
|
|
live_set = tmp_path / f"{transaction}.set"
|
|
txn = tmp_path / f"{transaction}.txn"
|
|
txn.mkdir()
|
|
expected.write_text("10.44.0.10/32\n10.44.0.11/32\n")
|
|
|
|
def table_with(elements, *, digest=FABRIC_DIGEST):
|
|
values = ", ".join(elements)
|
|
return _nft_rules_text(digest).replace(
|
|
" flags interval;",
|
|
" flags interval;\n"
|
|
f" elements = {{ {values} }}",
|
|
1,
|
|
)
|
|
|
|
def set_with(elements):
|
|
values = ", ".join(elements)
|
|
return (
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" set vpc_tenant_ips {\n"
|
|
" type ipv4_addr\n"
|
|
" flags interval\n"
|
|
f" elements = {{ {values} }}\n"
|
|
" }\n"
|
|
"}\n"
|
|
)
|
|
|
|
fake_nft = r'''
|
|
vc_test_nft() {
|
|
if [ "$*" = "list set inet virt_cloud_wg_fabric vpc_tenant_ips" ]; then
|
|
cat "$LIVE_SET"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
'''.strip()
|
|
|
|
def run(table_text, set_text, *, digest=FABRIC_DIGEST,
|
|
include_validator=True):
|
|
live_table.write_text(table_text)
|
|
live_set.write_text(set_text)
|
|
functions = guard if include_validator else wrapper_only
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
f"EXPECTED_DIGEST={digest}",
|
|
f"TXN={txn}",
|
|
f"LIVE_TABLE={live_table}",
|
|
f"LIVE_SET={live_set}",
|
|
f"EXPECTED={expected}",
|
|
"STAT=/usr/bin/stat",
|
|
fake_nft,
|
|
functions,
|
|
loader,
|
|
'VPC_TENANT_FILE="$EXPECTED"',
|
|
'nft_plan_owned "$(cat "$LIVE_TABLE")" "$EXPECTED_DIGEST"',
|
|
"vpc_state_nft_set_matches",
|
|
))
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
|
|
exact = ["10.44.0.10/32", "10.44.0.11/32"]
|
|
accepted = run(table_with(exact), set_with(exact))
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
|
|
# The validator itself is a required part of the generated transaction;
|
|
# a missing definition must never degrade to comment-count ownership.
|
|
assert run(
|
|
table_with(exact), set_with(exact),
|
|
include_validator=False,
|
|
).returncode != 0
|
|
assert run(table_with(exact), set_with(exact), digest="").returncode != 0
|
|
assert run(
|
|
table_with(exact, digest="ef" * 32), set_with(exact),
|
|
).returncode != 0
|
|
assert run(
|
|
table_with(exact).replace("counter drop", "counter accept", 1),
|
|
set_with(exact),
|
|
).returncode != 0
|
|
for tenant_elements in (
|
|
["10.44.0.10/32"],
|
|
[*exact, "10.44.0.12/32"],
|
|
["10.44.0.10-10.44.0.11"],
|
|
):
|
|
assert run(
|
|
table_with(exact), set_with(tenant_elements),
|
|
).returncode != 0
|
|
|
|
expected_assignment = f"EXPECTED_DIGEST={FABRIC_DIGEST}"
|
|
assert expected_assignment in script
|
|
strict_at = script.index("vc_fabric_nft_strict_shape() {")
|
|
owner_at = script.index("nft_plan_owned() {", strict_at)
|
|
loader_at = script.index('VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"')
|
|
assert script.index(expected_assignment) < strict_at < owner_at < loader_at
|
|
refresh_at = script.rindex("vpc_state_refresh_nft || exit 74")
|
|
exact_at = script.index("vpc_state_nft_set_matches || exit 74", refresh_at)
|
|
snapshot_at = script.index(
|
|
"vc_nft_table_snapshot inet virt_cloud_wg_fabric", exact_at)
|
|
final_strict = script.index(
|
|
"vc_vpc_fabric_nft_live_ready || exit 74", snapshot_at)
|
|
final_exact = script.index(
|
|
"vpc_state_nft_set_matches || exit 74", final_strict)
|
|
final_snapshot = script.index(
|
|
"vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric",
|
|
final_exact)
|
|
commit_at = script.rindex("COMMITTED=yes") if transaction == "apply" \
|
|
else script.rindex("set_phase committed")
|
|
assert refresh_at < exact_at < snapshot_at < final_strict
|
|
assert final_strict < final_exact < final_snapshot < commit_at
|
|
last_strict = script.rindex(
|
|
"vc_vpc_fabric_nft_live_ready || exit 74", 0, commit_at)
|
|
last_exact = script.index(
|
|
"vpc_state_nft_set_matches || exit 74", last_strict)
|
|
last_snapshot = script.index(
|
|
"vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric",
|
|
last_exact)
|
|
assert last_strict < last_exact < last_snapshot < commit_at
|
|
assert "connection.zone trusted" not in script
|
|
assert "firewall-cmd" not in script
|
|
|
|
|
|
def test_vpc_observe_strictly_binds_fabric_digest_semantics_and_tenant_set(
|
|
tmp_path):
|
|
script = render_observe_script(
|
|
_plan(), firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert NFT_LIVE_SNAPSHOT_SH in script
|
|
snapshot_functions = NFT_LIVE_SNAPSHOT_SH
|
|
guard_start = script.index("vc_fabric_nft_strict_shape() {")
|
|
loader_start = script.index(
|
|
'VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"', guard_start)
|
|
guard_functions = script[guard_start:loader_start]
|
|
loader_end = script.index("FABRIC_NFT_SNAPSHOT=", loader_start)
|
|
loader_functions = script[loader_start:loader_end]
|
|
observe_start = script.index("vc_vpc_fabric_observe_ready() {")
|
|
observe_end = script.index("firewall_mode_state=owned", observe_start)
|
|
observe_function = script[observe_start:observe_end]
|
|
|
|
live_table = tmp_path / "observe.table"
|
|
live_set = tmp_path / "observe.set"
|
|
expected = tmp_path / "observe.expected"
|
|
snapshot = tmp_path / "observe.snapshot"
|
|
expected.write_text("10.44.0.10/32\n10.44.0.11/32\n")
|
|
|
|
def table_with(elements, *, digest=FABRIC_DIGEST):
|
|
values = ", ".join(elements)
|
|
return _nft_rules_text(digest).replace(
|
|
" flags interval;",
|
|
" flags interval;\n"
|
|
f" elements = {{ {values} }}",
|
|
1,
|
|
)
|
|
|
|
def set_with(elements):
|
|
values = ", ".join(elements)
|
|
return (
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" set vpc_tenant_ips {\n"
|
|
" type ipv4_addr\n"
|
|
" flags interval\n"
|
|
f" elements = {{ {values} }}\n"
|
|
" }\n"
|
|
"}\n"
|
|
)
|
|
|
|
fake_nft = r'''
|
|
vc_test_nft() {
|
|
if [ "$1" = -s ] && [ "$2" = list ] && [ "$3" = table ]; then
|
|
cat "$LIVE_TABLE"
|
|
return 0
|
|
fi
|
|
if [ "$1" = list ] && [ "$2" = set ]; then
|
|
cat "$LIVE_SET"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
'''.strip()
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"CMP=/usr/bin/cmp",
|
|
"STAT=/usr/bin/stat",
|
|
f"EXPECTED_DIGEST={FABRIC_DIGEST}",
|
|
fake_nft,
|
|
snapshot_functions,
|
|
guard_functions,
|
|
loader_functions,
|
|
'VPC_TENANT_FILE="$EXPECTED"',
|
|
'FABRIC_NFT_SNAPSHOT="$SNAPSHOT"',
|
|
"vpc_state_load() { :; }",
|
|
observe_function,
|
|
"vc_vpc_fabric_observe_ready",
|
|
))
|
|
|
|
def run(table_text, set_text):
|
|
live_table.write_text(table_text)
|
|
live_set.write_text(set_text)
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, LIVE_TABLE=str(live_table),
|
|
LIVE_SET=str(live_set), EXPECTED=str(expected),
|
|
SNAPSHOT=str(snapshot)),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
exact = ["10.44.0.10/32", "10.44.0.11/32"]
|
|
accepted = run(table_with(exact), set_with(exact))
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
assert run(
|
|
table_with(exact, digest="ef" * 32), set_with(exact),
|
|
).returncode != 0
|
|
assert run(
|
|
table_with(exact).replace("counter drop", "counter accept", 1),
|
|
set_with(exact),
|
|
).returncode != 0
|
|
for tenant_elements in (
|
|
["10.44.0.10/32"],
|
|
[*exact, "10.44.0.12/32"],
|
|
["10.44.0.10-10.44.0.11"],
|
|
):
|
|
assert run(
|
|
table_with(tenant_elements), set_with(tenant_elements),
|
|
).returncode != 0
|
|
|
|
expected_assignment = f"EXPECTED_DIGEST={FABRIC_DIGEST}"
|
|
strict_at = script.index("vc_fabric_nft_strict_shape() {")
|
|
owner_at = script.index("nft_plan_owned() {", strict_at)
|
|
loader_at = script.index(
|
|
'VPC_STATE_DIR="/etc/virt-cloud/vpc-dataplane"', owner_at)
|
|
ready_at = script.index("vc_vpc_fabric_observe_ready() {", loader_at)
|
|
call_at = script.rindex("if vc_vpc_fabric_observe_ready; then")
|
|
marker_at = script.index("__VC_VPC_FABRIC_NFT__", call_at)
|
|
assert script.index(expected_assignment) < strict_at < owner_at
|
|
assert owner_at < loader_at < ready_at < call_at < marker_at
|
|
assert "VPC_STATE_OBSERVE=\"yes\"" in script
|
|
assert "trap 'vc_vpc_observe_cleanup' EXIT" in script
|
|
assert '"$VPC_TENANT_FILE.nft" "$FABRIC_NFT_SNAPSHOT"' in script
|
|
assert "vpc_state_nft_set_matches || return 1" in observe_function
|
|
assert ("vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric"
|
|
in observe_function)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_firewall_mode_is_validated_and_native_state_stays_explicit():
|
|
native = render_apply_script(_plan(), firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert "FIREWALL_MODE=native_nftables" in native
|
|
assert "firewall_mode|native_nftables" in native
|
|
assert '[ ! -e "$LEGACY_MANIFEST" ] && [ ! -L "$LEGACY_MANIFEST" ] || exit 71' in native
|
|
assert "'0|'|'1|native_nftables'" in native
|
|
assert '[ "$vpc_a" = native_nftables ] ||' in native
|
|
assert "nft_snapshot)" in native
|
|
assert "vpc_state_fail || return 1" in native
|
|
assert "firewall-cmd" not in native
|
|
native_ifup = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
assert f"\nauto {_plan().resources.bridge}\n" not in native_ifup
|
|
nft_ready = native_ifup.rindex("vc_vpc_desired_nft_ready || exit 72")
|
|
explicit_up = native_ifup.rindex(
|
|
'VC_FABRIC_APPLYING=1 "$IFUP" --force "$BRIDGE"')
|
|
assert nft_ready < explicit_up
|
|
for render, plan in (
|
|
(render_apply_script, _plan()),
|
|
(render_observe_script, _plan()),
|
|
(render_remove_script, _empty_local_plan()),
|
|
):
|
|
with pytest.raises(VpcDataplaneError) as invalid:
|
|
render(plan, firewall_mode="firewalld_nft")
|
|
assert invalid.value.error_code == "vpc_dataplane_firewall_mode_conflict"
|
|
|
|
|
|
def _transaction_stat_tool(tmp_path: Path) -> Path:
|
|
tool = tmp_path / "stat"
|
|
tool.write_text(
|
|
f"#!{sys.executable}\n"
|
|
"import os, stat, sys\n"
|
|
"value = os.lstat(sys.argv[3])\n"
|
|
"mode = format(stat.S_IMODE(value.st_mode), 'o')\n"
|
|
"fields = {'%u:%g:%a': '0:0:' + mode, '%s': str(value.st_size)}\n"
|
|
"print(fields[sys.argv[2]])\n",
|
|
encoding="utf-8",
|
|
)
|
|
tool.chmod(0o700)
|
|
return tool
|
|
|
|
|
|
def test_native_transaction_marker_is_single_line_and_private(tmp_path):
|
|
txn = tmp_path / "transaction"
|
|
txn.mkdir(mode=0o700)
|
|
stat_tool = _transaction_stat_tool(tmp_path)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", "\n".join((
|
|
f"TXN={txn}",
|
|
f"STAT={stat_tool}",
|
|
VPC_NATIVE_TRANSACTION_GUARD_SH,
|
|
"vc_vpc_write_native_transaction_mode",
|
|
"vc_vpc_native_transaction_owned",
|
|
))],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
marker = txn / "firewall_mode"
|
|
assert marker.read_text(encoding="utf-8") == "native_nftables\n"
|
|
assert stat.S_IMODE(marker.stat().st_mode) == 0o600
|
|
|
|
|
|
@pytest.mark.parametrize("legacy", (
|
|
"markerless",
|
|
"markerless_committed",
|
|
"markerless_wg_merged",
|
|
"firewalld_marker",
|
|
"firewalld_committed",
|
|
"firewalld_files_removed",
|
|
"firewalld_state",
|
|
"snapshot_state",
|
|
"nft_persist",
|
|
"reassert",
|
|
"desired_verify",
|
|
))
|
|
def test_native_transaction_guard_preserves_and_rejects_legacy_journal(
|
|
tmp_path, legacy,
|
|
):
|
|
txn = tmp_path / "transaction"
|
|
txn.mkdir(mode=0o700)
|
|
if not legacy.startswith("markerless"):
|
|
(txn / "firewall_mode").write_text(
|
|
"firewalld_nft\n" if legacy.startswith("firewalld_")
|
|
else "native_nftables\n",
|
|
encoding="utf-8",
|
|
)
|
|
(txn / "firewall_mode").chmod(0o600)
|
|
for phase in ("committed", "files_removed", "wg_merged"):
|
|
if legacy.endswith("_" + phase):
|
|
(txn / "state").write_text(phase + "\n", encoding="utf-8")
|
|
if legacy == "firewalld_state":
|
|
(txn / "state.before").write_text(
|
|
"firewall_mode|firewalld_nft\n", encoding="utf-8")
|
|
elif legacy == "snapshot_state":
|
|
(txn / "state.before").write_text(
|
|
"firewall_mode|native_nftables\nnft_snapshot|legacy\n",
|
|
encoding="utf-8",
|
|
)
|
|
elif legacy == "nft_persist":
|
|
(txn / "nft.persist.before.inet").write_text(
|
|
"legacy\n", encoding="utf-8")
|
|
elif legacy == "reassert":
|
|
(txn / "reassert.nft").write_text("legacy\n", encoding="utf-8")
|
|
elif legacy == "desired_verify":
|
|
(txn / "nft.desired.verify.inet").write_text(
|
|
"legacy\n", encoding="utf-8")
|
|
for path in txn.iterdir():
|
|
path.chmod(0o600)
|
|
before = {
|
|
path.name: (path.read_bytes(), stat.S_IMODE(path.stat().st_mode))
|
|
for path in txn.iterdir()
|
|
}
|
|
mutation = tmp_path / "mutation"
|
|
stat_tool = _transaction_stat_tool(tmp_path)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", "\n".join((
|
|
f"TXN={txn}",
|
|
f"STAT={stat_tool}",
|
|
VPC_NATIVE_TRANSACTION_GUARD_SH,
|
|
f"MUTATION={mutation}",
|
|
"vc_vpc_native_transaction_owned || exit 79",
|
|
': > "$MUTATION"',
|
|
))],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
assert result.returncode == 79, result.stderr
|
|
assert not mutation.exists()
|
|
assert txn.is_dir()
|
|
after = {
|
|
path.name: (path.read_bytes(), stat.S_IMODE(path.stat().st_mode))
|
|
for path in txn.iterdir()
|
|
}
|
|
assert after == before
|
|
|
|
|
|
@pytest.mark.parametrize("legacy", (
|
|
"legacy_manifest",
|
|
"fabric_transaction",
|
|
"emergency_owner",
|
|
"other_transaction",
|
|
"inet_snapshot",
|
|
"bridge_snapshot",
|
|
"arp_snapshot",
|
|
"vpc_firewalld_state",
|
|
"vpc_nft_snapshot_state",
|
|
"fabric_firewalld_mode",
|
|
"fabric_underlay",
|
|
"fabric_firewall_rule",
|
|
"live_firewalld",
|
|
"live_inet_emergency",
|
|
"live_bridge_emergency",
|
|
))
|
|
def test_firewall_boundary_blocks_global_legacy_state_before_mutation(
|
|
tmp_path, legacy,
|
|
):
|
|
etc = tmp_path / "etc" / "virt-cloud"
|
|
state_dir = etc / "vpc-dataplane"
|
|
run = tmp_path / "run" / "virt-cloud"
|
|
state_dir.mkdir(parents=True, mode=0o700)
|
|
run.mkdir(parents=True, mode=0o700)
|
|
txn = etc / ".vpc-dataplane-current.txn"
|
|
evidence: Path | None = None
|
|
nft_output = "table inet filter\n"
|
|
if legacy == "legacy_manifest":
|
|
evidence = etc / "nftables.conf"
|
|
evidence.write_text("legacy\n", encoding="utf-8")
|
|
elif legacy == "fabric_transaction":
|
|
evidence = etc / ".wg-fabric-txn"
|
|
evidence.mkdir(mode=0o700)
|
|
elif legacy == "emergency_owner":
|
|
evidence = run / "wg-fabric-emergency.owner"
|
|
evidence.write_text("legacy\n", encoding="utf-8")
|
|
elif legacy == "other_transaction":
|
|
evidence = etc / ".vpc-dataplane-foreign.txn"
|
|
evidence.mkdir(mode=0o700)
|
|
elif legacy.endswith("_snapshot"):
|
|
family = legacy.removesuffix("_snapshot")
|
|
evidence = state_dir / f"legacy.nft.{family}.live"
|
|
evidence.write_text("legacy\n", encoding="utf-8")
|
|
elif legacy in {"vpc_firewalld_state", "vpc_nft_snapshot_state"}:
|
|
record = (
|
|
"firewall_mode|firewalld_nft\n"
|
|
if legacy == "vpc_firewalld_state"
|
|
else "firewall_mode|native_nftables\nnft_snapshot|legacy\n"
|
|
)
|
|
evidence = state_dir / "legacy.state"
|
|
evidence.write_text(record, encoding="utf-8")
|
|
elif legacy.startswith("live_"):
|
|
nft_output = {
|
|
"live_firewalld": "table inet firewalld\n",
|
|
"live_inet_emergency": (
|
|
"table inet virt_cloud_fabric_emergency\n"),
|
|
"live_bridge_emergency": (
|
|
"table bridge virt_cloud_fabric_emergency\n"),
|
|
}[legacy]
|
|
else:
|
|
record = {
|
|
"fabric_firewalld_mode": "firewall|firewalld_nft\n",
|
|
"fabric_underlay": "underlay|203.0.113.1|51820\n",
|
|
"fabric_firewall_rule": "firewall_rule|legacy\n",
|
|
}[legacy]
|
|
evidence = etc / "wg-fabric0.conf"
|
|
evidence.write_text(record, encoding="utf-8")
|
|
if evidence is not None and evidence.is_file():
|
|
evidence.chmod(0o600)
|
|
evidence_bytes = evidence.read_bytes() if evidence and evidence.is_file() else None
|
|
guard = VPC_NATIVE_TRANSACTION_GUARD_SH.replace(
|
|
"/etc/virt-cloud", str(etc),
|
|
).replace(
|
|
"/run/virt-cloud", str(run),
|
|
)
|
|
mutation = tmp_path / "mutation"
|
|
stat_tool = _transaction_stat_tool(tmp_path)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", "\n".join((
|
|
f"TXN={txn}",
|
|
f"STAT={stat_tool}",
|
|
f"MUTATION={mutation}",
|
|
f"NFT_OUTPUT={shlex.quote(nft_output)}",
|
|
"vc_test_nft() {",
|
|
" [ \"$*\" = \"list tables\" ] || return 2",
|
|
" printf '%s' \"$NFT_OUTPUT\"",
|
|
"}",
|
|
"NFT=vc_test_nft",
|
|
guard,
|
|
"vc_vpc_firewall_boundary_clean || exit 79",
|
|
': > "$MUTATION"',
|
|
))],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
assert result.returncode == 79, result.stderr
|
|
assert not mutation.exists()
|
|
if evidence is not None:
|
|
assert evidence.exists()
|
|
if evidence_bytes is not None:
|
|
assert evidence.read_bytes() == evidence_bytes
|
|
|
|
|
|
def test_apply_and_remove_validate_native_transaction_before_host_mutation():
|
|
scripts = (
|
|
render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE,
|
|
),
|
|
render_remove_script(
|
|
_empty_local_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE,
|
|
),
|
|
)
|
|
for script in scripts:
|
|
boundary = script.index("vc_vpc_firewall_boundary_clean || exit 79")
|
|
transaction = script.index(
|
|
'if [ -e "$TXN" ] || [ -L "$TXN" ]; then', boundary)
|
|
ownership = script.index(
|
|
"vc_vpc_native_transaction_owned || exit 79", transaction)
|
|
quarantine = script.index("vc_nm_device_quarantine || exit 72", ownership)
|
|
assert boundary < transaction < ownership < quarantine
|
|
|
|
create = script.index('mkdir -m 700 "$TXN"', ownership)
|
|
marker = script.index(
|
|
"vc_vpc_write_native_transaction_mode || exit 79", create)
|
|
inflight = script.index('> "$TXN/inflight"', marker)
|
|
assert create < marker < inflight
|
|
|
|
|
|
def test_observe_refuses_readiness_while_any_transaction_path_exists():
|
|
script = render_observe_script(
|
|
_plan(), firewall_mode=FIREWALL_MODE_NATIVE)
|
|
readiness = script.index("firewall_mode_state=owned")
|
|
transaction = script.index(
|
|
'[ ! -e "$TXN" ] && [ ! -L "$TXN" ] || '
|
|
"firewall_mode_state=mismatch",
|
|
readiness,
|
|
)
|
|
marker = script.index("__VC_VPC_FIREWALL_STATE__", transaction)
|
|
assert readiness < transaction < marker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_networkmanager_disk_profile_requires_manual_boot_and_no_zone(tmp_path):
|
|
profile = tmp_path / "vcv-test.nmconnection"
|
|
profile.write_text(
|
|
"[connection]\n"
|
|
"id=vcv-test\n"
|
|
"uuid=11111111-1111-4111-8111-111111111111\n"
|
|
"type=bridge\n"
|
|
"interface-name=vcv-test\n"
|
|
"stable-id=virt-cloud-vpc-dataplane-v1 vpc=vpc-test\n"
|
|
"autoconnect=false\n"
|
|
"autoconnect-retries=0\n"
|
|
"\n[ipv4]\nmethod=disabled\n"
|
|
)
|
|
profile.chmod(0o600)
|
|
helper = NETWORKMANAGER_VPC_PROFILE_SH.replace(
|
|
"/etc/NetworkManager/system-connections/", f"{tmp_path}/")
|
|
script = "\n".join((
|
|
"vc_test_stat() { printf '%s\\n' '0:600'; }",
|
|
"STAT=vc_test_stat",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_MARKER='virt-cloud-vpc-dataplane-v1 vpc=vpc-test'",
|
|
helper,
|
|
'vc_nm_profile_disk_validate "$PROFILE" "$UUID"',
|
|
))
|
|
env = dict(
|
|
os.environ,
|
|
PROFILE=str(profile),
|
|
UUID="11111111-1111-4111-8111-111111111111",
|
|
)
|
|
|
|
accepted = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
|
|
for unsafe in ("zone=trusted\n", "autoconnect=true\n"):
|
|
content = profile.read_text()
|
|
if unsafe.startswith("autoconnect="):
|
|
content = content.replace("autoconnect=false\n", unsafe)
|
|
else:
|
|
content = content.replace("autoconnect=false\n",
|
|
"autoconnect=false\n" + unsafe)
|
|
profile.write_text(content)
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script], env=env,
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0
|
|
profile.write_text(
|
|
profile.read_text().replace("zone=trusted\n", "")
|
|
.replace("autoconnect=true\n", "autoconnect=false\n")
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_fabric_snapshot_verdict_drift_blocks_exact_restore_guard(tmp_path):
|
|
before = tmp_path / "fabric.before"
|
|
live = tmp_path / "fabric.live"
|
|
before.write_text(
|
|
'table inet virt_cloud_wg_fabric {\n'
|
|
' chain forward {\n'
|
|
' iifname "wg-fabric0" drop comment '
|
|
'"virt-cloud-wg-fabric-v1:0123456789abcdef"\n'
|
|
' }\n}\n'
|
|
)
|
|
live.write_text(before.read_text().replace(" drop comment", " accept comment"))
|
|
shell = "\n".join((
|
|
r'''
|
|
vc_test_nft() {
|
|
[ "$1" = -s ] && [ "$2" = list ] && [ "$3" = table ] || return 2
|
|
cat "$LIVE"
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
'''.strip(),
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric "$BEFORE"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", shell],
|
|
env=dict(os.environ, LIVE=str(live), BEFORE=str(before)),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0
|
|
|
|
|
|
def test_nft_stateless_snapshot_rejects_late_table_and_semantic_drift(tmp_path):
|
|
live = tmp_path / "live"
|
|
live.mkdir()
|
|
snapshot = tmp_path / "desired"
|
|
script = "\n".join((
|
|
r'''
|
|
vc_test_nft() {
|
|
[ "$1" = -s ] || return 2
|
|
shift
|
|
[ "$1" = list ] || return 2
|
|
if [ "$2" = tables ]; then
|
|
[ "${VC_INVENTORY_FAIL:-no}" = no ] || return 3
|
|
for live_path in "$LIVE/$3-"*; do
|
|
[ -f "$live_path" ] || continue
|
|
printf 'table %s %s\n' "$3" "${live_path##*-$3-}"
|
|
done
|
|
return 0
|
|
fi
|
|
[ "$2" = table ] || return 2
|
|
live_path="$LIVE/$3-$4"
|
|
[ -f "$live_path" ] || return 1
|
|
cat "$live_path"
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
'''.strip(),
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
'"$@"',
|
|
))
|
|
|
|
def run(*args):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script, "vc-test", *args],
|
|
env=dict(os.environ, LIVE=str(live)), capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
|
|
# The pre-apply view was absent. A foreign same-name table appearing
|
|
# before rollback must not be mistaken for a transaction-owned table.
|
|
captured_absent = run(
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(snapshot),
|
|
)
|
|
assert captured_absent.returncode == 0, captured_absent.stderr
|
|
assert (tmp_path / "desired.absent").is_file()
|
|
live_table = live / "inet-virt_cloud_vpc_test"
|
|
live_table.write_text(
|
|
'table inet virt_cloud_vpc_test {\n'
|
|
' chain forward { policy accept; }\n}\n',
|
|
)
|
|
late_table = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert late_table.returncode != 0
|
|
|
|
# A point lookup error alone is never proof of absence. If the complete
|
|
# family inventory cannot be read and parsed successfully, no absent
|
|
# marker may be created.
|
|
live_table.unlink()
|
|
ambiguous = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", script, "vc-test",
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(tmp_path / "ambiguous")],
|
|
env=dict(os.environ, LIVE=str(live), VC_INVENTORY_FAIL="yes"),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
assert ambiguous.returncode != 0
|
|
assert not (tmp_path / "ambiguous.absent").exists()
|
|
|
|
desired = (
|
|
'table inet virt_cloud_vpc_test {\n'
|
|
' chain forward {\n'
|
|
' ip saddr 10.44.0.10 drop comment '
|
|
'"virt-cloud-vpc-dataplane-v1:test:0123456789abcdef"\n'
|
|
' }\n}\n'
|
|
)
|
|
live_table.write_text(desired)
|
|
captured = run(
|
|
"vc_nft_table_snapshot", "inet", "virt_cloud_vpc_test",
|
|
str(snapshot),
|
|
)
|
|
assert captured.returncode == 0, captured.stderr
|
|
exact = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert exact.returncode == 0, exact.stderr
|
|
|
|
# Retaining the exact ownership comment is insufficient: changing the
|
|
# verdict or deleting an expression changes the complete canonical dump.
|
|
live_table.write_text(desired.replace(" drop comment", " accept comment"))
|
|
changed_verdict = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert changed_verdict.returncode != 0
|
|
live_table.write_text(desired.replace("ip saddr 10.44.0.10 ", ""))
|
|
removed_expression = run(
|
|
"vc_nft_table_matches_snapshot", "inet",
|
|
"virt_cloud_vpc_test", str(snapshot),
|
|
)
|
|
assert removed_expression.returncode != 0
|
|
|
|
|
|
def test_apply_rollback_requires_exact_post_apply_tables_before_delete():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
apply_call = script.index('"$NFT" -f "$NFT_BATCH" >/dev/null || exit 72')
|
|
applied_flag = script.index("NFT_APPLIED=yes", apply_call)
|
|
assert apply_call < applied_flag
|
|
assert "NFT_APPLIED=no" in script
|
|
assert "FABRIC_NFT_APPLIED=no" in script
|
|
assert '"$NFT" -s list table "$vc_nft_snapshot_family"' in script
|
|
assert 'vc_vpc_capture_nft_snapshot "$TXN/nft.desired"' in script
|
|
assert 'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"' in script
|
|
assert "nft_snapshot|" not in script
|
|
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
exact_check = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"')
|
|
delete_emit = rollback.index(
|
|
"printf 'delete table %s %s\\n' \"$rollback_family\" \"$TABLE\"")
|
|
assert exact_check < delete_emit
|
|
assert 'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric' in rollback
|
|
assert 'NFT_ROLLBACK_SAFE=no' in rollback
|
|
assert '[ "$NFT_ROLLBACK_SAFE" = yes ] || ROLLBACK_FAILED=yes' in rollback
|
|
assert 'if "$NFT" list table inet "$TABLE"' not in rollback
|
|
|
|
|
|
def test_networkmanager_rollback_restores_nontrusted_existing_profile_state():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
nft_restore = rollback.index('"$NFT" -f "$NFT_ROLLBACK"')
|
|
restore = rollback.index('[ "$NM_PROFILE_WAS_PRESENT" = yes ]')
|
|
release = rollback.index("vc_nm_device_release", restore)
|
|
activate = rollback.index(
|
|
'connection up uuid "$NM_PROFILE_UUID"', release)
|
|
topology = rollback.index(
|
|
"vc_vpc_bridge_topology_matches", activate)
|
|
assert nft_restore < restore < release < activate < topology
|
|
assert '[ "$NM_WAS_ACTIVE" = yes ]' in rollback[restore:topology]
|
|
assert "vc_nm_device_expect_managed yes" in rollback[restore:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_vpc_bridge_topology_parser_propagates_malformed_output():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
start = script.index("vc_vpc_bridge_port_names() {")
|
|
end = script.index("# Never mutate even the bridge state", start)
|
|
helpers = script[start:end]
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
r'''
|
|
vc_test_bridge() {
|
|
[ "$*" = "link show master vcv-test" ] || return 2
|
|
printf '%s\n' "$VC_BRIDGE_OUTPUT"
|
|
}
|
|
BRIDGE_CMD=vc_test_bridge
|
|
'''.strip(),
|
|
helpers,
|
|
"vc_vpc_bridge_port_names",
|
|
))
|
|
|
|
def run(output):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, VC_BRIDGE_OUTPUT=output),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
valid = run("2: tap0@if3: <BROADCAST> master vcv-test")
|
|
assert valid.returncode == 0, valid.stderr
|
|
assert valid.stdout == "tap0\n"
|
|
assert run("malformed").returncode != 0
|
|
assert run("2: ../../foreign: <BROADCAST> master vcv-test").returncode != 0
|
|
|
|
|
|
def test_vpc_bridge_link_state_requires_one_well_formed_record():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
start = script.index("vc_vpc_bridge_link_state() {")
|
|
end = script.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen() {", start)
|
|
helper = script[start:end]
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
r'''
|
|
vc_test_ip() {
|
|
[ "$*" = "-o link show dev vcv-test" ] || return 2
|
|
[ "${VC_QUERY_FAIL:-no}" = no ] || return 3
|
|
printf '%s' "$VC_LINK_OUTPUT"
|
|
}
|
|
IP=vc_test_ip
|
|
'''.strip(),
|
|
helper,
|
|
"vc_vpc_bridge_link_state",
|
|
))
|
|
|
|
def run(output, *, fail="no"):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, VC_LINK_OUTPUT=output, VC_QUERY_FAIL=fail),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
|
|
down = run("9: vcv-test: <BROADCAST>\n")
|
|
assert down.returncode == 0 and down.stdout == "down"
|
|
up = run("9: vcv-test: <BROADCAST,UP,LOWER_UP>\n")
|
|
assert up.returncode == 0 and up.stdout == "up"
|
|
assert run("").returncode != 0
|
|
assert run("malformed\n").returncode != 0
|
|
assert run("9: foreign: <BROADCAST>\n").returncode != 0
|
|
assert run("9: vcv-test: <BROADCAST>\n", fail="yes").returncode != 0
|
|
|
|
early = script.index("# Link quarantine deliberately runs before")
|
|
hard_down = script.index(
|
|
'"$IP" link set dev "$BRIDGE" down >/dev/null || exit 72', early)
|
|
exact_down = script.index(
|
|
"bridge_quarantine_link_state=$(vc_vpc_bridge_link_state)",
|
|
hard_down)
|
|
assert hard_down < exact_down
|
|
|
|
|
|
|
|
|
|
def test_ifupdown_apply_rollback_never_ifdowns_a_bridge_before_port_proof():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
assert '"$IFDOWN" --force "$BRIDGE"' not in rollback
|
|
created = rollback.index('if [ "$BRIDGE_CREATED" = yes ]; then')
|
|
inspect_created = rollback.index(
|
|
'ports=$($BRIDGE_CMD link show master "$BRIDGE"', created)
|
|
delete_created = rollback.index(
|
|
'"$IP" link del dev "$BRIDGE" type bridge', inspect_created)
|
|
existing = rollback.index(
|
|
'elif [ "$BRIDGE_WAS_UP" = no ] && "$IP" link show dev "$BRIDGE"',
|
|
delete_created)
|
|
inspect_existing = rollback.index(
|
|
'ports=$($BRIDGE_CMD link show master "$BRIDGE"', existing)
|
|
down_existing = rollback.index(
|
|
'"$IP" link set dev "$BRIDGE" down', inspect_existing)
|
|
assert created < inspect_created < delete_created
|
|
assert existing < inspect_existing < down_existing
|
|
assert "IF_ACTIVATION_ATTEMPTED" not in script
|
|
assert "IF_CREATED" not in script
|
|
|
|
|
|
def test_native_ifupdown_rollback_reopens_only_exact_sampled_up_existing_bridge():
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
helper_start = script.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen() {")
|
|
helper_end = script.index(
|
|
"# Never mutate even the bridge state", helper_start)
|
|
helper = script[helper_start:helper_end]
|
|
exact = helper.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"')
|
|
fabric = helper.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric', exact)
|
|
topology = helper.index("vc_vpc_bridge_topology_matches", fabric)
|
|
link_up = helper.index('"$IP" link set dev "$BRIDGE" up', topology)
|
|
verify_up = helper.index(
|
|
'vc_bridge_reopen_state=$(vc_vpc_bridge_link_state)', link_up)
|
|
hard_down = helper.index('"$IP" link set dev "$BRIDGE" down', verify_up)
|
|
assert exact < fabric < topology < link_up < verify_up < hard_down
|
|
|
|
rollback = script[script.index("rollback() {"):script.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
restore = rollback.index(
|
|
'if [ "$MODE" = ifupdown ] &&\n'
|
|
' [ "$BRIDGE_CREATED" = no ]')
|
|
restore_call = rollback.index(
|
|
"vc_vpc_native_ifupdown_rollback_reopen", restore)
|
|
assert '[ "$BRIDGE_CREATED" = no ]' in rollback[restore:restore_call]
|
|
assert '[ "$BRIDGE_WAS_UP" = yes ]' in rollback[restore:restore_call]
|
|
|
|
|
|
def test_native_ifupdown_reopen_failure_reisolates_bridge(tmp_path):
|
|
script = render_apply_script(
|
|
_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
start = script.index("vc_vpc_native_ifupdown_rollback_reopen() {")
|
|
end = script.index(
|
|
"# Never mutate even the bridge state", start)
|
|
helper = script[start:end]
|
|
link_state = tmp_path / "link"
|
|
topology_calls = tmp_path / "topology-calls"
|
|
link_state.write_text("down\n")
|
|
topology_calls.write_text("0\n")
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"TXN=/transaction",
|
|
f"LINK_STATE={link_state}",
|
|
f"TOPOLOGY_CALLS={topology_calls}",
|
|
r'''
|
|
vc_vpc_nft_matches_snapshot() { :; }
|
|
vc_nft_table_matches_snapshot() { :; }
|
|
vc_vpc_bridge_topology_matches() {
|
|
calls=$(cat "$TOPOLOGY_CALLS")
|
|
calls=$((calls + 1))
|
|
printf '%s\n' "$calls" > "$TOPOLOGY_CALLS"
|
|
[ "$calls" = 1 ]
|
|
}
|
|
vc_vpc_bridge_link_state() { cat "$LINK_STATE"; }
|
|
vc_test_ip() {
|
|
case "$*" in
|
|
"link set dev vcv-test up") printf '%s\n' up > "$LINK_STATE" ;;
|
|
"link set dev vcv-test down") printf '%s\n' down > "$LINK_STATE" ;;
|
|
*) return 2 ;;
|
|
esac
|
|
}
|
|
IP=vc_test_ip
|
|
'''.strip(),
|
|
helper,
|
|
r'''
|
|
if vc_vpc_native_ifupdown_rollback_reopen; then exit 9; fi
|
|
[ "$(cat "$LINK_STATE")" = down ]
|
|
'''.strip(),
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode == 0, rejected.stderr
|
|
assert topology_calls.read_text() == "2\n"
|
|
|
|
|
|
def test_networkmanager_profile_queries_propagate_command_failures():
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"BRIDGE=vcv-test",
|
|
"NM_PROFILE_UUID=11111111-1111-4111-8111-111111111111",
|
|
"NM_PROFILE_FILE=/owned.nmconnection",
|
|
"NM_PROFILE_MARKER='virt-cloud-vpc-dataplane-v1 vpc=vpc-test'",
|
|
r'''
|
|
vc_test_nmcli() {
|
|
field=; fields=
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
-g) shift; field=$1 ;;
|
|
-f) shift; fields=$1 ;;
|
|
esac
|
|
shift
|
|
done
|
|
if [ "$fields" = NAME ]; then
|
|
[ "${VC_LIST_FAIL:-no}" = no ] || return 3
|
|
printf '%s\n' "$BRIDGE"
|
|
return 0
|
|
fi
|
|
if [ "$fields" = UUID,FILENAME ]; then
|
|
[ "${VC_FILENAME_LIST_FAIL:-no}" = no ] || return 3
|
|
if [ "${VC_FILENAME_ROWS+x}" = x ]; then
|
|
printf '%b' "$VC_FILENAME_ROWS"
|
|
else
|
|
printf '%s:%s\n' "$NM_PROFILE_UUID" "$NM_PROFILE_FILE"
|
|
fi
|
|
return 0
|
|
fi
|
|
[ "$field" != GENERAL.FILENAME ] || return 97
|
|
[ "$field" != "${VC_FAIL_FIELD:-never}" ] || return 3
|
|
case "$field" in
|
|
connection.uuid) printf '%s\n' "$NM_PROFILE_UUID" ;;
|
|
connection.type) printf '%s\n' bridge ;;
|
|
connection.interface-name) printf '%s\n' "$BRIDGE" ;;
|
|
connection.stable-id) printf '%s\n' "$NM_PROFILE_MARKER" ;;
|
|
connection.autoconnect) printf '%s\n' no ;;
|
|
connection.autoconnect-retries) printf '%s\n' 0 ;;
|
|
ipv4.method|ipv6.method) printf '%s\n' disabled ;;
|
|
bridge.stp) printf '%s\n' no ;;
|
|
*) printf '%s\n' -- ;;
|
|
esac
|
|
}
|
|
NMCLI=vc_test_nmcli
|
|
'''.strip(),
|
|
_NMCLI_FILENAME_RESOLVER_SH,
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
"vc_nm_profile_disk_validate() { :; }",
|
|
r'''
|
|
case "$VC_ACTION" in
|
|
base) vc_nm_profile_base_validate ;;
|
|
resolve_ok)
|
|
vc_nm_profile_resolve
|
|
[ "$NM_PROFILE_FILE" = "$VC_EXPECT_FILE" ]
|
|
;;
|
|
resolve)
|
|
if vc_nm_profile_resolve; then exit 9; else status=$?; fi
|
|
[ "$status" = "$VC_EXPECT_STATUS" ]
|
|
;;
|
|
*) exit 2 ;;
|
|
esac
|
|
'''.strip(),
|
|
))
|
|
|
|
def run(**values):
|
|
return subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(os.environ, **values), capture_output=True, text=True,
|
|
check=False,
|
|
)
|
|
|
|
accepted = run(VC_ACTION="base")
|
|
assert accepted.returncode == 0, accepted.stderr
|
|
assert run(VC_ACTION="base", VC_FAIL_FIELD="ipv4.routes").returncode != 0
|
|
profile = (
|
|
"/etc/NetworkManager/system-connections/"
|
|
"bridge profile:primary.nmconnection"
|
|
)
|
|
resolved = run(
|
|
VC_ACTION="resolve_ok",
|
|
VC_EXPECT_FILE=profile,
|
|
VC_FILENAME_ROWS=(
|
|
"22222222-2222-4222-8222-222222222222:"
|
|
"/run/NetworkManager/system-connections/other.nmconnection\\n"
|
|
f"11111111-1111-4111-8111-111111111111:{profile}\\n"
|
|
),
|
|
)
|
|
assert resolved.returncode == 0, resolved.stderr
|
|
list_failure = run(
|
|
VC_ACTION="resolve", VC_LIST_FAIL="yes", VC_EXPECT_STATUS="1")
|
|
assert list_failure.returncode == 0, list_failure.stderr
|
|
filename_list_failure = run(
|
|
VC_ACTION="resolve", VC_FILENAME_LIST_FAIL="yes",
|
|
VC_EXPECT_STATUS="1",
|
|
)
|
|
assert filename_list_failure.returncode == 0, filename_list_failure.stderr
|
|
duplicate = run(
|
|
VC_ACTION="resolve", VC_EXPECT_STATUS="1",
|
|
VC_FILENAME_ROWS=(
|
|
"11111111-1111-4111-8111-111111111111:/first.nmconnection\\n"
|
|
"11111111-1111-4111-8111-111111111111:/second.nmconnection\\n"
|
|
),
|
|
)
|
|
assert duplicate.returncode == 0, duplicate.stderr
|
|
missing = run(
|
|
VC_ACTION="resolve", VC_EXPECT_STATUS="1",
|
|
VC_FILENAME_ROWS=(
|
|
"22222222-2222-4222-8222-222222222222:/other.nmconnection\\n"
|
|
),
|
|
)
|
|
assert missing.returncode == 0, missing.stderr
|
|
|
|
|
|
def test_networkmanager_created_profile_rollback_deletes_only_exact_uuid(tmp_path):
|
|
deleted = tmp_path / "deleted"
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
"NM_CREATED_UUID=11111111-1111-4111-8111-111111111111",
|
|
f"DELETED={deleted}",
|
|
NETWORKMANAGER_VPC_PROFILE_SH,
|
|
r'''
|
|
vc_nm_profile_resolve() {
|
|
if [ -f "$DELETED" ]; then return "$VC_AFTER_STATUS"; fi
|
|
NM_PROFILE_UUID=$VC_RESOLVED_UUID
|
|
return 0
|
|
}
|
|
vc_nm_profile_base_validate() { [ "${VC_BASE_VALID:-yes}" = yes ]; }
|
|
vc_test_nmcli() {
|
|
[ "$*" = "connection delete uuid $NM_CREATED_UUID" ] || return 2
|
|
printf '%s\n' "$NM_CREATED_UUID" > "$DELETED"
|
|
}
|
|
NMCLI=vc_test_nmcli
|
|
vc_nm_created_profile_remove
|
|
'''.strip(),
|
|
))
|
|
|
|
def run(resolved_uuid, after_status, *, base_valid="yes"):
|
|
deleted.unlink(missing_ok=True)
|
|
result = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness],
|
|
env=dict(
|
|
os.environ,
|
|
VC_RESOLVED_UUID=resolved_uuid,
|
|
VC_AFTER_STATUS=str(after_status),
|
|
VC_BASE_VALID=base_valid,
|
|
),
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
removed = deleted.read_text().strip() if deleted.exists() else None
|
|
return result, removed
|
|
|
|
exact, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 2)
|
|
assert exact.returncode == 0, exact.stderr
|
|
assert removed == "11111111-1111-4111-8111-111111111111"
|
|
|
|
foreign, removed = run(
|
|
"22222222-2222-4222-8222-222222222222", 2)
|
|
assert foreign.returncode != 0
|
|
assert removed is None
|
|
|
|
invalid_profile, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 2, base_valid="no")
|
|
assert invalid_profile.returncode != 0
|
|
assert removed is None
|
|
|
|
ambiguous, removed = run(
|
|
"11111111-1111-4111-8111-111111111111", 1)
|
|
assert ambiguous.returncode != 0
|
|
assert removed == "11111111-1111-4111-8111-111111111111"
|
|
|
|
|
|
def test_destructive_nft_batches_recheck_full_state_after_dry_run():
|
|
apply = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
baseline = apply.index(
|
|
'vc_vpc_capture_nft_precommit "$TXN/nft.precommit.before"')
|
|
apply_check = apply.index('"$NFT" -c -f "$NFT_BATCH"', baseline)
|
|
apply_precommit = apply.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"',
|
|
apply_check)
|
|
apply_commit = apply.index('"$NFT" -f "$NFT_BATCH"', apply_precommit)
|
|
apply_post = apply.index(
|
|
'vc_vpc_capture_nft_snapshot "$TXN/nft.desired"', apply_commit)
|
|
assert baseline < apply_check < apply_precommit < apply_commit < apply_post
|
|
|
|
rollback = apply[apply.index("rollback() {"):apply.index(
|
|
"trap rollback EXIT HUP INT TERM")]
|
|
rollback_check = rollback.index('"$NFT" -c -f "$NFT_ROLLBACK"')
|
|
rollback_vpc = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.desired"', rollback_check)
|
|
rollback_fabric = rollback.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric',
|
|
rollback_vpc)
|
|
rollback_commit = rollback.index(
|
|
'"$NFT" -f "$NFT_ROLLBACK"', rollback_fabric)
|
|
rollback_vpc_post = rollback.index(
|
|
'vc_vpc_nft_matches_snapshot "$TXN/nft.precommit.before"',
|
|
rollback_commit)
|
|
rollback_fabric_post = rollback.index(
|
|
'vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric',
|
|
rollback_vpc_post)
|
|
assert (rollback_check < rollback_vpc < rollback_fabric <
|
|
rollback_commit < rollback_vpc_post < rollback_fabric_post)
|
|
|
|
remove = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
remove_capture = remove.index(
|
|
'vc_remove_capture_nft_precommit "$TXN/nft.remove.precommit"')
|
|
remove_check = remove.index(
|
|
'"$NFT" -c -f "$NFT_REMOVE_BATCH"', remove_capture)
|
|
remove_precommit = remove.index(
|
|
'vc_remove_nft_precommit_matches "$TXN/nft.remove.precommit"',
|
|
remove_check)
|
|
remove_commit = remove.index(
|
|
'"$NFT" -f "$NFT_REMOVE_BATCH"', remove_precommit)
|
|
remove_post = remove.index("vc_remove_nft_tables_absent", remove_commit)
|
|
assert (remove_capture < remove_check < remove_precommit < remove_commit <
|
|
remove_post)
|
|
|
|
|
|
def test_fake_nft_dry_run_replacement_never_reaches_destructive_commit(tmp_path):
|
|
apply = render_apply_script(
|
|
_plan(), deployment_mode="networkmanager_nmcli",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
apply_start = apply.index("vc_vpc_capture_nft_precommit() {")
|
|
apply_end = apply.index("vc_firewall_mode_ready() {", apply_start)
|
|
apply_helpers = apply[apply_start:apply_end]
|
|
|
|
remove = render_remove_script(
|
|
_empty_local_plan(), deployment_mode="ifupdown",
|
|
firewall_mode=FIREWALL_MODE_NATIVE)
|
|
remove_start = remove.index("vc_remove_capture_nft_precommit() {")
|
|
remove_end = remove.index('case "$PHASE" in', remove_start)
|
|
remove_helpers = remove[remove_start:remove_end]
|
|
|
|
fake = r'''
|
|
vc_test_nft() {
|
|
if [ "$1" = -s ]; then
|
|
shift
|
|
[ "$1" = list ] || return 2
|
|
if [ "$2" = tables ]; then
|
|
for live_path in "$LIVE/$3-"*; do
|
|
[ -f "$live_path" ] || continue
|
|
printf 'table %s %s\n' "$3" "${live_path##*-$3-}"
|
|
done
|
|
return 0
|
|
fi
|
|
[ "$2" = table ] || return 2
|
|
live_path="$LIVE/$3-$4"
|
|
[ -f "$live_path" ] || return 1
|
|
cat "$live_path"
|
|
return 0
|
|
fi
|
|
if [ "$1" = -c ] && [ "$2" = -f ]; then
|
|
printf 'table inet %s {\n' "$RACE_TABLE" \
|
|
> "$LIVE/inet-$RACE_TABLE"
|
|
printf '%s\n' \
|
|
' chain forward { policy accept; }' '}' > \
|
|
"$LIVE/inet-$RACE_TABLE.tmp"
|
|
cat "$LIVE/inet-$RACE_TABLE.tmp" >> "$LIVE/inet-$RACE_TABLE"
|
|
rm -f "$LIVE/inet-$RACE_TABLE.tmp"
|
|
return 0
|
|
fi
|
|
if [ "$1" = -f ]; then
|
|
: > "$APPLIED"
|
|
return 0
|
|
fi
|
|
return 2
|
|
}
|
|
NFT=vc_test_nft
|
|
CMP=/usr/bin/cmp
|
|
TABLE=virt_cloud_vpc_race
|
|
RACE_TABLE=$TABLE
|
|
'''.strip()
|
|
|
|
cases = (
|
|
(
|
|
"apply",
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/precommit"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/precommit"',
|
|
False,
|
|
),
|
|
(
|
|
"reassert",
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/desired"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/desired"',
|
|
True,
|
|
),
|
|
(
|
|
"remove",
|
|
remove_helpers,
|
|
'vc_remove_capture_nft_precommit "$TXN/precommit"',
|
|
'vc_remove_nft_precommit_matches "$TXN/precommit"',
|
|
True,
|
|
),
|
|
)
|
|
for name, helpers, capture, precommit, initially_present in cases:
|
|
case_dir = tmp_path / name
|
|
live = case_dir / "live"
|
|
txn = case_dir / "txn"
|
|
live.mkdir(parents=True)
|
|
txn.mkdir()
|
|
applied = case_dir / "applied"
|
|
if initially_present:
|
|
for family in ("inet", "bridge", "arp"):
|
|
(live / f"{family}-virt_cloud_vpc_race").write_text(
|
|
f"table {family} virt_cloud_vpc_race {{\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
harness = "\n".join((
|
|
"set -eu",
|
|
f"LIVE={live}",
|
|
f"TXN={txn}",
|
|
f"APPLIED={applied}",
|
|
fake,
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
helpers,
|
|
capture,
|
|
'"$NFT" -c -f "$TXN/batch.nft"',
|
|
precommit,
|
|
'"$NFT" -f "$TXN/batch.nft"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0, name
|
|
assert not applied.exists(), name
|
|
|
|
# Rollback validates both the VPC desired snapshot and the complete Fabric
|
|
# desired snapshot after dry-run. Mutating only Fabric must also prevent
|
|
# the one restore batch from running.
|
|
case_dir = tmp_path / "rollback-fabric"
|
|
live = case_dir / "live"
|
|
txn = case_dir / "txn"
|
|
live.mkdir(parents=True)
|
|
txn.mkdir()
|
|
applied = case_dir / "applied"
|
|
for family in ("inet", "bridge", "arp"):
|
|
(live / f"{family}-virt_cloud_vpc_race").write_text(
|
|
f"table {family} virt_cloud_vpc_race {{\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
(live / "inet-virt_cloud_wg_fabric").write_text(
|
|
"table inet virt_cloud_wg_fabric {\n"
|
|
" chain forward { policy drop; }\n}\n"
|
|
)
|
|
fabric_harness = "\n".join((
|
|
"set -eu",
|
|
f"LIVE={live}",
|
|
f"TXN={txn}",
|
|
f"APPLIED={applied}",
|
|
fake,
|
|
"RACE_TABLE=virt_cloud_wg_fabric",
|
|
NFT_LIVE_SNAPSHOT_SH,
|
|
apply_helpers,
|
|
'vc_vpc_capture_nft_precommit "$TXN/vpc.desired"',
|
|
('vc_nft_table_snapshot inet virt_cloud_wg_fabric '
|
|
'"$TXN/fabric.desired"'),
|
|
'"$NFT" -c -f "$TXN/rollback.nft"',
|
|
'vc_vpc_nft_matches_snapshot "$TXN/vpc.desired"',
|
|
('vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric '
|
|
'"$TXN/fabric.desired"'),
|
|
'"$NFT" -f "$TXN/rollback.nft"',
|
|
))
|
|
rejected = subprocess.run(
|
|
["/bin/sh", "-eu", "-c", fabric_harness], capture_output=True,
|
|
text=True, check=False,
|
|
)
|
|
assert rejected.returncode != 0, rejected.stderr
|
|
assert not applied.exists()
|
|
|
|
|
|
|
|
|
|
|
|
def test_adapter_normalizes_legacy_test_contract_but_rejects_unknown_mode():
|
|
adapter = VpcPrivateHostAdapter(object(), {})
|
|
adapter._deployment_contract = lambda: (
|
|
"ifupdown", "/usr/sbin/ifup", "/usr/sbin/ifdown",
|
|
"/usr/sbin/ifquery",
|
|
)
|
|
assert adapter._render_contract()[-1] == FIREWALL_MODE_NATIVE
|
|
adapter._deployment_contract = lambda: (
|
|
"ifupdown", "/usr/sbin/ifup", "/usr/sbin/ifdown",
|
|
"/usr/sbin/ifquery", "foreign",
|
|
)
|
|
with pytest.raises(VpcDataplaneError):
|
|
adapter._render_contract()
|
|
|
|
|
|
def test_adapter_reports_firewall_mode_conflict_separately(monkeypatch):
|
|
from virsh_panel import fabric
|
|
|
|
monkeypatch.setattr(
|
|
fabric, "_profile_network_mode",
|
|
lambda _profile: "networkmanager_nmcli")
|
|
|
|
def reject_firewall(_profile):
|
|
raise fabric.FabricError(
|
|
"conflict", 409, "fabric_host_firewall_mode_conflict")
|
|
|
|
monkeypatch.setattr(fabric, "_profile_firewall_mode", reject_firewall)
|
|
adapter = VpcPrivateHostAdapter(object(), {})
|
|
with pytest.raises(VpcDataplaneError) as rejected:
|
|
adapter._deployment_contract()
|
|
assert rejected.value.error_code == "vpc_dataplane_firewall_mode_conflict"
|