Skip to content

Instantly share code, notes, and snippets.

@ulrich
Created June 23, 2022 14:27
Show Gist options
  • Save ulrich/aa04a793d54703998ecb015a0e2ff803 to your computer and use it in GitHub Desktop.
Save ulrich/aa04a793d54703998ecb015a0e2ff803 to your computer and use it in GitHub Desktop.
Simple health check for Keycloak
For more explanation you can take a look on this article : https://dev.to/ulrich/simple-health-check-for-keycloak-259p
#!/bin/bash
login_access=$(curl -k -X POST \
-H "Content-Type:application/x-www-form-urlencoded" \
-d "grant_type=password" \
-d "client_id=admin-cli" \
-d "username=alive" \
-d "password=[REDACTED]" \
'https://keyclaok.company.com/auth/realms/[REALM]/protocol/openid-connect/token')
error=$(jq -r .error <<< $login_access)
if [ $error == "null" ]; then
echo "Login successful for test user."
else
echo "Unable to login test user ($error)."
exit 1
fi
access_token=$(jq -r '.access_token' <<< "${login_access}")
refresh_token=$(jq -r '.refresh_token' <<< "${login_access}")
logout_response=$(curl -s -o /dev/null -w '%{http_code}' -k -X POST \
-H "Content-Type:application/x-www-form-urlencoded" \
-H "Authorization: Bearer $access_token" \
-d "client_id=[CLIENT_ID]" \
-d "refresh_token=$refresh_token" \
'https://keycloak.company.com/auth/realms/[REALM]/protocol/openid-connect/logout')
if [ $logout_response -eq 204 ]; then
echo "Logout successful for test user."
else
echo "Unable to logout test user ($logout_response)."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment