Files
CASAN/AINative_OKR_CASAN5/.specify/scripts/bash/incident.sh
T
thanhnvandClaude Opus 4.8 f24ed21324 feat(c7): incident response — severity classifier + scoped kill-switch + runbook (V23)
Closes the last fully-[planned] Track-C dimension (was scored 1).
- incident.sh raise <event>: classify severity via incident-severity.map
  (LOW/MED/HIGH/CRIT), record a structured entry (owner routing), and for
  HIGH/CRIT auto-engage the scoped kill-switch + dispatch an alert (reuses H6
  alert-dispatch.sh). Exit 2 on HIGH/CRIT so a pipeline gate goes red.
- kill-switch.sh engage/clear/check/status, scoped by project/model/provider
  (+ global). `check` exits 2 when engaged so gates honor it.
- casan-harness.sh honors an engaged kill-switch before running (opt-in
  CASAN_KILLSWITCH_ENFORCE=1, default OFF → baseline unchanged).
- incident-runbook.md: severity→owner→response + postmortem template + prod TODO.
- phase-c7-incident-tests.sh: 15 checks — severity grading, auto kill-switch on
  HIGH/CRIT, MED-only records, lifecycle, global scope, structured record, and
  the production wrapper refusing to run under an engaged switch.

Baselines: run-casan4 35/35, adversarial 44/44. New suite total: 175 → 190.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-05 20:41:48 +09:00

90 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -uo pipefail
# CASAN — Incident response (C7 / V23).
#
# Turns a detected security/ops event into a graded incident: classify severity,
# record a tamper-visible incident entry, and for HIGH/CRIT auto-engage the
# scoped kill-switch + fire an alert (reuses alert-dispatch.sh from H6 if present).
# Answers "when a gate catches an attack/spike/tamper — who is paged and what
# stops?" — severity, owner, kill-switch, runbook.
#
# Usage:
# incident.sh raise <event-type> [detail] [--scope <s>] [--id <id>]
# incident.sh status
# Exit: 0 recorded (LOW/MED) · 2 kill-switch engaged (HIGH/CRIT) · 64 usage.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
SEC_DIR="$PROJECT_ROOT/.specify/security"
LOG="$PROJECT_ROOT/.specify/logs/level5/incidents.jsonl"
RUNBOOK="$SEC_DIR/incident-runbook.md"
SEVMAP="$SEC_DIR/incident-severity.map"
mkdir -p "$(dirname "$LOG")"
# shellcheck source=casan-log.sh
source "$SCRIPT_DIR/casan-log.sh"
CMD="${1:-}"
ts() { date -u +"%Y-%m-%dT%H:%M:%SZ"; }
# owner routing by severity (production: on-call rota / IdP group).
owner_for() { case "$1" in CRIT) echo "security-oncall" ;; HIGH) echo "ops-oncall" ;; MED) echo "tech-lead" ;; *) echo "triage" ;; esac; }
if [[ "$CMD" == "status" ]]; then
n=$(grep -c . "$LOG" 2>/dev/null || echo 0)
echo "INCIDENTS total=$n log=$LOG"
[[ -f "$LOG" ]] && tail -5 "$LOG"
exit 0
fi
[[ "$CMD" == "raise" ]] || { echo "Usage: incident.sh raise <event-type> [detail] [--scope <s>] [--id <id>]" >&2; exit 64; }
EVENT="${2:-}"; DETAIL="${3:-}"
[[ -n "$EVENT" ]] || { echo "usage: incident.sh raise <event-type> [detail]" >&2; exit 64; }
SCOPE="project"; ID="${CASAN_PROJECT:-current}"
shift 2 2>/dev/null || true
while [[ "$#" -gt 0 ]]; do
case "$1" in
--scope) SCOPE="${2:-project}"; shift 2 ;;
--id) ID="${2:-current}"; shift 2 ;;
*) shift ;;
esac
done
# Classify severity from the map (fallback to default).
SEV="$(awk -v e="$EVENT" '$1==e {print $2; exit}' "$SEVMAP" 2>/dev/null)"
[[ -n "$SEV" ]] || SEV="$(awk '$1=="default" {print $2; exit}' "$SEVMAP" 2>/dev/null)"
[[ -n "$SEV" ]] || SEV="MED"
OWNER="$(owner_for "$SEV")"
TS="$(ts)"
ACTION="recorded"
# HIGH/CRIT → engage the scoped kill-switch (stop the blast radius).
if [[ "$SEV" == "CRIT" || "$SEV" == "HIGH" ]]; then
bash "$SCRIPT_DIR/kill-switch.sh" engage "$SCOPE" "$ID" "incident:$EVENT" >/dev/null 2>&1 || true
ACTION="kill_switch_engaged"
# Fire an alert through the H6 dispatcher if it is wired up.
if [[ -x "$SCRIPT_DIR/alert-dispatch.sh" ]]; then
bash "$SCRIPT_DIR/alert-dispatch.sh" "$SEV" "incident:$EVENT" "$DETAIL" >/dev/null 2>&1 || true
fi
casan_log error incident "INCIDENT sev=$SEV event=$EVENT scope=$SCOPE id=$ID → kill-switch ENGAGED owner=$OWNER"
else
casan_log warn incident "INCIDENT sev=$SEV event=$EVENT scope=$SCOPE id=$ID owner=$OWNER"
fi
# Record a structured incident entry.
python - "$LOG" "$TS" "$EVENT" "$SEV" "$OWNER" "$SCOPE" "$ID" "$ACTION" "$DETAIL" "$RUNBOOK" <<'PY' 2>/dev/null || \
printf '{"timestamp":"%s","event":"%s","severity":"%s","owner":"%s","scope":"%s","id":"%s","action":"%s"}\n' \
"$TS" "$EVENT" "$SEV" "$OWNER" "$SCOPE" "$ID" "$ACTION" >> "$LOG"
import json, sys
log, ts, event, sev, owner, scope, iid, action, detail, runbook = sys.argv[1:11]
with open(log, "a", encoding="utf-8") as f:
f.write(json.dumps({
"timestamp": ts, "event": event, "severity": sev, "owner": owner,
"scope": scope, "id": iid, "action": action, "detail": detail[:300],
"runbook": runbook,
}) + "\n")
PY
echo "INCIDENT_RAISED sev=$SEV event=$EVENT owner=$OWNER scope=$SCOPE id=$ID action=$ACTION runbook=$RUNBOOK"
[[ "$SEV" == "CRIT" || "$SEV" == "HIGH" ]] && exit 2 || exit 0