Files
CASAN/packages/casan-harness/scripts/bash/tool-audit-lib.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

63 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Shared tamper-evident appender for the central tool-call audit log
# (.specify/logs/audit/tool-calls.jsonl).
#
# Every record is chained: record_hash = SHA-256(previous_record_hash | core),
# where core is the canonical (sorted-key) JSON of the record minus record_hash.
# After each append the chain head is signed with the audit RSA key, so a
# re-forged chain (recomputed hashes) cannot produce a valid head signature
# without the private key. Used by both agent-metrics.sh and tool-registry-gate.sh
# so the combined audit is tamper-evident regardless of which writer appended.
#
# Production note: the private key must live off-repo (KMS/HSM); it is local
# here only for self-contained demonstration.
append_tool_audit() {
local record_json="$1"
local project_root="$2"
local audit_dir="$CASAN_STATE_ROOT/logs/audit"
local log="$audit_dir/tool-calls.jsonl"
mkdir -p "$audit_dir"
local head
head="$(python - "$log" "$record_json" <<'PY'
import hashlib, json, sys
log, rec_json = sys.argv[1], sys.argv[2]
rec = json.loads(rec_json)
prev = ""
try:
with open(log, encoding="utf-8") as f:
lines = [l for l in f if l.strip()]
if lines:
prev = json.loads(lines[-1]).get("record_hash", "")
except FileNotFoundError:
pass
rec.pop("record_hash", None)
rec["previous_record_hash"] = prev
core = json.dumps(rec, sort_keys=True, separators=(",", ":"))
rec["record_hash"] = hashlib.sha256((prev + "|" + core).encode()).hexdigest()
with open(log, "a", encoding="utf-8") as f:
f.write(json.dumps(rec) + "\n")
sys.stdout.write(rec["record_hash"])
PY
)"
command -v openssl >/dev/null 2>&1 || return 0
# Private signing key lives OFF-REPO (default ~/.casan/audit-keys); only the
# public key is committed, for verification. Production: replace with KMS/HSM.
local pub_dir="$CASAN_GOVERNANCE_ROOT"
local priv_dir="${CASAN_AUDIT_KEY_DIR:-$HOME/.casan/audit-keys}"
local priv="$priv_dir/audit-private.pem" pub="$pub_dir/audit-public.pem"
mkdir -p "$pub_dir" "$priv_dir"
if [[ ! -f "$priv" ]]; then
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$priv" 2>/dev/null
chmod 600 "$priv"
fi
# Always re-export the matching public key so a persisted off-repo private key
# never drifts out of sync with a freshly checked-out audit-public.pem on CI
# runners (see governance-check.sh for the full rationale).
openssl rsa -in "$priv" -pubout -out "$pub" 2>/dev/null || true
printf '%s' "$head" > "$audit_dir/tool-calls-head.txt"
openssl dgst -sha256 -sign "$priv" -out "$audit_dir/tool-calls-head.sig" "$audit_dir/tool-calls-head.txt" 2>/dev/null || true
}