Skip to content

Commit 7cb696a

Browse files
committed
chore: refactor create/update/refresh admin_api user
1 parent a070b17 commit 7cb696a

1 file changed

Lines changed: 55 additions & 37 deletions

File tree

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
#!/bin/bash
22

3-
4-
API_USERNAME="admin_api"
5-
API_PASSWORD=$(cat /opt/cloudharness/resources/auth/api_user_password 2>/dev/null || echo "")
3+
export API_USERNAME="admin_api"
4+
export API_PASSWORD=$(cat /opt/cloudharness/resources/auth/api_user_password 2>/dev/null || echo "")
5+
export TMP_CLIENT="tmp_client"
6+
export TMP_CLIENT_SECRET="${KC_BOOTSTRAP_ADMIN_USERNAME}"
67

78
echo "create_api_user: waiting for Keycloak to start..."
89

10+
create_temporary_client() {
11+
/opt/keycloak/bin/kc.sh bootstrap-admin service --client-id=${TMP_CLIENT} --client-secret:env=TMP_CLIENT_SECRET
12+
}
13+
14+
delete_temporary_client() {
15+
CLIENT_ID=$(/opt/keycloak/bin/kcadm.sh get clients -r master -q clientId=${TMP_CLIENT} --fields id --format csv|tr -d '"')
16+
if [ -n "$CLIENT_ID" ]; then
17+
/opt/keycloak/bin/kcadm.sh delete clients/$CLIENT_ID -r master
18+
fi
19+
}
20+
21+
create_kc_config() {
22+
/opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
23+
}
24+
25+
api_user_exists() {
26+
return $(/opt/keycloak/bin/kcadm.sh get users -q "username=$API_USERNAME" | grep -q "$API_USERNAME"; echo $?)
27+
}
28+
29+
create_api_user() {
30+
/opt/keycloak/bin/kcadm.sh create users -s "username=${API_USERNAME}" -s enabled=True
31+
}
32+
33+
set_password_and_roles() {
34+
/opt/keycloak/bin/kcadm.sh set-password --username "$API_USERNAME" --new-password "$API_PASSWORD"
35+
/opt/keycloak/bin/kcadm.sh add-roles --uusername "$API_USERNAME" --rolename admin
36+
}
37+
938
# Wait for Keycloak to be ready - just give it some time to start up
10-
sleep 120s
39+
1140

1241
echo "Attempting authentication..."
1342

@@ -19,40 +48,29 @@ if [ -n "$API_PASSWORD" ] && /opt/keycloak/bin/kcadm.sh config credentials \
1948
--password "$API_PASSWORD" 2>/dev/null; then
2049
echo "Successfully authenticated as $API_USERNAME"
2150
echo "Startup scripts not needed (admin_api user already exists)"
22-
else
23-
echo "admin_api user does not exist or authentication failed. Authenticating as bootstrap admin to create the user..."
24-
25-
# Authenticate as bootstrap admin to create admin_api user
26-
if ! /opt/keycloak/bin/kcadm.sh config credentials \
27-
--server http://localhost:8080 \
28-
--realm master \
29-
--user "$KC_BOOTSTRAP_ADMIN_USERNAME" \
30-
--password "$KC_BOOTSTRAP_ADMIN_PASSWORD"; then
31-
echo "ERROR: Failed to authenticate as bootstrap admin. You must manually create the ${API_USERNAME} with password from the secret api_user_password."
32-
echo "Continuing without running startup scripts..."
33-
exit 0
34-
fi
35-
36-
echo "Successfully authenticated as bootstrap admin"
37-
38-
echo "Checking if API user exists..."
39-
40-
# Check if user already exists
41-
if /opt/keycloak/bin/kcadm.sh get users -q "username=$API_USERNAME" | grep -q "$API_USERNAME"; then
42-
echo "ERROR: API user $API_USERNAME already exists, but password is out of sync. You may need to reset it manually."
43-
# /opt/keycloak/bin/kcadm.sh set-password --username "$API_USERNAME" --new-password "$API_PASSWORD"
44-
# Removed automatic password reset as that would only work if the main admin password is unchanged from the default password
45-
# That would create the false impression that the password is reset successfully when in fact it has not on production systems
46-
exit 0
47-
fi
51+
exit 0
52+
fi
4853

49-
echo "Creating API user $API_USERNAME"
50-
set -e
51-
# create the user and reload keycloak
52-
/opt/keycloak/bin/kcadm.sh create users -s "username=$API_USERNAME" -s enabled=True
53-
/opt/keycloak/bin/kcadm.sh set-password --username "$API_USERNAME" --new-password "$API_PASSWORD"
54-
/opt/keycloak/bin/kcadm.sh add-roles --uusername "$API_USERNAME" --rolename admin
54+
echo "admin_api user does not exist or authentication failed. Authenticating to create the user..."
55+
56+
set -e
57+
create_temporary_client
58+
create_kc_config
59+
echo "Temporary credentials successfully created."
5560

61+
echo "Checking if API user exists..."
62+
# Check if user already exists
63+
if ! api_user_exists; then
64+
echo "API user $API_USERNAME doesn't exists, creating..."
65+
create_api_user
5666
echo "API user created successfully"
57-
fi
67+
else
68+
echo "API user $API_USERNAME already exists."
69+
fi
70+
set +e
71+
72+
echo "Setting password and role."
73+
set_password_and_roles
5874

75+
echo "Cleaning up temporary client."
76+
delete_temporary_client

0 commit comments

Comments
 (0)