Repository navigation
Conversation
…h expansion removeTransientAdminRoles() called user.hasRole() for every role in the access token. Each user.hasRole() re-walks the whole role graph (direct mappings + every composite parent via KeycloakModelUtils.searchFor), so a token with N roles against a user with a deep role graph costs O(N × M) work on every KeycloakIdentity construction. Under many realms / many client roles per token this saturates the CPU (95%+ of samples in UserAdapter.hasRole / KeycloakModelUtils.searchFor) and makes Admin REST API latency grow 30-40x. Resolve the user's effective role set (direct + composite + group-inherited) exactly once via RoleUtils.getDeepUserMappings() — one batched composite expansion (O(M)) plus O(1) set lookups per role. Behavior is unchanged: getDeepUserRoleMappings() yields the same effective role set that hasRole() tests for. This is the same strategy AdminRoleTokenPostProcessor already uses. Signed-off-by: waterWang <waterWang@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes transient admin-role filtering by replacing repeated role-graph searches with effective-role ID lookups.
Changes:
- Expands effective user roles and filters by role ID.
- Adds client- and realm-role unit tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
KeycloakModelUtils.java |
Optimizes transient admin-role filtering. |
KeycloakModelUtilsTest.java |
Tests client and realm filtering behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1208
to
+1210
| Set<String> effectiveRoleIds = RoleUtils.getDeepUserRoleMappings(user).stream() | ||
| .map(RoleModel::getId) | ||
| .collect(Collectors.toSet()); |
Contributor
|
@waterWang #52042 is closed now since the reporter said the issue is fixed with KC26.7.3. I will go ahead and close this issue, feel free to comment here or reopen if you feel anything is missing, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #52042
Problem
After upgrading Keycloak 26.7.0 → 26.7.2 (CVE-2026-16102 fix, #51499),
Admin REST API latency grew 30-40x on deployments with many realms/users
(e.g.
/admin/realms/{realm}/users17ms → 508ms;/clients12.9ms → 546ms).JFR shows 95%+ of CPU samples in
KeycloakModelUtils.removeTransientAdminRoles→
UserAdapter.hasRole→KeycloakModelUtils.searchFor(HashMap/HashSet frames).Root cause
KeycloakIdentity.addRolesAsAttributes()callsremoveTransientAdminRoles()for the realm access and each resource access. For every role in the token
it calls
user.hasRole(adminRole), and eachhasRole()re-walks the wholerole graph (direct role mappings, every composite parent through
KeycloakModelUtils.searchFor, plus group roles). A token with N roles againsta user with a deep role hierarchy costs O(N × M) work, per request, per node.
Fix
Resolve the user's effective role set (direct + composite + group-inherited
roles) exactly once via
RoleUtils.getDeepUserRoleMappings(user)— a singlebatched composite expansion (O(M)) — then replace each
user.hasRole(adminRole)with an O(1)
Set.contains(adminRole.getId())lookup (O(N)).Behavior is unchanged:
getDeepUserRoleMappings()produces the same effectiverole set that
hasRole()tests against. This is the same strategyAdminRoleTokenPostProcessor.removeTransientAdminRolesalready uses.Test
KeycloakModelUtilsTestgains two tests exercisingremoveTransientAdminRoles:manage-usersonrealm-management; token claimsmanage-users+impersonation+some-role→ onlyimpersonationis strippedadmin; token claimsadmin+create-realm+view-realm→ onlyadminkeptSigned-off-by: waterWang waterWang@users.noreply.github.com