Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase-sec23-state-isolation-tests.sh
T
thanhnvandClaude Opus 4.8 664bd1f00c feat(plan-01): Phase 1 — relocate harness code to packages/casan-harness (symlink facade)
Physically move the pure-code subtrees out of .specify into the package, leaving
compat symlinks at the old .specify/<dir> paths so every existing reference (internal
CASAN_HARNESS_ROOT + external CI/docker/mjs) keeps resolving. Runtime state stays put.

Moved (git mv): scripts/ tests/ security/ templates/ config/ governance/ memory/
  .specify/<dir>  ->  packages/casan-harness/<dir>   (+ .specify/<dir> symlink)
Stays in .specify (state/governance/domain, handled later): logs/ agentops/ level5/
  init-options.json traceability-map.json

Python `.resolve()` self-location followed the compat symlink into packages and lost
the app root; generate-casan-demo-context.py, generate-agentops-dashboard.py and
dashboard-server.py now walk UP for the `.specify` state marker instead of a fixed
parent depth (fixes "missing trace files" in run-casan4).

Full gate: PASS=64 FAIL=0 SKIP=3 (CASAN_CI_STEP_TIMEOUT_SEC=1200 — track-a ~450s runs
close to the 600s default and can tip over under load; this is timing variance, not a
regression — it passed cleanly with headroom). Runtime log/audit artifacts kept unstaged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 00:06:00 +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