From edf75b49dcaa92e295ef7160c613f7d7f661b61b Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 31 Oct 2026 19:40:58 +0100 Subject: [PATCH] refactor(web-app-keycloak): group the RBAC REST scripts in files/python/rbac Gate pass 15 rejected rbac_reconcile.py and rbac_realm_groups.py as a prefix group. They are now rbac/reconcile.py and rbac/realm_groups.py with mirrored tests; the shared rest_script runner copies a script from its subpath and stages and runs it under its file name next to keycloak_admin.py. Co-Authored-By: Claude Opus 5.5 --- .../python/{rbac_realm_groups.py => rbac/realm_groups.py} | 0 .../files/python/{rbac_reconcile.py => rbac/reconcile.py} | 0 .../filter_plugins/build_realm_rbac_groups.py | 2 +- roles/web-app-keycloak/filter_plugins/rbac_per_app.py | 2 +- roles/web-app-keycloak/tasks/update/03_per_app_mappers.yml | 2 +- roles/web-app-keycloak/tasks/update/06_rbac_groups_realm.yml | 2 +- roles/web-app-keycloak/tasks/utils/rest_script.yml | 4 ++-- .../python/roles/web-app-keycloak/files/rbac/__init__.py | 5 +++++ .../{test_rbac_realm_groups.py => rbac/test_realm_groups.py} | 3 ++- .../files/{test_rbac_reconcile.py => rbac/test_reconcile.py} | 3 ++- 10 files changed, 15 insertions(+), 8 deletions(-) rename roles/web-app-keycloak/files/python/{rbac_realm_groups.py => rbac/realm_groups.py} (100%) rename roles/web-app-keycloak/files/python/{rbac_reconcile.py => rbac/reconcile.py} (100%) create mode 100644 tests/unit/python/roles/web-app-keycloak/files/rbac/__init__.py rename tests/unit/python/roles/web-app-keycloak/files/{test_rbac_realm_groups.py => rbac/test_realm_groups.py} (99%) rename tests/unit/python/roles/web-app-keycloak/files/{test_rbac_reconcile.py => rbac/test_reconcile.py} (98%) diff --git a/roles/web-app-keycloak/files/python/rbac_realm_groups.py b/roles/web-app-keycloak/files/python/rbac/realm_groups.py similarity index 100% rename from roles/web-app-keycloak/files/python/rbac_realm_groups.py rename to roles/web-app-keycloak/files/python/rbac/realm_groups.py diff --git a/roles/web-app-keycloak/files/python/rbac_reconcile.py b/roles/web-app-keycloak/files/python/rbac/reconcile.py similarity index 100% rename from roles/web-app-keycloak/files/python/rbac_reconcile.py rename to roles/web-app-keycloak/files/python/rbac/reconcile.py diff --git a/roles/web-app-keycloak/filter_plugins/build_realm_rbac_groups.py b/roles/web-app-keycloak/filter_plugins/build_realm_rbac_groups.py index f3b46e4f8f..44409a137d 100644 --- a/roles/web-app-keycloak/filter_plugins/build_realm_rbac_groups.py +++ b/roles/web-app-keycloak/filter_plugins/build_realm_rbac_groups.py @@ -11,7 +11,7 @@ Membership uses the same rule as the LDAP path: a user joins a role group iff that role name appears in the user's ``roles`` list. Members are restricted to the users update/06_users/realm.yml actually seeds (non-reserved, non-empty password) and are emitted by their ``username`` attribute (dict key fallback) so the realm -lookup in rbac_realm_groups.py matches the created accounts. The +lookup in rbac/realm_groups.py matches the created accounts. The implicit ``administrator`` role is auto-added for every RBAC application, matching the role-list contract. diff --git a/roles/web-app-keycloak/filter_plugins/rbac_per_app.py b/roles/web-app-keycloak/filter_plugins/rbac_per_app.py index b7524e2c29..0011674ea9 100644 --- a/roles/web-app-keycloak/filter_plugins/rbac_per_app.py +++ b/roles/web-app-keycloak/filter_plugins/rbac_per_app.py @@ -8,7 +8,7 @@ per-app `group-ldap-mapper` setup: These names are referenced from `vars/main.yml` (`KEYCLOAK_PER_APP_LDAP_ROLES_MAPPER_PAYLOADS`), which hands them to the -`files/python/rbac_reconcile.py` REST client. Centralising them here means a future +`files/python/rbac/reconcile.py` REST client. Centralising them here means a future rename of the mapper convention is a one-line change in this plugin and stays consistent everywhere. """ diff --git a/roles/web-app-keycloak/tasks/update/03_per_app_mappers.yml b/roles/web-app-keycloak/tasks/update/03_per_app_mappers.yml index a01d3fe6cf..41e3457881 100644 --- a/roles/web-app-keycloak/tasks/update/03_per_app_mappers.yml +++ b/roles/web-app-keycloak/tasks/update/03_per_app_mappers.yml @@ -49,7 +49,7 @@ apply: delegate_to: "{{ KEYCLOAK_HOST_NODE }}" vars: - rest_script: rbac_reconcile.py + rest_script: rbac/reconcile.py rest_script_env: LDAP_COMPONENT_ID: "{{ ldap_cmp_id | trim }}" RBAC_ROOT_PATH: "/{{ KEYCLOAK_RBAC_GROUP_NAME | trim('/') }}" diff --git a/roles/web-app-keycloak/tasks/update/06_rbac_groups_realm.yml b/roles/web-app-keycloak/tasks/update/06_rbac_groups_realm.yml index b13ea1823f..ff7e5cd3a9 100644 --- a/roles/web-app-keycloak/tasks/update/06_rbac_groups_realm.yml +++ b/roles/web-app-keycloak/tasks/update/06_rbac_groups_realm.yml @@ -11,7 +11,7 @@ apply: delegate_to: "{{ KEYCLOAK_HOST_NODE }}" vars: - rest_script: rbac_realm_groups.py + rest_script: rbac/realm_groups.py rest_script_env: {} rest_script_stdin: >- {{ diff --git a/roles/web-app-keycloak/tasks/utils/rest_script.yml b/roles/web-app-keycloak/tasks/utils/rest_script.yml index 2f3099f5d4..5923bb62b5 100644 --- a/roles/web-app-keycloak/tasks/utils/rest_script.yml +++ b/roles/web-app-keycloak/tasks/utils/rest_script.yml @@ -30,7 +30,7 @@ - name: "📄 Keycloak REST | Stage {{ rest_script }} with its client module" ansible.builtin.copy: src: "python/{{ kc_rest_script_file }}" - dest: "{{ kc_rest_script_dir.path }}/{{ kc_rest_script_file }}" + dest: "{{ kc_rest_script_dir.path }}/{{ kc_rest_script_file | basename }}" mode: "0600" loop: - keycloak_admin.py @@ -48,7 +48,7 @@ - --net - -- - python3 - - "{{ kc_rest_script_dir.path }}/{{ rest_script }}" + - "{{ kc_rest_script_dir.path }}/{{ rest_script | basename }}" stdin: "{{ rest_script_stdin }}" environment: "{{ rest_script_env | combine(kc_rest_script_base_env) }}" vars: diff --git a/tests/unit/python/roles/web-app-keycloak/files/rbac/__init__.py b/tests/unit/python/roles/web-app-keycloak/files/rbac/__init__.py new file mode 100644 index 0000000000..6290ad0f99 --- /dev/null +++ b/tests/unit/python/roles/web-app-keycloak/files/rbac/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +PROJECT_ROOT: Path = Path(__file__).resolve().parents[7] + +__all__ = ["PROJECT_ROOT"] diff --git a/tests/unit/python/roles/web-app-keycloak/files/test_rbac_realm_groups.py b/tests/unit/python/roles/web-app-keycloak/files/rbac/test_realm_groups.py similarity index 99% rename from tests/unit/python/roles/web-app-keycloak/files/test_rbac_realm_groups.py rename to tests/unit/python/roles/web-app-keycloak/files/rbac/test_realm_groups.py index d016a63b05..c78c2349f2 100644 --- a/tests/unit/python/roles/web-app-keycloak/files/test_rbac_realm_groups.py +++ b/tests/unit/python/roles/web-app-keycloak/files/rbac/test_realm_groups.py @@ -11,8 +11,9 @@ from collections import Counter from . import PROJECT_ROOT sys.path.insert(0, str(PROJECT_ROOT / "roles/web-app-keycloak/files/python")) +sys.path.insert(0, str(PROJECT_ROOT / "roles/web-app-keycloak/files/python/rbac")) -import rbac_realm_groups as m +import realm_groups as m class FakeKeycloak: diff --git a/tests/unit/python/roles/web-app-keycloak/files/test_rbac_reconcile.py b/tests/unit/python/roles/web-app-keycloak/files/rbac/test_reconcile.py similarity index 98% rename from tests/unit/python/roles/web-app-keycloak/files/test_rbac_reconcile.py rename to tests/unit/python/roles/web-app-keycloak/files/rbac/test_reconcile.py index d6b4545fbe..971bed7278 100644 --- a/tests/unit/python/roles/web-app-keycloak/files/test_rbac_reconcile.py +++ b/tests/unit/python/roles/web-app-keycloak/files/rbac/test_reconcile.py @@ -8,8 +8,9 @@ from pathlib import Path from . import PROJECT_ROOT sys.path.insert(0, str(PROJECT_ROOT / "roles/web-app-keycloak/files/python")) +sys.path.insert(0, str(PROJECT_ROOT / "roles/web-app-keycloak/files/python/rbac")) -import rbac_reconcile as m +import reconcile as m LDAP = "ldap-1"