-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkc-utils.sh
More file actions
executable file
·375 lines (329 loc) · 16 KB
/
kc-utils.sh
File metadata and controls
executable file
·375 lines (329 loc) · 16 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
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
#!/bin/bash
# SPDX-FileCopyrightText: 2025 @effndc
#
# SPDX-License-Identifier: Apache-2.0
# Note: Many functions exist that are not used or tested at this time
CYAN='\033[0;36m'
RED='\033[0;31m'
NC='\033[0m' # No Color
: '
required inputs to all fucntions are username, password, cluster_fqdn
some functions require additional input of project_name
Primary actions:
createOrg: create an organization within Edge Platform Orchestrators
createProjectInOrg: create project in Edge Platform Orchestrators
createProjectAdmin: create Keycloak User and assign permission groups
'
# retrieve API token
# Output: api_token
# AI Improved: api_token function with realm, client_id, and scope as parameters
# Usage: api_token <user> <pass> <cluster_fqdn> [realm] [client_id] [scope]
function api_token() {
local user="$1"
local pass="$2"
local cluster_fqdn="$3"
local realm="${4:-master}" # Default to 'master' if not provided
local client_id="${5:-system-client}" # Default to 'system-client' if not provided
local scope="${6:-openid}" # Default to 'openid' if not provided
api_token=$(curl "${CURL_FLAGS}" -s -X POST \
"https://keycloak.${cluster_fqdn}/realms/${realm}/protocol/openid-connect/token" \
-d "username=${user}" \
-d "password=${pass}" \
-d "grant_type=password" \
-d "client_id=${client_id}" \
-d "scope=${scope}" | jq -r '.access_token')
if [[ -z "$api_token" || "$api_token" == "null" ]]; then
echo -e "${RED}Cannot retrieve API Token from $cluster_fqdn for user ${user} in realm ${realm} ${NC}" >&2
exit 1
fi
#echo "Authentication token retrieved for ${user} in realm ${realm}"
echo "$api_token"
}
# Create Org in Orchestrator
# Usage: createOrg <org_name> <cluster_fqdn> <jwt_token>
function createOrg() {
local org_name=$1
local cluster_fqdn=$2
local jwt_token=$3
echo -e "${CYAN}Creating Organization ${org_name} ${NC}"
tmp="$(mktemp)"
http_code=$(curl "${CURL_FLAGS}" -o "$tmp" -w "%{http_code}" -X PUT "https://api.${cluster_fqdn}/v1/orgs/${org_name}" -H "accept: application" -H "Authorization: Bearer ${jwt_token}" -H "Content-Type: application/json" -d "{\"description\":\"${org_name}\"}")
# if http_code is not 200, print the error message
if [ "$http_code" -ne 200 ]; then
echo -e "${RED}Cannot create Organization ${org_name} ${NC}" >&2
cat "$tmp"
rm "$tmp"
exit 1
fi
rm "$tmp"
while [ "$(curl "${CURL_FLAGS}" --location "https://api.${cluster_fqdn}/v1/orgs/${org_name}" -H "accept: application/json" -H "Content-Type: application" -H "Authorization: Bearer ${jwt_token}" | jq -r .status.orgStatus.statusIndicator)" != "STATUS_INDICATION_IDLE" ]; do
echo "Waiting for ${org_name} to be provisioned..."
sleep 5
done
org_uuid=$(curl "${CURL_FLAGS}" --location "https://api.${cluster_fqdn}/v1/orgs/${org_name}" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${jwt_token}" | jq -r .status.orgStatus.uID)
if [ "$org_uuid" == "null" ]; then
echo -e "${RED}Cannot retrieve Org UID for ${org_name} ${NC}" >&2
exit 1
fi
echo -e "${CYAN}Organization ${org_name} has been successfully created -- Organization UUID is ${org_uuid} ${NC}"
}
# createOrgAdmin org_name cluster_fqdn jwt_token
function createOrgAdmin {
local org_name=$1
local cluster_fqdn=$2
local jwt_token=$3
# creating admin user in org
org_admin_user="${org_name}-admin"
if [ "$org_uuid" == "null" ]; then
echo -e "${RED}Organization UUID not known or does not exist ${NC}" >&2
exit 1
fi
echo -e "${CYAN}Creating user ${org_admin_user} within ${org_name}, organization UUID is ${org_uuid} ${NC}"
org_admin_uid=$(createKeycloakUser "${jwt_token}" "${org_admin_user}" "${ORG_ADMIN_PASS}" "${cluster_fqdn}")
if [ "$org_admin_uid" == "null" ]; then
echo -e "${RED}Failed to create admin user ${org_admin_user} in Keycloak ${NC}" >&2
exit 1
fi
org_project_manager_group_uid=$(curl "${CURL_FLAGS}" -X GET "https://keycloak.${cluster_fqdn}/admin/realms/master/groups?search=${org_uuid}_Project-Manager-Group" -H "Authorization: Bearer ${jwt_token}" | jq -r '.[0].id')
if [ "$org_project_manager_group_uid" == "null" ]; then
echo -e "${RED}Project Manager group not found in Keycloak for org ${org_name} (${org_uuid}, Organization creation may have failed ${NC}" >&2
exit 1
fi
#echo -e "Creating Organization admin user: ${org_admin_user}"
#curl ${CURL_FLAGS} -X PUT "https://keycloak.${cluster_fqdn}/admin/realms/master/users/${org_admin_uid}/groups/${org_project_manager_group_uid}" \
# -H "Authorization: Bearer ${jwt_token}" \
# -H "Content-Type: application/json" \
# -d '{}'
project_user_groups=("Project-Manager-Group")
for group in "${project_user_groups[@]}"; do
echo -e "Adding ${org_admin_user} to Organization groups: ${project_user_groups}"
# addGroupToKeycloakUser token user_id group_name cluster_fqdn
addGroupToKeycloakUser "${jwt_token}" "${org_admin_uid}" "${org_uuid}_${group}" "${cluster_fqdn}"
done
echo -e "${CYAN}Admin user ${org_admin_user} has been successfully created in ${org_name} ${NC}"
}
# Usage: createProjectInOrg <org_name> <project_name> <cluster_fqdn> <jwt_token>
function createProjectInOrg() {
local org_name=$1
local project_name=$2
local cluster_fqdn=$3
local jwt_token=$4
curl "${CURL_FLAGS}" -X PUT "https://api.${cluster_fqdn}/v1/projects/${project_name}" -H "accept: application/json" -H "Authorization: Bearer ${jwt_token}" -H "Content-Type: application/json" -d "{\"description\":\"${project_name}\"}"
while [ "$(curl "${CURL_FLAGS}" --location "https://api.${cluster_fqdn}/v1/projects/${project_name}" -H "accept: application/json" -H "Content-Type: application" -H "Authorization: Bearer ${jwt_token}" | jq -r .status.projectStatus.statusIndicator)" != "STATUS_INDICATION_IDLE" ]; do
echo "Waiting for ${project_name} to be provisioned..."
sleep 5
done
echo -e "${CYAN}Project ${project_name} has been successfully created in ${org_name} ${NC}"
}
# Usage: createProjectAdmin <username> <org_name> <cluster_fqdn> <jwt_token>
function createProjectAdmin() {
local username=$1
local org_name=$2
local cluster_fqdn=$3
local jwt_token=$4 # Admin token with permissions to create users in keycloak and read orgs
echo -e "${CYAN}Creating Project Admin ${username} in org ${org_name} ${NC}"
org_uuid=$(curl "${CURL_FLAGS}" --location "https://api.${cluster_fqdn}/v1/orgs/${org_name}" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${jwt_token}" | jq -r .status.orgStatus.uID)
if [ "$org_uuid" == "null" ]; then
echo -e "${RED}Cannot retrieve Org UID for ${org_name} ${NC}" >&2
exit 1
fi
if [ -z "${ORG_ADMIN_PASS=+xxx}" ]; then
echo -e "${CYAN} Org Admin account will be created as ${org_name}-admin"
echo -e "${GREEN}"
read -p '??? Provide requested user password : ' ORG_ADMIN_PASS
echo -e "${NC}"
else
echo -e "${CYAN}Organization admin will be created as ${org_name}-admin with password ${ORG_ADMIN_PASS} ${NC}"
fi
proj_admin_user_id=$(createKeycloakUser "${jwt_token}" "${username}" "${ORG_ADMIN_PASS}" "${cluster_fqdn}")
echo "User ${username} ID: ${proj_admin_user_id}"
project_user_groups=("${org_uuid}_Project-Manager-Group")
for group in "${project_user_groups[@]}"; do
addGroupToKeycloakUser "${jwt_token}" "${proj_admin_user_id}" "${group}" "${cluster_fqdn}"
done
echo -e "${CYAN}Project Admin user ${username} has been successfully created for orchestrator Organization ${org_name} ${NC}"
echo -e "${CYAN}You may now login to https://web-ui.${cluster_fqdn} with this user and password: ${ORG_ADMIN_PASS} ${NC}"
}
# Create Project User
# Usage: createProjectUser <username> <org_name> <project_name> <cluster_fqdn> <jwt_token> [admin_token] [groups]
function createProjectUser() {
local username=$1
local org_name=$2
local project_name=$3
local cluster_fqdn=$4
local jwt_token=$5 # User token with permission to read projects in the org
local admin_token=$6 # Admin token with permissions to create users in keycloak
#local groups=$7
#if [[ -z "$groups" ]]; then
# groups=("Edge-Manager-Group" "Edge-Onboarding-Group" "Edge-Operator-Group" "Host-Manager-Group")
#fi
groups=("Edge-Manager-Group" "Edge-Onboarding-Group" "Edge-Operator-Group" "Host-Manager-Group")
echo -e "${CYAN}Creating Project User ${username} in project ${project_name} and org ${org_name} ${NC}"
proj_uuid=$(curl "${CURL_FLAGS}" --location "https://api.${cluster_fqdn}/v1/projects/${project_name}" -H "accept: application/json" -H "Content-Type: application" -H "Authorization: Bearer ${jwt_token}" | jq -r .status.projectStatus.uID)
if [ "$proj_uuid" == "null" ]; then
echo -e "${RED}Cannot retrieve Project UID for ${project_name} in ${org_name} ${NC}" >&2
exit 1
fi
if [ -z "${ORG_USER_PASS+xxx}" ]; then
echo -e "${GREEN}"
read -p '??? Provide requested Org User password : ' org_user_pass
echo -e "${NC}"
else
echo -e "${CYAN}Organization admin will be created as ${org_name} with password ${ORG_USER_PASS} ${NC}"
fi
user_id=$(createKeycloakUser "${admin_token}" "${username}" "${ORG_USER_PASS}" "${cluster_fqdn}")
for group in "${groups[@]}"; do
addGroupToKeycloakUser "${admin_token}" "${user_id}" "${proj_uuid}_${group}" "${cluster_fqdn}"
done
echo -e "${CYAN}Project User ${username} has been successfully created in ${project_name} ${NC}"
}
# Create KeyCloakUser
# Usage: createKeycloakUser <token> <username> <password> <cluster_fqdn>
function createKeycloakUser() {
local token=$1
local username=$2
local password=$3
local cluster_fqdn=$4
curl "${CURL_FLAGS}" -X POST "https://keycloak.${cluster_fqdn}/admin/realms/master/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-d '{
"username": "'"${username}"'",
"email":"'"${username}@${cluster_fqdn}"'",
"enabled": true,
"emailVerified": true,
"credentials": [{
"type": "password",
"value": "'"${password}"'",
"temporary": false
}]
}' > /dev/null
curl "${CURL_FLAGS}" -X GET "https://keycloak.${cluster_fqdn}/admin/realms/master/users?username=${username}" -H "Authorization: Bearer ${token}" | jq -r '.[0].id'
}
# Add User to Groups in KeyCloak
# Usage: addGroupToKeycloakUser <token> <user_id> <group_name> <cluster_fqdn>
function OFFaddGroupToKeycloakUser() {
local token=$1
local user_id=$2
local group_name=$3
local cluster_fqdn=$4
echo -e "${CYAN}Adding user to group ${group_name} ${NC} "
group_id=$(curl "${CURL_FLAGS}" -X GET "https://keycloak.${cluster_fqdn}/admin/realms/master/groups?search=${group_name}" -H "Authorization: Bearer ${token}" | jq -r '.[0].id')
#TODO
# if group_id is null, exit with error
if [ "$group_id" == "null" ]; then
echo -e "${RED}Failed to find ${group_name} group in Keycloak ${NC}" >&2
exit 1
fi
curl "${CURL_FLAGS}" -X PUT "https://keycloak.${cluster_fqdn}/admin/realms/master/users/${user_id}/groups/${group_id}" \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
-d '{}'
}
# Usage: addGroupToKeycloakUser <token> <user_id> <group_name> <cluster_fqdn>
function addGroupToKeycloakUser() {
local token="$1"
local user_id="$2"
local group_name="$3"
local cluster_fqdn="$4"
echo -e "${CYAN}Adding user to group ${group_name} ${NC}"
group_id=$(curl "${CURL_FLAGS}" -X GET "https://keycloak.${cluster_fqdn}/admin/realms/master/groups?search=${group_name}" \
-H "Authorization: Bearer ${token}" | jq -r '.[0].id')
if [ -z "$group_id" ] || [ "$group_id" == "null" ]; then
echo -e "${RED}Failed to find ${group_name} group in Keycloak ${NC}" >&2
exit 1
fi
curl "${CURL_FLAGS}" -X PUT "https://keycloak.${cluster_fqdn}/admin/realms/master/users/${user_id}/groups/${group_id}" \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
-d '{}'
}
# Get all orgs from Orchestrator
# Usage getOrgs <cluster_fqdn> <jwt_token
function getOrgs() {
local cluster_fqdn=$1
local jwt_token=$2
orgs_list=$(curl "${CURL_FLAGS}" -X GET "https://api.${cluster_fqdn}/v1/orgs" -H "accept: application/json" -H "Authorization: Bearer ${jwt_token}" -H "Content-Type: application/json" | jq '.[].name' )
echo "${orgs_list}"
}
# Usage: deleteOrg <org_name> <cluster_fqdn> <jwt_token>
function deleteOrg() {
local org_name=$1
local cluster_fqdn=$2
local jwt_token=$3
echo -e "${CYAN}Deleting Organization ${org_name} ${NC}"
response=$(curl "${CURL_FLAGS}" -o /dev/null -w "%{http_code}" -X DELETE "https://api.${cluster_fqdn}/v1/orgs/${org_name}" \
-H "Authorization: Bearer ${jwt_token}")
if [ "$response" -eq 200 ]; then
echo "Organization ${org_name} deleted successfully."
else
echo "Failed to delete Organization ${org_name}. HTTP response code: $response"
exit 1
fi
}
# Usage: getProjects <cluster_fqdn> <jwt_token>
# Returns: list of projects with their names and UIDs
# Org selection is based on the JWT token provided, so the user must have permissions to delete the project
function getProjects () {
local cluster_fqdn=$1
local jwt_token=$2
echo -e "${CYAN}Retrieving all projects ${NC}"
echo "| Project Name | Project UID |"
curl "${CURL_FLAGS}" -X GET "https://api.${cluster_fqdn}/v1/projects" -H "Authorization: Bearer ${jwt_token}" | jq -r '.[] | select(.name and .status.projectStatus.uID) | "| \(.name) | \(.status.projectStatus.uID)"'
}
# Usage: deleteProject <project_name> <cluster_fqdn> <jwt_token>
# Org selection is based on the JWT token provided, so the user must have permissions to delete the project
function deleteProject() {
local project_name=$1
local cluster_fqdn=$2
local jwt_token=$3
echo -e "${CYAN}Deleting Project ${project_name} ${NC}"
response=$(curl "${CURL_FLAGS}" -o /dev/null -w "%{http_code}" -X DELETE "https://api.${cluster_fqdn}/v1/projects/${project_name}" \
-H "Authorization: Bearer ${jwt_token}")
if [ "$response" -eq 200 ]; then
echo "Project ${project_name} deleted successfully."
else
echo "Failed to delete Project ${project_name}. HTTP response code: $response"
exit 1
fi
}
# Usage: get_keycloak_user_id <username> <cluster_fqdn> <jwt_token>
# Returns: user_id
function get_keycloak_user_id() {
local username="$1"
local cluster_fqdn="$2"
local jwt_token="$3"
echo -e "Retrieving userID for user ${username}"
user_id=$(curl "${CURL_FLAGS}" -X GET "https://keycloak.${cluster_fqdn}/admin/realms/master/users?q=username:${username}&exact=true" -H "Authorization: Bearer ${jwt_token}" | jq -r '.[0].id')
if [ "$user_id" == "null" ]; then
set +x
echo "Failed to fetch user ID from Keycloak."
exit 1
fi
echo -e "Username ${username} has ID of ${user_id}"
}
#KC get all users:
# Usage: getkcUsers <cluster_fqdn> <jwt_token>
function getkcUsers() {
local cluster_fqdn=$1
local jwt_token=$2
echo "${CYAN}Retrieving all Keycloak users ${NC}"
echo "| Username | Email | UID |"
curl "${CURL_FLAGS}" -X GET https://keycloak.${CLUSTER_FQDN}/admin/realms/master/users -H "Authorization: Bearer ${jwt_token}"|jq -r '.[] | select(.username and .email and .id) | "| \(.username) | \(.email) | \(.id)"'
}
# Usage: delete_keycloak_user <username> <cluster_fqdn> <admin_token>
function delete_keycloak_user() {
local username=$1
local admin_token=$3
local cluster_fqdn=$2
# Fetch user ID by username
local userid=$(get_keycloak_user_id "$username" "$cluster_fqdn" "$admin_token")
response=$(curl "${CURL_FLAGS}" -o /dev/null -w "%{http_code}" -X DELETE "https://keycloak.${cluster_fqdn}/admin/realms/master/users/${userid}" \
-H "Authorization: Bearer ${admin_token}")
if [ "$response" -eq 204 ]; then
echo "Keycloak user deleted successfully."
else
echo "Failed to delete Keycloak user. HTTP response code: $response"
exit 1
fi
}