-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtruststore.sh
More file actions
executable file
·408 lines (333 loc) · 14.1 KB
/
truststore.sh
File metadata and controls
executable file
·408 lines (333 loc) · 14.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/bin/bash
# Script to manage the NRLF truststore files
#
# Note that all creative and destructive operations are local only apart from
# "push-all". Files are not uploaded to or deleted from S3 automatically.
#
# Once changes have been made locally and have been tested thoroughly, the files
# in the truststore directories can be uploaded to S3 using the appropriate AWS
# CLI commands or the "push-all" command below.
#
set -o errexit -o pipefail -o nounset
BUCKET="nhsd-nrlf--truststore"
function _truststore_help() {
echo
echo "$0 <command> [options]"
echo
echo "commands:"
echo " help - this help screen"
echo " build-ca <name> <fqdn> - Build a single CA cert"
echo " build-cert <name> <ca> <fqdn> - Build a single client cert + private key"
echo " build-all - Build the standard trust store certs"
echo " pull-ca <ca> - Pull the certificate authority"
echo " pull-ca-key <ca> - Pull the certificate authority private key"
echo " pull-client <env> - pull the files needed for a client connection"
echo " pull-server <env> - pull the files needed for a server connection"
echo " pull-all-for-account <acc> - pull all the truststore files for all environments in a given account"
echo " pull-all <env> - pull all the truststore files for an environment"
echo " push-all <env> - push all the truststore files for an environment"
echo " rotate-ca <env> - rotate the certificate authority, archiving the previous one"
echo " rotate-cert <env> - rotate the client certificate, archiving the previous one"
echo " disable-archived-ca <env> - disable an archived certificate authority"
echo " restore-archived-ca <env> - restore an archived certificate authority"
echo " restore-archived-cert <env> - restore an archived client certificate"
echo
return 0
}
# read an input file and substitute all the ${} entries
function substitute_env_in_file() {
infile=$1
outfile=$2
output=$(eval "cat <<EOF
$(cat ${infile})
EOF"
)
cat > ${outfile} <<EOF
${output}
EOF
return 0
}
# build a certificate authority
function _truststore_build_ca() {
if [[ $# -ne 2 ]]; then
echo "Usage: $0 build-ca <name> <fqdn>"
return 1
fi
env=$1
fqdn=$2
substitute_env_in_file ./truststore/config/ca.conf /tmp/ca.conf
echo -e "🚧 Building CA: $env \t(FQDN: $fqdn)"
openssl req -newkey rsa:4096 \
-nodes \
-keyout ./truststore/ca/$env.key \
-new \
-x509 \
-days 36500 \
-out ./truststore/ca/$env.crt \
-config /tmp/ca.conf \
-extensions v3_req \
-extensions v3_ca &> /dev/null
rm /tmp/ca.conf
echo -e "✅ Successfully Built CA: $env (FQDN: $fqdn)"
cat ./truststore/ca/$env.crt > ./truststore/server/$env.pem
echo -e "✅ Successfully Built Server Truststore: $env"
return 0
}
# build a certificate
function _truststore_build_cert() {
if [[ $# -ne 3 ]]; then
echo "Usage: $0 build-cert <name> <ca> <fqdn>"
return 1;
fi
cert_name=$1
ca_name=$2
fqdn=$3
serial=$(date +%s%3N)
substitute_env_in_file ./truststore/config/client.conf /tmp/client.conf
echo -e "🚧 Generating $cert_name keypair"
openssl req \
-newkey rsa:4096 \
-nodes \
-keyout truststore/client/$cert_name.key \
-new \
-out truststore/client/$cert_name.csr \
-config /tmp/client.conf \
-extensions v3_req \
-extensions usr_cert &> /dev/null
echo -e "🚧 Signing $cert_name certificate with ca=$ca_name (serial: $serial)"
openssl x509 \
-req \
-in truststore/client/$cert_name.csr \
-CA truststore/ca/$ca_name.crt \
-CAkey truststore/ca/$ca_name.key \
-set_serial $serial \
-out truststore/client/$cert_name.crt \
-days 36500 \
-sha256 \
-extfile /tmp/client.conf \
-extensions v3_req \
-extensions usr_cert &> /dev/null
echo -e "✅ Successfully generated $cert_name keypair for ca=$ca_name (FQDN: $fqdn)"
rm /tmp/client.conf
rm truststore/client/$cert_name.csr
return 0
}
# Rotate a CA - archive the existing CA and build a new one
# The previous CA is added to the new CA bundle (can be removed after clients have updated)
function _truststore_rotate_ca() {
if [[ $# -ne 2 ]]; then
echo "Usage: $0 rotate-ca <env> <fqdn>"
return 1;
fi
env="$1"
fqdn="$2"
if [[ ! -f "truststore/ca/$env.crt" ]] ||
[[ ! -f "truststore/ca/$env.key" ]] ||
[[ ! -f "truststore/server/$env.pem" ]]; then
echo "Error: One or more ca cert truststore files not found for environment $env - cannot rotate CA" 1>&2
echo "Try running this first:" 1>&2
echo " $0 pull-all $env && $0 pull-ca-key dev" 1>&2
return 1
fi
# Archive the existing ca certs
archive_date="$(date +%Y-%m-%d)"
if [[ -f "truststore/ca/$env.archived_$archive_date.crt" ]] ||
[[ -f "truststore/ca/$env.archived_$archive_date.key" ]] ||
[[ -f "truststore/server/$env.archived_$archive_date.pem" ]]; then
echo "Error: Archive files already exist for date $archive_date - please resolve before rotating the CA" 1>&2
return 1
fi
mv "truststore/ca/$env.crt" "truststore/ca/$env.archived_$archive_date.crt"
mv "truststore/ca/$env.key" "truststore/ca/$env.archived_$archive_date.key"
mv "truststore/server/$env.pem" "truststore/server/$env.archived_$archive_date.pem"
# Build a new CA
_truststore_build_ca "$env" "$fqdn"
# Add the previous CA to the new CA bundle (can be removed after clients have updated)
cat "truststore/ca/$env.archived_$archive_date.crt" >> "truststore/server/$env.pem"
echo -e "✅ Successfully rotated CA for $env - previous CA archived with date: $archive_date"
return 0
}
# Rotate a client cert - archive the existing cert and build a new one
function _truststore_rotate_cert() {
if [[ $# -ne 3 ]]; then
echo "Usage: $0 rotate-cert <env> <ca> <fqdn>"
exit 1;
fi
cert_name="$1"
ca_name="$2"
fqdn="$3"
if [[ ! -f "truststore/client/$cert_name.crt" ]] ||
[[ ! -f "truststore/client/$cert_name.key" ]]; then
echo "Error: One or more client cert truststore files not found for $cert_name - cannot rotate client cert" 1>&2
echo "Try running this first:" 1>&2
echo " $0 pull-all $cert_name" 1>&2
return 1
fi
# Archive the existing client certs
archive_date=$(date +%Y-%m-%d)
if [[ -f "truststore/client/$cert_name.archived_$archive_date.crt" ]] ||
[[ -f "truststore/client/$cert_name.archived_$archive_date.key" ]]; then
echo "Error: Archive files already exist for date $archive_date - please resolve before rotating the client cert" 1>&2
return 1
fi
mv "truststore/client/$cert_name.crt" "truststore/client/$cert_name.archived_$archive_date.crt"
mv "truststore/client/$cert_name.key" "truststore/client/$cert_name.archived_$archive_date.key"
# Build a new client cert
_truststore_build_cert "$cert_name" "$ca_name" "$fqdn"
echo -e "✅ Successfully rotated client cert for $cert_name - previous cert archived with date: $archive_date"
return 0
}
# Disable an archived CA by removing it from the server pem file
function _disable_archived_ca() {
env="$1"
cat "truststore/ca/$env.crt" > "truststore/server/$env.pem"
echo -e "✅ Successfully disabled archived CA for $env"
return 0
}
# Restore an archived CA by moving the archived files back to their original names
function _restore_archived_ca() {
env="$1"
archive_date="$2"
mv "truststore/ca/$env.archived_$archive_date.crt" "truststore/ca/$env.crt"
mv "truststore/ca/$env.archived_$archive_date.key" "truststore/ca/$env.key"
mv "truststore/server/$env.archived_$archive_date.pem" "truststore/server/$env.pem"
echo -e "✅ Successfully restored archived CA for $env from date: $archive_date"
return 0
}
# Restore an archived client cert by moving the archived files back to their original names
function _restore_archived_cert() {
env="$1"
archive_date="$2"
mv "truststore/client/$env.archived_$archive_date.crt" "truststore/client/$env.crt"
mv "truststore/client/$env.archived_$archive_date.key" "truststore/client/$env.key"
echo -e "✅ Successfully restored archived client cert for $env from date: $archive_date"
return 0
}
function _truststore_build_all() {
_truststore_build_ca "prod" "record-locator.national.nhs.uk_CA2"
_truststore_build_ca "int" "record-locator.int.national.nhs.uk_CA2"
_truststore_build_ca "ref" "record-locator.ref.national.nhs.uk_CA2"
_truststore_build_ca "perftest" "perftest.record-locator.national.nhs.uk_CA2"
_truststore_build_ca "qa" "qa.record-locator.national.nhs.uk_CA2"
_truststore_build_ca "dev" "record-locator.dev.national.nhs.uk_CA2"
_truststore_build_cert "prod" "prod" "api.record-locator.national.nhs.uk"
_truststore_build_cert "int" "int" "int.api.record-locator.int.national.nhs.uk"
_truststore_build_cert "ref" "ref" "ref.api.record-locator.ref.national.nhs.uk"
_truststore_build_cert "perftest" "perftest" "api.perftest.record-locator.national.nhs.uk"
_truststore_build_cert "qa" "qa" "api.qa.record-locator.national.nhs.uk"
_truststore_build_cert "dev" "dev" "dev.api.record-locator.dev.national.nhs.uk"
echo -e "✅ Successfully built all truststore files"
return 0
}
function _truststore_pull_ca() {
env=$1
echo "Pulling ${env} ca certificate"
aws s3 cp "s3://${BUCKET}/ca/${env}.crt" "truststore/ca/${env}.crt"
echo -e "✅ Successfully pulled ${env} ca certificate from s3://${BUCKET}"
return 0
}
function _truststore_pull_ca_key() {
env=$1
echo "Pulling ${env} ca private key"
aws s3 cp "s3://${BUCKET}/ca/${env}.key" "truststore/ca/${env}.key"
echo -e "✅ Successfully pulled ${env} ca private key from s3://${BUCKET}"
return 0
}
function _truststore_pull_client() {
env=$1
echo "Pulling ${env} client certificate"
aws s3 cp "s3://${BUCKET}/client/${env}.key" "truststore/client/${env}.key"
aws s3 cp "s3://${BUCKET}/client/${env}.crt" "truststore/client/${env}.crt"
echo -e "✅ Successfully pulled ${env} client truststore files from s3://${BUCKET}"
return 0
}
function _truststore_pull_server() {
env=$1
echo "Pulling ${env} server trust certificate"
aws s3 cp "s3://${BUCKET}/server/${env}.pem" "truststore/server/${env}.pem"
echo -e "✅ Successfully pulled ${env} server truststore files from s3://${BUCKET}"
return 0
}
function _truststore_pull_all() {
env=$1
_truststore_pull_ca $env
_truststore_pull_client $env
_truststore_pull_server $env
echo -e "✅ Successfully pulled all ${env} truststore files from s3://${BUCKET}"
return 0
}
function _truststore_pull_all_for_account() {
account=$1
# sets envs_array
source ./scripts/get-envs-for-account.sh $account
for env in ${envs_array[@]}; do
# don't need to pull sandbox certs
if [[ $env != *"-sandbox" ]];
then
echo "⏳ Pulling ${env} truststore certs"
_truststore_pull_ca $env
_truststore_pull_client $env
_truststore_pull_server $env
fi
done
echo -e "✅ Successfully pulled all ${account} truststore files from s3://${BUCKET}"
return 0
}
function _truststore_push_all() {
env=$1
timestamp="$(date +%Y-%m-%d_%H%M%S)"
backup_dir="truststore/backup/${env}_${timestamp}"
echo -e "Backing up existing files to ${backup_dir}...."
mkdir -p "${backup_dir}/ca" "${backup_dir}/client" "${backup_dir}/server"
for f in "ca/${env}.crt" "ca/${env}.key" "client/${env}.crt" "client/${env}.key" "server/${env}.pem"
do
echo
aws s3 cp "s3://${BUCKET}/$f" "${backup_dir}/$f" || echo "No existing file s3://${BUCKET}/$f to back up"
if [[ -f "${backup_dir}/$f" ]]
then
diff --brief "truststore/$f" "${backup_dir}/$f" || true
fi
done
echo
echo -n "WARNING: You are about to upload files to the ${env} truststore - are you sure? [yes/NO] "
read answer
if [[ "$answer" != "yes" ]]; then
echo "Aborting upload to ${env} truststore" 1>&2
return 1
fi
echo "Uploading ${env} ca certificate"
aws s3 cp "truststore/ca/${env}.crt" "s3://${BUCKET}/ca/${env}.crt"
echo "Uploading ${env} ca private key"
aws s3 cp "truststore/ca/${env}.key" "s3://${BUCKET}/ca/${env}.key"
echo "Uploading ${env} client certificate"
aws s3 cp "truststore/client/${env}.key" "s3://${BUCKET}/client/${env}.key"
aws s3 cp "truststore/client/${env}.crt" "s3://${BUCKET}/client/${env}.crt"
echo "Uploading ${env} server trust certificate"
aws s3 cp "truststore/server/${env}.pem" "s3://${BUCKET}/server/${env}.pem"
echo -e "✅ Successfully uploaded all ${env} truststore files to s3://${BUCKET}"
return 0
}
function _truststore() {
local command=$1; shift
local args=$@
case $command in
"build-all") _truststore_build_all $args ;;
"build-ca") _truststore_build_ca $args ;;
"build-cert") _truststore_build_cert $args ;;
"pull-all") _truststore_pull_all $args ;;
"pull-all-for-account") _truststore_pull_all_for_account $args ;;
"pull-server") _truststore_pull_server $args ;;
"pull-client") _truststore_pull_client $args ;;
"pull-ca") _truststore_pull_ca $args ;;
"pull-ca-key") _truststore_pull_ca_key $args ;;
"push-all") _truststore_push_all $args ;;
"rotate-ca") _truststore_rotate_ca $args ;;
"rotate-cert") _truststore_rotate_cert $args ;;
"disable-archived-ca") _disable_archived_ca $args ;;
"restore-archived-ca") _restore_archived_ca $args ;;
"restore-archived-cert") _restore_archived_cert $args ;;
*) _truststore_help $args ;;
esac
return 0
}
_truststore $@