-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_certificates.sh
executable file
·82 lines (69 loc) · 2.44 KB
/
create_certificates.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
75
76
77
78
79
80
81
82
#!/bin/bash
OUTPUT_DIR="./certs"
mkdir -p $OUTPUT_DIR
for row in $(cat conf.json | jq -c '.[]'); do
_jq() {
echo ${row} | jq -r ${1}
}
COUNTRY=$(_jq '.country')
STATE=$(_jq '.state')
LOCALITY=$(_jq '.locality')
ORGANIZATION=$(_jq '.organization')
ORGANIZATION_UNIT=$(_jq '.organization_unit')
COMMON_NAME=$(_jq '.common_name')
DOMAINS=$(_jq '.domains')
OUTPUT_NAME=$(_jq '.output_name')
EXPIRE_DAYS=$(_jq '.expire_days')
SKIP_IF_FOUND=$(_jq '.skip_if_found')
I=1
if [ "$SKIP_IF_FOUND" = true ]; then
if [ -f "$OUTPUT_DIR/$OUTPUT_NAME.key" ]; then
# private key already exists, so skip
echo "Skipped $COMMON_NAME, since $OUTPUT_DIR/$OUTPUT_NAME.key was found and skip_if_found was set."
continue;
fi
fi
DNS=""
for domain in $(echo "${DOMAINS}" | jq -r '.[]'); do
DOMAIN=${domain}
if [ -z "$DNS" ]; then
DNS="DNS.$I:"$DOMAIN
else
DNS=$DNS",DNS.$I:"$DOMAIN
fi
I=$(($I + 1))
done
# Generate private key
openssl genrsa -out $OUTPUT_DIR/$OUTPUT_NAME.key 4096
# ugly but working
if [ -z "$DNS" ]; then
openssl req -new -sha256 \
-out $OUTPUT_DIR/$OUTPUT_NAME.csr \
-key $OUTPUT_DIR/$OUTPUT_NAME.key \
-subj "/C=$COUNTRY/ST=$STATE/L=$LOCALITY/O=$ORGANIZATION/OU=$ORGANIZATION_UNIT/CN=$COMMON_NAME"
openssl x509 -req \
-sha256 \
-days $EXPIRE_DAYS \
-in $OUTPUT_DIR/$OUTPUT_NAME.csr \
-signkey $OUTPUT_DIR/$OUTPUT_NAME.key \
-out $OUTPUT_DIR/$OUTPUT_NAME.crt
else
openssl req -new -sha256 \
-out $OUTPUT_DIR/$OUTPUT_NAME.csr \
-key $OUTPUT_DIR/$OUTPUT_NAME.key \
-config <(cat ./ssl.conf <(printf "subjectAltName=$DNS")) \
-extensions v3_req \
-subj "/C=$COUNTRY/ST=$STATE/L=$LOCALITY/O=$ORGANIZATION/OU=$ORGANIZATION_UNIT/CN=$COMMON_NAME"
openssl x509 -req \
-sha256 \
-days $EXPIRE_DAYS \
-in $OUTPUT_DIR/$OUTPUT_NAME.csr \
-signkey $OUTPUT_DIR/$OUTPUT_NAME.key \
-out $OUTPUT_DIR/$OUTPUT_NAME.crt \
-extfile <(cat ./ssl.conf <(printf "subjectAltName=$DNS")) \
-extensions v3_req
fi
openssl rsa -in $OUTPUT_DIR/$OUTPUT_NAME.key -text > $OUTPUT_DIR/$OUTPUT_NAME-key.pem
openssl x509 -inform PEM -in $OUTPUT_DIR/$OUTPUT_NAME.crt > $OUTPUT_DIR/$OUTPUT_NAME-cert.pem
done
exit 0