diff --git a/.claude/hooks/guard-live-infra.sh b/.claude/hooks/guard-live-infra.sh new file mode 100755 index 0000000..471a17e --- /dev/null +++ b/.claude/hooks/guard-live-infra.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# PreToolUse hook for Bash: forces a permission prompt before any command that +# changes live infrastructure. The ask rules in .claude/settings.json match the +# command text as Claude usually writes it; this catches the same commands when +# they are wrapped (sh -c '...', env VAR=x ..., absolute paths). +set -euo pipefail + +input=$(cat) +cmd=$(jq -r '.tool_input.command // empty' <<<"$input") +cwd=$(jq -r '.cwd // empty' <<<"$input") + +[ -z "$cmd" ] && exit 0 + +# A subcommand must follow whitespace, so flags like `preview --refresh` don't match. +# It may be followed by a closing quote or paren when the command is wrapped. +end="([[:space:]\"')]|\$)" +pulumi_re="pulumi[[:space:]]+([^|;&]*[[:space:]])?(up|destroy|refresh|import|state|cancel)$end" +kubectl_re="kubectl[[:space:]]+([^|;&]*[[:space:]])?(apply|delete|patch|edit|replace|create|drain|cordon|uncordon|taint|scale|rollout|exec|annotate|label|set|cp)$end" +ssh_re='(^|[^[:alnum:]_./-])(ssh|scp)[[:space:]]' + +what="" +if [[ $cmd =~ $pulumi_re ]]; then + what="pulumi ${BASH_REMATCH[2]}" +elif [[ $cmd =~ $kubectl_re ]]; then + what="kubectl ${BASH_REMATCH[2]}" +elif [[ $cmd =~ $ssh_re ]]; then + what="${BASH_REMATCH[2]} to a node" +fi + +[ -z "$what" ] && exit 0 + +stack=$(grep -oE '0[1-5]-[a-z0-9-]+' <<<"$cmd $cwd" | head -n 1 || true) +reason="Live infrastructure: '$what'${stack:+ in $stack}. Approve only if you want Claude to run this instead of running it yourself." + +jq -n --arg reason "$reason" '{ + hookSpecificOutput: { + hookEventName: "PreToolUse", + permissionDecision: "ask", + permissionDecisionReason: $reason + } +}' diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000..a0829a6 --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,68 @@ +{ + "permissions": { + "defaultMode": "plan", + "disableAutoMode": "disable", + "allow": [ + "Bash(pulumi preview *)", + "Bash(pulumi stack ls *)", + "Bash(pulumi whoami *)", + "Bash(pulumi version *)", + "Bash(npm ci)", + "Bash(npm install)", + "Bash(npm run typecheck *)", + "Bash(npx tsc *)", + "Bash(kubectl get *)", + "Bash(kubectl describe *)", + "Bash(kubectl logs *)", + "Bash(git add *)", + "Bash(git commit *)", + "Bash(git switch *)", + "WebFetch(domain:www.pulumi.com)", + "WebFetch(domain:github.com)", + "WebFetch(domain:raw.githubusercontent.com)", + "WebFetch(domain:kube-vip.io)", + "WebFetch(domain:longhorn.io)", + "WebFetch(domain:registry.terraform.io)" + ], + "ask": [ + "Bash(pulumi up *)", + "Bash(pulumi destroy *)", + "Bash(pulumi refresh *)", + "Bash(pulumi import *)", + "Bash(pulumi state *)", + "Bash(pulumi config set *)", + "Bash(pulumi config rm *)", + "Bash(pulumi stack output *)", + "Bash(kubectl get secret*)", + "Bash(kubectl apply *)", + "Bash(kubectl delete *)", + "Bash(kubectl patch *)", + "Bash(kubectl drain *)", + "Bash(kubectl exec *)", + "Bash(ssh *)", + "Bash(scp *)", + "Bash(curl *)", + "Bash(git push *)" + ], + "deny": [ + "Bash(env)", + "Bash(printenv *)", + "Bash(pulumi stack export *)", + "Read(**/.env)", + "Edit(**/Pulumi.dev.yaml)" + ] + }, + "hooks": { + "PreToolUse": [ + { + "matcher": "Bash", + "hooks": [ + { + "type": "command", + "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-live-infra.sh" + } + ] + } + ] + } +}