-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsvc-bip-api-certgen.sh
executable file
·74 lines (59 loc) · 2.7 KB
/
svc-bip-api-certgen.sh
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
#!/bin/bash
# Purpose: This script is used to generate keystore, truststore base64 encoded strings and password
# for svc-bip-api service
#
# output.json contains 3 key-value pairs, which should be pasted into to Vault
#
# Following keys/values are generated
# BIP_KEYSTORE_BASE64
# BIP_PASSWORD
# BIP_TRUSTSTORE_BASE64
#
# Pre-requisite:
# - get kube_config file from GFE machine and set it as the default config
# - install kubectl tool on local machine
# Input:
# $1 Input environment name like dev, qa, sandbox, prod-test, prod. Defaulted to dev if input is not provided
# Set the default environment to 'dev'
# Set default environment
env=${1:-"dev"}
# Check if kubectl is installed
if ! command -v kubectl &> /dev/null; then
echo "Error: kubectl is not installed."
exit 1
fi
echo "Using environment: $env"
secret_name="va-abd-rrd-${env}-va-gov-tls"
# Clean up existing files
rm -f ca.crt tls.crt tls.key truststore.p12 keystore.p12 output.json VA-Internal-S2-ICA11.cer VA-Internal-S2-RCA2.cer
# Get and process the Kubernetes secret
secret_yaml=$(kubectl get secret "$secret_name" -o yaml)
if [ $? -ne 0 ]; then
echo "Failed to get secret from kubectl"
exit 1
fi
# Function to extract, decode, and save data
extract_and_save() {
echo "$secret_yaml" | grep "$1" | awk '{print $2}' | base64 --decode > "$1"
}
# Extract, decode, and save certificates and key
extract_and_save "ca.crt"
extract_and_save "tls.crt"
extract_and_save "tls.key"
# Download the specific certificates
curl -o VA-Internal-S2-ICA11.cer "http://aia.pki.va.gov/PKI/AIA/VA/VA-Internal-S2-ICA11.cer"
curl -o VA-Internal-S2-RCA2.cer "http://aia.pki.va.gov/PKI/AIA/VA/VA-Internal-S2-RCA2.cer"
# Generate a random password
PASSWORD=$(openssl rand -base64 20 | tr -dc 'A-Za-z0-9@#$%^&*()_-+=' | head -c 16)
# Create PKCS12 Keystore
openssl pkcs12 -export -in tls.crt -inkey tls.key -out keystore.p12 -passout pass:"$PASSWORD"
# Create PKCS12 Truststore and import CA certificates
keytool -import -trustcacerts -alias va_internal_s2_ica11 -file VA-Internal-S2-ICA11.cer -keystore truststore.p12 -storetype PKCS12 -storepass "$PASSWORD" -noprompt
keytool -import -trustcacerts -alias va_internal_s2_rca2 -file VA-Internal-S2-RCA2.cer -keystore truststore.p12 -storetype PKCS12 -storepass "$PASSWORD" -noprompt
# Base64 encode keystore and truststore
keystore=$(base64 < keystore.p12 | tr -d '\n')
truststore=$(base64 < truststore.p12 | tr -d '\n')
# Create output JSON
echo -e "{\n\"BIP_KEYSTORE_BASE64\": \"$keystore\",\n\"BIP_PASSWORD\": \"$PASSWORD\",\n\"BIP_TRUSTSTORE_BASE64\": \"$truststore\"\n}" > output.json
# Cleanup
rm -f keystore.p12 ca.crt tls.crt tls.key truststore.p12 VA-Internal-S2-ICA11.cer VA-Internal-S2-RCA2.cer