Files
CASAN/packages/casan-harness/tests/phase-sec23-rbac-tenant-tests.sh
T
thanhnvandClaude Opus 4.8 36a4812ef3 refactor(structure): promote app to repo root + remove redundant workspace cruft
Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

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

71 lines
3.5 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-16 SEC-23 (23.13, MT-01) — RBAC tenant boundary at the DATA layer +
# casan-harness tenant-scoped state wiring.
#
# Proves:
# * a same-tenant authorized action is ALLOWED,
# * a cross-tenant request is DENIED even when role/project would allow it,
# * an ORG-ADMIN of tenant A cannot act on tenant B (tenant isolation > org wildcard),
# * org-admin within its own tenant is still ALLOWED,
# * with no tenant args the decision is backward compatible,
# * a casan-harness run under CASAN_TENANT_ID writes telemetry under the tenant
# partition (tenant-paths.sh is sourced by the harness).
#
# Deterministic; hermetic; no model/network for the RBAC checks.
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"
RBAC="$S/rbac-check.py"
HARNESS="$S/casan-harness.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; }
echo "===== Plan-16 SEC-23 (23.13): RBAC tenant boundary + harness wiring ====="
# --- same-tenant authorized action -> ALLOW ---
[[ "$(rc_of python3 "$RBAC" check --role project-admin --resource settings --action write \
--role-project P --target-project P --role-tenant alpha --target-tenant alpha)" -eq 0 ]] \
&& pass "same-tenant authorized action ALLOW" || fail "same-tenant action wrongly denied"
# --- cross-tenant -> DENY (data layer, before role grant) ---
rc=$(set +e; python3 "$RBAC" check --role project-admin --resource settings --action write \
--role-project P --target-project P --role-tenant alpha --target-tenant beta >/dev/null 2>"$WORK/e1"; echo $?)
{ [[ "$rc" -eq 1 ]] && grep -q CROSS_TENANT_DENY "$WORK/e1"; } \
&& pass "cross-tenant DENY at data layer" || fail "cross-tenant not denied (rc=$rc)"
# --- org-admin cannot cross tenant (stronger than the org wildcard) ---
rc=$(set +e; python3 "$RBAC" check --role org-admin --resource settings --action write \
--role-tenant alpha --target-tenant beta >/dev/null 2>"$WORK/e2"; echo $?)
{ [[ "$rc" -eq 1 ]] && grep -q CROSS_TENANT_DENY "$WORK/e2"; } \
&& pass "org-admin of A cannot act on tenant B" || fail "org-admin crossed tenant (rc=$rc)"
# --- org-admin within its own tenant -> ALLOW ---
[[ "$(rc_of python3 "$RBAC" check --role org-admin --resource settings --action write \
--role-tenant alpha --target-tenant alpha)" -eq 0 ]] \
&& pass "org-admin within its tenant ALLOW" || fail "org-admin within tenant wrongly denied"
# --- backward compatible with no tenant args ---
[[ "$(rc_of python3 "$RBAC" check --role org-admin --resource settings --action write)" -eq 0 ]] \
&& pass "no tenant args = backward compatible (org-admin ALLOW)" || fail "backward compat broken"
# --- harness wiring: run under a tenant -> telemetry under the tenant partition ---
printf 'a benign requirement line.\n' > "$WORK/in.txt"
CASAN_TENANT_ID=alpha bash "$HARNESS" "$WORK/in.txt" "$WORK/out.txt" act -- bash -c 'echo done' >/dev/null 2>&1 || true
[[ -d "$WORK/tenants/alpha/telemetry/cost" ]] \
&& pass "casan-harness run under tenant scopes telemetry to the tenant partition" \
|| fail "harness did not tenant-scope telemetry (tenant-paths not wired)"
echo ""
echo "===== SEC-23 23.13 SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1