Skip to content

Commit b92f851

Browse files
committed
Add bash scripts for encrypting and decrypting secrets
Introduces bash scripts to encrypt and decrypt environment and key files for CI/CD workflows. Includes shared config and functions, main scripts for encryption/decryption, and a test script for decryption. Scripts use OpenSSL and generate a report for encrypted files.
1 parent a007fde commit b92f851

5 files changed

Lines changed: 193 additions & 0 deletions

File tree

bash-sample/decrypt_secrets.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# decrypt_secrets.sh
4+
# Decrypts encrypted environment and key files for CI/CD
5+
# Usage: ./decrypt_secrets.sh <ENCRYPTION_PASSWORD>
6+
7+
set -e # Exit on error
8+
9+
# Load shared config
10+
source "./encrypt_config.sh"
11+
source "./encrypt_functions.sh"
12+
13+
# Check if encryption password is provided
14+
if [ -z "$1" ]; then
15+
echo -e "${RED}❌ Error: Encryption password not provided${NC}"
16+
echo "Usage: $0 <ENCRYPTION_PASSWORD>"
17+
exit 1
18+
fi
19+
20+
ENCRYPTION_PASSWORD="$1"
21+
22+
echo -e "${YELLOW}🔐 Starting decryption process...${NC}"
23+
echo ""
24+
25+
decrypt_file "$OUT_DIR/$ENV_OUT" "$ENV_IN"
26+
decrypt_file "$OUT_DIR/$KEYSTORE_OUT" "$KEYSTORE_IN"
27+
decrypt_file "$OUT_DIR/$KEYPROPERTIES_OUT" "$KEYPROPERTIES_IN"
28+
decrypt_file "$OUT_DIR/$SERVICE_KEY_OUT" "$SERVICE_KEY_IN"
29+
30+
cp "$ENV_IN" ".env"
31+
32+
echo ""
33+
echo ""
34+
35+
echo -e "${GREEN}🎉 All secrets decrypted successfully!${NC}"

bash-sample/encrypt_config.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Colors for output
2+
GREEN='\033[0;32m'
3+
YELLOW='\033[1;33m'
4+
RED='\033[0;31m'
5+
BLUE='\033[0;34m'
6+
NC='\033[0m' # No Color
7+
8+
# Files and DIRs
9+
OUT_DIR="enc_keys"
10+
TEST_OUT_DIR="test_dec_keys"
11+
12+
ENV_IN=".env.prod"
13+
ENV_OUT=".env.prod.encrypted"
14+
15+
KEYSTORE_IN="android/app/keystore.jks"
16+
KEYSTORE_OUT="keystore.jks.encrypted"
17+
18+
KEYPROPERTIES_IN="android/key.properties"
19+
KEYPROPERTIES_OUT="key.properties.encrypted"
20+
21+
SERVICE_KEY_IN="android/service-key.json"
22+
SERVICE_KEY_OUT="service-key.json.encrypted"
23+
24+
REPORT_FILE="$OUT_DIR/report.txt"

bash-sample/encrypt_functions.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
decrypt_file() {
3+
local IN_FILE="$1"
4+
local OUT_FILE="$2"
5+
6+
echo -e "${YELLOW}📄 Decrypting $IN_FILE...${NC}"
7+
8+
if openssl enc -aes-256-cbc -d -pbkdf2 \
9+
-in "$IN_FILE" \
10+
-out "$OUT_FILE" \
11+
-k "$ENCRYPTION_PASSWORD"; then
12+
13+
echo -e "${GREEN}$IN_FILE decrypted successfully -> $OUT_FILE ${NC}"
14+
else
15+
echo -e "${RED}❌ Failed to decrypt $IN_FILE${NC}" >&2
16+
exit 1
17+
fi
18+
19+
echo ""
20+
}
21+
22+
encrypt_file() {
23+
local IN_FILE="$1"
24+
local OUT_FILE="$2"
25+
26+
echo -e "${YELLOW}📦 Encrypting $IN_FILE...${NC}"
27+
28+
if openssl enc -aes-256-cbc -salt -pbkdf2 \
29+
-in "$IN_FILE" \
30+
-out "$OUT_FILE" \
31+
-k "$ENCRYPTION_PASSWORD"; then
32+
33+
echo -e "${GREEN}$IN_FILE encrypted successfully -> $OUT_FILE${NC}"
34+
35+
local FILE_SIZE LINE_COUNT LAST_MODIFIED
36+
37+
FILE_SIZE=$(stat -c%s "$IN_FILE" 2>/dev/null || stat -f%z "$IN_FILE")
38+
LINE_COUNT=$(wc -l < "$IN_FILE")
39+
LAST_MODIFIED=$(stat -c%y "$IN_FILE" 2>/dev/null || stat -f"%Sm" -t "%Y-%m-%d %H:%M:%S" "$IN_FILE")
40+
41+
{
42+
echo "File: $IN_FILE"
43+
echo "Encrypted As: $OUT_FILE"
44+
echo "Size (bytes): $FILE_SIZE"
45+
echo "Lines: $LINE_COUNT"
46+
echo "Last Modified: $LAST_MODIFIED"
47+
echo "----------------------------------------"
48+
echo ""
49+
} >> "$REPORT_FILE"
50+
51+
else
52+
echo -e "${RED}❌ Failed to encrypt $IN_FILE${NC}" >&2
53+
exit 1
54+
fi
55+
echo ""
56+
}

