-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl-gen.sh
executable file
·216 lines (188 loc) · 5.91 KB
/
ssl-gen.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
# generate certs for etcd or consul using CloudFlare's cfssl [201611.22MeV]
# https://www.digitalocean.com/community/tutorials/how-to-secure-your-coreos-cluster-with-tls-ssl-and-firewall-rules
# https://github.com/cloudflare/cfssl
# config file is in the form
# DNS name, internal AWS DNS, internal AWS IP, external AWS IP
if [ "`echo $PATH | grep /usr/local/bin`" == "" ]; then PATH=$PATH:/usr/local/bin; fi
function USAGE {
cat <<-EOFUSAGE
generate SSL certs for etcd or consul using config file
Usage: ${0##*/} -dhn <CONFIG_FILE>
where
-h this help function
-n dry-run (don't generate certs but loop through config file)
EOFUSAGE
exit 2
}
# parse arguments
OPT_d=1; OPT_n=1; QUIET="--quiet"
while getopts ":dhn" OPT; do
case ${OPT} in
d) OPT_D=0 # DEBUG mode
QUIET=""
;;
n) OPT_n=0 # dry-run mode
;;
h|?) USAGE
;;
esac
done
shift $((OPTIND-1)) # move to next argument after options
[ $# -lt 1 ] && echo "?config filename missing" && USAGE
[ ! -e ${1} ] && echo "?config file missing" && USAGE
## defaults
#
conf_file=${1}
tmp=${1##*/} # longest occurance of slash from back
conf_name=${tmp%%.*} # longest occurance of dot from front
cur_d=$(pwd)
ca=ca
ca_csr="/tmp/${ca}-csr.json"
ca_config="/tmp/${ca}-config.json"
# rsa/2048...ecdsa is a smaller key but takes 10x to encode/decode
key_algo=rsa
key_size=2048
cert_expire=`expr 5 \* 365 \* 24` # hours in 5 years
C="US"
L="CA"
O="Portworx_Inc"
ST="Los_Altos"
OU="testing"
[ ! -e /usr/local/bin/ ] && mkdir -P /usr/local/bin
case $(uname) in
Darwin)
if [ ! -e /usr/local/bin/cfssl ]; then
curl -s -L -o /usr/local/bin/cfssl \
https://pkg.cfssl.org/R1.2/cfssl_darwin-amd64
fi
if [ ! -e /usr/local/bin/cfssljson ]; then
curl -s -L -o /usr/local/bin/cfssljson \
https://pkg.cfssl.org/R1.2/cfssljson_darwin-amd64
fi
if [ ! -e /usr/local/bin/mkbundle ]; then
curl -s -L -o /usr/local/bin/mkbundle \
https://pkg.cfssl.org/R1.2/mkbundle_darwin-amd64
fi
;;
Linux)
if [ ! -e /usr/local/bin/cfssl ]; then
curl -s -L -o /usr/local/bin/cfssl \
https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
fi
if [ ! -e /usr/local/bin/cfssljson ]; then
curl -s -L -o /usr/local/bin/cfssljson \
https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
fi
if [ ! -e /usr/local/bin/mkbundle ]; then
curl -s -L -o /usr/local/bin/mkbundle \
https://pkg.cfssl.org/R1.2/mkbundle_linux-amd64
fi
;;
*)
echo "OS $(uname) not supported"
exit 2
;;
esac
chmod +x /usr/local/bin/{cfssl,cfssljson,mkbundle}
[ ! -e ${conf_name} ] && mkdir -p ${conf_name}
/bin/cp ${conf_file} ${conf_name}/
cd ${conf_name}
[ $OPT_n -eq 1 ] && cat <<-EOFCACONFIG > ${ca_config}
{
"signing": {
"default": {
"expiry": "${cert_expire}h"
},
"profiles": {
"server": {
"expiry": "${cert_expire}h",
"usages": [
"signing",
"key encipherment",
"server auth"
]
},
"client": {
"expiry": "${cert_expire}h",
"usages": [
"signing",
"key encipherment",
"client auth"
]
},
"client-server": {
"expiry": "${cert_expire}h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOFCACONFIG
[ $OPT_n -eq 1 ] && cat <<-EOFCACSR > ${ca_csr}
{
"CN": "${conf_name}",
"key": {
"algo": "${key_algo}",
"size": ${key_size}
},
"names": [
{
"C": "${C}",
"L": "${L}",
"O": "${O}",
"ST": "${ST}",
"OU": "${OU}"
}
]
}
EOFCACSR
# from https://github.com/coreos/docs/blob/master/os/generate-self-signed-certificates.md
# these certs work for connecting between nodes [201608.25MeV]
# this code works for using the ca_csr above or in-line
# works for etcdctl if you use server cert, key, and CA key.
echo "================================================== generating self-signed CA cert+key"
if [ ${OPT_n} -eq 1 ]; then
cfssl gencert -initca ${ca_csr} | cfssljson -bare ${ca}
mv ${ca}-key.pem ${ca}.key
chmod 600 ${ca}.key
echo -n "checking ${ca}.pem for crlsign..."
openssl verify -purpose crlsign -CAfile ${ca}.pem ${ca}.pem
openssl x509 -in ${ca}.pem -text -noout
fi
# config file is in the form: DNS-name,internal-AWS-DNS,internal-AWS-IP,external-AWS-IP
for host in $(cat ${conf_file}) ; do
#h=$(echo ${host} | sed -e 's/,/","/g' -e 's/^/"/' -e 's/$/"/')
h=$(echo ${host} | sed -e 's/,/","/g')
fqdn=$(echo ${host} | awk -F"," '{print $1}') # use 1st entry for server name
s=${fqdn%%.*} # remove longest occurance of dot from back
# NOTE: CN must match CN of CA key
f=$(printf '{"CN":"%s","hosts":["%s"],"key":{"algo":"%s","size":%s}}' ${conf_name} ${h} ${key_algo} ${key_size})
echo "================================================== generating ${s} certs+keys"
if [ $OPT_n -eq 1 ]; then
echo ${f} | cfssl gencert -ca=${ca}.pem -ca-key=${ca}.key -config=${ca_config} \
-hostname="${host}" -profile=client-server - | cfssljson -bare ${s}
echo -n "verifying with ${ca}.pem for sslserver..."
openssl verify -purpose sslserver -CAfile ${ca}.pem ${s}.pem
echo -n "verifying with ${ca}.pem for sslclient..."
openssl verify -purpose sslclient -CAfile ${ca}.pem ${s}.pem
openssl x509 -in ${s}.pem -text -noout
fi
mv ${s}-key.pem ${s}.key
chmod 600 ${s}.key
done
rm -f *.csr ${conf_file}
rm -f ${ca_csr}* ${ca_config}*
#echo "================================================== bundling all certs"
#if [ $OPT_n -eq 1 ]; then
# mkbundle -f ${conf_name}.crt .
# openssl x509 -in ${conf_name}.crt -text -noout
#fi
[ "*.pem" != "" ] && chmod 644 *.pem
cd ${cur_d}
exit