Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase-sec23-state-isolation-tests.sh
T
thanhnvandClaude Opus 4.8 3adc48ed82 feat(plan-01): Phase 4a — make harness location-independent (facade-free capable)
Resolve every root by marker walk-up instead of a fixed depth that only lands on the
app via the .specify compat symlink, so the harness runs correctly when invoked by its
real packages/casan-harness path — proven by a full gate run via that path: 64/0/0.

- 95 scripts/tests: PROJECT_ROOT/ROOT "$SCRIPT_DIR/../.."-style computations -> $CASAN_APP_ROOT.
- 6 leaf scripts (infra-lab, context-validate, secrets-scan, path-guard, toolchain-verify,
  phase2-sourcegen) now source casan-paths + use CASAN_APP_ROOT.
- run-casan4: source casan-paths as a package sibling (facade-independent), PROJECT_ROOT=CASAN_APP_ROOT.
- 8 Python files: project_root()/REPO_ROOT/bundle_root walk UP for the .specify marker
  (control-plane-settings, loop_common, model-call, context-compress, test-integrity,
  bundle-integrity, traceability-matrix; generate-* fixed earlier).
- evidence-pack-build.py + traceability-matrix.py: domain refs -> apps/okr/domain
  (input/, corpus/redteam-vectors.jsonl, traceability-map.json).
- ci-harness-gate.sh: export CASAN_TESTS_DIR/CASAN_TEST_MANIFEST/CASAN_BUNDLE_ROOT so the
  integrity Python resolves via the harness root regardless of invocation path; ROOT=CASAN_APP_ROOT.
- Remove the domain compat symlinks from packages/casan-harness/security (redteam-corpus,
  redteam-vectors, benign-corpus) — packages now holds NO domain data.

Both invocation paths pass (compat facade still present): .specify/... and packages/...

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 10:50:50 +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="$CASAN_APP_ROOT"
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