bash-sample/encrypt_secrets.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# encrypt_secrets.sh
4+
# Encrypts environment and key files for CI/CD
5+
# Usage: ./encrypt_secrets.sh then enter password when prompted
6+
7+
set -e # Exit on error
8+
9+
# Load shared config
10+
source "./encrypt_config.sh"
11+
source "./encrypt_functions.sh"
12+
13+
14+
echo -e "${BLUE}🔐 Enter password to encrypt your secrets:${NC}"
15+
read -s ENCRYPTION_PASSWORD
16+
17+
echo ""
18+
echo -e "${BLUE}🔑 (Optional) Enter a password hint (leave blank to skip):${NC}"
19+
read PASSWORD_HINT
20+
21+
echo ""
22+
echo -e "${BLUE}📝 (Optional) Enter a short note (leave blank for default):${NC}"
23+
read SHORT_NOTE
24+
25+
if [ -z "$SHORT_NOTE" ]; then
26+
SHORT_NOTE="Encrypted secrets for CI/CD"
27+
fi
28+
29+
echo ""
30+
31+
mkdir -p "$OUT_DIR"
32+
33+
{
34+
echo "Encryption Report"
35+
echo "================="
36+
echo ""
37+
echo "Note: $SHORT_NOTE"
38+
echo "Password Hint: ${PASSWORD_HINT:-N/A}"
39+
echo "Created at: $(date)"
40+
echo "================="
41+
echo ""
42+
echo ""
43+
} > "$REPORT_FILE"
44+
45+
encrypt_file "$ENV_IN" "$OUT_DIR/$ENV_OUT"
46+
encrypt_file "$KEYSTORE_IN" "$OUT_DIR/$KEYSTORE_OUT"
47+
encrypt_file "$KEYPROPERTIES_IN" "$OUT_DIR/$KEYPROPERTIES_OUT"
48+
encrypt_file "$SERVICE_KEY_IN" "$OUT_DIR/$SERVICE_KEY_OUT"
49+
50+
echo ""
51+
echo -e "${GREEN}✅ Encryption complete. All files saved to $OUT_DIR${NC}"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# test_decrypt_secrets.sh
4+
# Decrypts encrypted environment and key files for CI/CD
5+
# Usage: ./test_decrypt_secrets.sh then enter password when prompted
6+
7+
set -e # Exit on error
8+
9+
# Load shared config
10+
source "./encrypt_config.sh"
11+
source "./encrypt_functions.sh"
12+
13+
14+
echo -e "${BLUE}🔐 [TEST] - Enter password to encrypt your secrets:${NC}"
15+
read -s ENCRYPTION_PASSWORD
16+
17+
mkdir -p "$TEST_OUT_DIR"
18+
19+
echo -e "${YELLOW}🔐 [TEST] - Starting decryption process...${NC}"
20+
echo ""
21+
22+
decrypt_file "$OUT_DIR/$ENV_OUT" "$TEST_OUT_DIR/.env.prod"
23+
decrypt_file "$OUT_DIR/$KEYSTORE_OUT" "$TEST_OUT_DIR/keystore.jks"
24+
decrypt_file "$OUT_DIR/$KEYPROPERTIES_OUT" "$TEST_OUT_DIR/key.properties"
25+
decrypt_file "$OUT_DIR/$SERVICE_KEY_OUT" "$TEST_OUT_DIR/service-key.json"
26+
27+
echo -e "${GREEN}🎉 All secrets decrypted successfully!${NC}"

0 commit comments

Comments
 (0)