-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeycloak-1.6_acct_admin.sh
executable file
·213 lines (183 loc) · 6.08 KB
/
keycloak-1.6_acct_admin.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
#!/bin/bash
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Eugene Chow
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE
#
# Author: Eugene Chow
# Description: Administer Keycloak accounts from the command-line
#
# ## Prerequisites
# In the Keycloak realm:
# 1) Create client (eg. keycloak_acct_admin) for this script. Access Type: public.
# 2) Add the realm admin user (eg. realm_admin) to the realm
# 3) In the realm admin user's settings > Client Role > "realm-management", assign it all available roles
# 4) In realm, enable Direct Grant API at Settings > Login
#### Config
# Change these parameters to suit your installation
base_url="https://key.cloak.url/auth" #Keycloak base URL
realm="master" #realm name
client_id="keycloak_acct_admin" #create this client in the above Keycloak realm
#### Globals
access_token=""
refresh_token=""
userid=""
#### Helpers
process_result() {
expected_status="$1"
result="$2"
msg="$3"
err_msg=${result% *}
actual_status=${result##* }
printf "[HTTP $actual_status] $msg "
if [ "$actual_status" == "$expected_status" ]; then
echo "successful"
return 0
else
echo "failed"
echo -e "\t$err_msg"
return 1
fi
}
kc_login() {
read -p "Admin username: " admin_id
read -s -p "Password: " admin_pwd; echo
result=$(curl --write-out " %{http_code}" -s -k --request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "username=$admin_id&password=$admin_pwd&client_id=$client_id&grant_type=password" \
"$base_url/realms/$realm/protocol/openid-connect/token")
admin_pwd="" #clear password
msg="Login"
process_result "200" "$result" "$msg"
if [ $? -ne 0 ]; then
echo "Please correct error before retrying. Exiting."
exit 1 #no point continuing if login fails
fi
# Extract access_token
access_token=$(sed -E -n 's/.*"access_token":"([^"]+)".*/\1/p' <<< "$result")
refresh_token=$(sed -E -n 's/.*"refresh_token":"([^"]+)".*/\1/p' <<< "$result")
}
kc_create_user() {
firstname="$1"
lastname="$2"
username="$3"
email="$4"
result=$(curl -i -s -k --request POST \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $access_token" \
--data '{
"enabled": "true",
"username": "'"$username"'",
"email": "'"$email"'",
"firstName": "'"$firstname"'",
"lastName": "'"$lastname"'"
}' "$base_url/admin/realms/$realm/users")
userid=$(echo "$result" | grep -o "Location: .*" | egrep -o '[a-zA-Z0-9]+(-[a-zA-Z0-9]+)+') #parse userid
http_code=$(sed -E -n 's,HTTP[^ ]+ ([0-9]{3}) .*,\1,p' <<< "$result") #parse HTTP code
msg="$username: insert"
process_result "201" "$http_code" "$msg"
return $? #return status from process_result
}
kc_delete_user() {
userid="$1"
result=$(curl --write-out " %{http_code}" -s -k --request DELETE \
--header "Authorization: Bearer $access_token" \
"$base_url/admin/realms/$realm/users/$userid")
msg="$username: delete"
process_result "204" "$result" "$msg"
return $? #return status from process_result
}
kc_set_pwd() {
userid="$1"
password="$2"
result=$(curl --write-out " %{http_code}" -s -k --request PUT \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $access_token" \
--data '{
"type": "password",
"value": "'"$password"'",
"temporary": "false"
}' "$base_url/admin/realms/$realm/users/$userid/reset-password")
msg="$username: password set"
process_result "204" "$result" "$msg"
return $? #return status from process_result
}
kc_logout() {
result=$(curl --write-out " %{http_code}" -s -k --request POST \
--header "Content-Type: application/x-www-form-urlencoded" \
--data "client_id=$client_id&refresh_token=$refresh_token" \
"$base_url/realms/$realm/protocol/openid-connect/logout")
msg="Logout"
process_result "204" "$result" "$msg" #print HTTP status message
return $? #return status from process_result
}
## Unit tests for helper functions
# Use this to check that the helper functions work
unit_test() {
echo "Testing normal behaviour. These operations should succeed"
kc_login
kc_create_user john tan johntan [email protected]
kc_set_pwd "$userid" "test"
kc_delete_user "$userid"
#kc_logout
echo "Testing abnormal behaviour. These operations should fail"
kc_create_user john tan johntan [email protected] #try to create acct after logout
kc_set_pwd "$userid" "test"
kc_delete_user "$userid" #try to delete acct after logout
}
## Bulk import accounts
# Reads and creates accounts using a CSV file as the source
# CSV file format: "first name, last name, username, email, password"
import_accts() {
kc_login
# Import accounts line-by-line
while read -r line; do
IFS=',' read -ra arr <<< "$line"
kc_create_user "${arr[0]}" "${arr[1]}" "${arr[2]}" "${arr[3]}"
[ $? -ne 0 ] || kc_set_pwd "$userid" "${arr[4]}" #skip if kc_create_user failed
done < "$csv_file"
#kc_logout
}
#### Main
if [ $# -lt 1 ]; then
echo "Keycloak account admin script"
echo "Usage: $0 [--test | --import csv_file]"
exit 1
fi
flag=$1
case $flag in
"--test" )
unit_test
;;
"--import")
csv_file="$2"
if [ -z "$csv_file" ]; then
echo "Error: missing 'csv_file' argument"
exit 1
fi
import_accts $csv_file
;;
*)
echo "Unrecognised flag '$flag'"
exit 1
;;
esac
exit 0