|
|
|
|
@ -65,6 +65,49 @@ _APPLY_RECEIPT_LIMIT = 1024
|
|
|
|
|
_APPLY_RECEIPTS: OrderedDict[tuple[str, ...], tuple[str, str]] = OrderedDict()
|
|
|
|
|
_APPLY_RECEIPT_LOCK = Lock()
|
|
|
|
|
|
|
|
|
|
# The synchronous VPC SSH boundary intentionally discards arbitrary remote
|
|
|
|
|
# stderr because host tools may print paths or configuration. Keep one
|
|
|
|
|
# closed, host-local journal record instead so an apply failure can be placed
|
|
|
|
|
# before or after network activation without exposing command output.
|
|
|
|
|
VPC_APPLY_SAFE_LOG_SH = r'''
|
|
|
|
|
LOGGER=
|
|
|
|
|
for vc_vpc_logger_path in /usr/bin/logger /bin/logger /usr/sbin/logger /sbin/logger; do
|
|
|
|
|
if [ -x "$vc_vpc_logger_path" ]; then LOGGER=$vc_vpc_logger_path; break; fi
|
|
|
|
|
done
|
|
|
|
|
VC_VPC_APPLY_PHASE=entry
|
|
|
|
|
vc_vpc_apply_failure_log() {
|
|
|
|
|
vc_vpc_log_status=$1
|
|
|
|
|
case "$vc_vpc_log_status" in ''|*[!0-9]*) vc_vpc_log_status=1 ;; esac
|
|
|
|
|
[ "$vc_vpc_log_status" -le 255 ] 2>/dev/null || vc_vpc_log_status=1
|
|
|
|
|
[ "$vc_vpc_log_status" != 0 ] || return 0
|
|
|
|
|
case "${VC_VPC_APPLY_PHASE:-}" in
|
|
|
|
|
entry|tools|preflight|transaction_snapshot|quarantine|firewall|bridge|persistence|activation|post_activation_sysctl|runtime|proxy|neighbors|routes|policy_rules|fabric_acl|wireguard|final_readback|commit|rollback)
|
|
|
|
|
vc_vpc_log_phase=$VC_VPC_APPLY_PHASE ;;
|
|
|
|
|
*) vc_vpc_log_phase=unknown ;;
|
|
|
|
|
esac
|
|
|
|
|
[ -n "${LOGGER:-}" ] || return 0
|
|
|
|
|
"$LOGGER" -t virt-cloud-vpc -- \
|
|
|
|
|
"apply_state=failed apply_phase=$vc_vpc_log_phase exit_status=$vc_vpc_log_status" \
|
|
|
|
|
>/dev/null 2>&1 || true
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
vc_vpc_apply_log_exit() {
|
|
|
|
|
vc_vpc_log_exit_status=$?
|
|
|
|
|
trap - EXIT
|
|
|
|
|
vc_vpc_apply_failure_log "$vc_vpc_log_exit_status" || true
|
|
|
|
|
exit "$vc_vpc_log_exit_status"
|
|
|
|
|
}
|
|
|
|
|
vc_vpc_apply_signal_exit() {
|
|
|
|
|
vc_vpc_signal_status=$1
|
|
|
|
|
trap - HUP INT TERM
|
|
|
|
|
exit "$vc_vpc_signal_status"
|
|
|
|
|
}
|
|
|
|
|
trap vc_vpc_apply_log_exit EXIT
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 129' HUP
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 130' INT
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 143' TERM
|
|
|
|
|
'''.strip()
|
|
|
|
|
|
|
|
|
|
# iproute2 prints host routes without /32 and suppresses dev when its query
|
|
|
|
|
# already selected that device. Infer only the explicitly supplied filter;
|
|
|
|
|
# unfiltered inventories must still contain a unique output device. A private
|
|
|
|
|
@ -243,7 +286,7 @@ vc_vpc_neighbor_helper_ready() {
|
|
|
|
|
[ "$(sed -n '3p' "$vc_ln_helper")" = "# fabric-plan-digest=$EXPECTED_DIGEST" ] &&
|
|
|
|
|
[ "$(sed -n '4p' "$vc_ln_helper")" = "PLAN_DIGEST=\"$EXPECTED_DIGEST\"" ] &&
|
|
|
|
|
[ "$(sed -n '6p' "$vc_ln_helper")" = '# vpc-local-neighbor-contract-v1' ] &&
|
|
|
|
|
[ "$(sed -n '7p' "$vc_ln_helper")" = '# vpc-state-route-scope-contract-v3' ]
|
|
|
|
|
[ "$(sed -n '7p' "$vc_ln_helper")" = '# vpc-state-route-scope-contract-v4' ]
|
|
|
|
|
}
|
|
|
|
|
vc_vpc_neighbors_snapshot() {
|
|
|
|
|
vc_ln_snapshot=$(vc_local_neighbor_records "$BRIDGE") || return 1
|
|
|
|
|
@ -933,6 +976,11 @@ class VpcDataplanePlan:
|
|
|
|
|
# separate from ``peers`` because one VPC can use only a subset of the
|
|
|
|
|
# peers present on a shared Fabric.
|
|
|
|
|
fabric_peer_keys: frozenset[str] | None = None
|
|
|
|
|
# Authoritative public-key -> overlay /32 closure from the complete
|
|
|
|
|
# engine-validated Fabric plan. The key-only field remains for legacy
|
|
|
|
|
# callers, but only this mapping can prove and converge cryptokey routing
|
|
|
|
|
# for peers outside this VPC's participant set.
|
|
|
|
|
fabric_peer_overlays: frozenset[tuple[str, str]] | None = None
|
|
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
|
_fabric_plan_digest(self.fabric_plan_digest)
|
|
|
|
|
@ -1033,6 +1081,23 @@ def _key(value: Any) -> str:
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fabric_overlay_cidr(value: Any) -> str:
|
|
|
|
|
try:
|
|
|
|
|
interface = ipaddress.ip_interface(value)
|
|
|
|
|
except (TypeError, ValueError) as exc:
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 地址无效", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid") from exc
|
|
|
|
|
fabric_overlay = ipaddress.IPv4Network("100.64.0.0/10")
|
|
|
|
|
if (not isinstance(interface, ipaddress.IPv4Interface)
|
|
|
|
|
or interface.network.prefixlen != 32
|
|
|
|
|
or interface.ip not in fabric_overlay
|
|
|
|
|
or interface.ip.is_loopback
|
|
|
|
|
or interface.ip.is_link_local):
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 地址无效", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
return f"{interface.ip}/32"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _mac(value: Any, label: str = "私网 MAC") -> str:
|
|
|
|
|
if not isinstance(value, str) or _MAC.fullmatch(value) is None:
|
|
|
|
|
raise VpcDataplaneError(f"{label}无效")
|
|
|
|
|
@ -1252,6 +1317,7 @@ def parse_vpc_dataplane_plan(
|
|
|
|
|
fabric_peers: Sequence[Mapping[str, Any]] | None = None,
|
|
|
|
|
trusted_tenant_allowed_ips: Mapping[str, Sequence[str]] | None = None,
|
|
|
|
|
fabric_peer_keys: Sequence[str] | None = None,
|
|
|
|
|
fabric_peer_overlays: Mapping[str, str] | None = None,
|
|
|
|
|
fabric_plan_digest: str | None = None,
|
|
|
|
|
) -> VpcDataplanePlan:
|
|
|
|
|
"""Parse existing VPC DTO plus an optional engine-only Fabric snapshot."""
|
|
|
|
|
@ -1337,9 +1403,65 @@ def parse_vpc_dataplane_plan(
|
|
|
|
|
raise VpcDataplaneError("VPC peer 不属于 Fabric 公钥闭集", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
authoritative_keys = frozenset(canonical_keys)
|
|
|
|
|
authoritative_overlays: frozenset[tuple[str, str]] | None = None
|
|
|
|
|
if fabric_peer_overlays is not None:
|
|
|
|
|
if (not isinstance(fabric_peer_overlays, Mapping)
|
|
|
|
|
or len(fabric_peer_overlays) > 64):
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 闭集无效", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
canonical_overlays: dict[str, str] = {}
|
|
|
|
|
seen_overlay_cidrs: set[str] = set()
|
|
|
|
|
for raw_key, raw_overlay in fabric_peer_overlays.items():
|
|
|
|
|
key = _key(raw_key)
|
|
|
|
|
overlay_cidr = _fabric_overlay_cidr(raw_overlay)
|
|
|
|
|
if key in canonical_overlays or overlay_cidr in seen_overlay_cidrs:
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 闭集重复", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
canonical_overlays[key] = overlay_cidr
|
|
|
|
|
seen_overlay_cidrs.add(overlay_cidr)
|
|
|
|
|
overlay_keys = frozenset(canonical_overlays)
|
|
|
|
|
if authoritative_keys is not None and overlay_keys != authoritative_keys:
|
|
|
|
|
raise VpcDataplaneError("Fabric peer 公钥与 overlay 闭集不一致", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
for peer in peers:
|
|
|
|
|
if canonical_overlays.get(peer.public_key) != peer.overlay_cidr:
|
|
|
|
|
raise VpcDataplaneError("VPC peer overlay 不属于 Fabric 闭集", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
authoritative_keys = overlay_keys
|
|
|
|
|
authoritative_overlays = frozenset(canonical_overlays.items())
|
|
|
|
|
return VpcDataplanePlan(
|
|
|
|
|
vpc, host_id, tuple(members), peers, resource_names(vpc.vpc_id),
|
|
|
|
|
fabric_plan_digest, authoritative_keys)
|
|
|
|
|
fabric_plan_digest, authoritative_keys, authoritative_overlays)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _authoritative_fabric_peer_overlays(
|
|
|
|
|
plan: VpcDataplanePlan,
|
|
|
|
|
) -> dict[str, str] | None:
|
|
|
|
|
"""Validate and materialize the engine-only full Fabric peer closure."""
|
|
|
|
|
if plan.fabric_peer_overlays is None:
|
|
|
|
|
return None
|
|
|
|
|
mapping: dict[str, str] = {}
|
|
|
|
|
seen_overlays: set[str] = set()
|
|
|
|
|
for row in plan.fabric_peer_overlays:
|
|
|
|
|
if not isinstance(row, tuple) or len(row) != 2:
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 闭集无效", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
key = _key(row[0])
|
|
|
|
|
overlay = _fabric_overlay_cidr(row[1])
|
|
|
|
|
if key in mapping or overlay in seen_overlays:
|
|
|
|
|
raise VpcDataplaneError("Fabric peer overlay 闭集重复", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
mapping[key] = overlay
|
|
|
|
|
seen_overlays.add(overlay)
|
|
|
|
|
if (plan.fabric_peer_keys is None
|
|
|
|
|
or frozenset(mapping) != plan.fabric_peer_keys):
|
|
|
|
|
raise VpcDataplaneError("Fabric peer 公钥与 overlay 闭集不一致", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
for peer in plan.peers:
|
|
|
|
|
if mapping.get(peer.public_key) != peer.overlay_cidr:
|
|
|
|
|
raise VpcDataplaneError("VPC peer overlay 不属于 Fabric 闭集", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
return dict(sorted(mapping.items()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _routes(plan: VpcDataplanePlan) -> tuple[dict[str, Any], ...]:
|
|
|
|
|
@ -1674,11 +1796,18 @@ def _state(
|
|
|
|
|
metric=route.get("metric", 100)))
|
|
|
|
|
for member in sorted(plan.local_members, key=lambda item: item.private_ip):
|
|
|
|
|
lines.append(f"local_neighbor|{member.private_ip}|{member.private_mac}")
|
|
|
|
|
wg_route_ips = {
|
|
|
|
|
str(route["destination_cidr"])
|
|
|
|
|
for route in routes if route["device"] == FABRIC_INTERFACE
|
|
|
|
|
}
|
|
|
|
|
owned_peer_ips = _expected_peer_tenant_ips(plan.vpc)
|
|
|
|
|
for peer in plan.peers:
|
|
|
|
|
ips = sorted(set(
|
|
|
|
|
[m.private_ip + "/32" for m in plan.remote_members
|
|
|
|
|
if m.host_id == peer.host_id]
|
|
|
|
|
+ list(peer.tenant_allowed_ips)))
|
|
|
|
|
# Persist only this VPC's route-backed contribution. The authenticated
|
|
|
|
|
# aggregate remains on ``peer.tenant_allowed_ips`` for live WireGuard
|
|
|
|
|
# convergence/readback, while old aggregate state is filtered by the
|
|
|
|
|
# loader during a mixed-version rollout.
|
|
|
|
|
ips = sorted(set(owned_peer_ips.get(peer.host_id, frozenset()))
|
|
|
|
|
& wg_route_ips)
|
|
|
|
|
if ips:
|
|
|
|
|
lines.append(f"peer|{peer.host_id}|{peer.public_key}|{','.join(ips)}")
|
|
|
|
|
return "\n".join(lines) + "\n"
|
|
|
|
|
@ -1860,9 +1989,36 @@ def _render_apply_script_v2(
|
|
|
|
|
proxy_lines = "\n".join(
|
|
|
|
|
str(ipaddress.ip_network(item["destination_cidr"], strict=True).network_address)
|
|
|
|
|
for item in routes)
|
|
|
|
|
peers_by_key = {peer.public_key: peer for peer in plan.peers}
|
|
|
|
|
authoritative_peer_overlays = _authoritative_fabric_peer_overlays(plan)
|
|
|
|
|
if (authoritative_peer_overlays is None
|
|
|
|
|
and plan.fabric_peer_keys is not None
|
|
|
|
|
and not plan.fabric_peer_keys.issubset(peers_by_key)):
|
|
|
|
|
raise VpcDataplaneError(
|
|
|
|
|
"完整 Fabric peer 收敛缺少权威 overlay 闭集", 409,
|
|
|
|
|
"vpc_fabric_peer_set_invalid")
|
|
|
|
|
# A VPC revision can stop using one Fabric peer while the peer itself
|
|
|
|
|
# remains part of the host's WireGuard Fabric. Converge that full
|
|
|
|
|
# authoritative closure after rewriting durable state, otherwise the
|
|
|
|
|
# removed peer retains the withdrawn tenant /32 indefinitely. Direct
|
|
|
|
|
# parser callers without the engine-only closure retain the historical
|
|
|
|
|
# current-VPC scope; production plans always provide the full key-to-overlay
|
|
|
|
|
# closure.
|
|
|
|
|
apply_fabric_peer_keys = tuple(sorted(
|
|
|
|
|
authoritative_peer_overlays
|
|
|
|
|
if authoritative_peer_overlays is not None
|
|
|
|
|
else (plan.fabric_peer_keys
|
|
|
|
|
if plan.fabric_peer_keys is not None else peers_by_key)
|
|
|
|
|
))
|
|
|
|
|
peer_lines = "\n".join(
|
|
|
|
|
f"{peer.host_id}|{peer.public_key}|{_allowed(plan, peer)}"
|
|
|
|
|
for peer in plan.peers)
|
|
|
|
|
f"{peers_by_key[key].host_id if key in peers_by_key else '-'}|"
|
|
|
|
|
f"{key}|{authoritative_peer_overlays[key]}"
|
|
|
|
|
if authoritative_peer_overlays is not None
|
|
|
|
|
else f"{peers_by_key[key].host_id}|{key}|{peers_by_key[key].overlay_cidr}"
|
|
|
|
|
for key in apply_fabric_peer_keys)
|
|
|
|
|
apply_fabric_peer_key_words = " ".join(apply_fabric_peer_keys)
|
|
|
|
|
apply_fabric_peer_keys_provided = (
|
|
|
|
|
"yes" if authoritative_peer_overlays is not None else "no")
|
|
|
|
|
state = _state(plan, routes, firewall_mode)
|
|
|
|
|
# Local service /32s use a guest address as the next hop on an otherwise
|
|
|
|
|
# unnumbered private bridge. Linux therefore needs the explicit `onlink`
|
|
|
|
|
@ -1965,6 +2121,7 @@ set -eu
|
|
|
|
|
umask 077
|
|
|
|
|
PATH=/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
|
export PATH
|
|
|
|
|
{VPC_APPLY_SAFE_LOG_SH}
|
|
|
|
|
IP=$(for p in /usr/sbin/ip /usr/bin/ip /sbin/ip /bin/ip; do [ -x "$p" ] && {{ printf '%s' "$p"; break; }}; done)
|
|
|
|
|
WG=$(for p in /usr/bin/wg /usr/sbin/wg /bin/wg /sbin/wg; do [ -x "$p" ] && {{ printf '%s' "$p"; break; }}; done)
|
|
|
|
|
NFT=$(for p in /usr/sbin/nft /usr/bin/nft /sbin/nft /bin/nft; do [ -x "$p" ] && {{ printf '%s' "$p"; break; }}; done)
|
|
|
|
|
@ -2009,12 +2166,14 @@ nft_plan_owned() {{
|
|
|
|
|
[ "$(printf '%s\\n' "$nft_plan_text" |
|
|
|
|
|
grep -Foc 'comment "virt-cloud-wg-fabric-v1:'"$nft_plan_digest"'"' || true)" = 8 ]
|
|
|
|
|
}}
|
|
|
|
|
VC_VPC_APPLY_PHASE=tools
|
|
|
|
|
[ -n "$IP" ] && [ -n "$WG" ] && [ -n "$NFT" ] && [ -n "$FLOCK" ] && [ -n "$STAT" ] && [ -n "$CMP" ] && [ -n "$SYSCTL" ] && [ -n "$BRIDGE_CMD" ] && [ -n "$SYSTEMCTL" ] || exit 70
|
|
|
|
|
if [ "$MODE" = networkmanager_nmcli ]; then
|
|
|
|
|
NMCLI=$(for p in /usr/bin/nmcli /usr/sbin/nmcli /bin/nmcli /sbin/nmcli; do [ -x "$p" ] && {{ printf '%s' "$p"; break; }}; done)
|
|
|
|
|
[ -n "$NMCLI" ] || exit 70
|
|
|
|
|
fi
|
|
|
|
|
[ "$FIREWALL_MODE" = native_nftables ] || exit 71
|
|
|
|
|
VC_VPC_APPLY_PHASE=preflight
|
|
|
|
|
if [ -L /run/virt-cloud ] || {{ [ -e /run/virt-cloud ] && [ ! -d /run/virt-cloud ]; }}; then exit 71; fi
|
|
|
|
|
if [ -L /etc/virt-cloud ] || {{ [ -e /etc/virt-cloud ] && [ ! -d /etc/virt-cloud ]; }}; then exit 71; fi
|
|
|
|
|
if [ -L {state_dir} ] || {{ [ -e {state_dir} ] && [ ! -d {state_dir} ]; }}; then exit 71; fi
|
|
|
|
|
@ -2280,13 +2439,17 @@ fi
|
|
|
|
|
vc_ip_rule_scope_check {_q(plan.vpc.cidr)} "$ROUTE_TABLE" "$RULE_PRIORITY" "$WG_IFACE" managed 73 73 || exit $?
|
|
|
|
|
# Read-only preflight leaves the original ACL and link intact. The journal
|
|
|
|
|
# must contain the runtime baseline before quarantine can flush kernel state.
|
|
|
|
|
VC_VPC_APPLY_PHASE=transaction_snapshot
|
|
|
|
|
mkdir -m 700 "$TXN"
|
|
|
|
|
vc_vpc_write_native_transaction_mode || exit 79
|
|
|
|
|
printf '%s\\n' '{DATAPLANE_OWNER_MARKER}:{plan.vpc.vpc_id}:{plan.host_id}:{plan.desired_revision}:{plan.desired_digest}' > "$TXN/inflight"
|
|
|
|
|
chmod 600 "$TXN/inflight"
|
|
|
|
|
printf '%s\\n' preparing > "$TXN/state"; chmod 600 "$TXN/state"
|
|
|
|
|
rollback() {{
|
|
|
|
|
status=$?; [ "$COMMITTED" = yes ] && exit "$status"; trap - EXIT HUP INT TERM
|
|
|
|
|
status=$?
|
|
|
|
|
trap - EXIT HUP INT TERM
|
|
|
|
|
vc_vpc_apply_failure_log "$status" || true
|
|
|
|
|
[ "$COMMITTED" = yes ] && exit "$status"
|
|
|
|
|
set +e
|
|
|
|
|
if [ "$MUTATION_STARTED" = no ]; then
|
|
|
|
|
# Snapshot/preflight failure has not touched any dataplane resource.
|
|
|
|
|
@ -2679,10 +2842,17 @@ rollback() {{
|
|
|
|
|
fi
|
|
|
|
|
"$IP" link set dev "$BRIDGE" down >/dev/null 2>&1 || true
|
|
|
|
|
fi
|
|
|
|
|
if [ "$ROLLBACK_FAILED" = yes ]; then printf '%s\\n' rollback_failed > "$TXN/state"; chmod 600 "$TXN/state"; exit 79; fi
|
|
|
|
|
if [ "$ROLLBACK_FAILED" = yes ]; then
|
|
|
|
|
VC_VPC_APPLY_PHASE=rollback
|
|
|
|
|
vc_vpc_apply_failure_log 79 || true
|
|
|
|
|
printf '%s\\n' rollback_failed > "$TXN/state"; chmod 600 "$TXN/state"; exit 79
|
|
|
|
|
fi
|
|
|
|
|
rm -rf "$TXN"; exit "$status"
|
|
|
|
|
}}
|
|
|
|
|
trap rollback EXIT HUP INT TERM
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 129' HUP
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 130' INT
|
|
|
|
|
trap 'vc_vpc_apply_signal_exit 143' TERM
|
|
|
|
|
if [ -f "$OWNER_FILE" ]; then
|
|
|
|
|
[ ! -L "$OWNER_FILE" ] && grep -Fqx "$OWNER_MARKER" "$OWNER_FILE" || exit 71
|
|
|
|
|
cp -p "$OWNER_FILE" "$TXN/owner.before"
|
|
|
|
|
@ -2734,6 +2904,7 @@ if [ "$BRIDGE_TOPOLOGY_CAPTURED" = yes ]; then
|
|
|
|
|
# Kernel DOWN and NM unmanaged can flush proxy neighbours and routes. The
|
|
|
|
|
# original ACL, owner, manager, topology and runtime are now journaled.
|
|
|
|
|
vc_vpc_bridge_topology_matches || exit 72
|
|
|
|
|
VC_VPC_APPLY_PHASE=quarantine
|
|
|
|
|
MUTATION_STARTED=yes
|
|
|
|
|
RUNTIME_QUARANTINED=yes
|
|
|
|
|
if [ "$MODE" = networkmanager_nmcli ]; then
|
|
|
|
|
@ -2795,8 +2966,10 @@ vc_vpc_nft_matches_snapshot "$TXN/nft.desired" || exit 72
|
|
|
|
|
}}
|
|
|
|
|
# Install and exactly read back the raw tenant guard before restoring link
|
|
|
|
|
# administration in either persistence mode.
|
|
|
|
|
VC_VPC_APPLY_PHASE=firewall
|
|
|
|
|
MUTATION_STARTED=yes
|
|
|
|
|
vc_vpc_install_rules
|
|
|
|
|
VC_VPC_APPLY_PHASE=bridge
|
|
|
|
|
if "$IP" link show dev "$BRIDGE" >/dev/null 2>&1; then
|
|
|
|
|
"$IP" -d link show dev "$BRIDGE" | grep -Eq '(^|[[:space:]])bridge[[:space:]]+(forward_delay|stp_state|hello_time|ageing_time)([[:space:]]|$)' || exit 71
|
|
|
|
|
[ -f "$OWNER_FILE" ] || exit 71
|
|
|
|
|
@ -2884,6 +3057,7 @@ for vc_runtime_key in proxy_arp proxy_arp_pvlan; do
|
|
|
|
|
cat "$vc_runtime_path" > "$TXN/$vc_runtime_key.before" || exit 72
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
VC_VPC_APPLY_PHASE=persistence
|
|
|
|
|
if [ "$MODE" = networkmanager_nmcli ]; then
|
|
|
|
|
[ -n "$NMCLI" ] || exit 70
|
|
|
|
|
NM_PROFILE_UUID=
|
|
|
|
|
@ -2910,11 +3084,13 @@ if [ "$MODE" = networkmanager_nmcli ]; then
|
|
|
|
|
vc_nm_device_release || exit 72
|
|
|
|
|
NM_DEVICE_QUARANTINED=no
|
|
|
|
|
fi
|
|
|
|
|
VC_VPC_APPLY_PHASE=activation
|
|
|
|
|
if [ "$NM_PRESERVE_ACTIVE_BRIDGE" = yes ]; then
|
|
|
|
|
vc_nm_preserve_bridge_ready || exit 72
|
|
|
|
|
else
|
|
|
|
|
"$NMCLI" connection up uuid "$NM_PROFILE_UUID" >/dev/null
|
|
|
|
|
fi
|
|
|
|
|
VC_VPC_APPLY_PHASE=post_activation_sysctl
|
|
|
|
|
vc_vpc_rp_filter_set || exit 72
|
|
|
|
|
else
|
|
|
|
|
[ -x "$IFUP" ] && [ -x "$IFQUERY" ] && [ -x "$IFDOWN" ] || exit 70
|
|
|
|
|
@ -2949,9 +3125,12 @@ else
|
|
|
|
|
# have acquired a libvirt tap before a later step fails. The rollback path
|
|
|
|
|
# below inspects ports first, then deletes only an empty newly-created
|
|
|
|
|
# bridge or restores an empty existing bridge to its sampled DOWN state.
|
|
|
|
|
VC_VPC_APPLY_PHASE=activation
|
|
|
|
|
VC_FABRIC_APPLYING=1 "$IFUP" --force "$BRIDGE" >/dev/null 2>&1 || exit 72
|
|
|
|
|
VC_VPC_APPLY_PHASE=post_activation_sysctl
|
|
|
|
|
vc_vpc_rp_filter_set || exit 72
|
|
|
|
|
fi
|
|
|
|
|
VC_VPC_APPLY_PHASE=runtime
|
|
|
|
|
if [ "$BRIDGE_TOPOLOGY_CAPTURED" = yes ]; then
|
|
|
|
|
vc_vpc_bridge_topology_matches || exit 72
|
|
|
|
|
fi
|
|
|
|
|
@ -2961,6 +3140,7 @@ printf '%s\\n' 0 > "/proc/sys/net/ipv4/conf/$BRIDGE/proxy_arp" || exit 72
|
|
|
|
|
PROXY_ARP_PVLAN_CHANGED=yes
|
|
|
|
|
printf '%s\\n' 1 > "/proc/sys/net/ipv4/conf/$BRIDGE/proxy_arp_pvlan" || exit 72
|
|
|
|
|
[ "$(cat "/proc/sys/net/ipv4/conf/$BRIDGE/proxy_arp_pvlan" 2>/dev/null)" = 1 ] || exit 72
|
|
|
|
|
VC_VPC_APPLY_PHASE=proxy
|
|
|
|
|
vc_vpc_proxy_snapshot "$TXN/proxy.activation.current" || exit 72
|
|
|
|
|
vc_vpc_runtime_subset "$TXN/proxy.activation.current" "$TXN/proxy.before" || exit 71
|
|
|
|
|
PROXY_ENTRIES_CHANGED=yes
|
|
|
|
|
@ -2989,7 +3169,9 @@ cat <<'{proxy_marker}' | LC_ALL=C sort -u > "$TXN/proxy.desired"
|
|
|
|
|
{proxy_marker}
|
|
|
|
|
"$CMP" -s "$TXN/proxy.after" "$TXN/proxy.desired" || exit 72
|
|
|
|
|
# Restore exact member bindings after the manager may have cleared them.
|
|
|
|
|
VC_VPC_APPLY_PHASE=neighbors
|
|
|
|
|
vc_vpc_neighbors_reconcile || exit 72
|
|
|
|
|
VC_VPC_APPLY_PHASE=routes
|
|
|
|
|
ROUTES_CHANGED=yes
|
|
|
|
|
cat > "$TXN/routes.desired" <<'{route_marker}'
|
|
|
|
|
{route_lines}
|
|
|
|
|
@ -3039,6 +3221,7 @@ done <<'{route_marker}'
|
|
|
|
|
# Recheck the complete RPDB scope after route convergence, immediately before
|
|
|
|
|
# adding missing rules. An out-of-band rule created during the transaction
|
|
|
|
|
# must force rollback rather than make a successful apply read back degraded.
|
|
|
|
|
VC_VPC_APPLY_PHASE=policy_rules
|
|
|
|
|
# vc-policy-rule-preflight-begin
|
|
|
|
|
: > "$TXN/rules.before"
|
|
|
|
|
: > "$TXN/rules.attempted"
|
|
|
|
|
@ -3052,6 +3235,7 @@ vc_ip_rule_preflight "$WG_RULE_PRIORITY" "$WG_IFACE" {_q(plan.vpc.cidr)} "$ROUTE
|
|
|
|
|
vc_ip_rule_ensure "$RULE_PRIORITY" - {_q(plan.vpc.cidr)} "$ROUTE_TABLE" 73 73 "$TXN/rules.attempted" "$TXN/rules.created" || exit $?
|
|
|
|
|
vc_ip_rule_ensure "$WG_RULE_PRIORITY" "$WG_IFACE" {_q(plan.vpc.cidr)} "$ROUTE_TABLE" 73 73 "$TXN/rules.attempted" "$TXN/rules.created" || exit $?
|
|
|
|
|
# vc-policy-rule-ensure-end
|
|
|
|
|
VC_VPC_APPLY_PHASE=fabric_acl
|
|
|
|
|
cat > "$STATE_FILE" <<'{state_marker}'
|
|
|
|
|
{state.rstrip()}
|
|
|
|
|
{state_marker}
|
|
|
|
|
@ -3068,23 +3252,54 @@ vc_nft_table_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
[ ! -e "$TXN/fabric.nft.desired.absent" ] || exit 74
|
|
|
|
|
vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
"$TXN/fabric.nft.desired" || exit 74
|
|
|
|
|
while IFS='|' read -r peer_host key allowed; do
|
|
|
|
|
VC_VPC_APPLY_PHASE=wireguard
|
|
|
|
|
expected_fabric_peer_keys={_q(apply_fabric_peer_key_words)}
|
|
|
|
|
if [ "{apply_fabric_peer_keys_provided}" = yes ]; then
|
|
|
|
|
live_fabric_peer_keys=$("$WG" show "$WG_IFACE" peers 2>/dev/null) || exit 74
|
|
|
|
|
for live_peer_key in $live_fabric_peer_keys; do
|
|
|
|
|
case " $expected_fabric_peer_keys " in
|
|
|
|
|
*" $live_peer_key "*) : ;;
|
|
|
|
|
*) exit 74 ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
for expected_peer_key in $expected_fabric_peer_keys; do
|
|
|
|
|
printf '%s\n' "$live_fabric_peer_keys" |
|
|
|
|
|
grep -Fqx "$expected_peer_key" || exit 74
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
while IFS='|' read -r peer_host key expected_overlay; do
|
|
|
|
|
[ -n "$peer_host" ] || continue
|
|
|
|
|
"$WG" show "$WG_IFACE" peers | grep -Fqx "$key" || exit 74
|
|
|
|
|
case ",$allowed," in *',0.0.0.0/0,'*|*',0.0.0.0/1,'*|*',128.0.0.0/1,'* ) exit 74 ;; esac
|
|
|
|
|
case ",$expected_overlay," in *',0.0.0.0/0,'*|*',0.0.0.0/1,'*|*',128.0.0.0/1,'* ) exit 74 ;; esac
|
|
|
|
|
# Fabric has one AllowedIPs list per public key. Merge the complete,
|
|
|
|
|
# validated tenant set from every panel-owned VPC state file while keeping
|
|
|
|
|
# the peer's single overlay /32. Never replace the list with this VPC's
|
|
|
|
|
# partial view: doing so would black-hole another concurrently active VPC.
|
|
|
|
|
merge_actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$key") || exit 74
|
|
|
|
|
merge_overlay=$(vpc_state_overlay_from_allowed "$merge_actual") || exit 74
|
|
|
|
|
vpc_state_overlay_from_allowed "$merge_actual" >/dev/null || exit 74
|
|
|
|
|
merge_extra=$(vpc_state_allowed_for_peer "$key") || exit 74
|
|
|
|
|
merge_allowed="$merge_overlay"
|
|
|
|
|
merge_allowed="$expected_overlay"
|
|
|
|
|
[ -z "$merge_extra" ] || merge_allowed="$merge_allowed,$merge_extra"
|
|
|
|
|
"$WG" set "$WG_IFACE" peer "$key" allowed-ips "$merge_allowed" || exit 74
|
|
|
|
|
done <<'{peer_marker}'
|
|
|
|
|
{peer_lines}
|
|
|
|
|
{peer_marker}
|
|
|
|
|
# `wg set` success is not an exact-state receipt. Re-read every authoritative
|
|
|
|
|
# peer and prove its Fabric overlay and route-backed tenant union exactly.
|
|
|
|
|
while IFS='|' read -r final_peer_host final_peer_key final_overlay; do
|
|
|
|
|
[ -n "$final_peer_host" ] || continue
|
|
|
|
|
final_actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$final_peer_key") || exit 74
|
|
|
|
|
vpc_state_overlay_from_allowed "$final_actual" >/dev/null || exit 74
|
|
|
|
|
final_extra=$(vpc_state_allowed_for_peer "$final_peer_key") || exit 74
|
|
|
|
|
final_expected="$final_overlay"
|
|
|
|
|
[ -z "$final_extra" ] || final_expected="$final_expected,$final_extra"
|
|
|
|
|
final_actual_set=$(printf '%s\n' "$final_actual" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
final_expected_set=$(printf '%s\n' "$final_expected" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
[ -n "$final_actual_set" ] &&
|
|
|
|
|
[ "$final_actual_set" = "$final_expected_set" ] || exit 74
|
|
|
|
|
done <<'{peer_marker}'
|
|
|
|
|
{peer_lines}
|
|
|
|
|
{peer_marker}
|
|
|
|
|
# The shared Fabric quarantine table is the last enforcement boundary before
|
|
|
|
|
# a VPC bridge is admitted. Reparse the complete stateless grammar,
|
|
|
|
|
# bind every owner comment to the engine-authenticated full Fabric digest,
|
|
|
|
|
@ -3092,6 +3307,7 @@ done <<'{peer_marker}'
|
|
|
|
|
# prove the whole table is still the post-refresh snapshot. A retained
|
|
|
|
|
# comment with altered drop semantics, a range element, or a late mutation
|
|
|
|
|
# therefore fails closed.
|
|
|
|
|
VC_VPC_APPLY_PHASE=final_readback
|
|
|
|
|
vc_vpc_fabric_nft_live_ready || exit 74
|
|
|
|
|
vpc_state_nft_set_matches || exit 74
|
|
|
|
|
vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
@ -3105,6 +3321,7 @@ vc_ln_final_expected=$(cat "$TXN/neighbors.desired") || exit 74
|
|
|
|
|
vc_ln_final_actual=$(vc_local_neighbor_records "$BRIDGE") || exit 74
|
|
|
|
|
vc_ln_final_wanted=$(printf '%s\\n' "$vc_ln_final_expected" | awk 'NF {{print $0 "|PERMANENT"}}') || exit 74
|
|
|
|
|
[ "$vc_ln_final_actual" = "$vc_ln_final_wanted" ] || exit 74
|
|
|
|
|
VC_VPC_APPLY_PHASE=commit
|
|
|
|
|
rm -f "$VPC_PEER_FILE" "$VPC_ROUTE_FILE" "$VPC_TENANT_FILE" "$VPC_TENANT_FILE.nft"
|
|
|
|
|
printf '%s\\n' ready > "$TXN/state"; chmod 600 "$TXN/state"
|
|
|
|
|
printf '%s\\n' '__VC_VPC_APPLIED__=yes'
|
|
|
|
|
@ -3160,6 +3377,15 @@ def render_remove_script(
|
|
|
|
|
fragment = f"/etc/network/interfaces.d/virt-cloud-vpc-{r.token}"
|
|
|
|
|
sysctl_file = f"/etc/sysctl.d/99-virt-cloud-vpc-{r.token}.conf"
|
|
|
|
|
proxy_file = f"/etc/virt-cloud/vpc-dataplane/{r.token}.proxy"
|
|
|
|
|
authoritative_peer_overlays = _authoritative_fabric_peer_overlays(plan)
|
|
|
|
|
remove_peer_lines = "\n".join(
|
|
|
|
|
f"{key}|{overlay}"
|
|
|
|
|
for key, overlay in (authoritative_peer_overlays or {}).items())
|
|
|
|
|
remove_peer_key_words = " ".join(authoritative_peer_overlays or ())
|
|
|
|
|
remove_peer_marker = "__VC_VPC_REMOVE_FABRIC_PEERS_" + hashlib.sha256(
|
|
|
|
|
remove_peer_lines.encode()).hexdigest()[:12]
|
|
|
|
|
remove_peer_closure_provided = (
|
|
|
|
|
"yes" if authoritative_peer_overlays is not None else "no")
|
|
|
|
|
state_loader = _VPC_STATE_LOADER.replace(
|
|
|
|
|
"__VC_FABRIC_HOST_ID__", plan.host_id).replace(
|
|
|
|
|
"__VC_FABRIC_ID__", plan.vpc.fabric_id).replace(
|
|
|
|
|
@ -3877,13 +4103,57 @@ vc_nft_table_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
[ ! -e "$TXN/fabric.nft.desired.absent" ] || exit 74
|
|
|
|
|
vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
"$TXN/fabric.nft.desired" || exit 74
|
|
|
|
|
for key in $("$WG" show "$WG_IFACE" peers 2>/dev/null); do
|
|
|
|
|
actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$key") || exit 74
|
|
|
|
|
overlay=$(vpc_state_overlay_from_allowed "$actual") || exit 74
|
|
|
|
|
extra=$(vpc_state_allowed_for_peer "$key") || exit 74
|
|
|
|
|
merged="$overlay"; [ -z "$extra" ] || merged="$merged,$extra"
|
|
|
|
|
"$WG" set "$WG_IFACE" peer "$key" allowed-ips "$merged" || exit 74
|
|
|
|
|
done
|
|
|
|
|
expected_fabric_peer_keys={_q(remove_peer_key_words)}
|
|
|
|
|
if [ "{remove_peer_closure_provided}" = yes ]; then
|
|
|
|
|
live_fabric_peer_keys=$("$WG" show "$WG_IFACE" peers 2>/dev/null) || exit 74
|
|
|
|
|
for live_peer_key in $live_fabric_peer_keys; do
|
|
|
|
|
case " $expected_fabric_peer_keys " in
|
|
|
|
|
*" $live_peer_key "*) : ;;
|
|
|
|
|
*) exit 74 ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
for expected_peer_key in $expected_fabric_peer_keys; do
|
|
|
|
|
printf '%s\n' "$live_fabric_peer_keys" |
|
|
|
|
|
grep -Fqx "$expected_peer_key" || exit 74
|
|
|
|
|
done
|
|
|
|
|
while IFS='|' read -r key expected_overlay; do
|
|
|
|
|
[ -n "$key" ] || continue
|
|
|
|
|
actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$key") || exit 74
|
|
|
|
|
vpc_state_overlay_from_allowed "$actual" >/dev/null || exit 74
|
|
|
|
|
extra=$(vpc_state_allowed_for_peer "$key") || exit 74
|
|
|
|
|
merged="$expected_overlay"; [ -z "$extra" ] || merged="$merged,$extra"
|
|
|
|
|
"$WG" set "$WG_IFACE" peer "$key" allowed-ips "$merged" || exit 74
|
|
|
|
|
done <<'{remove_peer_marker}'
|
|
|
|
|
{remove_peer_lines}
|
|
|
|
|
{remove_peer_marker}
|
|
|
|
|
else
|
|
|
|
|
# Legacy direct callers lack a full authenticated overlay mapping. Keep
|
|
|
|
|
# their historical route-withdrawal behavior without treating it as an
|
|
|
|
|
# authoritative Fabric closure receipt.
|
|
|
|
|
for key in $("$WG" show "$WG_IFACE" peers 2>/dev/null); do
|
|
|
|
|
actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$key") || exit 74
|
|
|
|
|
overlay=$(vpc_state_overlay_from_allowed "$actual") || exit 74
|
|
|
|
|
extra=$(vpc_state_allowed_for_peer "$key") || exit 74
|
|
|
|
|
merged="$overlay"; [ -z "$extra" ] || merged="$merged,$extra"
|
|
|
|
|
"$WG" set "$WG_IFACE" peer "$key" allowed-ips "$merged" || exit 74
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
if [ "{remove_peer_closure_provided}" = yes ]; then
|
|
|
|
|
while IFS='|' read -r final_peer_key final_overlay; do
|
|
|
|
|
[ -n "$final_peer_key" ] || continue
|
|
|
|
|
final_actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$final_peer_key") || exit 74
|
|
|
|
|
vpc_state_overlay_from_allowed "$final_actual" >/dev/null || exit 74
|
|
|
|
|
final_extra=$(vpc_state_allowed_for_peer "$final_peer_key") || exit 74
|
|
|
|
|
final_expected="$final_overlay"
|
|
|
|
|
[ -z "$final_extra" ] || final_expected="$final_expected,$final_extra"
|
|
|
|
|
final_actual_set=$(printf '%s\n' "$final_actual" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
final_expected_set=$(printf '%s\n' "$final_expected" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
[ -n "$final_actual_set" ] &&
|
|
|
|
|
[ "$final_actual_set" = "$final_expected_set" ] || exit 74
|
|
|
|
|
done <<'{remove_peer_marker}'
|
|
|
|
|
{remove_peer_lines}
|
|
|
|
|
{remove_peer_marker}
|
|
|
|
|
fi
|
|
|
|
|
vc_vpc_fabric_nft_live_ready || exit 74
|
|
|
|
|
vpc_state_nft_set_matches || exit 74
|
|
|
|
|
vc_nft_table_matches_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
@ -3953,15 +4223,25 @@ def render_observe_script(
|
|
|
|
|
f"{x['destination_cidr']}|{x['device']}|{x.get('via') or '-'}|{x.get('metric', 100)}"
|
|
|
|
|
for x in routes)
|
|
|
|
|
peer_lines = "\n".join(
|
|
|
|
|
f"{p.host_id}|{p.public_key}|{_allowed(plan, p)}"
|
|
|
|
|
# The host's validated state files, rather than the control-plane
|
|
|
|
|
# catalog aggregate, own the tenant half of AllowedIPs. The current
|
|
|
|
|
# plan still supplies the authenticated peer identity and overlay /32.
|
|
|
|
|
f"{p.host_id}|{p.public_key}|{p.overlay_cidr}"
|
|
|
|
|
for p in plan.peers)
|
|
|
|
|
fabric_peer_key_words = " ".join(sorted(plan.fabric_peer_keys or ()))
|
|
|
|
|
authoritative_peer_overlays = _authoritative_fabric_peer_overlays(plan)
|
|
|
|
|
fabric_peer_overlay_lines = "\n".join(
|
|
|
|
|
f"{key}|{overlay}"
|
|
|
|
|
for key, overlay in (authoritative_peer_overlays or {}).items())
|
|
|
|
|
fabric_peer_key_words = " ".join(authoritative_peer_overlays or ())
|
|
|
|
|
fabric_peer_key_arg = _q(fabric_peer_key_words)
|
|
|
|
|
fabric_peer_keys_provided = "yes" if plan.fabric_peer_keys is not None else "no"
|
|
|
|
|
fabric_peer_keys_provided = (
|
|
|
|
|
"yes" if authoritative_peer_overlays is not None else "no")
|
|
|
|
|
route_marker = "__VC_VPC_OBS_ROUTES_" + hashlib.sha256(
|
|
|
|
|
route_lines.encode()).hexdigest()[:12]
|
|
|
|
|
peer_marker = "__VC_VPC_OBS_PEERS_" + hashlib.sha256(
|
|
|
|
|
peer_lines.encode()).hexdigest()[:12]
|
|
|
|
|
fabric_peer_marker = "__VC_VPC_OBS_FABRIC_PEERS_" + hashlib.sha256(
|
|
|
|
|
fabric_peer_overlay_lines.encode()).hexdigest()[:12]
|
|
|
|
|
state_loader = _VPC_STATE_LOADER.replace(
|
|
|
|
|
"__VC_FABRIC_HOST_ID__", plan.host_id).replace(
|
|
|
|
|
"__VC_FABRIC_ID__", plan.vpc.fabric_id).replace(
|
|
|
|
|
@ -4114,7 +4394,7 @@ vc_vpc_observe_cleanup() {{
|
|
|
|
|
trap 'vc_vpc_observe_cleanup' EXIT
|
|
|
|
|
trap 'exit 79' HUP INT TERM
|
|
|
|
|
vc_vpc_fabric_observe_ready() {{
|
|
|
|
|
vpc_state_load || return 1
|
|
|
|
|
[ "$VPC_STATE_STATUS" = owned ] || return 1
|
|
|
|
|
vc_nft_table_snapshot inet virt_cloud_wg_fabric \
|
|
|
|
|
"$FABRIC_NFT_SNAPSHOT" || return 1
|
|
|
|
|
[ -f "$FABRIC_NFT_SNAPSHOT" ] &&
|
|
|
|
|
@ -4383,18 +4663,30 @@ route_extra=$(printf '%s\\n' "$route_records" | awk -F'|' '{{
|
|
|
|
|
}} END {{ if (count != {len(routes)}) extra=1; print extra ? "yes" : "no" }}')
|
|
|
|
|
printf '%s\\n' "__VC_VPC_ROUTE_EXTRA__=$route_extra"
|
|
|
|
|
{route_checks}
|
|
|
|
|
while IFS='|' read -r peer_host key expected_allowed; do
|
|
|
|
|
vpc_state_loaded=no
|
|
|
|
|
if vpc_state_load && [ "$VPC_STATE_STATUS" = owned ]; then
|
|
|
|
|
vpc_state_loaded=yes
|
|
|
|
|
fi
|
|
|
|
|
while IFS='|' read -r peer_host key expected_overlay; do
|
|
|
|
|
[ -n "$peer_host" ] || continue
|
|
|
|
|
actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$key") || actual=invalid
|
|
|
|
|
# ``wg show`` does not promise AllowedIPs order. Canonicalize both
|
|
|
|
|
# normalized lists before comparing, and retain the complete set in
|
|
|
|
|
# the marker. The Python adapter projects this to this VPC's tenant /32s
|
|
|
|
|
# after proving that no extra or missing entry exists.
|
|
|
|
|
expected_allowed=$expected_overlay
|
|
|
|
|
state=missing
|
|
|
|
|
if [ "$vpc_state_loaded" = yes ]; then
|
|
|
|
|
state_allowed=$(vpc_state_allowed_for_peer "$key") || state_allowed=invalid
|
|
|
|
|
if [ "$state_allowed" != invalid ] && [ -n "$state_allowed" ]; then
|
|
|
|
|
expected_allowed="$expected_allowed,$state_allowed"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
# ``wg show`` does not promise AllowedIPs order. Canonicalize the live set
|
|
|
|
|
# and the exact overlay + route-backed union derived by the shared state
|
|
|
|
|
# loader. The Python adapter separately proves this VPC's required routes
|
|
|
|
|
# are a subset before projecting its own tenant /32s.
|
|
|
|
|
expected_set=$(printf '%s\\n' "$expected_allowed" | tr ',' '\\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
actual_set=$(printf '%s\\n' "$actual" | tr ',' '\\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
allowed=$(printf '%s\\n' "$actual_set" | awk 'BEGIN {{ ORS=""; sep="" }} NF {{ printf "%s%s", sep, $0; sep="," }}')
|
|
|
|
|
state=missing
|
|
|
|
|
if [ -n "$actual_set" ] && [ "$actual_set" = "$expected_set" ]; then
|
|
|
|
|
if [ "$vpc_state_loaded" = yes ] && [ "$state_allowed" != invalid ] &&
|
|
|
|
|
[ -n "$actual_set" ] && [ "$actual_set" = "$expected_set" ]; then
|
|
|
|
|
state=owned
|
|
|
|
|
else
|
|
|
|
|
allowed=missing
|
|
|
|
|
@ -4407,8 +4699,13 @@ done <<'{peer_marker}'
|
|
|
|
|
expected_fabric_peer_keys={fabric_peer_key_arg}
|
|
|
|
|
wg_peer_set=unverified
|
|
|
|
|
if [ "{fabric_peer_keys_provided}" = yes ]; then
|
|
|
|
|
live_fabric_peer_keys=$("$WG" show "$WG_IFACE" peers 2>/dev/null || true)
|
|
|
|
|
wg_peer_set=exact
|
|
|
|
|
if live_fabric_peer_keys=$("$WG" show "$WG_IFACE" peers 2>/dev/null); then
|
|
|
|
|
:
|
|
|
|
|
else
|
|
|
|
|
live_fabric_peer_keys=
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
fi
|
|
|
|
|
for live_peer_key in $live_fabric_peer_keys; do
|
|
|
|
|
case " $expected_fabric_peer_keys " in
|
|
|
|
|
*" $live_peer_key "*) : ;;
|
|
|
|
|
@ -4418,6 +4715,41 @@ if [ "{fabric_peer_keys_provided}" = yes ]; then
|
|
|
|
|
for expected_peer_key in $expected_fabric_peer_keys; do
|
|
|
|
|
printf '%s\n' "$live_fabric_peer_keys" | grep -Fqx "$expected_peer_key" || wg_peer_set=missing
|
|
|
|
|
done
|
|
|
|
|
# Key presence alone cannot prove the shared cryptokey-routing boundary.
|
|
|
|
|
# A VPC revision may have withdrawn the final route for a peer that remains
|
|
|
|
|
# in the Fabric. Require every authoritative key to carry exactly one live
|
|
|
|
|
# overlay /32 plus only the route-backed tenant union from durable host
|
|
|
|
|
# state, including peers outside this VPC's current participant set.
|
|
|
|
|
if [ "$wg_peer_set" = exact ] && [ "$vpc_state_loaded" = yes ]; then
|
|
|
|
|
while IFS='|' read -r expected_peer_key expected_overlay; do
|
|
|
|
|
[ -n "$expected_peer_key" ] || continue
|
|
|
|
|
closure_actual=$(vc_wg_allowed_for_peer "$WG_IFACE" "$expected_peer_key") || {{
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
break
|
|
|
|
|
}}
|
|
|
|
|
vpc_state_overlay_from_allowed "$closure_actual" >/dev/null || {{
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
break
|
|
|
|
|
}}
|
|
|
|
|
closure_extra=$(vpc_state_allowed_for_peer "$expected_peer_key") || {{
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
break
|
|
|
|
|
}}
|
|
|
|
|
closure_expected="$expected_overlay"
|
|
|
|
|
[ -z "$closure_extra" ] || closure_expected="$closure_expected,$closure_extra"
|
|
|
|
|
closure_actual_set=$(printf '%s\n' "$closure_actual" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
closure_expected_set=$(printf '%s\n' "$closure_expected" | tr ',' '\n' | awk 'NF' | LC_ALL=C sort -u)
|
|
|
|
|
if [ -z "$closure_actual_set" ] ||
|
|
|
|
|
[ "$closure_actual_set" != "$closure_expected_set" ]; then
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
done <<'{fabric_peer_marker}'
|
|
|
|
|
{fabric_peer_overlay_lines}
|
|
|
|
|
{fabric_peer_marker}
|
|
|
|
|
elif [ "$wg_peer_set" = exact ]; then
|
|
|
|
|
wg_peer_set=missing
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
printf '%s\n' "__VC_VPC_WG_PEER_SET__=$wg_peer_set"
|
|
|
|
|
fabric_nft_state=no
|
|
|
|
|
@ -4998,41 +5330,39 @@ class VpcPrivateHostAdapter:
|
|
|
|
|
x.get("via"), str(x.get("metric", 100)))
|
|
|
|
|
for x in parsed.get("routes", [])
|
|
|
|
|
}
|
|
|
|
|
expected_wg_full = {
|
|
|
|
|
# The live `wg show` value includes the peer's overlay /32 as the
|
|
|
|
|
# first AllowedIP. Compare the complete cryptokey-routing set;
|
|
|
|
|
# project tenant entries only after that exact check.
|
|
|
|
|
p.host_id: (p.public_key, set(_allowed(plan, p).split(",")))
|
|
|
|
|
for p in plan.peers
|
|
|
|
|
}
|
|
|
|
|
wg_allowed_rows = parsed.get("wg_allowed_ips", [])
|
|
|
|
|
actual_wg_full = {
|
|
|
|
|
x.get("peer_host_id"): (x.get("public_key"),
|
|
|
|
|
set(x.get("tenant_allowed_ips", [])))
|
|
|
|
|
for x in parsed.get("wg_allowed_ips", [])
|
|
|
|
|
for x in wg_allowed_rows
|
|
|
|
|
}
|
|
|
|
|
# The live peer may carry /32s belonging to other VPCs sharing this
|
|
|
|
|
# Fabric. Verify the complete aggregate here, then expose only this
|
|
|
|
|
# VPC's tenant set to the PHP contract.
|
|
|
|
|
# Durable host state owns the complete live tenant union. The shell
|
|
|
|
|
# proves that union is exact; here retain the plan-level obligation:
|
|
|
|
|
# this VPC's peer key, overlay, members and service routes must all be
|
|
|
|
|
# present before the result can be accepted or projected to PHP.
|
|
|
|
|
required_tenant = _expected_peer_tenant_ips(plan.vpc)
|
|
|
|
|
expected_tenant: dict[str, set[str]] = {
|
|
|
|
|
p.host_id: {m.private_ip + "/32" for m in plan.remote_members
|
|
|
|
|
if m.host_id == p.host_id}
|
|
|
|
|
p.host_id: set(required_tenant.get(p.host_id, frozenset()))
|
|
|
|
|
for p in plan.peers
|
|
|
|
|
}
|
|
|
|
|
members_by_id = {m.member_id: m for m in plan.members}
|
|
|
|
|
for route in plan.vpc.routes:
|
|
|
|
|
target = (members_by_id.get(route.next_hop_member_id)
|
|
|
|
|
if route.next_hop_member_id is not None else None)
|
|
|
|
|
target_host = target.host_id if target is not None else route.next_hop_host_id
|
|
|
|
|
if target_host in expected_tenant:
|
|
|
|
|
expected_tenant[target_host].add(route.destination_cidr)
|
|
|
|
|
expected_wg = {
|
|
|
|
|
p.host_id: (p.public_key, expected_tenant.get(p.host_id, set()))
|
|
|
|
|
for p in plan.peers
|
|
|
|
|
}
|
|
|
|
|
actual_wg = {
|
|
|
|
|
host: (key, ips & expected_wg.get(host, (None, set()))[1])
|
|
|
|
|
for host, (key, ips) in actual_wg_full.items()
|
|
|
|
|
required_wg = {
|
|
|
|
|
p.host_id: (p.public_key,
|
|
|
|
|
{p.overlay_cidr} | expected_tenant.get(p.host_id, set()))
|
|
|
|
|
for p in plan.peers
|
|
|
|
|
}
|
|
|
|
|
wg_required_verified = (
|
|
|
|
|
len(wg_allowed_rows) == len(actual_wg_full) == len(required_wg)
|
|
|
|
|
and set(actual_wg_full) == set(required_wg)
|
|
|
|
|
and all(
|
|
|
|
|
actual_wg_full[host][0] == key
|
|
|
|
|
and required_ips.issubset(actual_wg_full[host][1])
|
|
|
|
|
for host, (key, required_ips) in required_wg.items()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
members = _member_nics(plan, xml_by_instance)
|
|
|
|
|
actual_route_state = {
|
|
|
|
|
(x.get("destination_cidr"), x.get("device"), str(x.get("table")),
|
|
|
|
|
@ -5059,6 +5389,14 @@ class VpcPrivateHostAdapter:
|
|
|
|
|
for x in parsed.get("rules", []) if x.get("state") == "owned"
|
|
|
|
|
}
|
|
|
|
|
expected_peer_states = {p.host_id: "owned" for p in plan.peers}
|
|
|
|
|
wg_state_marker_count = sum(
|
|
|
|
|
line.startswith("__VC_VPC_WG_STATE__=")
|
|
|
|
|
for line in output.splitlines()
|
|
|
|
|
)
|
|
|
|
|
wg_peer_set_marker_count = sum(
|
|
|
|
|
line.startswith("__VC_VPC_WG_PEER_SET__=")
|
|
|
|
|
for line in output.splitlines()
|
|
|
|
|
)
|
|
|
|
|
expected_bridge_nft = "yes" if plan.local_members else "none"
|
|
|
|
|
host_dataplane_verified = (parsed.get("bridge") == plan.resources.bridge
|
|
|
|
|
and parsed.get("bridge_owner") == DATAPLANE_OWNER_MARKER
|
|
|
|
|
@ -5094,9 +5432,13 @@ class VpcPrivateHostAdapter:
|
|
|
|
|
and actual_routes == actual_route_state
|
|
|
|
|
and route_flags_valid
|
|
|
|
|
and expected_rules == actual_rules
|
|
|
|
|
and expected_wg_full == actual_wg_full
|
|
|
|
|
and (plan.fabric_peer_keys is None
|
|
|
|
|
or parsed.get("wg_peer_set") == "exact")
|
|
|
|
|
and wg_required_verified
|
|
|
|
|
and ((plan.fabric_peer_keys is None
|
|
|
|
|
and plan.fabric_peer_overlays is None)
|
|
|
|
|
or (plan.fabric_peer_overlays is not None
|
|
|
|
|
and wg_peer_set_marker_count == 1
|
|
|
|
|
and parsed.get("wg_peer_set") == "exact"))
|
|
|
|
|
and wg_state_marker_count == len(expected_peer_states)
|
|
|
|
|
and expected_peer_states == parsed.get("wg_states", {})
|
|
|
|
|
and parsed.get("arp_nft") == ("yes" if plan.local_members else "none"))
|
|
|
|
|
host_config_verified = host_dataplane_verified and all(
|
|
|
|
|
@ -5108,7 +5450,7 @@ class VpcPrivateHostAdapter:
|
|
|
|
|
and parsed.get("firewall_mode") == firewall_mode
|
|
|
|
|
and parsed.get("firewall_state") == "owned"
|
|
|
|
|
and parsed.get("state_firewall_mode") == firewall_mode
|
|
|
|
|
and plan.fabric_peer_keys is not None
|
|
|
|
|
and plan.fabric_peer_overlays is not None
|
|
|
|
|
and parsed.get("wg_peer_set") == "exact"
|
|
|
|
|
and parsed.get("helper_contract") == "owned"
|
|
|
|
|
and parsed.get("bridge_admin") == "up"
|
|
|
|
|
|