-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtruststore.sh
More file actions
executable file
·165 lines (137 loc) · 4.74 KB
/
truststore.sh
File metadata and controls
executable file
·165 lines (137 loc) · 4.74 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
#!/bin/bash
# Script to manage the NRLF truststore files
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-client <env> - pull the files needed for a client connection"
echo " pull-server <env> - pull the files needed for a server connection"
echo
}
# 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
}
# build a certificate authority
function _truststore_build_ca() {
if [ $# -ne 2 ]; then
echo "Usage: $0 build-ca <name> <fqdn>"
exit 1
fi
ca=$1
fqdn=$2
substitute_env_in_file ./truststore/config/ca.conf /tmp/ca.conf
echo -e "🚧 Building CA: $ca \t(FQDN: $fqdn)"
openssl req -newkey rsa:4096 \
-nodes \
-keyout ./truststore/ca/$ca.key \
-new \
-x509 \
-days 36500 \
-out ./truststore/ca/$ca.crt \
-config /tmp/ca.conf \
-extensions v3_req \
-extensions v3_ca &> /dev/null
rm /tmp/ca.conf
echo -e "✅ Successfully Built CA: $ca"
}
# buld a certificate
function _truststore_build_cert() {
if [ $# -ne 3 ]; then
echo "Usage: $0 build-cert <name> <ca> <fqdn>"
exit 1;
fi
client=$1
ca=$2
fqdn=$3
serial=$(date +%s%3N)
substitute_env_in_file ./truststore/config/client.conf /tmp/client.conf
echo -e "🚧 Generating $client keypair"
openssl req \
-newkey rsa:4096 \
-nodes \
-keyout truststore/client/$client.key \
-new \
-out truststore/client/$client.csr \
-config /tmp/client.conf \
-extensions v3_req \
-extensions usr_cert &> /dev/null
openssl x509 \
-req \
-in truststore/client/$client.csr \
-CA truststore/ca/$ca.crt \
-CAkey truststore/ca/$ca.key \
-set_serial $serial \
-out truststore/client/$client.crt \
-days 36500 \
-sha256 \
-extfile /tmp/client.conf \
-extensions v3_req \
-extensions usr_cert &> /dev/null
cat truststore/client/$client.crt truststore/ca/$ca.crt > truststore/server/$client.pem
echo -e "✅ Successfully generated $client keypair"
rm /tmp/client.conf
}
function _truststore_build_all() {
_truststore_build_ca "prod" "record-locator.national.nhs.uk"
_truststore_build_ca "int" "record-locator.int.national.nhs.uk"
_truststore_build_ca "ref" "record-locator.ref.national.nhs.uk"
_truststore_build_ca "qa" "qa.record-locator.national.nhs.uk"
_truststore_build_ca "dev" "record-locator.dev.national.nhs.uk"
_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 "qa" "qa" "api.qa.record-locator.national.nhs.uk"
_truststore_build_cert "dev" "dev" "dev.api.record-locator.dev.national.nhs.uk"
}
function _truststore_pull_ca() {
env=$1
aws s3 cp "s3://${BUCKET}/ca/${env}.crt" "truststore/ca/${env}.crt"
}
function _truststore_pull_client() {
env=$1
aws s3 cp "s3://${BUCKET}/client/${env}.key" "truststore/client/${env}.key"
aws s3 cp "s3://${BUCKET}/client/${env}.crt" "truststore/client/${env}.crt"
}
function _truststore_pull_server() {
env=$1
echo "Pulling truststore/server/${env}.pem"
aws s3 cp "s3://${BUCKET}/server/${env}.pem" "truststore/server/${env}.pem"
}
function _truststore_pull_all() {
env=$1
_truststore_pull_ca $env
_truststore_pull_client $env
_truststore_pull_server $env
}
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-server") _truststore_pull_server $args ;;
"pull-client") _truststore_pull_client $args ;;
"pull-ca") _truststore_pull_server $args ;;
*) _truststore_help $args ;;
esac
}
_truststore $@