forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecret-rotator.sh
More file actions
126 lines (93 loc) · 3.79 KB
/
Copy pathsecret-rotator.sh
File metadata and controls
126 lines (93 loc) · 3.79 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# Secret Rotation Script
# Rotates database credentials, API keys, and other secrets
set -e
VAULT_ADDR=${VAULT_ADDR:-"http://vault:8200"}
VAULT_TOKEN=${VAULT_TOKEN}
ROTATION_INTERVAL=${ROTATION_INTERVAL:-86400} # Default 24 hours
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# Rotate database credentials
rotate_db_credentials() {
local db_path=$1
log "Rotating database credentials for: $db_path"
vault read -format=json "$db_path" | jq '.data' > /tmp/old_creds.json
# Rotate by requesting new credentials
vault read -format=json "database/creds/${db_path##*/}" > /tmp/new_creds.json
log "Database credentials rotated successfully"
}
# Rotate API keys
rotate_api_keys() {
local secret_path=$1
log "Rotating API keys for: $secret_path"
# Get current secret
current=$(vault kv get -format=json "$secret_path")
# Generate new API key (example - adjust based on your API)
new_key=$(openssl rand -hex 32)
# Update secret
vault kv put "$secret_path" api_key="$new_key" \
rotated_at="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
rotation_version="$(echo $current | jq -r '.data.data.rotation_version | tonumber + 1')"
log "API keys rotated successfully"
}
# Rotate environment-specific secrets
rotate_env_secrets() {
local env=$1
local secrets_path="secret/data/$env"
log "Rotating secrets for environment: $env"
# Get list of all secrets for this environment
secrets=$(vault kv list -format=json "$secrets_path" 2>/dev/null | jq -r '.data.keys[]' || echo "")
if [ -z "$secrets" ]; then
log "No secrets found for environment: $env"
return
fi
for secret in $secrets; do
if [[ "$secret" == *".next-rotate" ]]; then
next_rotation=$(vault kv get -field=next_rotate "$secrets_path/$secret" 2>/dev/null || echo "")
if [[ -z "$next_rotation" ]] || [[ "$(date +%s)" -ge "$(date -d "$next_rotation" +%s)" ]]; then
log "Rotating secret: $secret"
new_value=$(openssl rand -base64 32)
next_rotate=$(date -d "+30 days" -u +'%Y-%m-%dT%H:%M:%SZ')
vault kv put "$secrets_path/$secret" \
value="$new_value" \
rotated_at="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
next_rotate="$next_rotate"
log "Secret rotated: $secret"
fi
fi
done
}
# Main rotation function
main() {
log "Starting secret rotation service"
log "Vault Address: $VAULT_ADDR"
log "Rotation Interval: $ROTATION_INTERVAL seconds"
log "Poll Interval: 300 seconds"
# Set up Vault
export VAULT_ADDR
export VAULT_TOKEN
while true; do
log "Beginning rotation cycle..."
{
# Rotate database credentials
for db_path in "database/creds/postgres" "database/creds/mysql"; do
rotate_db_credentials "$db_path" || log "Failed to rotate $db_path"
done
# Rotate API keys
for api_key_path in "secret/api-keys/sendgrid" "secret/api-keys/stripe"; do
rotate_api_keys "$api_key_path" || log "Failed to rotate $api_key_path"
done
# Rotate environment-specific secrets
for env in "staging" "production"; do
rotate_env_secrets "$env" || log "Failed to rotate secrets for $env"
done
log "Rotation cycle completed successfully"
} || {
log "Rotation cycle failed with exit code $?"
}
log "Next rotation in $ROTATION_INTERVAL seconds"
sleep "$ROTATION_INTERVAL"
done
}
main "$@"