|
|
|
|
@ -15,6 +15,7 @@ from virsh_panel.fabric import (
|
|
|
|
|
FIREWALL_MODE_FIREWALLD,
|
|
|
|
|
_apply_script,
|
|
|
|
|
_ifupdown_fragment,
|
|
|
|
|
_nm_profile_template,
|
|
|
|
|
_nft_rules_text,
|
|
|
|
|
_sysctl_text,
|
|
|
|
|
parse_fabric_plan,
|
|
|
|
|
@ -102,6 +103,11 @@ elif name == "ip":
|
|
|
|
|
up.touch()
|
|
|
|
|
elif args == ["link", "set", "dev", "wg-fabric0", "down"]:
|
|
|
|
|
up.unlink(missing_ok=True)
|
|
|
|
|
elif args == ["link", "del", "dev", "wg-fabric0"]:
|
|
|
|
|
present.unlink(missing_ok=True)
|
|
|
|
|
up.unlink(missing_ok=True)
|
|
|
|
|
(root / "proc" / "ipv4" / "conf" / "wg-fabric0" /
|
|
|
|
|
"rp_filter").unlink(missing_ok=True)
|
|
|
|
|
else:
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
elif name == "nft":
|
|
|
|
|
@ -151,15 +157,50 @@ elif name == "firewall-cmd":
|
|
|
|
|
elif "--get-zones" in args:
|
|
|
|
|
print("public trusted")
|
|
|
|
|
elif any(arg.startswith("--query-interface=") for arg in args):
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
zone = root / "runtime-zone"
|
|
|
|
|
wanted = next(arg.split("=", 1)[1] for arg in args
|
|
|
|
|
if arg.startswith("--zone="))
|
|
|
|
|
raise SystemExit(0 if "--permanent" not in args and zone.is_file()
|
|
|
|
|
and zone.read_text().strip() == wanted else 1)
|
|
|
|
|
elif args == ["--zone=trusted", "--remove-interface=wg-fabric0"]:
|
|
|
|
|
(root / "runtime-zone").unlink(missing_ok=True)
|
|
|
|
|
elif any(arg.startswith("--query-rich-rule=") for arg in args):
|
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
else:
|
|
|
|
|
raise SystemExit(2)
|
|
|
|
|
elif name == "nmcli":
|
|
|
|
|
profile = (root / "NetworkManager" / "system-connections" /
|
|
|
|
|
"virt-cloud-wg-fabric0.nmconnection")
|
|
|
|
|
identity = root / "nm-loaded-uuid"
|
|
|
|
|
uuid = identity.read_text().strip() if identity.is_file() else ""
|
|
|
|
|
if args == ["-t", "--escape", "no", "-g", "connection.uuid",
|
|
|
|
|
"connection", "show", "virt-cloud-wg-fabric0"]:
|
|
|
|
|
pass
|
|
|
|
|
if uuid:
|
|
|
|
|
print(uuid)
|
|
|
|
|
elif args == ["-t", "--escape", "no", "-f", "NAME,UUID",
|
|
|
|
|
"connection", "show"] and profile.is_file():
|
|
|
|
|
print("virt-cloud-wg-fabric0:" + uuid)
|
|
|
|
|
elif args == ["-t", "--escape", "no", "-f", "UUID,FILENAME",
|
|
|
|
|
"connection", "show"] and profile.is_file():
|
|
|
|
|
print(uuid + ":" + str(profile))
|
|
|
|
|
elif args == ["-t", "--escape", "no", "-g", "GENERAL.CON-UUID",
|
|
|
|
|
"device", "show", "wg-fabric0"]:
|
|
|
|
|
if (root / "link-present").exists() and not (root / "nm-unmanaged").exists():
|
|
|
|
|
print(uuid)
|
|
|
|
|
elif args == ["-t", "--escape", "no", "-g", "GENERAL.NM-MANAGED",
|
|
|
|
|
"device", "show", "wg-fabric0"]:
|
|
|
|
|
print("no" if (root / "nm-unmanaged").exists() else "yes")
|
|
|
|
|
elif args == ["device", "set", "wg-fabric0", "managed", "no"]:
|
|
|
|
|
(root / "nm-unmanaged").touch()
|
|
|
|
|
elif args == ["-t", "--escape", "no", "-g", "connection.zone",
|
|
|
|
|
"connection", "show", "uuid", uuid] and profile.is_file():
|
|
|
|
|
zone = root / "nm-zone"
|
|
|
|
|
print(zone.read_text().strip() if zone.is_file() else "")
|
|
|
|
|
elif args == ["connection", "modify", "--temporary", "uuid", uuid,
|
|
|
|
|
"connection.zone", ""] and profile.is_file():
|
|
|
|
|
(root / "nm-zone").write_text("")
|
|
|
|
|
elif args == ["connection", "delete", "uuid", uuid] and uuid:
|
|
|
|
|
identity.unlink()
|
|
|
|
|
else:
|
|
|
|
|
raise SystemExit(2)
|
|
|
|
|
elif name in ("wg", "bridge", "flock", "sync", "ifup", "ifquery", "ifdown"):
|
|
|
|
|
@ -255,7 +296,8 @@ def _fixture(tmp_path: Path, mode: str):
|
|
|
|
|
return plan, helper_path, base, runtime, roots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _journal(plan, base: Path, mode: str, helper: Path, root: Path):
|
|
|
|
|
def _journal(plan, base: Path, mode: str, helper: Path, root: Path, *,
|
|
|
|
|
nft_phase: str = "installing"):
|
|
|
|
|
journal = base / ".wg-fabric-txn"
|
|
|
|
|
journal.mkdir(mode=0o700)
|
|
|
|
|
# Read the rendered UUID; the fixture must not invent an independent plan.
|
|
|
|
|
@ -286,7 +328,7 @@ def _journal(plan, base: Path, mode: str, helper: Path, root: Path):
|
|
|
|
|
f"{key}={value}\n" for key, value in metadata.items()
|
|
|
|
|
))
|
|
|
|
|
_write(journal / "state", "ready\n")
|
|
|
|
|
_write(journal / "nft.phase", "installing\n")
|
|
|
|
|
_write(journal / "nft.phase", nft_phase + "\n")
|
|
|
|
|
_write(journal / "nft.table", "")
|
|
|
|
|
for name in ("conf", "owner", "helper", "unit", "repair", "timer",
|
|
|
|
|
"gate_generator", "nm_profile", "ifupdown_fragment",
|
|
|
|
|
@ -298,6 +340,9 @@ def _journal(plan, base: Path, mode: str, helper: Path, root: Path):
|
|
|
|
|
desired = _nft_rules_text(plan.digest)
|
|
|
|
|
_write(journal / "nft_rules.desired", desired)
|
|
|
|
|
_write(root / "tables" / "inet--virt_cloud_wg_fabric", desired)
|
|
|
|
|
if nft_phase == "refreshed":
|
|
|
|
|
_write(journal / "nft.installed", desired)
|
|
|
|
|
_write(journal / "nft.applied", desired)
|
|
|
|
|
return journal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -339,6 +384,148 @@ def test_first_deploy_complete_entry_recovers_absent_generator_and_timer(
|
|
|
|
|
("start", "enable") for call in calls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
|
|
|
|
|
@pytest.mark.parametrize("helper_published", [False, True])
|
|
|
|
|
def test_ready_refreshed_complete_entry_recovers_exact_first_deploy_snapshots(
|
|
|
|
|
tmp_path, mode, helper_published):
|
|
|
|
|
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
|
|
|
|
|
journal = _journal(plan, base, mode, helper, tmp_path,
|
|
|
|
|
nft_phase="refreshed")
|
|
|
|
|
if helper_published:
|
|
|
|
|
# The real first-deploy apply may publish its persistent helper before
|
|
|
|
|
# failing final readback. Recovery must run that helper's full entry,
|
|
|
|
|
# then use the cleanup_helper phase to safely remove its exact image.
|
|
|
|
|
content = helper.read_text(encoding="utf-8")
|
|
|
|
|
_write(journal / "helper.desired", content, 0o700)
|
|
|
|
|
helper = (roots["/usr/local/libexec"] / "virt-cloud-wg-fabric")
|
|
|
|
|
_write(helper, content, 0o700)
|
|
|
|
|
result = _run(helper, tmp_path, (
|
|
|
|
|
"--recover-only" if helper_published else "--recover-current"
|
|
|
|
|
))
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
assert not journal.exists()
|
|
|
|
|
if helper_published:
|
|
|
|
|
assert not helper.exists()
|
|
|
|
|
assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists()
|
|
|
|
|
assert (base / "wg-fabric0.key").read_text(encoding="utf-8") == (
|
|
|
|
|
"test-private-key-snapshot\n")
|
|
|
|
|
assert (runtime / "wg-fabric-emergency.owner").is_file()
|
|
|
|
|
for family in ("inet", "bridge"):
|
|
|
|
|
assert (tmp_path / "tables" /
|
|
|
|
|
f"{family}--virt_cloud_fabric_emergency").is_file()
|
|
|
|
|
calls = [json.loads(line) for line in
|
|
|
|
|
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
|
|
|
|
|
assert any(call[0] == "nft" and call[1] == "-f" for call in calls)
|
|
|
|
|
assert not any(call[0] == "systemctl" and call[1] in
|
|
|
|
|
("start", "enable") for call in calls)
|
|
|
|
|
assert not any(call[:5] == ["ip", "link", "set", "dev", "wg-fabric0"]
|
|
|
|
|
and call[-1] == "up" for call in calls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
|
|
|
|
|
def test_ready_refreshed_persistent_entry_recovers_created_trusted_link(
|
|
|
|
|
tmp_path, mode):
|
|
|
|
|
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
|
|
|
|
|
journal = _journal(plan, base, mode, helper, tmp_path,
|
|
|
|
|
nft_phase="refreshed")
|
|
|
|
|
content = helper.read_text(encoding="utf-8")
|
|
|
|
|
_write(journal / "helper.desired", content, 0o700)
|
|
|
|
|
helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric"
|
|
|
|
|
_write(helper, content, 0o700)
|
|
|
|
|
|
|
|
|
|
def publish(path: Path, name: str, value: str, permissions=0o600):
|
|
|
|
|
_write(journal / (name + ".desired"), value, permissions)
|
|
|
|
|
_write(path, value, permissions)
|
|
|
|
|
|
|
|
|
|
publish(base / "wg-fabric0.owner", "owner", "virt-cloud-wg-fabric-v1\n")
|
|
|
|
|
if mode == "networkmanager_nmcli":
|
|
|
|
|
uuid = re.search(r'^CONNECTION_UUID="([^"]+)"$', content,
|
|
|
|
|
re.MULTILINE).group(1)
|
|
|
|
|
profile = _nm_profile_template(
|
|
|
|
|
plan, plan.nodes[0], uuid, FIREWALL_MODE_FIREWALLD,
|
|
|
|
|
)
|
|
|
|
|
publish(roots["/etc/NetworkManager"] / "system-connections" /
|
|
|
|
|
"virt-cloud-wg-fabric0.nmconnection", "nm_profile", profile)
|
|
|
|
|
_write(tmp_path / "nm-loaded-uuid", uuid + "\n")
|
|
|
|
|
_write(tmp_path / "nm-zone", "trusted\n")
|
|
|
|
|
else:
|
|
|
|
|
fragment = _ifupdown_fragment(plan, plan.nodes[0]).replace(
|
|
|
|
|
"/usr/local/libexec", str(roots["/usr/local/libexec"]),
|
|
|
|
|
)
|
|
|
|
|
publish(roots["/etc/network"] / "interfaces.d" /
|
|
|
|
|
"virt-cloud-wg-fabric0", "ifupdown_fragment", fragment)
|
|
|
|
|
_write(tmp_path / "link-present", "")
|
|
|
|
|
_write(tmp_path / "link-up", "")
|
|
|
|
|
_write(tmp_path / "runtime-zone", "trusted\n")
|
|
|
|
|
rpf = roots["/proc/sys/net/ipv4"] / "conf" / "wg-fabric0" / "rp_filter"
|
|
|
|
|
_write(rpf, "2\n")
|
|
|
|
|
|
|
|
|
|
result = _run(helper, tmp_path, "--recover-only")
|
|
|
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
assert not journal.exists()
|
|
|
|
|
assert not helper.exists()
|
|
|
|
|
assert not (tmp_path / "link-present").exists()
|
|
|
|
|
assert not (tmp_path / "link-up").exists()
|
|
|
|
|
assert not (tmp_path / "runtime-zone").exists()
|
|
|
|
|
assert not rpf.exists()
|
|
|
|
|
assert not (base / "wg-fabric0.owner").exists()
|
|
|
|
|
assert not (tmp_path / "tables" / "inet--virt_cloud_wg_fabric").exists()
|
|
|
|
|
assert (runtime / "wg-fabric-emergency.owner").is_file()
|
|
|
|
|
for family in ("inet", "bridge"):
|
|
|
|
|
assert (tmp_path / "tables" /
|
|
|
|
|
f"{family}--virt_cloud_fabric_emergency").is_file()
|
|
|
|
|
calls = [json.loads(line) for line in
|
|
|
|
|
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
|
|
|
|
|
assert ["ip", "link", "del", "dev", "wg-fabric0"] in calls
|
|
|
|
|
assert ["firewall-cmd", "--zone=trusted",
|
|
|
|
|
"--remove-interface=wg-fabric0"] in calls
|
|
|
|
|
if mode == "networkmanager_nmcli":
|
|
|
|
|
assert ["nmcli", "device", "set", "wg-fabric0", "managed", "no"] in calls
|
|
|
|
|
assert ["nmcli", "connection", "delete", "uuid", uuid] in calls
|
|
|
|
|
assert not any(call[0] == "systemctl" and call[1] in
|
|
|
|
|
("start", "enable") for call in calls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ["networkmanager_nmcli", "ifupdown"])
|
|
|
|
|
@pytest.mark.parametrize("failure", ["live_verdict", "applied_digest", "applied_mode"])
|
|
|
|
|
def test_ready_refreshed_persistent_entry_rejects_unproved_post_images(
|
|
|
|
|
tmp_path, mode, failure):
|
|
|
|
|
plan, helper, base, runtime, roots = _fixture(tmp_path, mode)
|
|
|
|
|
journal = _journal(plan, base, mode, helper, tmp_path,
|
|
|
|
|
nft_phase="refreshed")
|
|
|
|
|
content = helper.read_text(encoding="utf-8")
|
|
|
|
|
_write(journal / "helper.desired", content, 0o700)
|
|
|
|
|
helper = roots["/usr/local/libexec"] / "virt-cloud-wg-fabric"
|
|
|
|
|
_write(helper, content, 0o700)
|
|
|
|
|
live = tmp_path / "tables" / "inet--virt_cloud_wg_fabric"
|
|
|
|
|
if failure == "live_verdict":
|
|
|
|
|
_write(live, live.read_text(encoding="utf-8").replace(
|
|
|
|
|
"counter drop comment", "counter accept comment", 1,
|
|
|
|
|
))
|
|
|
|
|
elif failure == "applied_digest":
|
|
|
|
|
applied = journal / "nft.applied"
|
|
|
|
|
_write(applied, applied.read_text(encoding="utf-8").replace(
|
|
|
|
|
plan.digest, "f" * 64,
|
|
|
|
|
))
|
|
|
|
|
else:
|
|
|
|
|
(journal / "nft.applied").chmod(0o644)
|
|
|
|
|
before = live.read_bytes()
|
|
|
|
|
result = _run(helper, tmp_path, "--recover-only")
|
|
|
|
|
assert result.returncode == 79, result.stderr
|
|
|
|
|
assert journal.is_dir()
|
|
|
|
|
assert helper.read_text(encoding="utf-8") == content
|
|
|
|
|
assert live.read_bytes() == before
|
|
|
|
|
assert (runtime / "wg-fabric-emergency.owner").is_file()
|
|
|
|
|
for family in ("inet", "bridge"):
|
|
|
|
|
assert (tmp_path / "tables" /
|
|
|
|
|
f"{family}--virt_cloud_fabric_emergency").is_file()
|
|
|
|
|
calls = [json.loads(line) for line in
|
|
|
|
|
(tmp_path / "calls").read_text(encoding="utf-8").splitlines()]
|
|
|
|
|
assert not any(call[0] == "nft" and call[1] == "-f" for call in calls)
|
|
|
|
|
assert not any(call[0] == "systemctl" and call[1] in
|
|
|
|
|
("start", "enable") for call in calls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _transition_fixture(tmp_path: Path, gate_state: str):
|
|
|
|
|
plan, helper, base, runtime, roots = _fixture(tmp_path, "ifupdown")
|
|
|
|
|
_write(base / "wg-fabric0.owner", "virt-cloud-wg-fabric-v1\n")
|
|
|
|
|
|