-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate_flocker_certs.sh
executable file
·352 lines (296 loc) · 9.15 KB
/
generate_flocker_certs.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash -e
#
# Script for generating Flocker certificated based on OpenSSL CLI
#
# Authors: See AUTHORS.md
# Ensure that we're in the right directory for the config paths to be correct
# when invoked from other dirs
SCRIPT_DIR="$(dirname $(readlink -f $0))"
CURRENT_DIR="$(pwd)"
cd "$SCRIPT_DIR"
HELP_MSG="""
Usage:
$0 new (-i=<control_ip> | -d=<control_fqdn>) [--force] [-f=openssl_conf] [-n=<node>[,<node> ... ]] -c=<cluster_name>
$0 node [-f=openssl_conf] [--force] -c=<cluster_name> -n=<node>[,<node> ... ]
# Positional arguments
new Creates new cluster keypair group
node Creates/signs node keypairs with existing cluster keypair
(Assumes output dir contains cluster.crt and key)
# Arguments
-i=, --control_ip= Control Service IP
-d=, --control_fqdn= Control Service FQDN
-c=, --cluster_name= Cluster name. Should be unique (Default=mycluster)
-k=, --key_size= RSA keysize (Default=4096)
-o=, --output-dir= Key destination (Default=./clusters/<cluster_name>)
-f=, --openssl_file= OpenSSL conf file location (Default=./openssl.cnf)
-n=, --nodes= Comma seperated list of nodes
--force Force overwrite of files if they already exist
# Other
-h, --help This help message
"""
# XXX: Doing the assignment after check is to ensure we have >1 args
OP_TYPE=""
if [ "${1}" == "node" ] || [ "${1}" == "new" ]; then
OP_TYPE="${1}"
shift
else
echo "ERROR! Operation type not specified!"
echo "${HELP_MSG}"
exit 1
fi
FORCE_OVERWRITE=false
for arg in "$@"; do
case $arg in
-i=*|--control_ip=*)
CONTROL_IP="${arg#*=}"
shift
;;
-d=*|--control_fqdn=*)
CONTROL_FQDN="${arg#*=}"
shift
;;
-c=*|--cluster_name=*)
CLUSTER_NAME="${arg#*=}"
shift
;;
-f=*|--openssl_file=*)
OPENSSL_FILE="${arg#*=}"
shift
;;
-n=*|--nodes=*)
NODES="${arg#*=}"
shift
;;
-o=*|--output-dir=*)
OUTPUT_DIR="${arg#*=}"
shift
;;
--force)
FORCE_OVERWRITE=true
shift
;;
-k=*|--key_size=*)
KEY_SIZE="${arg#*=}"
shift
;;
-h|--help)
echo "${HELP_MSG}" && exit 0;
shift
;;
*)
# unknown option
;;
esac
done
control_service_fqdn=${CONTROL_FQDN:=""}
control_service_ip=${CONTROL_IP:=""}
key_size=${KEY_SIZE:="4096"}
# Sanity checks
if [ ! -z "$NODES" ] && [ -z "${NODES// }" ]; then
echo "ERROR! Node list is empty! Exiting!"
echo "${HELP_MSG}"
exit 1
fi
if [ -z "$CLUSTER_NAME" ] || [ -z "${CLUSTER_NAME// }" ]; then
echo "No cluster name provided! Exiting!"
echo "${HELP_MSG}"
exit 1
fi
# Split nodes into discrete values
IFS=","
nodes=()
for node in ${NODES}; do
nodes+=( $node )
done
unset IFS
cluster_name=${CLUSTER_NAME}
openssl_cnf_path=${OPENSSL_FILE:="openssl.cnf"}
# set the CERT_HOST_ID environment variable early on since its used in
# openssl.cnf. Use FQDN first if its there.
if [ "$control_service_fqdn" != "" ]; then
export CERT_HOST_ID=DNS:control-service,DNS:$control_service_fqdn
control_host=$control_service_fqdn
elif [ "$control_service_ip" != "" ]; then
export CERT_HOST_ID=DNS:control-service,IP:$control_service_ip
control_host=$control_service_ip
elif [ ! "${OP_TYPE}" == "node" ]; then
echo "ERROR! No control service FQDN or IP provided! Exiting!"
echo "${HELP_MSG}"
exit 1
else
# XXX: The conf requires this env var even for non-ctrl-service keypair
# generation which is an error but for now we just make sure it's
# not empty
export CERT_HOST_ID=""
fi
echo "Cleaning up old CA dirs"
rm -rf flockerssl
echo "Create needed CA fs layout"
mkdir -p flockerssl/csr
mkdir -p flockerssl/newcerts
touch flockerssl/index.txt
echo '1000' > flockerssl/serial
echo 'unique_subject = no' > flockerssl/index.txt.attr
generate_key(){
# generate_key <output_path>
local output_path="${1}"
echo -n "- Generating key $output_path"
openssl genrsa -out "$output_path" $key_size &>/dev/null
echo " [OK]"
}
generate_csr() {
# generate_csr <key_path> <subject> <output_path>
local key_path="${1}"
local subject="${2}"
local output_path="${3}"
# Sanity check
if [ $# -lt 3 ]; then
echo "ERROR! generate_csr not properly invoked! Exiting!"
exit 1
fi
echo -n "- Creating csr for $key_path"
openssl req -config "$openssl_cnf_path" \
-key "$key_path" \
-new \
-days 7300 \
-sha256 \
-subj "$subject" \
-out "$output_path" &>/dev/null
echo " ($output_path) [OK]"
}
sign_csr() {
# sign_csr <csr_path> <ca_keypair> <output_path> [<extension>]
# Sanity check
if [ $# -lt 3 ]; then
echo "ERROR! sign_csr not properly invoked! Exiting!"
exit 1
fi
local csr_path="${1}"
local ca_key="${2}.key"
local ca_crt="${2}.crt"
local output_path="${3}"
local extensions=""
if [ $# -gt 3 ] && [ ! -z "${4}" ]; then
extensions="-extensions ${4}"
fi
openssl ca -batch \
-config "$openssl_cnf_path" \
-keyfile "$ca_key" \
-cert "$ca_crt" \
-days 7300 \
-notext \
-md sha256 \
$extensions \
-in "$csr_path" \
-out "$output_path"
}
generate_and_sign_cert() {
# generate_and_sign_cert <keypair_path> <subject> <ca_keypair> [<extensions>]
# Sanity check
if [ $# -lt 3 ]; then
echo "ERROR! generate_and_sign_cert not properly invoked! Exiting!"
exit 1
fi
local key_path="${1}.key"
local crt_output="${1}.crt"
local subject="${2}"
local ca_keypair="${3}"
local extensions="${4}"
local temp_csr_path=$(mktemp -q "$(basename $0).XXXXX.tmp")
generate_key "$key_path"
generate_csr "$key_path" "$subject" "$temp_csr_path"
sign_csr "$temp_csr_path" \
"$ca_keypair" \
"$crt_output" \
"$extensions"
rm -f "$temp_csr_path"
}
generate_and_sign_node_certs() {
# generate_and_sign_node_certs <ca_keypair> <nodes> <output_dir>
# Sanity check
if [ $# -lt 3 ]; then
echo "ERROR! generate_and_sign_node_certs not properly invoked! Exiting!"
exit 1
fi
local ca_keypair="${1}"
local nodes=("${!2}") # De-ref the array name
local output_dir="${3}"
echo "- Will create ${#nodes[@]} node keypair(s)"
local cluster_uuid=$(openssl x509 -noout -subject -in $ca_keypair.crt \
| sed -e 's/.*\/OU=\(.*\)[\/]*.*/\1/')
echo "- Using Cluster UUID: $cluster_uuid"
for node_hostname in ${nodes[@]}; do
if [ -e "$output_dir/node-${node_hostname}.key" ] && \
[ ! "$FORCE_OVERWRITE" == "true" ]; then
echo "Node '$node_hostname' keypair already created. Skipping"
continue
fi
generate_and_sign_cert "$output_dir/node-$node_hostname" \
"/CN=node-$(uuidgen)/OU=$cluster_uuid" \
"$ca_keypair"
done
}
# If we want an output path and its relative, we need to root it in our
# invocation directory and not where we are right now
if [ ! -z "$OUTPUT_DIR" ] && [ ! "${OUTPUT_DIR:0:1}" = "/" ]; then
OUTPUT_DIR="$CURRENT_DIR/$OUTPUT_DIR"
fi
output_dir=${OUTPUT_DIR:="$CURRENT_DIR/clusters/$cluster_name"}
echo "Output directory is $output_dir"
if [ -d $output_dir ]; then
if [ "$FORCE_OVERWRITE" == "true" ]; then
echo "Forcefuly removing old data from old cluster"
rm -f "${output_dir}/*.crt"
rm -f "${output_dir}/*.key"
elif [ "$OP_TYPE" == "new" ]; then
echo "ERROR! Cluster already created and '--force' not specified! Exiting!"
exit 1
fi
fi
mkdir -p $output_dir
cluster_keypair_path="$output_dir/cluster"
# Special case of us needing to create/sign node certs
if [ "${OP_TYPE}" == "node" ]; then
echo "Generating and signing node keypair(s)"
# XXX: Array passed as a name, not a value
generate_and_sign_node_certs $cluster_keypair_path \
"nodes[@]" \
$output_dir
echo
exit
fi
cluster_uuid=$(uuidgen)
echo "Generating the CA keypair (cluster_keypair_path)"
generate_key "$cluster_keypair_path.key"
echo -n "Self-signing CA cert"
openssl req -batch \
-config "$openssl_cnf_path" \
-key "${cluster_keypair_path}.key" \
-new \
-x509 \
-days 7300 \
-sha256 \
-extensions v3_ca \
-subj "/CN=$cluster_name/OU=$cluster_uuid" \
-out "${cluster_keypair_path}.crt"
echo " [OK]"
echo
echo "Generating the control service keypair"
generate_and_sign_cert "$output_dir/control-service" \
"/CN=control-service/OU=$cluster_uuid" \
"$cluster_keypair_path" \
"control_service_extension"
echo
echo "Generating node keypair(s)"
# XXX: Array passed as a name, not a value
generate_and_sign_node_certs $cluster_keypair_path \
"nodes[@]" \
$output_dir
echo
echo "Generating API keypair"
api_username=plugin
generate_and_sign_cert "$output_dir/$api_username" \
"/CN=user-$api_username/OU=$cluster_uuid" \
"$cluster_keypair_path" \
"client_api_ext"
echo "Done!"