4545 cat > ${outfile} << EOF
4646${output}
4747EOF
48+
49+ return 0
4850}
4951
5052# build a certificate authority
5153function _truststore_build_ca() {
52- if [ $# -ne 2 ]; then
54+ if [[ $# -ne 2 ] ]; then
5355 echo " Usage: $0 build-ca <name> <fqdn>"
54- exit 1
56+ return 1
5557 fi
5658
5759 env=$1
@@ -77,13 +79,14 @@ function _truststore_build_ca() {
7779
7880 cat ./truststore/ca/$env .crt > ./truststore/server/$env .pem
7981 echo -e " ✅ Successfully Built Server Truststore: $env "
82+ return 0
8083}
8184
8285# build a certificate
8386function _truststore_build_cert() {
84- if [ $# -ne 3 ]; then
87+ if [[ $# -ne 3 ] ]; then
8588 echo " Usage: $0 build-cert <name> <ca> <fqdn>"
86- exit 1;
89+ return 1;
8790 fi
8891
8992 cert_name=$1
@@ -123,98 +126,104 @@ function _truststore_build_cert() {
123126 echo -e " ✅ Successfully generated $cert_name keypair for ca=$ca_name (FQDN: $fqdn )"
124127 rm /tmp/client.conf
125128 rm truststore/client/$cert_name .csr
129+ return 0
126130}
127131
128132# Rotate a CA - archive the existing CA and build a new one
129133# The previous CA is added to the new CA bundle (can be removed after clients have updated)
130134function _truststore_rotate_ca() {
131- if [ $# -ne 2 ]; then
135+ if [[ $# -ne 2 ] ]; then
132136 echo " Usage: $0 rotate-ca <env> <fqdn>"
133- exit 1;
137+ return 1;
134138 fi
135139
136- env=$1
137- fqdn=$2
140+ env=" $1 "
141+ fqdn=" $2 "
138142
139143 # Archive the existing ca certs
140- archive_date=$( date +%Y-%m-%d)
141- if [ -f " truststore/ca/$env .archived_$archive_date .crt" ] ||
142- [ -f " truststore/ca/$env .archived_$archive_date .key" ] ||
143- [ -f " truststore/server/$env .archived_$archive_date .pem" ]; then
144- echo " Error: Archive files already exist for date $archive_date - please resolve before rotating the CA"
145- exit 1
144+ archive_date=" $( date +%Y-%m-%d) "
145+ if [[ -f " truststore/ca/$env .archived_$archive_date .crt" ] ] ||
146+ [[ -f " truststore/ca/$env .archived_$archive_date .key" ] ] ||
147+ [[ -f " truststore/server/$env .archived_$archive_date .pem" ] ]; then
148+ echo " Error: Archive files already exist for date $archive_date - please resolve before rotating the CA" 1>&2
149+ return 1
146150 fi
147151
148- mv truststore/ca/$env .crt truststore/ca/$env .archived_$archive_date .crt
149- mv truststore/ca/$env .key truststore/ca/$env .archived_$archive_date .key
150- mv truststore/server/$env .pem truststore/server/$env .archived_$archive_date .pem
152+ mv " truststore/ca/$env .crt" " truststore/ca/$env .archived_$archive_date .crt"
153+ mv " truststore/ca/$env .key" " truststore/ca/$env .archived_$archive_date .key"
154+ mv " truststore/server/$env .pem" " truststore/server/$env .archived_$archive_date .pem"
151155
152156 # Build a new CA
153- _truststore_build_ca $env $fqdn
157+ _truststore_build_ca " $env " " $fqdn "
154158
155159 # Add the previous CA to the new CA bundle (can be removed after clients have updated)
156- cat truststore/ca/$env .archived_$archive_date .crt >> truststore/server/$env .pem
160+ cat " truststore/ca/$env .archived_$archive_date .crt" >> " truststore/server/$env .pem"
157161
158162 echo -e " ✅ Successfully rotated CA for $env - previous CA archived with date: $archive_date "
163+ return 0
159164}
160165
161166# Rotate a client cert - archive the existing cert and build a new one
162167function _truststore_rotate_cert() {
163- if [ $# -ne 3 ]; then
168+ if [[ $# -ne 3 ] ]; then
164169 echo " Usage: $0 rotate-cert <env> <ca> <fqdn>"
165170 exit 1;
166171 fi
167172
168- cert_name=$1
169- ca_name=$2
170- fqdn=$3
173+ cert_name=" $1 "
174+ ca_name=" $2 "
175+ fqdn=" $3 "
171176
172177 # Archive the existing client certs
173178 archive_date=$( date +%Y-%m-%d)
174- if [ -f " truststore/client/$cert_name .archived_$archive_date .crt" ] ||
175- [ -f " truststore/client/$cert_name .archived_$archive_date .key" ]; then
176- echo " Error: Archive files already exist for date $archive_date - please resolve before rotating the client cert"
177- exit 1
179+ if [[ -f " truststore/client/$cert_name .archived_$archive_date .crt" ] ] ||
180+ [[ -f " truststore/client/$cert_name .archived_$archive_date .key" ] ]; then
181+ echo " Error: Archive files already exist for date $archive_date - please resolve before rotating the client cert" 1>&2
182+ return 1
178183 fi
179184
180- mv truststore/client/$cert_name .crt truststore/client/$cert_name .archived_$archive_date .crt
181- mv truststore/client/$cert_name .key truststore/client/$cert_name .archived_$archive_date .key
185+ mv " truststore/client/$cert_name .crt" " truststore/client/$cert_name .archived_$archive_date .crt"
186+ mv " truststore/client/$cert_name .key" " truststore/client/$cert_name .archived_$archive_date .key"
182187
183188 # Build a new client cert
184- _truststore_build_cert $cert_name $ca_name $fqdn
189+ _truststore_build_cert " $cert_name " " $ca_name " " $fqdn "
185190
186191 echo -e " ✅ Successfully rotated client cert for $cert_name - previous cert archived with date: $archive_date "
192+ return 0
187193}
188194
189195# Disable an archived CA by removing it from the server pem file
190196function _disable_archived_ca() {
191- env=$1
192- cat truststore/ca/$env .crt > truststore/server/$env .pem
197+ env=" $1 "
198+ cat " truststore/ca/$env .crt" > " truststore/server/$env .pem"
193199
194200 echo -e " ✅ Successfully disabled archived CA for $env "
201+ return 0
195202}
196203
197204# Restore an archived CA by moving the archived files back to their original names
198205function _restore_archived_ca() {
199- env=$1
200- archive_date=$2
206+ env=" $1 "
207+ archive_date=" $2 "
201208
202- mv truststore/ca/$env .archived_$archive_date .crt truststore/ca/$env .crt
203- mv truststore/ca/$env .archived_$archive_date .key truststore/ca/$env .key
204- mv truststore/server/$env .archived_$archive_date .pem truststore/server/$env .pem
209+ mv " truststore/ca/$env .archived_$archive_date .crt" " truststore/ca/$env .crt"
210+ mv " truststore/ca/$env .archived_$archive_date .key" " truststore/ca/$env .key"
211+ mv " truststore/server/$env .archived_$archive_date .pem" " truststore/server/$env .pem"
205212
206213 echo -e " ✅ Successfully restored archived CA for $env from date: $archive_date "
214+ return 0
207215}
208216
209217# Restore an archived client cert by moving the archived files back to their original names
210218function _restore_archived_cert() {
211- env=$1
212- archive_date=$2
219+ env=" $1 "
220+ archive_date=" $2 "
213221
214- mv truststore/client/$env .archived_$archive_date .crt truststore/client/$env .crt
215- mv truststore/client/$env .archived_$archive_date .key truststore/client/$env .key
222+ mv " truststore/client/$env .archived_$archive_date .crt" " truststore/client/$env .crt"
223+ mv " truststore/client/$env .archived_$archive_date .key" " truststore/client/$env .key"
216224
217225 echo -e " ✅ Successfully restored archived client cert for $env from date: $archive_date "
226+ return 0
218227}
219228
220229function _truststore_build_all() {
@@ -229,38 +238,56 @@ function _truststore_build_all() {
229238 _truststore_build_cert " ref" " ref" " ref.api.record-locator.ref.national.nhs.uk"
230239 _truststore_build_cert " qa" " qa" " api.qa.record-locator.national.nhs.uk"
231240 _truststore_build_cert " dev" " dev" " dev.api.record-locator.dev.national.nhs.uk"
241+
242+ echo -e " ✅ Successfully built all truststore files"
243+ return 0
232244}
233245
234246function _truststore_pull_ca() {
235247 env=$1
236248 echo " Pulling ${env} ca certificate"
237249 aws s3 cp " s3://${BUCKET} /ca/${env} .crt" " truststore/ca/${env} .crt"
250+
251+ echo -e " ✅ Successfully pulled ${env} ca certificate from s3://${BUCKET} "
252+ return 0
238253}
239254
240255function _truststore_pull_ca_key() {
241256 env=$1
242257 echo " Pulling ${env} ca private key"
243258 aws s3 cp " s3://${BUCKET} /ca/${env} .key" " truststore/ca/${env} .key"
259+
260+ echo -e " ✅ Successfully pulled ${env} ca private key from s3://${BUCKET} "
261+ return 0
244262}
245263
246264function _truststore_pull_client() {
247265 env=$1
248266 echo " Pulling ${env} client certificate"
249267 aws s3 cp " s3://${BUCKET} /client/${env} .key" " truststore/client/${env} .key"
250268 aws s3 cp " s3://${BUCKET} /client/${env} .crt" " truststore/client/${env} .crt"
269+
270+ echo -e " ✅ Successfully pulled ${env} client truststore files from s3://${BUCKET} "
271+ return 0
251272}
252273
253274function _truststore_pull_server() {
254275 env=$1
255276 echo " Pulling ${env} server trust certificate"
256277 aws s3 cp " s3://${BUCKET} /server/${env} .pem" " truststore/server/${env} .pem"
278+
279+ echo -e " ✅ Successfully pulled ${env} server truststore files from s3://${BUCKET} "
280+ return 0
257281}
258282
259283function _truststore_pull_all() {
260284 env=$1
261285 _truststore_pull_ca $env
262286 _truststore_pull_client $env
263287 _truststore_pull_server $env
288+
289+ echo -e " ✅ Successfully pulled all ${env} truststore files from s3://${BUCKET} "
290+ return 0
264291}
265292
266293function _truststore_push_all() {
@@ -276,7 +303,7 @@ function _truststore_push_all() {
276303 echo
277304 aws s3 cp " s3://${BUCKET} /$f " " ${backup_dir} /$f " || echo " No existing file s3://${BUCKET} /$f to back up"
278305
279- if [ -f " ${backup_dir} /$f " ]
306+ if [[ -f " ${backup_dir} /$f " ] ]
280307 then
281308 diff --brief " truststore/$f " " ${backup_dir} /$f " || true
282309 fi
@@ -285,9 +312,9 @@ function _truststore_push_all() {
285312 echo
286313 echo -n " WARNING: You are about to upload files to the ${env} truststore - are you sure? [yes/NO] "
287314 read answer
288- if [ " $answer " != " yes" ]; then
289- echo " Aborting upload to ${env} truststore"
290- exit 1
315+ if [[ " $answer " != " yes" ] ]; then
316+ echo " Aborting upload to ${env} truststore" 1>&2
317+ return 1
291318 fi
292319
293320 echo " Uploading ${env} ca certificate"
@@ -302,6 +329,9 @@ function _truststore_push_all() {
302329
303330 echo " Uploading ${env} server trust certificate"
304331 aws s3 cp " truststore/server/${env} .pem" " s3://${BUCKET} /server/${env} .pem"
332+
333+ echo -e " ✅ Successfully uploaded all ${env} truststore files to s3://${BUCKET} "
334+ return 0
305335}
306336
307337function _truststore() {
0 commit comments