-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_keys.sh
More file actions
executable file
·86 lines (74 loc) · 2 KB
/
encrypt_keys.sh
File metadata and controls
executable file
·86 lines (74 loc) · 2 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
#!/bin/bash
#
# API Cockpit - Key Encryption Utility
# Encrypts/decrypts API keys using openssl AES-256-CBC
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_DIR="${SCRIPT_DIR}/config"
KEY_FILE="${CONFIG_DIR}/.key" # Master encryption key
# Generate a random master key if not exists
generate_key() {
if [ ! -f "${KEY_FILE}" ]; then
openssl rand -base64 32 > "${KEY_FILE}"
chmod 600 "${KEY_FILE}"
echo "Master key generated at ${KEY_FILE}"
fi
}
# Encrypt a value
encrypt() {
local value="$1"
if [ -f "${KEY_FILE}" ]; then
echo "${value}" | openssl enc -aes-256-cbc -salt -pbkdf2 -pass file:"${KEY_FILE}" -base64
else
echo "${value}" # Fallback: no encryption if key missing
fi
}
# Decrypt a value
decrypt() {
local encrypted="$1"
if [ -f "${KEY_FILE}" ]; then
echo "${encrypted}" | openssl enc -aes-256-cbc -d -pbkdf2 -pass file:"${KEY_FILE}" -base64
else
echo "${encrypted}" # Fallback: assume plain text
fi
}
# Encrypt .env file
encrypt_env() {
local input="$1"
local output="${input}.enc"
if [ ! -f "${input}" ]; then
echo "Error: ${input} not found"
exit 1
fi
generate_key
openssl enc -aes-256-cbc -salt -pbkdf2 -pass file:"${KEY_FILE}" -in "${input}" -out "${output}"
rm "${input}"
echo "Encrypted ${input} -> ${output}"
}
# Decrypt .env file
decrypt_env() {
local input="$1"
local output="${input%.enc}"
if [ ! -f "${input}" ]; then
echo "Error: ${input} not found"
exit 1
fi
openssl enc -aes-256-cbc -d -pbkdf2 -pass file:"${KEY_FILE}" -in "${input}" -out "${output}"
echo "Decrypted ${input} -> ${output}"
}
# Main
case "${1:-}" in
encrypt)
encrypt_env "${2:-config/.env}"
;;
decrypt)
decrypt_env "${2:-config/.env.enc}"
;;
generate)
generate_key
;;
*)
echo "Usage: $0 {encrypt|decrypt|generate} [file]"
;;
esac