fix(web-app-keycloak): create the dictionary's client scopes before converging the client

The client update converges defaultClientScopes and optionalClientScopes
and fails with "Desired client scope does not exist in the realm" for a
scope the realm lacks. A realm imported from the current dictionary has
them all, but a realm an older release created does not, and the RBAC
scope was only created later, in 04_rbac_client_scope.yml, after the
convergence had already failed. The Keycloak stage on echoserver stopped
there on its legacy realm.

01_client.yml now reads the realm's client scopes and creates every scope
of KEYCLOAK_DICTIONARY_REALM.clientScopes that is missing, before the
convergence; existing scopes are left alone, and on an imported realm the
step creates nothing. The RBAC-only create in 04 is gone with it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-23 16:16:51 +02:00
1 parent 9007d144d8
commit a1ce0e6491
2 files changed
+19 -16

No files matched your search

@@ -1,4 +1,23 @@
---
- name: ⬇️ Read the client scopes of the realm
shell: "{{ KEYCLOAK_EXEC_KCADM }} get client-scopes -r {{ KEYCLOAK_REALM }} --format json"
register: kc_realm_client_scopes
changed_when: false
- name: ✅ Create the client scopes the realm dictionary declares and the realm lacks
shell: |
cat <<'JSON' | {{ KEYCLOAK_EXEC_KCADM }} create client-scopes -r {{ KEYCLOAK_REALM }} -f -
{{ kc_client_scope | to_json }}
JSON
loop: >-
{{ KEYCLOAK_DICTIONARY_REALM.clientScopes
| rejectattr('name', 'in', kc_realm_client_scopes.stdout | kcadm_json | map(attribute='name') | list)
| list }}
loop_control:
loop_var: kc_client_scope
label: "{{ kc_client_scope.name }}"
changed_when: true
- name: "🔄 Update Client settings"
vars:
kc_object_kind: "client"
@@ -1,20 +1,4 @@
---
- name: ✅ Ensure RBAC client scope exists
shell: |
cat <<'JSON' | {{ KEYCLOAK_EXEC_KCADM }} create client-scopes -r {{ KEYCLOAK_REALM }} -f -
{{
(
KEYCLOAK_DICTIONARY_REALM.clientScopes
| selectattr('name','equalto', KEYCLOAK_RBAC_GROUP_CLAIM)
| list | first
) | to_json
}}
JSON
register: create_rbac_scope
changed_when: create_rbac_scope.rc == 0
failed_when: create_rbac_scope.rc != 0 and ('already exists' not in (create_rbac_scope.stderr | lower))
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: ⬇️ Get all client scopes
shell: "{{ KEYCLOAK_EXEC_KCADM }} get client-scopes -r {{ KEYCLOAK_REALM }} --format json"
register: all_scopes