Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions applications/accounts/scripts/create_api_user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ export API_PASSWORD=$(cat /opt/cloudharness/resources/auth/api_user_password 2>/
export TMP_CLIENT="tmp_api_client"
export TMP_CLIENT_SECRET="${KC_BOOTSTRAP_ADMIN_USERNAME}"

sleep 120
check_kc_ready() {
if echo >/dev/tcp/127.0.0.1/${ACCOUNTS_SERVICE_PORT}; then
return 0
fi 2> /dev/null
return 1
}

echo "create_api_user: waiting for Keycloak to start..."
while ! check_kc_ready; do
echo "create_api_user: waiting for Keycloak to be ready..."
sleep 10
done

create_temporary_client() {
/opt/keycloak/bin/kc.sh bootstrap-admin service --client-id=${TMP_CLIENT} --client-secret:env=TMP_CLIENT_SECRET --http-management-port 9876
Expand All @@ -21,7 +29,7 @@ delete_temporary_client() {
}

create_kc_config() {
/opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
/opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:${ACCOUNTS_SERVICE_PORT} --realm master --client ${TMP_CLIENT} --secret ${TMP_CLIENT_SECRET}
}

api_user_exists() {
Expand All @@ -40,39 +48,39 @@ set_password_and_roles() {
# Wait for Keycloak to be ready - just give it some time to start up


echo "Attempting authentication..."
echo "create_api_user: attempting authentication..."

# First, try to authenticate as admin_api
if [ -n "$API_PASSWORD" ] && /opt/keycloak/bin/kcadm.sh config credentials \
--server http://localhost:8080 \
--server http://localhost:${ACCOUNTS_SERVICE_PORT} \
--realm master \
--user "$API_USERNAME" \
--password "$API_PASSWORD" 2>/dev/null; then
echo "Successfully authenticated as $API_USERNAME"
echo "Startup scripts not needed (admin_api user already exists)"
echo "create_api_user: successfully authenticated as $API_USERNAME"
echo "create_api_user: sStartup scripts not needed (admin_api user already exists)"
exit 0
fi

echo "admin_api user does not exist or authentication failed. Authenticating to create the user..."
echo "create_api_user: admin_api user does not exist or authentication failed. Authenticating to create the user..."

set -e
create_temporary_client
create_kc_config
echo "Temporary credentials successfully created."
echo "create_api_user: temporary credentials successfully created."

echo "Checking if API user exists..."
echo "create_api_user: checking if API user exists..."
# Check if user already exists
if ! api_user_exists; then
echo "API user $API_USERNAME doesn't exists, creating..."
echo "create_api_user: API user $API_USERNAME doesn't exists, creating..."
create_api_user
echo "API user created successfully"
echo "create_api_user: API user created successfully"
else
echo "API user $API_USERNAME already exists."
echo "create_api_user: API user $API_USERNAME already exists."
fi
set +e

echo "Setting password and role."
echo "create_api_user: setting password and role."
set_password_and_roles

echo "Cleaning up temporary client."
echo "create_api_user: cleaning up temporary client."
delete_temporary_client
Loading