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>
105 lines
3.9 KiB
Bash
Executable File
105 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Verify CASAN H5 append-only hash-chain audit log.
|
|
# Usage:
|
|
# verify-audit-chain.sh [audit-jsonl]
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
source "$SCRIPT_DIR/casan-paths.sh"
|
|
PROJECT_ROOT="$CASAN_APP_ROOT"
|
|
AUDIT_LOG="${1:-$CASAN_STATE_ROOT/logs/audit/audit.jsonl}"
|
|
|
|
if [[ ! -f "$AUDIT_LOG" ]]; then
|
|
echo "AUDIT_CHAIN_MISSING file=$AUDIT_LOG" >&2
|
|
exit 1
|
|
fi
|
|
|
|
COMPUTED_HEAD="$(python - "$AUDIT_LOG" <<'PY'
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
|
|
path = sys.argv[1]
|
|
previous = ""
|
|
count = 0
|
|
|
|
with open(path, encoding="utf-8") as f:
|
|
for line_no, line in enumerate(f, 1):
|
|
if not line.strip():
|
|
continue
|
|
record = json.loads(line)
|
|
expected_previous = record.get("previous_record_hash", "")
|
|
if expected_previous != previous:
|
|
raise SystemExit(
|
|
f"AUDIT_CHAIN_BROKEN line={line_no} expected_previous={previous} actual_previous={expected_previous}"
|
|
)
|
|
|
|
core = "|".join(
|
|
[
|
|
record.get("timestamp", ""),
|
|
record.get("trace_id", ""),
|
|
record.get("action", ""),
|
|
record.get("actor", ""),
|
|
record.get("risk_level", ""),
|
|
record.get("decision", ""),
|
|
record.get("approval_status", ""),
|
|
record.get("approver", ""),
|
|
record.get("input_hash", ""),
|
|
record.get("output_hash", ""),
|
|
expected_previous,
|
|
]
|
|
)
|
|
expected_hash = hashlib.sha256(core.encode()).hexdigest()
|
|
actual_hash = record.get("record_hash", "")
|
|
if expected_hash != actual_hash:
|
|
raise SystemExit(
|
|
f"AUDIT_HASH_MISMATCH line={line_no} expected={expected_hash} actual={actual_hash}"
|
|
)
|
|
previous = actual_hash
|
|
count += 1
|
|
|
|
# Emit count and head on stderr (human) and the head on stdout (captured).
|
|
sys.stderr.write(f"AUDIT_CHAIN_INTEGRITY_OK records={count}\n")
|
|
sys.stdout.write(previous)
|
|
PY
|
|
)"
|
|
|
|
# --- External anchor verification ---
|
|
# Recomputing a forged chain yields a different head; the stored head signature
|
|
# was produced with a private key the forger does not have, so it will not match.
|
|
AUDIT_DIR="$(dirname "$AUDIT_LOG")"
|
|
HEAD_FILE="$AUDIT_DIR/audit-head.txt"
|
|
HEAD_SIG="$AUDIT_DIR/audit-head.sig"
|
|
AUDIT_PUB="$CASAN_GOVERNANCE_ROOT/audit-public.pem"
|
|
|
|
if [[ -f "$HEAD_FILE" && -f "$HEAD_SIG" && -f "$AUDIT_PUB" ]] && command -v openssl >/dev/null 2>&1; then
|
|
STORED_HEAD="$(cat "$HEAD_FILE")"
|
|
if [[ "$STORED_HEAD" != "$COMPUTED_HEAD" ]]; then
|
|
echo "AUDIT_HEAD_MISMATCH computed=$COMPUTED_HEAD stored=$STORED_HEAD" >&2
|
|
exit 1
|
|
fi
|
|
if ! openssl dgst -sha256 -verify "$AUDIT_PUB" -signature "$HEAD_SIG" "$HEAD_FILE" >/dev/null 2>&1; then
|
|
echo "AUDIT_HEAD_SIGNATURE_INVALID head=$COMPUTED_HEAD" >&2
|
|
exit 1
|
|
fi
|
|
echo "AUDIT_CHAIN_VALID anchor=signed last_hash=$COMPUTED_HEAD"
|
|
else
|
|
# SEC-01 (H-01): in enforced mode a missing/unverifiable signature is a FAILURE,
|
|
# not "valid unsigned". Otherwise deleting audit-head.sig (or the pubkey) after
|
|
# tampering + recomputing the chain would pass verification. Permissive mode
|
|
# (dev default) keeps the previous unsigned-OK behaviour. Self-contained check
|
|
# (this script is copied into sandboxes by tests, so it must not source common.sh):
|
|
# CASAN_PROFILE=prod (SEC-17) enables all enforce flags; CASAN_VERIFY_STRICT=1 this one.
|
|
if [[ "${CASAN_PROFILE:-}" == "prod" || "${CASAN_VERIFY_STRICT:-}" == "1" ]]; then
|
|
MISSING=""
|
|
[[ -f "$HEAD_FILE" ]] || MISSING="$MISSING head-file"
|
|
[[ -f "$HEAD_SIG" ]] || MISSING="$MISSING head-sig"
|
|
[[ -f "$AUDIT_PUB" ]] || MISSING="$MISSING pubkey"
|
|
command -v openssl >/dev/null 2>&1 || MISSING="$MISSING openssl"
|
|
echo "AUDIT_CHAIN_UNSIGNED_STRICT_FAIL last_hash=$COMPUTED_HEAD missing=${MISSING# }" >&2
|
|
exit 1
|
|
fi
|
|
echo "AUDIT_CHAIN_VALID anchor=unsigned last_hash=$COMPUTED_HEAD"
|
|
fi
|