-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
executable file
·390 lines (309 loc) · 9.76 KB
/
setup.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
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
#!/bin/bash -x
#################
set -x
set -e
AUTHDIR=${1:-""}
echo "Running test on: $(hostname)"
echo "Hostname -f shows: $(hostname)"
echo "Running test for AUTHDIR=${AUTHDIR}"
#KC_VERSION=16.1.1
#KC_VERSION=latest
KC_VERSION=22.0.1
#################
if [ -f /etc/os-release ]; then
. /etc/os-release
VER_MAJOR=$(echo $VERSION_ID|cut -f1 -d.)
VER_MINOR=$(echo $VERSION_ID|cut -f2 -d.)
if [ "$ID" = "rhel" ]; then
dnf config-manager --enable rhel-*
if [ $VER_MAJOR -eq 8 -a $VER_MINOR -le 3 ]; then
KC_VERSION=18.0.2
echo "$(hostname -i|awk '{print $1}') $(hostname)" >> /etc/hosts
fi
fi
fi
dnf -y install \
keycloak-httpd-client-install \
mod_auth_openidc \
python3-lxml \
python3-requests \
python3-pytest \
python3-distro \
dnf-utils \
openssl \
mod_ssl \
httpd \
podman
if [ "$ID" = "rhel" -o "$ID" = "centos" ] && [ $VER_MAJOR -lt 10 ]; then
dnf -y install \
java-11-openjdk-headless \
mod_auth_mellon \
python3-lasso
else
dnf -y install \
java-21-openjdk-headless
fi
#################
if [ -d /etc/httpd/federation ]; then
tar zcvf /tmp/httpd.federation.$(date +%Y%m%d-%H%M%S).tgz /etc/httpd/federation
fi
if [ -d /tmp/https ]; then
tar zcvf /tmp/https.$(date +%Y%m%d-%H%M%S).tgz /tmp/https
rm -rf /tmp/https
fi
if [ -f /etc/httpd/conf.d/example_app_ssl.conf ]; then
rm /etc/httpd/conf.d/example_app_ssl.conf
fi
podman container rm -af || echo "No containers to remove...continuing"
#################
mkdir /tmp/https
pushd /tmp/https
cat > ca.cnf <<EOF
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = .
database = \$dir/index.txt
new_certs_dir = \$dir/newcerts
certificate = \$dir/rootCA.crt
serial = \$dir/serial
private_key = \$dir/rootCA.key
RANDFILE = \$dir/rand
default_days = 365
default_crl_days = 30
default_md = sha256
policy = policy_any
email_in_dn = no
name_opt = ca_default
cert_opt = ca_default
copy_extensions = copy
[ usr_cert ]
authorityKeyIdentifier = keyid, issuer
[ v3_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
basicConstraints = CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
[ policy_any ]
organizationName = supplied
organizationalUnitName = supplied
commonName = supplied
emailAddress = optional
[ req ]
distinguished_name = req_distinguished_name
prompt = no
[ req_distinguished_name ]
O = IdM Federation Example
OU = IdM Federation Example Test
CN = IdM Federation Example Test CA
EOF
#################
cat > tls.cnf <<EOF
[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
req_extensions = req_ext
prompt = no
[ req_distinguished_name ]
O = IdM Federation Example
OU = IdM Federation Example Test
CN = $(hostname)
[ req_ext ]
subjectAltName = @alt_names
[ v3_ca ]
extendedKeyUsage = serverAuth, clientAuth, codeSigning, emailProtection
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = $(hostname)
EOF
#################
touch serial index.txt crlnumber index.txt.attr
mkdir newcerts
echo 'unique_subject = no' > index.txt.attr
openssl rand -hex 16 > serial
# Key/Cert pair for RootCA
openssl genrsa -out rootCA.key 4096
openssl req -batch -config ca.cnf \
-x509 -new -nodes -key rootCA.key -sha256 -days 10000 \
-set_serial 0 -extensions v3_ca -out rootCA.crt
# Key/Cert pair for Keycloak
openssl genrsa -out tls.key 4096
openssl req -new -nodes -key tls.key \
-reqexts req_ext -config tls.cnf -out tls.csr
openssl ca -config ca.cnf -batch -notext -keyfile rootCA.key \
-extensions v3_ca -extfile tls.cnf \
-in tls.csr -days 365 -out tls.crt
# Key/Cert pair for Web server
openssl genrsa -out web.key 4096
openssl req -new -nodes -key web.key \
-reqexts req_ext -config tls.cnf -out web.csr
openssl ca -config ca.cnf -batch -notext -keyfile rootCA.key \
-extensions v3_ca -extfile tls.cnf \
-in web.csr -days 365 -out web.crt
cp /tmp/https/web.crt /etc/pki/tls/certs/web.crt
cp /tmp/https/web.key /etc/pki/tls/private/web.key
cp /tmp/https/rootCA.crt /etc/pki/ca-trust/source/anchors/
cp /tmp/https/tls.crt /etc/pki/ca-trust/source/anchors/
cp /tmp/https/web.crt /etc/pki/ca-trust/source/anchors/
update-ca-trust
# keycloak runs as UID 1000 so we need the certs to be accessible to the
# keycloak user
chown -R 1000:1000 /tmp/https
popd
#################
if [ -f /usr/bin/firewall-cmd ]; then
systemctl start firewalld
firewall-cmd --add-port 80/tcp
firewall-cmd --add-port 443/tcp
firewall-cmd --add-port 8443/tcp
firewall-cmd --add-port 60443/tcp
firewall-cmd --runtime-to-permanent
fi
################
keytool -import \
-keystore /tmp/https/truststore.keystore \
-file /tmp/https/rootCA.crt \
-alias federation_example \
-trustcacerts -storepass Secret123 -noprompt
podman pull quay.io/keycloak/keycloak:$KC_VERSION
# podman unshare not needed for rootful containers
# podman unshare chown 1000 -R /tmp/https
podman run --name keycloak -d \
-p 8080:8080 \
-p 8443:8443 \
-e KEYCLOAK_ADMIN=admin \
-e KEYCLOAK_ADMIN_PASSWORD=Secret123 \
-e KC_LOG_LEVEL=debug \
-e KC_HOSTNAME=$(hostname) \
-e KC_HTTPS_CERTIFICATE_FILE=/etc/x509/https/tls.crt \
-e KC_HTTPS_CERTIFICATE_KEY_FILE=/etc/x509/https/tls.key \
-e KC_HTTPS_TRUST_STORE_FILE=/etc/x509/https/truststore.keystore \
-e KC_HTTPS_TRUST_STORE_PASSWORD=Secret123 \
-e KC_HTTP_RELATIVE_PATH=${AUTHDIR} \
-v /tmp/https:/etc/x509/https:Z \
quay.io/keycloak/keycloak:$KC_VERSION start
# Removing deprecated auto-build option
# --auto-build
sleep 15
kcadm="podman exec keycloak /opt/keycloak/bin/kcadm.sh"
set +e
for count in {1..10}; do
$kcadm config truststore --trustpass Secret123 \
/etc/x509/https/truststore.keystore
if [ $? -eq 0 ]; then
break
else
sleep 30
fi
done
if [ $count -eq 10 ]; then
echo "----- Begin keycloak container logs for iteration $count"
podman logs keycloak
echo "----- End keycloak container logs for iteration $count"
exit 1
fi
for count in {1..3}; do
$kcadm config credentials --server https://$(hostname):8443${AUTHDIR}/ \
--realm master --user admin --password Secret123
if [ $? -eq 0 ]; then
break
else
sleep 30
fi
done
if [ $count -eq 3 ]; then
echo "----- Begin keycloak container logs for iteration $count"
podman logs keycloak
echo "----- End keycloak container logs for iteration $count"
exit 1
fi
set -e
# For positive tests
$kcadm create users -r master -s username=testuser -s enabled=true -s email=testuser@master
$kcadm set-password -r master --username testuser --new-password Secret123
# For negative tests
$kcadm create users -r master -s username=neguser -s enabled=true -s email=neguser@master
$kcadm set-password -r master --username neguser --new-password Secret123
################
cp /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/example_app_ssl.conf
sed -i 's/443/60443/g' /etc/httpd/conf.d/example_app_ssl.conf
sed -i 's/localhost.crt/web.crt/' /etc/httpd/conf.d/example_app_ssl.conf
sed -i 's/localhost.key/web.key/' /etc/httpd/conf.d/example_app_ssl.conf
################
mkdir -p /var/www/html/openidc_root/private/static/private_static
cat > /var/www/html/openidc_root/logged_out.html <<EOF
<html>
<title>Logout</title>
<p>
Congratulations, you've been logged out!
</p>
<p>
Now try to <a href="/openidc_root/private/">log back in</a>
</p>
</html>
EOF
cat > /var/www/html/openidc_root/private/index.html <<EOF
<html><title>Secure</title>Hello there...from SP ...<br>
<a href="/openidc_root/private/redirect_uri?logout=https://$(hostname):60443/openidc_root/logged_out.html">Logout</a>
<hr>
<pre><!--#printenv --></pre>
EOF
cat > /etc/httpd/conf.d/openidc_example_app_private.conf <<EOF
<Directory /var/www/html/openidc_root/private>
Options +Includes
AddOutputFilter INCLUDES .html
</Directory>
EOF
cp /var/www/html/openidc_root/private/index.html \
/var/www/html/openidc_root/private/static/
cp /var/www/html/openidc_root/private/index.html \
/var/www/html/openidc_root/private/static/private_static/
systemctl start httpd
################
if [ "$ID" = "rhel" -o "$ID" = "centos" ] && [ $VER_MAJOR -ge 10 ]; then
echo "mod_auth_mellon not supported in RHEL-10 and later"
echo "Skipping mellon setup"
exit 0
fi
mkdir -p /var/www/html/mellon_root/private/static/private_static
cat > /var/www/html/mellon_root/logged_out.html <<EOF
<html>
<title>Logout</title>
<p>
Congratulations, you've been logged out!
</p>
<p>
Now try to <a href="/mellon_root/private/">log back in</a>
</p>
</html>
EOF
cat > /var/www/html/mellon_root/private/index.html <<EOF
<html><title>Secure</title>Hello there...from SP ...<br>
<a href="https://$HOSTNAME:60443/mellon_root/mellon/logout?ReturnTo=https://$HOSTNAME:60443/mellon_root/logged_out.html">Log out</a>
<hr>
<pre><!--#printenv --></pre>
EOF
cat > /etc/httpd/conf.d/mellon_example_app_private.conf <<EOF
<Directory /var/www/html/mellon_root/private>
Options +Includes
AddOutputFilter INCLUDES .html
</Directory>
EOF
cat > /etc/httpd/conf.d/mellon_example_app_private_static.conf <<EOF
<Location /mellon_root/private/static>
MellonEnable info
Require all granted
</Location>
<Location /mellon_root/private/static/private_static>
MellonEnable auth
Require valid-user
</Location>
EOF
cp /var/www/html/mellon_root/private/index.html \
/var/www/html/mellon_root/private/static/
cp /var/www/html/mellon_root/private/index.html \
/var/www/html/mellon_root/private/static/private_static/
systemctl start httpd