From c1382d3f04990e6e497ee38ec3b89ce22265d399 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 18 Nov 2026 18:19:11 +0100 Subject: [PATCH] upgrade-keycloak.sh: Use readiness endpoint and derive expected issuer from Keycloak config The post-upgrade check compared the issuer against FQHN_HOSTNAME, which can differ from Keycloak's public 'hostname' setting, so a healthy Keycloak was reported as failed. - Wait for /health/ready on the management port (fall back to the well-known endpoint if health is not enabled), timeout 120s. - Derive the expected issuer from 'hostname' in the new keycloak.conf, falling back to FQHN_HOSTNAME. - Report an issuer mismatch as a warning instead of aborting. - Keep the curl error instead of overwriting it with the journal output. Also fix missing comma in .claude/settings.json. Co-Authored-By: Claude Opus 5.5 --- .claude/settings.json | 2 +- upgrade-keycloak.sh | 51 +++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.claude/settings.json b/.claude/settings.json index 42705ae..c49f19d 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -6,7 +6,7 @@ "Bash(scp * iam-nd.oopen.de:*)", "Bash(scp iam-nd.oopen.de:* *)", "Bash(rsync * iam-nd.oopen.de:*)", - "Bash(rsync iam-nd.oopen.de:* *)" + "Bash(rsync iam-nd.oopen.de:* *)", "Bash(ssh keycloak-nd.oopen.de)", "Bash(ssh keycloak-nd.oopen.de *)", "Bash(scp * keycloak-nd.oopen.de:*)", diff --git a/upgrade-keycloak.sh b/upgrade-keycloak.sh index 05006e4..de4297e 100755 --- a/upgrade-keycloak.sh +++ b/upgrade-keycloak.sh @@ -913,24 +913,37 @@ echo echo -e "\033[37m\033[1mSome post-installation stuff..\033[m" echo +# Readiness check: prefer the health endpoint on the management interface +# (requires 'health-enabled=true'), otherwise fall back to the OIDC +# well-known endpoint of the master realm. +# +NEW_KC_CONF="${KEYCLOAK_INSTALL_DIR}/conf/keycloak.conf" +if grep -Eq '^[[:space:]]*health-enabled[[:space:]]*=[[:space:]]*true' "${NEW_KC_CONF}" 2>/dev/null ; then + _ready_url="http://127.0.0.1:9000/health/ready" +else + _ready_url="http://127.0.0.1:8080/realms/master/.well-known/openid-configuration" +fi + +# Expected issuer: derived from 'hostname' in Keycloak's keycloak.conf (may be +# a full URL or a bare hostname), falling back to FQHN_HOSTNAME. +# +_kc_hostname="$(sed -n -E 's/^[[:space:]]*hostname[[:space:]]*=[[:space:]]*([^[:space:]]+).*/\1/p' "${NEW_KC_CONF}" 2>/dev/null | tail -n 1)" +[[ -z "${_kc_hostname}" ]] && _kc_hostname="${FQHN_HOSTNAME}" +[[ "${_kc_hostname}" =~ ^https?:// ]] || _kc_hostname="https://${_kc_hostname}" +_kc_hostname="${_kc_hostname%/}" +_expected_issuer="${_kc_hostname}/realms/master" +_issuer_host="${_kc_hostname#*://}" +_issuer_host="${_issuer_host%%/*}" + echononl "Wait until the Keycloak service has started completely." echo_wait declare -i index=0 -declare -i _max_secs_waiting=60 +declare -i _max_secs_waiting=120 keycloak_service_started=false while true ; do - # Try to establish a connection to localhost:8080 - # if systemctl is-active --quiet keycloak.service && \ - curl --fail --silent --show-error \ - -H "Host: ${FQHN_HOSTNAME}" \ - -H "X-Forwarded-Host: ${FQHN_HOSTNAME}" \ - -H "X-Forwarded-Proto: https" \ - "http://127.0.0.1:8080/realms/master/.well-known/openid-configuration" \ - -o "${LOCK_DIR}/openid-configuration.json" 2> "$log_file" && \ - grep -Eq '"issuer"[[:space:]]*:[[:space:]]*"https://'"${FQHN_HOSTNAME//./\.}"'/realms/master"' \ - "${LOCK_DIR}/openid-configuration.json" ; then + curl --fail --silent --show-error -o /dev/null "${_ready_url}" > "${LOCK_DIR}/ready-check.err" 2>&1 ; then echo_ok keycloak_service_started=true break @@ -938,10 +951,24 @@ while true ; do if [[ ${index} -ge ${_max_secs_waiting} ]]; then echo_failed journalctl -u keycloak.service -n 50 --no-pager > "$log_file" 2>&1 - fatal "Keycloak did not become healthy within ${_max_secs_waiting} seconds or returned an unexpected issuer. The old installation remains at '${CUR_INSTALL_DIR}'. Do not start it against a possibly migrated database without checking compatibility or restoring '${DB_DUMP_FILE}'. Last log messages:\n$(cat "$log_file")" + fatal "Keycloak did not become ready within ${_max_secs_waiting} seconds (checked: ${_ready_url}). Last check error: $(cat "${LOCK_DIR}/ready-check.err" 2>/dev/null). The old installation remains at '${CUR_INSTALL_DIR}'. Do not start it against a possibly migrated database without checking compatibility or restoring '${DB_DUMP_FILE}'. Last log messages:\n$(cat "$log_file")" fi (( index++ )) sleep 1 done +echononl "Check issuer of realm 'master' (${_expected_issuer}).." +_issuer="$(curl --fail --silent --show-error \ + -H "Host: ${_issuer_host}" \ + -H "X-Forwarded-Host: ${_issuer_host}" \ + -H "X-Forwarded-Proto: https" \ + "http://127.0.0.1:8080/realms/master/.well-known/openid-configuration" 2> "$log_file" \ + | sed -n -E 's/.*"issuer"[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p')" +if [[ "${_issuer}" == "${_expected_issuer}" ]]; then + echo_ok +else + echo_failed + warn "Unexpected issuer for realm 'master'.\n\n\tExpected: ${_expected_issuer}\n\tReturned: ${_issuer:-}\n\n\t$(cat "$log_file")\n\n\tKeycloak is running; please check 'hostname' in '${NEW_KC_CONF}' and FQHN_HOSTNAME in '${conf_file}'." +fi + clean_up 0