11#! /bin/bash
22
3- NAMESPACE=${CH_ACCOUNTS_REALM}
4- USERNAME=admin_api
5- PASSWORD=$( cat /opt/cloudharness/resources/auth/api_user_password)
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_api_client"
6+ export TMP_CLIENT_SECRET=" ${KC_BOOTSTRAP_ADMIN_USERNAME} "
67
7- echo " Checking if API user exists... "
8+ sleep 120
89
9- # Check if user already exists
10- if /opt/keycloak/bin/kcadm.sh get users -q " username=$USERNAME " | grep -q " $USERNAME " ; then
11- echo " ERROR: API user $USERNAME already exists, but password is out of sync. You may need to reset it manually."
12- # /opt/keycloak/bin/kcadm.sh set-password --username "$USERNAME" --new-password "$PASSWORD"
13- # Removed automatic password reset as that would only work if the main admin password is unchanged from the default password
14- # That would create the false impression that the password is reset successfully when in fact it has not on production systems
10+ echo " create_api_user: waiting for Keycloak to start..."
11+
12+ create_temporary_client () {
13+ /opt/keycloak/bin/kc.sh bootstrap-admin service --client-id=${TMP_CLIENT} --client-secret:env=TMP_CLIENT_SECRET --http-management-port 9876
14+ }
15+
16+ delete_temporary_client () {
17+ CLIENT_ID=$( /opt/keycloak/bin/kcadm.sh get clients -r master -q clientId=${TMP_CLIENT} --fields id --format csv| tr -d ' "' )
18+ if [ -n " $CLIENT_ID " ]; then
19+ /opt/keycloak/bin/kcadm.sh delete clients/$CLIENT_ID -r master
20+ fi
21+ }
22+
23+ create_kc_config () {
24+ /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
25+ }
26+
27+ api_user_exists () {
28+ return $( /opt/keycloak/bin/kcadm.sh get users -q " username=$API_USERNAME " | grep -q " $API_USERNAME " ; echo $? )
29+ }
30+
31+ create_api_user () {
32+ /opt/keycloak/bin/kcadm.sh create users -s " username=${API_USERNAME} " -s enabled=True
33+ }
34+
35+ set_password_and_roles () {
36+ /opt/keycloak/bin/kcadm.sh set-password --username " $API_USERNAME " --new-password " $API_PASSWORD "
37+ /opt/keycloak/bin/kcadm.sh add-roles --uusername " $API_USERNAME " --rolename admin
38+ }
39+
40+ # Wait for Keycloak to be ready - just give it some time to start up
41+
42+
43+ echo " Attempting authentication..."
44+
45+ # First, try to authenticate as admin_api
46+ if [ -n " $API_PASSWORD " ] && /opt/keycloak/bin/kcadm.sh config credentials \
47+ --server http://localhost:8080 \
48+ --realm master \
49+ --user " $API_USERNAME " \
50+ --password " $API_PASSWORD " 2> /dev/null; then
51+ echo " Successfully authenticated as $API_USERNAME "
52+ echo " Startup scripts not needed (admin_api user already exists)"
1553 exit 0
1654fi
1755
18- echo " Creating API user $USERNAME "
19- set -e
20- # create the user and reload keycloak
21- /opt/keycloak/bin/kcadm.sh create users -s " username=$USERNAME " -s enabled=True
22- /opt/keycloak/bin/kcadm.sh set-password --username " $USERNAME " --new-password " $PASSWORD "
23- /opt/keycloak/bin/kcadm.sh add-roles --uusername " $USERNAME " --rolename admin
56+ echo " admin_api user does not exist or authentication failed. Authenticating to create the user..."
57+
58+ set -e
59+ create_temporary_client
60+ create_kc_config
61+ echo " Temporary credentials successfully created."
62+
63+ echo " Checking if API user exists..."
64+ # Check if user already exists
65+ if ! api_user_exists; then
66+ echo " API user $API_USERNAME doesn't exists, creating..."
67+ create_api_user
68+ echo " API user created successfully"
69+ else
70+ echo " API user $API_USERNAME already exists."
71+ fi
72+ set +e
73+
74+ echo " Setting password and role."
75+ set_password_and_roles
2476
25- echo " API user created successfully"
77+ echo " Cleaning up temporary client."
78+ delete_temporary_client
0 commit comments