ration/scripts/pre-commit.sh
caelum0x 13abc31645 feat(ration): observability, infra-as-code, security hardening, and test depth
Phase 2 sections 2-6 and 8 of docs/SCALE-EXPANSION.md:

- pkg/telemetry + pkg/metrics: OpenTelemetry trace propagation and
  Prometheus metrics across every service hop (CLI -> agent -> gateway ->
  card-issuance/tenant-registry/auth -> ledger-core), tracing added to
  ledger-core itself via services/ledger-core/src/telemetry.rs.
- infra/: otel-collector + Prometheus + Grafana docker-compose stack, a
  Terraform ECS Fargate module + dev/staging/prod envs (plan-only, no
  apply — real AWS spend is an owner action), and a dev-only mTLS CA
  (infra/dev-ca) wired through pkg/mtls + services/ledger-core/src/mtls.rs.
- services/audit-log (from the prior commit) gets its async mirror writer
  in services/ledger-core/src/audit_mirror.rs, and services/gateway gets
  a per-tenant token-bucket rate limiter (internal/ratelimit).
- Test depth: a Rust proptest suite proving concurrent ApplyEntry never
  breaches the reserve floor (tests/apply_entry_proptest.rs, passing), a Go
  fuzz seed corpus for the policy envelope (rules_fuzz_test.go), a libFuzzer
  harness for the C WAL frame parser (services/wal/fuzz, deliberately not
  wired into `make test` per its own Makefile comment - needs a
  libFuzzer-bundled clang), and a real chaos test
  (scripts/chaos/kill-ledger-mid-write.sh) that SIGKILLs ledger-core
  mid-write under concurrent load and verifies via ListEntries that
  nothing was lost or double-applied.
- docs/formal/ledger.tla: a TLA+ spec of the ledger state machine (balance
  >= reserve_floor safety, HOLD-eventually-resolves liveness) plus a TLC
  model config - not run through TLC in this pass, that's a precise
  documented TODO, not faked.
- docs/adr/0001-0004, docs/RUNBOOK.md, docs/SLO.md, docs/THREAT-MODEL.md.

Verified: `make build && make test` clean (C WAL, Rust incl. the new
proptest suite, all 10 Go modules) from a cold target.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-19 12:25:58 +03:00

95 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# scripts/pre-commit.sh — the static-analysis gate for `make lint` and, if
# installed as a git hook, for `git commit`.
#
# Runs every repo-wide static check CI (.github/workflows/ci.yml) runs for
# Go/Rust/C/shell, minus the actual test suites (that's `make test` — this
# script is lint-speed, not test-suite-speed) and minus `cargo audit` (a
# network call against the advisory DB; CI runs it, this script doesn't, to
# keep local runs usable offline).
#
# Deliberately checks the whole tree, not just staged files: a partial,
# staged-only lint pass is exactly the kind of thing that looks green
# locally and then goes red in CI the moment an untouched-but-affected file
# is in scope. `make lint` passing should reliably predict CI's lint jobs
# passing.
#
# Install as a git hook (optional):
# ln -sf ../../scripts/pre-commit.sh .git/hooks/pre-commit
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
STATUS=0
section() { printf '\n== %s ==\n' "$1"; }
fail() { printf 'FAIL: %s\n' "$1" >&2; STATUS=1; }
require() {
if ! command -v "$1" >/dev/null 2>&1; then
printf 'pre-commit.sh: missing required tool "%s" (%s)\n' "$1" "$2" >&2
exit 127
fi
}
# --- Go: gofmt, go vet, golangci-lint, for every discovered module --------
section "Go"
require gofmt "install Go: https://go.dev/dl/"
require golangci-lint "install: https://golangci-lint.run/usage/install/"
mapfile -d '' -t go_modules < <(find . -name go.mod -not -path './.git/*' -print0 | sort -z)
for modfile in "${go_modules[@]}"; do
mod_dir="$(dirname "$modfile")"
echo "-- $mod_dir --"
unformatted="$(gofmt -l "$mod_dir")"
if [ -n "$unformatted" ]; then
fail "$mod_dir: gofmt -l found unformatted file(s):"$'\n'"$unformatted"
fi
(cd "$mod_dir" && go vet ./...) || fail "$mod_dir: go vet"
(cd "$mod_dir" && golangci-lint run ./...) || fail "$mod_dir: golangci-lint"
done
# --- Rust: cargo fmt --check, cargo clippy -D warnings ---------------------
section "Rust (ledger-core)"
require cargo "install Rust: https://rustup.rs"
(cd services/ledger-core && cargo fmt --check) || fail "services/ledger-core: cargo fmt --check"
(cd services/ledger-core && cargo clippy --all-targets -- -D warnings) \
|| fail "services/ledger-core: cargo clippy -D warnings"
# --- C: services/wal builds clean under -Wall -Wextra ----------------------
section "C (wal)"
require cc "install a C compiler (clang or gcc)"
( cd services/wal && make clean >/dev/null && make all ) || fail "services/wal: make all"
# --- Proto: protoc --fatal_warnings ----------------------------------------
# See .github/workflows/ci.yml's proto-lint job for why this is the protoc
# fallback (not `buf lint`) — same reasoning applies here.
section "Proto"
require protoc "install: https://grpc.io/docs/protoc-installation/"
protoc --proto_path=proto --fatal_warnings -o /dev/null proto/*.proto \
|| fail "proto/*.proto: protoc --fatal_warnings"
# --- Shell: shellcheck every script in scripts/ (best-effort — not every
# contributor machine has shellcheck, so this is a warn-and-continue check
# rather than a hard `require`, unlike the gates above which are load-bearing
# for the things this repo claims to guarantee (ledger correctness, proto
# contracts)). ---
section "Shell (scripts/*.sh)"
if command -v shellcheck >/dev/null 2>&1; then
shellcheck scripts/*.sh || fail "scripts/*.sh: shellcheck"
else
echo "shellcheck not installed — skipping (install: https://www.shellcheck.net/) — not a hard requirement, but CI-equivalent coverage needs it locally too"
fi
if [ "$STATUS" -eq 0 ]; then
printf '\nAll lint checks passed.\n'
else
printf '\nOne or more lint checks failed — see FAIL lines above.\n' >&2
fi
exit "$STATUS"