Files
aevolution/tools/ops/verify-isolation.sh
T
dingoandClaude Opus 5.5 df0c4f4efb Add M8.2 environment isolation and World0 protection
Per-environment platform layers (dev, stage, world0) with restricted pod
security, quotas, limit ranges, default-deny networking and token-less
service accounts, applied by an administrator rather than Argo. A native
ValidatingAdmissionPolicy denies deleting World0 data, identity and Argo
objects to every requester without the break-glass group. Stage and
World0 Argo projects are scoped to one namespace, World0 blocks
automated sync, and a cleanup tool refuses World0. An offline policy
check runs as a new CI suite; a live verification script uses only
can-i, reads and server dry runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 12:43:58 -04:00

111 lines
5.2 KiB
Bash

#!/usr/bin/env bash
# M8.2 live isolation checks. Changes nothing: it only asks `kubectl auth can-i`
# (impersonating the environments' service accounts), reads objects, and sends
# DELETE requests with --dry-run=server, which run admission but never persist.
#
# tools/ops/verify-isolation.sh [--context <name>]
#
# Needs an identity that may impersonate service accounts and read namespaces,
# policies and Argo CD projects (a cluster administrator). Prints PASS/FAIL/SKIP per
# check and exits nonzero if any check fails.
set -uo pipefail
k=(kubectl --request-timeout=20s)
[ "${1:-}" = "--context" ] && [ -n "${2:-}" ] && k+=(--context "$2")
fails=0
pass() { echo "PASS $*"; }
fail() { echo "FAIL $*"; fails=$((fails + 1)); }
skip() { echo "SKIP $*"; }
envs=(dev stage world0)
present=()
for env in "${envs[@]}"; do
if "${k[@]}" get namespace "evolution-$env" > /dev/null 2>&1; then
present+=("$env")
else
skip "namespace evolution-$env does not exist"
fi
done
# 1. Guardrails and labels in every present environment.
for env in "${present[@]}"; do
ns="evolution-$env"
got="$("${k[@]}" get namespace "$ns" -o jsonpath='{.metadata.labels.aevolution\.io/environment} {.metadata.labels.pod-security\.kubernetes\.io/enforce} {.metadata.labels.aevolution\.io/protected}')"
want="$env restricted"; [ "$env" = world0 ] && want="$env restricted true"
[ "$(echo "$got" | xargs)" = "$want" ] && pass "$ns labels ($got)" || fail "$ns labels '$got' (want '$want')"
for object in resourcequota/aevolution-quota limitrange/aevolution-defaults \
networkpolicy/aevolution-default-deny networkpolicy/aevolution-allow-dns \
serviceaccount/aevolution-migrator serviceaccount/aevolution-region serviceaccount/aevolution-backup; do
"${k[@]}" get -n "$ns" "$object" > /dev/null 2>&1 && pass "$ns has $object" || fail "$ns lacks $object"
done
done
# 2. No environment identity can read secrets or change anything anywhere.
for env in "${present[@]}"; do
for sa in aevolution-migrator aevolution-region aevolution-backup; do
who="system:serviceaccount:evolution-$env:$sa"
for target in "${envs[@]}"; do
for action in "get secrets" "list secrets" "create pods" "delete persistentvolumeclaims" "create rolebindings"; do
answer="$("${k[@]}" auth can-i $action -n "evolution-$target" --as "$who" 2>/dev/null)"
[ "$answer" = "no" ] && pass "$who cannot $action in evolution-$target" ||
fail "$who can $action in evolution-$target"
done
done
answer="$("${k[@]}" auth can-i delete namespaces --as "$who" 2>/dev/null)"
[ "$answer" = "no" ] && pass "$who cannot delete namespaces" || fail "$who can delete namespaces"
done
done
# 3. World0 admission protection, exercised with server-side dry runs.
if "${k[@]}" get validatingadmissionpolicybinding aevolution-world0-protection > /dev/null 2>&1; then
pass "world0 protection policy is bound"
else
fail "world0 protection policy binding missing"
fi
denied() { # denied <description> <kubectl delete args...> (always --dry-run=server)
local what="$1"; shift
local out
out="$("${k[@]}" delete --dry-run=server --wait=false "$@" 2>&1)"
if echo "$out" | grep -q "aevolution:world0-break-glass"; then
pass "denied: $what"
elif echo "$out" | grep -qi "not found"; then
skip "$what (object absent)"
else
fail "not denied: $what -> $out"
fi
}
if printf '%s\n' "${present[@]}" | grep -qx world0; then
denied "delete namespace evolution-world0" namespace evolution-world0
denied "delete service account in evolution-world0" -n evolution-world0 serviceaccount aevolution-region
secret="$("${k[@]}" get secrets -n evolution-world0 -o name 2>/dev/null | head -1)"
[ -n "$secret" ] && denied "delete $secret in evolution-world0" -n evolution-world0 "$secret" ||
skip "no Secret in evolution-world0 to test"
pvc="$("${k[@]}" get pvc -n evolution-world0 -o name 2>/dev/null | head -1)"
[ -n "$pvc" ] && denied "delete $pvc in evolution-world0" -n evolution-world0 "$pvc" ||
skip "no PVC in evolution-world0 to test"
else
skip "world0 admission dry runs (namespace absent)"
fi
# 4. Argo CD projects: each AEvolution project targets only its own namespace.
for env in "${envs[@]}"; do
dest="$("${k[@]}" get appproject "aevolution-$env" -n argocd -o jsonpath='{range .spec.destinations[*]}{.namespace}{" "}{end}{.spec.clusterResourceWhitelist}' 2>/dev/null)" || {
skip "appproject aevolution-$env not present"; continue; }
[ "$(echo "$dest" | xargs)" = "evolution-$env" ] && pass "appproject aevolution-$env destinations '$dest'" ||
fail "appproject aevolution-$env destinations/cluster kinds '$dest'"
done
wide="$("${k[@]}" get appproject default -n argocd -o jsonpath='{.spec.destinations[*].namespace}' 2>/dev/null)"
[ "$wide" = "*" ] && echo "WARN appproject default allows every namespace; its applications could create objects in evolution-* (deletion is still denied in world0 by admission)"
# 5. The cleanup tool refuses World0 before touching the cluster.
root="$(cd "$(dirname "$0")/../.." && pwd)"
for bad in world0 evolution-world0 World0 ""; do
if bash "$root/tools/ops/cleanup-environment.sh" "$bad" > /dev/null 2>&1; then
fail "cleanup accepted '$bad'"
else
pass "cleanup refused '$bad'"
fi
done
echo "failures=$fails"
[ "$fails" -eq 0 ]