Repository navigation
refactor: adding a roles service for v2 - #55049
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The unpaginated by-name endpoint now uses a search overload that adds a database query containing all composite IDs on every request, even when the role cache is warm.
1 open issue
What changed in this PR
Extracts shared role-management logic from the v1 REST resources so the v2 client API can manage roles without depending on v1 resources, supporting #47631.
Changes:
- Introduces a shared
RoleServicefor role operations and permission checks. - Migrates v1 resources and v2 client role reconciliation to the service, keeping admin-event handling with callers.
- Adds regression coverage for role errors, composite permissions, and v2 client events.
| File | Description |
|---|---|
tests/base/src/test/java/org/keycloak/tests/admin/realm/RealmRolesCRUDTest.java |
Tests duplicate names, rename conflicts, and unknown composites. |
tests/base/src/test/java/org/keycloak/tests/admin/authz/fgap/RoleResourceTypeEvaluationTest.java |
Tests composite-mapping permissions during role creation. |
services/src/main/java/org/keycloak/services/ServiceException.java |
Adds shared service errors with suggested HTTP statuses. |
services/src/main/java/org/keycloak/services/roles/RoleService.java |
Centralizes role operations and authorization checks. |
services/src/main/java/org/keycloak/services/resources/admin/RoleResource.java |
Adds shared helpers and v1 error translation. |
services/src/main/java/org/keycloak/services/resources/admin/RoleContainerResource.java |
Delegates name-based role operations to the service. |
services/src/main/java/org/keycloak/services/resources/admin/RoleByIdResource.java |
Delegates ID-based role operations to the service. |
rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/AdminEventV2Test.java |
Verifies role changes emit only a client event. |
rest/admin-v2/services/src/main/java/org/keycloak/services/RolesService.java |
Removes the wrapper around v1 role resources. |
rest/admin-v2/services/src/main/java/org/keycloak/services/client/scim/ClientResourceTypeProvider.java |
Uses the shared service for declarative client roles. |
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
When search is omitted, the roles-by-ID endpoint paginates an unordered HashSet stream, producing arbitrary pages instead of the name-ordered results expected by existing tests.
1 open issue
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
A whitespace-only search now returns all composites when pagination is omitted but remains filtered when pagination is supplied, so the cache optimization changes existing search semantics.
1 open issue
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
A nonempty composite-role search without pagination incorrectly returns all visible composites instead of matching roles, breaking the existing search assertion.
1 open issue
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
Other than the small thing below, it looks fine. Thanks, Steve!
There was a problem hiding this comment.
Would be good to document eventing now works differently for v2, i.e. no role events are fired.
| protected void deleteRole(RoleModel role) { | ||
| if (!role.getContainer().removeRole(role)) { | ||
| throw new NotFoundException("Role not found"); | ||
| } | ||
| } |
There was a problem hiding this comment.
Just a nit. This doesn't seem to be used anymore?
There was a problem hiding this comment.
The method is still there.
There was a problem hiding this comment.
Sorry I should have said the the RealmRolesPartialImport.RoleHelper constructor was removed after looking at this.
Unless there is further refactoring, deleteRole is still used via the RoleHelper.
There was a problem hiding this comment.
🟡 Changes recommended
Role additions and removals lose audit events by default because per-role events are suppressed while replacement CLIENT events remain gated off, and the new test masks this regression by explicitly enabling the gate.
2 open issues
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Role additions and removals lose admin audit events under the default configuration because replacement client events remain disabled, and the test masks this regression by explicitly enabling them.
2 open issues
Review effort: Balanced
| Stream<RoleModel> composites = (StringUtil.isNullOrEmpty(search) && !skip && !limit | ||
| ? role.getCompositesStream() | ||
| : role.getCompositesStream(search, null, null)) |
There was a problem hiding this comment.
One thing I did not notice before but is probably ok...
Before, we always called getCompositesStream(search, ...) which resulted in a JPA call that returned sorted roles. Now, we allow calling getCompositesStream() that doesn't guarantee order. But neither do we on the endpoint, so should be fine...
There was a problem hiding this comment.
That is correct - I see it as preferable to use cache. It would be better still if the cache entry were ordered, but I looked into that change and it was too much for this PR.
There was a problem hiding this comment.
🔵 Needs a closer look
Role additions and removals lose admin audit events under default settings because replacement client events require an opt-in property that the test explicitly enables, masking the regression.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
closes: keycloak#47631 Signed-off-by: Steve Hawkins <shawkins@redhat.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Switching to the event-free RoleService removes role audit events while replacement client events remain disabled by default, leaving role additions and removals unaudited in normal configurations.
No open issues 🎉
Review effort: Balanced
Closes keycloak#55698 Signed-off-by: Steve Hawkins <shawkins@redhat.com>


closes: #47631
Adds a common role service. Since the role classes hold very little state, this is just directly passing the values to the methods. If we want a better way to transfer state, then we could consider adding directly creatable versions of the model classes (like we are already doing for client models) or a service specific representation.