-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure-keycloak.sh
181 lines (162 loc) · 4.81 KB
/
configure-keycloak.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
#!/bin/bash -xeu
function random-alphanumeric {
len=$1
tr -dc A-Za-z0-9 </dev/urandom | head -c "${len}"
}
function ensure-random-alphanumeric {
path=$1
len=$2
if ! [ -f "${path}" ] ; then
random-alphanumeric "${len}" > "${path}"
fi
}
function ensure-realm {
realm=$1
if ! kcadm.sh get realms -F realm | grep -q '"'"${realm}"'"'; then
kcadm.sh create realms -s realm="${realm}" -s enabled=true
fi
}
function ensure-client {
realm=$1
id=$2
callback_url=$3
dir=$4
if ! kcadm.sh get clients \
-r "${realm}" \
-q clientId="${id}" \
-F clientId \
| tee /dev/stderr \
| grep -q "${id}" \
; then
client_uid=$(
kcadm.sh create clients \
-r "${realm}" \
-s clientId="${id}" \
-s clientAuthenticatorType=client-secret \
-s redirectUris='["'"${callback_url}"'"]' \
-i
)
else
client_uid=$(
kcadm.sh get clients \
-r "${realm}" \
-q clientId="${id}" \
-F id \
| tee /dev/stderr \
| grep '"id"' \
| sed -E 's/.*"id" : "([^"]+)".*/\1/' \
| tee /dev/stderr
)
fi
mkdir -p "${dir}"
echo -n "${id}" > "${dir}/client-id"
if [ -f "${dir}/client-secret" ] ; then
echo "Client ID/Secret appears to already be configured for ${id}, skipping. Delete the kubernetes secret and re-run the chart if you wish to regenerate the secret"
else
kcadm.sh create "clients/${client_uid}/client-secret" \
-r "${realm}"
kcadm.sh get clients/${client_uid}/client-secret \
-r "${realm}" \
-F value \
| tee /dev/stderr \
| grep '"value"' \
| sed -E 's/.*"value" : "([^"]+)".*/\1/' \
| tee /dev/stderr \
| tr -d '\n' \
> "${dir}/client-secret"
fi
}
function ensure-user {
realm=$1
username=$2
password=$3
shift 3
if ! kcadm.sh get users \
-r "${realm}" \
-q username="${username}" \
-F username \
| tee /dev/stderr \
| grep -q '"'"${username}"'"' \
; then
user_id=$(
kcadm.sh create users \
-r "${realm}" \
-s username="${username}" \
-s email="${username}@example.com" \
-s emailVerified=true \
-s credentials='[{"type": "password", "value": "'"${password}"'", "temporary": false}]' \
-s enabled=true
)
else
user_id=$(
kcadm.sh get users \
-r "${realm}" \
-q username="${username}" \
-F id \
| tee /dev/stderr \
| grep '"id"' \
| sed -E 's/.*"id" : "([^"]+)".*/\1/' \
| tee /dev/stderr
)
fi
kcadm.sh get-roles \
-r "${realm}" \
--uusername "${username}" \
-F name \
| tee /dev/stderr \
| grep '"name"' \
| sed -E 's/.*"name" : "([^"]+)".*/\1/' \
| tee /dev/stderr \
| while read -r role ; do
kcadm.sh remove-roles \
-r "${realm}" \
--rolename "${role}" \
--uusername "${username}"
done
for role in "$@"; do
kcadm.sh add-roles \
-r "${realm}" \
--rolename "${role}" \
--uusername "${username}"
done
}
function ensure-role {
realm=$1
role=$2
if ! kcadm.sh get "roles/${role}" \
-r "${realm}" \
-F name \
; then
kcadm.sh create roles \
-r "${realm}" \
-s name="${role}" \
-i
fi
}
ensure-random-alphanumeric "/tmp/cookie-secret" 32
rm -f /tmp/trust.jks
keytool -import \
-keystore /tmp/trust.jks \
-storepass truststore-password \
-file /opt/bitnami/keycloak/certs/tls.crt \
-noprompt
kcadm.sh config truststore \
--trustpass truststore-password /tmp/trust.jks
while ! curl -v --cacert /opt/bitnami/keycloak/certs/tls.crt "${KEYCLOAK_URL}" ; do
echo "Keycloak is not yet ready, waiting..."
sleep 15
done
kcadm.sh config credentials \
--server "${KEYCLOAK_URL}" \
--realm master \
--user admin \
--password "${KEYCLOAK_ADMIN_PASSWORD}" \
--client admin-cli
ensure-realm "${NEXUS_REALM}"
ensure-client "${NEXUS_REALM}" "${NEXUS_CLIENT_ID}" "${NEXUS_CALLBACK_URL}" /tmp
for role in ${CREATE_ROLES:-}; do
ensure-role "${NEXUS_REALM}" "${role}"
done
tr ';' '\n' <<< "${CREATE_USERS:-}" | while read -r username password roles ; do
ensure-user "${NEXUS_REALM}" "${username}" "${password}" ${roles}
done