-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws-production-guard.sh
More file actions
executable file
·52 lines (46 loc) · 1.64 KB
/
Copy pathaws-production-guard.sh
File metadata and controls
executable file
·52 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# aws-production-guard.sh — Block dangerous AWS CLI operations
#
# Prevents: Accidental deletion of production resources.
# Blocks: aws s3 rm --recursive, aws ec2 terminate-instances,
# aws rds delete-db-instance, aws cloudformation delete-stack
#
# TRIGGER: PreToolUse
# MATCHER: "Bash|PowerShell"
# Without jq, the parse below silently yields empty and this hook stops
# guarding - with no error anywhere. Say so. We deliberately do not exit
# here: blocking would halt every tool call, and exiting 0 would change
# the behaviour of guards that do not depend on the parsed value.
if ! command -v jq >/dev/null 2>&1; then
_nojq_warned="/tmp/cc-nojq-warned-aws-production-guard-$PPID"
[ -f "$_nojq_warned" ] || {
echo "WARNING [aws-production-guard]: jq not found - this hook cannot inspect tool calls and is NOT protecting you. Install jq." >&2
: > "$_nojq_warned"
}
fi
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
[ -z "$COMMAND" ] && exit 0
# Only check AWS CLI commands
echo "$COMMAND" | grep -qE '^\s*aws\s' || exit 0
# Block destructive operations
BLOCKED_PATTERNS=(
"s3.*rm.*--recursive"
"s3.*rb\s"
"ec2.*terminate-instances"
"rds.*delete-db"
"cloudformation.*delete-stack"
"lambda.*delete-function"
"dynamodb.*delete-table"
"iam.*delete-user"
"iam.*delete-role"
)
for pattern in "${BLOCKED_PATTERNS[@]}"; do
if echo "$COMMAND" | grep -qiE "aws\s+$pattern"; then
echo "BLOCKED: Destructive AWS operation detected." >&2
echo " Pattern: $pattern" >&2
echo " Use the AWS Console for destructive operations." >&2
exit 2
fi
done
exit 0