mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
- Startup fail-closed: LockDownKeyDir explicitly chmods 0700 an existing key dir (counteracting host bind mounts) and 0600 existing key files; adds keys/ itself alongside jwt.key, secret.key, and config.json to IsolationSelfTest; makes IsolationSelfTest fatal unless ARTEX_ALLOW_UNISOLATED=1 is set. - Secret rotation: adds db.RotateSecretKey (single atomic transaction re-encrypting all sealed columns/settings and bumping auth.password_version), cmd/artex -rotate-secrets, scripts/rotate-secrets.sh, and OPS/SECURITY docs. - Scoped defaults, backfill & posture warnings: TargetGate defaults ON for tasks with CompanyIDs or InterceptRules (explicit opt-out persisted in tasks.target_gate_opt_out); migration 0022_target_gate_backfill.sql backfills existing active scoped tasks that did not explicitly opt out; egress-guard defaults to EGRESS_MODE=enforce when scope lists exist; loud startup warning when target gate or egress enforcement is off. - Kernel-backed local kill-switch: enables PR_SET_CHILD_SUBREAPER and scans /proc (without trusting agent-writable state) on Engine.Pause/BeginDelete to kill escaped setsid, nohup, and nested double-fork daemons (plus all execPriv.UID processes in isolated mode) within <= 5s with zero post-pause egress.
124 lines
4.6 KiB
Bash
Executable file
124 lines
4.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Atomically rotate ARTEX's jwt.key and secret.key, re-encrypting database
|
|
# secrets at rest and invalidating all active web/API sessions.
|
|
#
|
|
# Usage:
|
|
# scripts/rotate-secrets.sh [--key-dir DIR] [--dsn DSN] [--artex-bin PATH] [--files-only]
|
|
#
|
|
# Environment:
|
|
# ARTEX_JWT_KEY_DIR Key directory containing jwt.key and secret.key (default: ./keys)
|
|
# ARTEX_PG_DSN PostgreSQL DSN for atomic DB secret re-encryption & session invalidation
|
|
# ARTEX_BIN Path to the artex binary (default: auto-detected or `go run ./cmd/artex`)
|
|
#
|
|
# Guarantees:
|
|
# 1. Tightens KEY_DIR to 0700 and creates staged keys with umask 077 (0600).
|
|
# 2. When a database DSN is configured (and --files-only is not set), decrypts
|
|
# all encrypted DB columns with the old secret.key, re-encrypts them with
|
|
# the new secret.key, and increments auth.password_version in ONE atomic
|
|
# PostgreSQL transaction BEFORE moving the new secret.key into place.
|
|
# If the DB transaction fails, neither key file is replaced.
|
|
# 3. Replaces jwt.key and secret.key via POSIX atomic rename (mv within KEY_DIR).
|
|
# 4. Invalidates every existing JWT session (new signing key + bumped auth.password_version).
|
|
# Restart the artex server process/container after rotation so it reloads jwt.key & secret.key.
|
|
set -euo pipefail
|
|
umask 077
|
|
|
|
KEY_DIR="${ARTEX_JWT_KEY_DIR:-./keys}"
|
|
DSN="${ARTEX_PG_DSN:-}"
|
|
ARTEX_BIN="${ARTEX_BIN:-}"
|
|
FILES_ONLY=0
|
|
|
|
usage() {
|
|
sed -n '2,23p' "$0" | sed 's/^# \{0,1\}//'
|
|
}
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--key-dir) KEY_DIR="$2"; shift 2 ;;
|
|
--dsn) DSN="$2"; shift 2 ;;
|
|
--artex-bin) ARTEX_BIN="$2"; shift 2 ;;
|
|
--files-only) FILES_ONLY=1; shift ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) echo "rotate-secrets.sh: unknown argument: $1" >&2; usage >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
mkdir -p "$KEY_DIR"
|
|
chmod 0700 "$KEY_DIR"
|
|
|
|
JWT_PATH="$KEY_DIR/jwt.key"
|
|
SECRET_PATH="$KEY_DIR/secret.key"
|
|
TMP_JWT="$KEY_DIR/.jwt.key.new.$$"
|
|
TMP_SECRET="$KEY_DIR/.secret.key.new.$$"
|
|
|
|
cleanup() {
|
|
rm -f "$TMP_JWT" "$TMP_SECRET"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
gen_jwt_key() {
|
|
# 32 bytes hex = 64 alphanumeric chars (satisfies loadOrCreateJWTKey >= 32 bytes)
|
|
if command -v openssl >/dev/null 2>&1; then
|
|
openssl rand -hex 32
|
|
else
|
|
python3 -c 'import secrets; print(secrets.token_hex(32))'
|
|
fi
|
|
}
|
|
|
|
gen_secret_key() {
|
|
# 32 random bytes base64-encoded (matches db.ParseSecretKey / loadSecretKey)
|
|
if command -v openssl >/dev/null 2>&1; then
|
|
openssl rand -base64 32
|
|
else
|
|
python3 -c 'import base64, os; print(base64.b64encode(os.urandom(32)).decode("ascii"))'
|
|
fi
|
|
}
|
|
|
|
# Stage new keys in the same directory as targets so rename(2) is atomic.
|
|
gen_jwt_key > "$TMP_JWT"
|
|
chmod 0600 "$TMP_JWT"
|
|
|
|
gen_secret_key > "$TMP_SECRET"
|
|
chmod 0600 "$TMP_SECRET"
|
|
|
|
# Re-encrypt DB secrets and invalidate active sessions BEFORE replacing secret.key on disk.
|
|
if [ "$FILES_ONLY" -eq 0 ] && [ -n "$DSN" ]; then
|
|
if [ -f "$SECRET_PATH" ]; then
|
|
OLD_SECRET_FOR_DB="$SECRET_PATH"
|
|
else
|
|
# First-time DB with no prior secret.key file: pass staged key as both old/new
|
|
# so backfill/session invalidation still runs atomically.
|
|
OLD_SECRET_FOR_DB="$TMP_SECRET"
|
|
fi
|
|
if [ -n "$ARTEX_BIN" ]; then
|
|
ARTEX_PG_DSN="$DSN" "$ARTEX_BIN" -rotate-secrets -rotate-old-secret-file "$OLD_SECRET_FOR_DB" -rotate-new-secret-file "$TMP_SECRET"
|
|
elif command -v artex >/dev/null 2>&1; then
|
|
ARTEX_PG_DSN="$DSN" artex -rotate-secrets -rotate-old-secret-file "$OLD_SECRET_FOR_DB" -rotate-new-secret-file "$TMP_SECRET"
|
|
else
|
|
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
|
(cd "$REPO_ROOT" && ARTEX_PG_DSN="$DSN" go run ./cmd/artex -rotate-secrets -rotate-old-secret-file "$OLD_SECRET_FOR_DB" -rotate-new-secret-file "$TMP_SECRET")
|
|
fi
|
|
elif [ "$FILES_ONLY" -eq 0 ] && [ -f "$SECRET_PATH" ]; then
|
|
echo "[rotate-secrets] WARN: ARTEX_PG_DSN/--dsn not set; rotating key files only." >&2
|
|
echo "[rotate-secrets] WARN: If PostgreSQL holds enc:v1:* rows sealed with the old secret.key, re-run with --dsn or restore the old secret.key before starting artex." >&2
|
|
fi
|
|
|
|
# Atomic POSIX rename into place; sync directory metadata if python3 is available.
|
|
mv -f "$TMP_SECRET" "$SECRET_PATH"
|
|
mv -f "$TMP_JWT" "$JWT_PATH"
|
|
chmod 0600 "$SECRET_PATH" "$JWT_PATH"
|
|
chmod 0700 "$KEY_DIR"
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
python3 - "$KEY_DIR" <<'PY'
|
|
import os, sys
|
|
fd = os.open(sys.argv[1], os.O_RDONLY)
|
|
try:
|
|
os.fsync(fd)
|
|
finally:
|
|
os.close(fd)
|
|
PY
|
|
fi
|
|
|
|
echo "[rotate-secrets] rotated $JWT_PATH and $SECRET_PATH (mode 0600 in 0700 dir)."
|
|
echo "[rotate-secrets] all existing JWT sessions are invalidated; restart ARTEX to load the new keys."
|