|
1 | 1 | #!/usr/bin/env bash |
2 | | -# Phase: register this VM's dashboard redirect URI on the shared Keycloak client. |
3 | | -# Cluster-side only; the dashboard itself is started by the in-VM installer. |
| 2 | +# Phase: register this VM's redirect URIs on the shared Keycloak client: the |
| 3 | +# dashboard's (the *-webui route) and one per sandbox web UI route |
| 4 | +# (UI_ROUTE_HOSTS). Cluster-side only; the proxies run in the VM. |
4 | 5 | # Expects: VM_NAME, NS, OIDC_KEYCLOAK_NAME, OIDC_REALM, KEYCLOAK_NS, |
5 | | -# DASHBOARD_CLIENT_ID, OIDC_ISSUER_URL |
| 6 | +# DASHBOARD_CLIENT_ID, OIDC_ISSUER_URL, DASHBOARD_ENABLED, UI_ROUTE_HOSTS |
6 | 7 |
|
7 | | -WEBUI_ROUTE_HOST="$(kubectl get route "${VM_NAME}-webui" -n "${NS}" -o jsonpath='{.spec.host}' 2>/dev/null || true)" |
8 | | -if [[ -z "${WEBUI_ROUTE_HOST}" ]]; then |
9 | | - echo "WARNING: ${VM_NAME}-webui route not found — skipping dashboard setup" |
10 | | - return 0 2>/dev/null || true |
| 8 | +HOSTS=() |
| 9 | +if [[ "${DASHBOARD_ENABLED:-false}" == "true" ]]; then |
| 10 | + WEBUI_ROUTE_HOST="$(kubectl get route "${VM_NAME}-webui" -n "${NS}" -o jsonpath='{.spec.host}' 2>/dev/null || true)" |
| 11 | + if [[ -n "${WEBUI_ROUTE_HOST}" ]]; then |
| 12 | + HOSTS+=("${WEBUI_ROUTE_HOST}") |
| 13 | + else |
| 14 | + echo "WARNING: ${VM_NAME}-webui route not found — skipping the dashboard redirect URI" |
| 15 | + fi |
| 16 | +fi |
| 17 | +for host in ${UI_ROUTE_HOSTS:-}; do |
| 18 | + HOSTS+=("${host}") |
| 19 | +done |
| 20 | +if [[ ${#HOSTS[@]} -eq 0 ]]; then |
| 21 | + echo "No redirect URIs to register." |
| 22 | + return 0 2>/dev/null || exit 0 |
11 | 23 | fi |
12 | 24 |
|
13 | | -DASHBOARD_REDIRECT_URL="https://${WEBUI_ROUTE_HOST}/oauth2/callback" |
14 | | - |
15 | | -# Register redirect URI on Keycloak client |
16 | 25 | KC_ADMIN_SECRET="${OIDC_KEYCLOAK_NAME}-initial-admin" |
17 | 26 | KC_ADMIN_USER="$(kubectl get secret "${KC_ADMIN_SECRET}" -n "${KEYCLOAK_NS}" -o jsonpath='{.data.username}' 2>/dev/null | base64 -d || true)" |
18 | 27 | KC_ADMIN_PASS="$(kubectl get secret "${KC_ADMIN_SECRET}" -n "${KEYCLOAK_NS}" -o jsonpath='{.data.password}' 2>/dev/null | base64 -d || true)" |
19 | 28 | KC_BASE="$(echo "${OIDC_ISSUER_URL}" | sed 's#/realms/.*##')" |
20 | | -if [[ -n "${KC_ADMIN_USER}" && -n "${KC_BASE}" ]]; then |
21 | | - echo "Registering redirect URI on Keycloak client '${DASHBOARD_CLIENT_ID}'..." |
22 | | - KC_TOKEN_RESPONSE="$(curl -sk -X POST "${KC_BASE}/realms/master/protocol/openid-connect/token" \ |
23 | | - -d "grant_type=password" -d "client_id=admin-cli" \ |
24 | | - -d "username=${KC_ADMIN_USER}" -d "password=${KC_ADMIN_PASS}")" |
25 | | - KC_ADMIN_TOKEN="$(echo "${KC_TOKEN_RESPONSE}" | jq -r '.access_token // empty')" |
26 | | - if [[ -z "${KC_ADMIN_TOKEN}" ]]; then |
27 | | - echo "WARNING: Keycloak admin token fetch failed: $(echo "${KC_TOKEN_RESPONSE}" | jq -r '.error_description // .error // "no response / unparseable response"')" |
28 | | - fi |
29 | | - if [[ -n "${KC_ADMIN_TOKEN}" ]]; then |
30 | | - CLIENT_UUID="$(curl -sk -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" \ |
31 | | - "${KC_BASE}/admin/realms/${OIDC_REALM}/clients?clientId=${DASHBOARD_CLIENT_ID}" \ |
32 | | - | jq -r '.[0].id // empty')" |
33 | | - if [[ -n "${CLIENT_UUID}" ]]; then |
34 | | - CLIENT_JSON="$(curl -sk -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" \ |
35 | | - "${KC_BASE}/admin/realms/${OIDC_REALM}/clients/${CLIENT_UUID}")" |
36 | | - WEBUI_ORIGIN="https://${WEBUI_ROUTE_HOST}" |
37 | | - UPDATED_JSON="$(echo "${CLIENT_JSON}" | jq \ |
38 | | - --arg redirect "${DASHBOARD_REDIRECT_URL}" --arg origin "${WEBUI_ORIGIN}" \ |
39 | | - '.redirectUris = ((.redirectUris // []) + [$redirect] | unique) | |
40 | | - .webOrigins = ((.webOrigins // []) + [$origin] | unique)')" |
41 | | - HTTP_CODE="$(curl -sk -o /dev/null -w '%{http_code}' -X PUT \ |
42 | | - -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" -H "Content-Type: application/json" \ |
43 | | - -d "${UPDATED_JSON}" \ |
44 | | - "${KC_BASE}/admin/realms/${OIDC_REALM}/clients/${CLIENT_UUID}")" |
45 | | - if [[ "${HTTP_CODE}" == "204" ]]; then |
46 | | - echo "Redirect URI registered: ${DASHBOARD_REDIRECT_URL}" |
47 | | - else |
48 | | - echo "WARNING: failed to register redirect URI (HTTP ${HTTP_CODE}) — dashboard OIDC login will fail" |
49 | | - fi |
50 | | - else |
51 | | - echo "WARNING: Keycloak client '${DASHBOARD_CLIENT_ID}' not found — dashboard OIDC login will fail" |
52 | | - fi |
53 | | - else |
54 | | - echo "WARNING: could not obtain Keycloak admin token — dashboard OIDC login will fail" |
55 | | - fi |
| 29 | +if [[ -z "${KC_ADMIN_USER}" || -z "${KC_BASE}" ]]; then |
| 30 | + echo "WARNING: Keycloak admin credentials not found — OIDC login through ${HOSTS[*]} will fail" |
| 31 | + return 0 2>/dev/null || exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "Registering redirect URIs on Keycloak client '${DASHBOARD_CLIENT_ID}': ${HOSTS[*]}" |
| 35 | +KC_TOKEN_RESPONSE="$(curl -sk -X POST "${KC_BASE}/realms/master/protocol/openid-connect/token" \ |
| 36 | + -d "grant_type=password" -d "client_id=admin-cli" \ |
| 37 | + -d "username=${KC_ADMIN_USER}" -d "password=${KC_ADMIN_PASS}")" |
| 38 | +KC_ADMIN_TOKEN="$(echo "${KC_TOKEN_RESPONSE}" | jq -r '.access_token // empty')" |
| 39 | +if [[ -z "${KC_ADMIN_TOKEN}" ]]; then |
| 40 | + echo "WARNING: Keycloak admin token fetch failed: $(echo "${KC_TOKEN_RESPONSE}" | jq -r '.error_description // .error // "no response / unparseable response"')" |
| 41 | + return 0 2>/dev/null || exit 0 |
| 42 | +fi |
| 43 | +CLIENT_UUID="$(curl -sk -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" \ |
| 44 | + "${KC_BASE}/admin/realms/${OIDC_REALM}/clients?clientId=${DASHBOARD_CLIENT_ID}" \ |
| 45 | + | jq -r '.[0].id // empty')" |
| 46 | +if [[ -z "${CLIENT_UUID}" ]]; then |
| 47 | + echo "WARNING: Keycloak client '${DASHBOARD_CLIENT_ID}' not found — OIDC login will fail" |
| 48 | + return 0 2>/dev/null || exit 0 |
| 49 | +fi |
| 50 | + |
| 51 | +REDIRECTS="$(printf 'https://%s/oauth2/callback\n' "${HOSTS[@]}" | jq -R . | jq -s .)" |
| 52 | +ORIGINS="$(printf 'https://%s\n' "${HOSTS[@]}" | jq -R . | jq -s .)" |
| 53 | +CLIENT_JSON="$(curl -sk -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" \ |
| 54 | + "${KC_BASE}/admin/realms/${OIDC_REALM}/clients/${CLIENT_UUID}")" |
| 55 | +# Added, never removed: other VMs register on the same client. |
| 56 | +UPDATED_JSON="$(echo "${CLIENT_JSON}" | jq \ |
| 57 | + --argjson redirects "${REDIRECTS}" --argjson origins "${ORIGINS}" \ |
| 58 | + '.redirectUris = ((.redirectUris // []) + $redirects | unique) | |
| 59 | + .webOrigins = ((.webOrigins // []) + $origins | unique)')" |
| 60 | +HTTP_CODE="$(curl -sk -o /dev/null -w '%{http_code}' -X PUT \ |
| 61 | + -H "Authorization: Bearer ${KC_ADMIN_TOKEN}" -H "Content-Type: application/json" \ |
| 62 | + -d "${UPDATED_JSON}" \ |
| 63 | + "${KC_BASE}/admin/realms/${OIDC_REALM}/clients/${CLIENT_UUID}")" |
| 64 | +if [[ "${HTTP_CODE}" == "204" ]]; then |
| 65 | + echo "Redirect URIs registered: $(echo "${REDIRECTS}" | jq -r 'join(" ")')" |
56 | 66 | else |
57 | | - echo "WARNING: Keycloak admin credentials not found — dashboard OIDC login will fail" |
| 67 | + echo "WARNING: failed to register redirect URIs (HTTP ${HTTP_CODE}) — OIDC login will fail" |
58 | 68 | fi |
0 commit comments