Files
CASAN/AINative_OKR_CASAN5/.specify/tests/phase-sec23-state-isolation-tests.sh
T
thanhnvandClaude Opus 4.8 2c765c9a45 feat(plan-01): Phase 0.5 — path indirection via casan-paths.sh (no file moves)
Task 1.2: introduce a single path resolver so no harness script hardcodes
`.specify/...` scattered across the tree. casan-paths.sh resolves four roots
(HARNESS/STATE/GOVERNANCE/APP) by marker-based walk-up from its own location —
never `git rev-parse` (git root is the repo PARENT here, not the app dir).

- 101 bash scripts/tests: 238 hardcoded `$PROJECT_ROOT/.specify/...` refs rewritten
  to CASAN_HARNESS_ROOT (code) / CASAN_STATE_ROOT (logs,state) / CASAN_GOVERNANCE_ROOT.
  Sandbox test vars ($WORK/$TP/$FP/$T1_WORK) left untouched.
- Roots are NOT exported: each script/subprocess self-resolves from its own tree,
  matching the original per-script semantics and preserving hermetic sandbox isolation
  (node casan-step.mjs, copied telemetry/rollback scripts must not inherit real roots).
- Sandbox tests that copy a harness script now also copy casan-paths.sh (its new
  sibling dependency): adversarial (verify-audit-chain/verify-tool-audit/rollback) +
  track-a (security-check/telemetry-integrity).
- control-plane-settings.json reclassified as STATE (untracked runtime store).

Roots all still resolve to `.specify` in this monolithic layout, so behavior is
unchanged. Full gate: PASS=64 FAIL=0 SKIP=3 (adversarial 44/0, track-a 25/0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-07 22:07:41 +09:00

91 lines
4.5 KiB
Bash

#!/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)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
S="$CASAN_HARNESS_ROOT/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() { # <tenant> <key> -> 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() { # <tenant> <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