fix: restore preserved VPC bridges before routes
parent
68775a30cd
commit
8172a3f3be
@ -0,0 +1,328 @@
|
||||
"""Prove a live VPC firewall protects a preserved bridge before it opens."""
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from test_vpc_nft_dataplane import _plan
|
||||
from virsh_panel.fabric import _BOOT_HELPER
|
||||
from virsh_panel.vpc import parse_vpc_plan
|
||||
from virsh_panel.vpc_dataplane import render_vpc_nft_rules
|
||||
|
||||
|
||||
SHELLS = [path for path in ("/bin/sh", "/bin/dash", "/bin/bash")
|
||||
if Path(path).exists()]
|
||||
GUARDS = (
|
||||
"anti-spoof-local-ip",
|
||||
"anti-spoof-public-to-vpc-bridge",
|
||||
"anti-spoof-public-to-vpc-fabric",
|
||||
"anti-spoof-bridge-vpc-to-foreign",
|
||||
"anti-spoof-fabric-vpc-to-foreign",
|
||||
"member-egress-isolation",
|
||||
)
|
||||
BOUNDED_SETS = ("member_ips", "local_member_ips", "vpc_dest_ips")
|
||||
|
||||
|
||||
def _listing(plan=None):
|
||||
plan = _plan() if plan is None else plan
|
||||
rules = render_vpc_nft_rules(plan)
|
||||
start = rules.index(f"table inet {plan.resources.nft_table} {{")
|
||||
return rules[start:rules.index("\n}\n", start) + 3]
|
||||
|
||||
|
||||
def _guard_line(listing, name):
|
||||
return next(line for line in listing.splitlines()
|
||||
if f":{name}\"" in line)
|
||||
|
||||
|
||||
def _set_line(listing, name):
|
||||
return next(line for line in listing.splitlines()
|
||||
if line.startswith(f" set {name} {{"))
|
||||
|
||||
|
||||
def _normalized_listing(listing=None):
|
||||
# nft's stateless live serializer uses tabs, multiline sets, and a
|
||||
# separate base-chain declaration. Keep all generated rules unchanged.
|
||||
listing = _listing() if listing is None else listing
|
||||
listing = re.sub(r"\b((?:[0-9]{1,3}\.){3}[0-9]{1,3})/32\b", r"\1", listing)
|
||||
for protocol in ("tcp", "udp"):
|
||||
listing = listing.replace(f"ip protocol {protocol} {protocol} dport",
|
||||
f"{protocol} dport")
|
||||
lines = []
|
||||
for line in listing.splitlines():
|
||||
set_line = re.fullmatch(r" set (\w+) \{ (.*) \}", line)
|
||||
chain = re.fullmatch(r" chain (\w+) \{ (.*)", line)
|
||||
if set_line:
|
||||
lines.append("\tset " + set_line[1] + " {")
|
||||
lines.extend("\t\t" + item.strip() + ";"
|
||||
for item in set_line[2].split(";") if item.strip())
|
||||
lines.append("\t}")
|
||||
elif chain:
|
||||
lines.extend(("\tchain " + chain[1] + " {",
|
||||
"\t\t" + chain[2]))
|
||||
elif line.startswith(" "):
|
||||
lines.append("\t\t" + line.strip())
|
||||
elif line.startswith(" "):
|
||||
lines.append("\t" + line.strip())
|
||||
else:
|
||||
lines.append(line)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
def _check(tmp_path, shell, listing, *, nft_status=0):
|
||||
plan = _plan()
|
||||
nft = tmp_path / "nft"
|
||||
calls = tmp_path / "calls"
|
||||
nft.write_text(
|
||||
"#!/bin/sh\nset -eu\n"
|
||||
f"printf '%s\\n' \"$*\" >> {shlex.quote(str(calls))}\n"
|
||||
f"[ \"$*\" = '-s list table inet {plan.resources.nft_table}' ] || exit 90\n"
|
||||
"printf '%s' " + shlex.quote(listing) + "\n"
|
||||
f"exit {nft_status}\n")
|
||||
nft.chmod(0o700)
|
||||
start = _BOOT_HELPER.index("vc_vpc_preserved_isolation_ready() {")
|
||||
function = _BOOT_HELPER[start:_BOOT_HELPER.index("\n}\n", start) + 3]
|
||||
script = "\n".join((
|
||||
"set -eu",
|
||||
"NFT=" + shlex.quote(str(nft)),
|
||||
"AWK=awk",
|
||||
function,
|
||||
"vc_vpc_preserved_isolation_ready "
|
||||
+ " ".join(shlex.quote(value) for value in
|
||||
(plan.resources.bridge, plan.vpc.cidr,
|
||||
plan.resources.nft_table)) + " || exit 71",
|
||||
"printf 'ready\\n'",
|
||||
))
|
||||
result = subprocess.run([shell, "-s"], input=script,
|
||||
text=True, capture_output=True)
|
||||
assert calls.read_text().splitlines() == [
|
||||
f"-s list table inet {plan.resources.nft_table}"]
|
||||
return result
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("style", ["rendered", "normalized"])
|
||||
def test_complete_live_vpc_isolation_allows_preserved_activation(tmp_path, shell, style):
|
||||
listing = _listing() if style == "rendered" else _normalized_listing()
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == "ready\n"
|
||||
|
||||
|
||||
def _acl_plan():
|
||||
plan = _plan()
|
||||
payload = plan.vpc.canonical()
|
||||
payload["acls"] = [
|
||||
{"acl_id": "https", "direction": "ingress", "action": "allow",
|
||||
"protocol": "tcp", "source_cidr": "10.44.0.0/24",
|
||||
"destination_cidr": "10.44.0.10/32", "port_from": 443,
|
||||
"port_to": 443, "priority": 1},
|
||||
{"acl_id": "udp-range", "direction": "egress", "action": "allow",
|
||||
"protocol": "udp", "source_cidr": "10.44.0.10/32",
|
||||
"destination_cidr": "10.44.0.11/32", "port_from": 10000,
|
||||
"port_to": 10100, "priority": 2},
|
||||
{"acl_id": "icmp", "direction": "ingress", "action": "allow",
|
||||
"protocol": "icmp", "source_cidr": "10.44.0.0/24",
|
||||
"destination_cidr": "10.44.0.0/24", "priority": 3},
|
||||
{"acl_id": "deny-rest", "direction": "egress", "action": "deny",
|
||||
"protocol": "any", "source_cidr": "10.44.0.0/24",
|
||||
"destination_cidr": "10.44.0.0/24", "priority": 4},
|
||||
]
|
||||
return replace(plan, vpc=parse_vpc_plan(payload))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("style", ["rendered", "normalized"])
|
||||
@pytest.mark.parametrize("counters", [False, True])
|
||||
def test_generated_port_icmp_and_multiple_acls_remain_supported(tmp_path, shell, style, counters):
|
||||
listing = _listing(_acl_plan())
|
||||
assert "tcp dport 443" in listing
|
||||
assert "udp dport 10000-10100" in listing
|
||||
assert "ip protocol icmp" in listing
|
||||
if counters:
|
||||
listing = re.sub(r'(accept|drop) comment ("[^"]*:acl:[^"]*")',
|
||||
r'counter \1 comment \2', listing)
|
||||
if style == "normalized":
|
||||
listing = _normalized_listing(listing)
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == "ready\n"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("name", BOUNDED_SETS)
|
||||
@pytest.mark.parametrize("mutation", ["missing", "duplicate"])
|
||||
def test_each_address_set_is_required_exactly_once(tmp_path, shell, name, mutation):
|
||||
listing = _listing()
|
||||
definition = _set_line(listing, name) + "\n"
|
||||
listing = listing.replace(definition, "" if mutation == "missing"
|
||||
else definition + definition)
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("name", BOUNDED_SETS)
|
||||
@pytest.mark.parametrize("address", ["0.0.0.0/0", "203.0.113.7", "10.45.0.10/32"])
|
||||
def test_every_referenced_address_set_stays_within_the_vpc(tmp_path, shell, name, address):
|
||||
listing = _listing()
|
||||
definition = _set_line(listing, name)
|
||||
altered = re.sub(r"elements = \{[^}]*\}",
|
||||
"elements = { " + address + " }", definition)
|
||||
assert altered != definition
|
||||
result = _check(tmp_path, shell, listing.replace(definition, altered))
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("body", [
|
||||
"type ipv4_addr; flags interval; elements = { 10.44.0.10-10.44.0.11 };",
|
||||
"type ipv4_addr; flags interval; elements = { 10.44.0.0/24 };",
|
||||
"type ipv4_addr; flags interval; elements = { 10.44.0.999 };",
|
||||
"type ipv6_addr; elements = { 10.44.0.10 };",
|
||||
"type ipv4_addr; timeout 60s; elements = { 10.44.0.10 };",
|
||||
"type ipv4_addr; flags dynamic; elements = { 10.44.0.10 };",
|
||||
"type ipv4_addr; type ipv4_addr; elements = { 10.44.0.10 };",
|
||||
"elements = { 10.44.0.10 };",
|
||||
])
|
||||
def test_address_set_parser_rejects_non_generated_metadata_and_elements(tmp_path, shell, body):
|
||||
listing = _listing()
|
||||
name = "vpc_dest_ips"
|
||||
replacement = f" set {name} {{ {body} }}"
|
||||
result = _check(tmp_path, shell,
|
||||
listing.replace(_set_line(listing, name), replacement))
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("semicolons", [False, True])
|
||||
@pytest.mark.parametrize("interval", [False, True])
|
||||
@pytest.mark.parametrize("empty", [False, True])
|
||||
def test_multiline_address_sets_accept_optional_interval_and_listing_semicolons(
|
||||
tmp_path, shell, semicolons, interval, empty):
|
||||
listing = _listing()
|
||||
separator = ";" if semicolons else ""
|
||||
for name in BOUNDED_SETS:
|
||||
lines = [f"\tset {name} {{", "\t\ttype ipv4_addr" + separator]
|
||||
if interval:
|
||||
lines.append("\t\tflags interval" + separator)
|
||||
if not empty:
|
||||
lines.extend(("\t\telements = {", "\t\t\t10.44.0.10/32,",
|
||||
"\t\t\t10.44.0.11", "\t\t}" + separator))
|
||||
lines.append("\t}")
|
||||
listing = listing.replace(_set_line(listing, name), "\n".join(lines))
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 0, result.stderr
|
||||
assert result.stdout == "ready\n"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("position", ["before", "after"])
|
||||
@pytest.mark.parametrize("effect", [
|
||||
"ip daddr set 203.0.113.7",
|
||||
'dup to 203.0.113.7 device "br0"',
|
||||
'fwd to "br0"',
|
||||
"meta mark set 0x1",
|
||||
"ip daddr set ip daddr map @destination_map",
|
||||
"ip saddr vmap @verdict_map",
|
||||
"tcp dport set 4444",
|
||||
"flow add @fastpath",
|
||||
])
|
||||
def test_unmarked_side_effects_are_rejected_on_both_sides_of_guards(tmp_path, shell, position, effect):
|
||||
listing = _listing()
|
||||
anchor = _guard_line(listing, GUARDS[0] if position == "before" else GUARDS[-1])
|
||||
injected = " " + effect
|
||||
replacement = (injected + "\n" + anchor if position == "before"
|
||||
else anchor + "\n" + injected)
|
||||
result = _check(tmp_path, shell, listing.replace(anchor, replacement))
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("name", GUARDS)
|
||||
@pytest.mark.parametrize("mutation", ["missing", "duplicate"])
|
||||
def test_each_boundary_rule_is_required_exactly_once(tmp_path, shell, name, mutation):
|
||||
listing = _listing()
|
||||
rule = _guard_line(listing, name) + "\n"
|
||||
listing = listing.replace(rule, "" if mutation == "missing" else rule + rule)
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("mutation", [
|
||||
"bridge", "fabric", "cidr", "source_set", "destination_set", "verdict",
|
||||
])
|
||||
def test_altered_boundary_cannot_authorize_activation(tmp_path, shell, mutation):
|
||||
plan = _plan()
|
||||
listing = _listing()
|
||||
name = ("member-egress-isolation" if mutation in
|
||||
{"source_set", "destination_set"} else GUARDS[2])
|
||||
rule = _guard_line(listing, name)
|
||||
altered = {
|
||||
"bridge": lambda: rule.replace(plan.resources.bridge, "vcv-foreign", 1),
|
||||
"fabric": lambda: rule.replace('"wg-fabric0"', '"wg-other"', 1),
|
||||
"cidr": lambda: rule.replace(plan.vpc.cidr, "10.45.0.0/24", 1),
|
||||
"source_set": lambda: rule.replace("@local_member_ips", "@member_ips", 1),
|
||||
"destination_set": lambda: rule.replace("@vpc_dest_ips", "@member_ips", 1),
|
||||
"verdict": lambda: rule.replace(" drop comment", " accept comment", 1),
|
||||
}[mutation]()
|
||||
assert altered != rule
|
||||
result = _check(tmp_path, shell, listing.replace(rule, altered))
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("verdict", ["accept", "jump delegated", "goto delegated", "return"])
|
||||
@pytest.mark.parametrize("before", [GUARDS[0], GUARDS[-1]])
|
||||
def test_early_control_flow_cannot_skip_the_complete_boundary(tmp_path, shell, verdict, before):
|
||||
listing = _listing()
|
||||
rule = _guard_line(listing, before)
|
||||
listing = listing.replace(rule, " " + verdict + "\n" + rule)
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("declaration", [
|
||||
"type filter hook input priority -190; policy accept;",
|
||||
"type filter hook forward priority -191; policy accept;",
|
||||
"type filter hook forward priority -190; policy drop;",
|
||||
"type filter hook forward priority -190;",
|
||||
])
|
||||
def test_forward_hook_priority_and_policy_are_part_of_the_proof(tmp_path, shell, declaration):
|
||||
listing = _listing().replace(
|
||||
"type filter hook forward priority -190; policy accept;", declaration)
|
||||
result = _check(tmp_path, shell, listing)
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shell", SHELLS)
|
||||
@pytest.mark.parametrize("mutation", ["chain", "table", "family", "empty", "read_error"])
|
||||
def test_unavailable_or_wrong_live_table_cannot_authorize_activation(tmp_path, shell, mutation):
|
||||
plan = _plan()
|
||||
listing = _listing()
|
||||
if mutation == "chain":
|
||||
listing = listing.replace("chain forward {", "chain unrelated {")
|
||||
elif mutation == "table":
|
||||
listing = listing.replace("table inet " + plan.resources.nft_table,
|
||||
"table inet unrelated", 1)
|
||||
elif mutation == "family":
|
||||
listing = listing.replace("table inet ", "table bridge ", 1)
|
||||
elif mutation == "empty":
|
||||
listing = ""
|
||||
result = _check(tmp_path, shell, listing,
|
||||
nft_status=1 if mutation == "read_error" else 0)
|
||||
assert result.returncode == 71, result.stderr
|
||||
assert result.stdout == ""
|
||||
Loading…
Reference in New Issue