#!/usr/bin/env bash set -uo pipefail # CASAN Plan-16 SEC-23 Phase 2 (MT-01) — per-tenant governance state isolation. # # The control-plane settings store (settings + its embedded audit hash-chain) and # telemetry are now partitioned per tenant. Proves: # * 23.5 a setting written by tenant A lands under A's partition; tenant B does # NOT see it (separate store), # * 23.4 the store (incl. its audit chain) is a distinct on-disk file per tenant, # * cross-tenant guard denies B access to A's store path, # * an invalid tenant id fails CLOSED in the control-plane tool, # * 23.6 tenant-paths.sh resolves telemetry + CP paths under the tenant partition, # and an explicit override still wins. # # Deterministic; hermetic (temp tenant-state root); no model/network. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" S="$PROJECT_ROOT/.specify/scripts/bash" CPS="$S/control-plane-settings.py" TS="$S/tenant-store.sh" TP="$S/tenant-paths.sh" WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT export CASAN_TENANT_STATE_ROOT="$WORK/tenants" PASS=0; FAIL=0 pass() { echo "PASS: $1"; PASS=$((PASS + 1)); } fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); } rc_of() { set +e; "$@" >/dev/null 2>&1; echo $?; set -e 2>/dev/null || true; } getval() { # -> value or __none__ CASAN_TENANT_ID="$1" python3 "$CPS" get "$2" 2>/dev/null \ | python3 -c 'import json,sys try: print(json.load(sys.stdin).get("value")) except Exception: print("__none__")' 2>/dev/null || echo "__none__" } tp_var() { # CASAN_TENANT_ID="$1" bash -c "source '$TP' >/dev/null 2>&1; printf '%s' \"\${$2:-}\"" } echo "===== Plan-16 SEC-23 Phase 2: per-tenant governance state isolation =====" # --- 23.5 write in tenant A, read isolation from B --- CASAN_TENANT_ID=alpha python3 "$CPS" set compression.enabled true --actor a --reason r >/dev/null 2>&1 ALPHA_STORE="$WORK/tenants/alpha/control-plane/settings.json" [[ -f "$ALPHA_STORE" ]] && pass "23.5 tenant A setting written under A's partition" \ || fail "23.5 tenant A store not under partition ($ALPHA_STORE)" [[ "$(getval alpha compression.enabled)" == "True" || "$(getval alpha compression.enabled)" == "true" ]] \ && pass "23.5 tenant A reads back its own setting" || fail "23.5 tenant A cannot read its setting" vb="$(getval beta compression.enabled)" [[ "$vb" != "True" && "$vb" != "true" ]] \ && pass "23.5 tenant B does NOT see tenant A's setting (isolated)" || fail "23.5 tenant B saw A's setting ($vb)" # --- 23.4 distinct store file per tenant --- CASAN_TENANT_ID=beta python3 "$CPS" set compression.enabled false --actor b --reason r >/dev/null 2>&1 BETA_STORE="$WORK/tenants/beta/control-plane/settings.json" { [[ "$ALPHA_STORE" != "$BETA_STORE" && -f "$BETA_STORE" ]] && ! grep -q '"compression.enabled": *true' "$BETA_STORE" 2>/dev/null; } \ && pass "23.4 each tenant has a distinct store + audit chain on disk" \ || fail "23.4 tenant stores not distinct/isolated" # --- cross-tenant guard on A's store from B --- [[ "$(rc_of env CASAN_TENANT_ID=beta bash "$TS" guard "$ALPHA_STORE")" -eq 3 ]] \ && pass "cross-tenant guard denies B access to A's store path" || fail "cross-tenant guard did not deny" # --- invalid tenant fails closed in the control-plane tool --- [[ "$(rc_of env CASAN_TENANT_ID=../evil python3 "$CPS" get compression.enabled)" -ne 0 ]] \ && pass "invalid tenant id fails CLOSED in control-plane tool" || fail "invalid tenant not rejected" # --- 23.6 tenant-paths resolver: telemetry + CP under partition, per tenant --- mA="$(tp_var alpha CASAN_METRICS_DIR)"; mB="$(tp_var beta CASAN_METRICS_DIR)" { [[ "$mA" == *"/alpha/telemetry/cost" && "$mB" == *"/beta/telemetry/cost" && "$mA" != "$mB" ]]; } \ && pass "23.6 tenant-paths resolves telemetry dir per tenant" || fail "23.6 telemetry not partitioned (A=$mA B=$mB)" cpA="$(tp_var alpha CASAN_CP_STORE_FILE)" [[ "$cpA" == *"/alpha/control-plane/settings.json" ]] \ && pass "tenant-paths resolves CP store under tenant partition" || fail "CP store not partitioned ($cpA)" # --- explicit override still wins --- ov="$(CASAN_CP_STORE_FILE=/tmp/explicit.json CASAN_TENANT_ID=alpha bash -c "source '$TP' >/dev/null 2>&1; printf '%s' \"\$CASAN_CP_STORE_FILE\"")" [[ "$ov" == "/tmp/explicit.json" ]] \ && pass "explicit CASAN_CP_STORE_FILE override wins over tenant default" || fail "explicit override lost ($ov)" echo "" echo "===== SEC-23 Phase 2 SUMMARY: PASS=$PASS FAIL=$FAIL =====" [[ "$FAIL" -eq 0 ]] || exit 1