-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·226 lines (200 loc) · 5.94 KB
/
run.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
#!/bin/bash
set -euo pipefail
printf 'Purge existing dockers..'
out="$(docker-compose down -v 2>&1)"
if [ $? -ne 0 ]; then
printf 'failed! %s\n' "$out"
fi
printf 'OK!\n'
rm -rf step-ca conf hydra-sqlite || true
mkdir -p conf
cat > conf/Caddyfile <<EOF
https://caddy:10000 {
tls /cert.crt /cert.key {
ca_root /etc/ssl/certs/ca-certificates.crt
}
reverse_proxy http://step-ca-host:10000
}
https://caddy:443 {
tls /cert.crt /cert.key {
ca_root /etc/ssl/certs/ca-certificates.crt
}
reverse_proxy https://step-ca
}
https://caddy:4444 {
tls /cert.crt /cert.key {
ca_root /etc/ssl/certs/ca-certificates.crt
}
reverse_proxy https://hydra:4444
}
https://caddy:3000 {
tls /cert.crt /cert.key {
ca_root /etc/ssl/certs/ca-certificates.crt
}
reverse_proxy http://consent:3000
}
EOF
cat > conf/hydra.yaml <<EOF
serve:
cookies:
same_site_mode: Lax
public:
tls:
enabled: true
cert:
path: /cert.crt
key:
path: /cert.key
admin:
tls:
enabled: true
cert:
path: /cert.crt
key:
path: /cert.key
urls:
self:
issuer: https://caddy:4444
consent: https://caddy:3000/consent
login: https://caddy:3000/login
logout: https://caddy:3000/logout
secrets:
system: [ $(cat /proc/sys/kernel/random/uuid) ]
oidc:
subject_identifiers:
supported_types:
- pairwise
- public
pairwise:
salt: $(cat /proc/sys/kernel/random/uuid)
log:
leak_sensitive_values: true
EOF
mkdir -p step-ca/secrets/
cat /proc/sys/kernel/random/uuid > step-ca/secrets/password
cat /proc/sys/kernel/random/uuid > step-ca/secrets/provisioner-password
touch conf/ca-certificates.crt
out="$(docker-compose run --rm --entrypoint step step-ca ca init \
--name step-ca \
--dns step-ca \
--address :443 \
--deployment-type standalone \
--provisioner admin \
--provisioner-password-file /home/step/secrets/provisioner-password \
--password-file /home/step/secrets/password 2>&1 \
#
)"
if [ $? -ne 0 ]; then
printf 'Step-ca init failed: %s\n' "$out"
fi
cat step-ca/certs/root_ca.crt > conf/ca-certificates.crt
printf 'Starting step-ca..'
out="$(docker-compose up -d step-ca 2>&1)"
while [ "$(docker-compose exec step-ca step ca health 2>/dev/null | tr -d '\r\n')" != 'ok' ]; do
printf '.'
sleep 1
done
printf 'OK!\n'
out="$(docker-compose exec step-ca step ca certificate \
--san hydra \
--san consent \
--san step-ca \
--san localhost \
--san caddy \
--provisioner-password-file /home/step/secrets/provisioner-password step-ca '/conf/cert.crt' '/conf/cert.key' 2>&1 \
#
)"
if [ $? -ne 0 ]; then
printf 'Step-ca certificate failed: %s\n' "$out"
fi
chmod a+r ./conf/cert.crt ./conf/cert.key
printf 'Starting caddy..'
out="$(docker-compose up -d caddy 2>&1)"
i=0
while [ "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt --write-out '%{http_code}' --output /dev/null --silent https://caddy 2>/dev/null)" != '404' ]; do
if [ $i -eq 10 ]; then
printf 'NOT OK: %s\n %s\n' "$out" "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt -i https://caddy 2>&1)"
exit 1
fi
printf '.'
i=$((i+1))
done
printf 'OK!\n'
out="$(docker-compose run --rm hydra 'migrate' '-c' '/etc/ory/hydra.yaml' 'sql' '-e' '--yes')"
if [ $? -ne 0 ]; then
printf 'Hydra init failed: %s\n' "$out"
fi
printf 'Starting hydra..'
out="$(docker-compose up -d hydra 2>&1)"
i=0
while [ "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt https://hydra:4444/health/ready 2>/dev/null | jq -c 2>/dev/null)" != '{"status":"ok"}' ]; do
if [ $i -eq 10 ]; then
printf 'NOT OK: %s\n %s\n' "$out" "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt -i https://caddy 2>&1)"
exit 1
fi
printf '.'
i=$((i+1))
done
printf 'OK!\n'
printf 'Starting consent..'
out="$(docker-compose up -d consent 2>&1)"
i=0
while [ "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt --write-out '%{http_code}' --output /dev/null --silent http://consent:3000 2>/dev/null)" != '200' ]; do
if [ $i -eq 10 ]; then
printf 'NOT OK: %s\n %s\n' "$out" "$(docker-compose run --rm curl --cacert /etc/ssl/certs/ca-certificates.crt -i https://caddy 2>&1)"
exit 1
fi
printf '.'
i=$((i+1))
done
printf 'OK!\n'
sleep 2
printf 'Hydra: Adding step-ca client\n'
out="$(docker-compose exec hydra hydra clients \
create \
--grant-types client_credentials \
--grant-types authorization_code \
--name step-ca \
--secret step-ca \
--response-types=code \
--token-endpoint-auth-method client_secret_post \
--scope="openid" \
--scope="email" \
--callbacks http://127.0.0.1 \
--id step-ca 2>&1 )"
if [ $? -ne 0 ]; then
printf 'Hydra client add failed: %s\n' "$out"
fi
out="$(docker-compose exec step-ca step ca provisioner add hydra \
--type oidc \
--ca-config /home/step/config/ca.json \
--client-id step-ca \
--client-secret step-ca \
--configuration-endpoint https://caddy:4444 2>&1 )"
if [ $? -ne 0 ]; then
printf 'Hydra client add failed: %s\n' "$out"
fi
printf 'Step-ca: restarting\n'
out="$(docker-compose restart step-ca 2>&1)"
i=0
while [ "$(docker-compose exec step-ca step ca health 2>/dev/null | tr -d '\r\n')" != 'ok' ]; do
if [ $i -eq 10 ]; then
printf 'step-ca restart failed: %s\n %s\n' "$out" "$(docker-compose exec step-ca step ca health 2>&1)"
exit 1
fi
printf '.'
i=$((i+1))
done
printf 'OK!\n'
printf 'Starting to auth\n'
coproc ( docker-compose run --rm -T step-ca-host step ca certificate \
--root /etc/ssl/certs/ca-certificates.crt \
--provisioner hydra \
--ca-url https://caddy [email protected] /conf/client.crt /conf/client.key 2>&1)
while read -r o <&"${COPROC[0]}"; do
if [ "${o:0:4}" == "http" ]; then
printf 'Authing to: %s\n' "$o"
./auth.sh "$o"
fi
done 2>/dev/null
printf 'Subject of private key: %s\n' "$(openssl x509 -in ./conf/client.crt -subject -noout)"