Repository navigation
Conversation
…s disabled Organizations.isReadOnlyOrganizationMember (and its copy in UserStorageManager) runs for every user lookup, including user listing, organization member listing, token and refresh requests. It walked all organizations of the user and evaluated the managed membership check, a database round trip, before the cheap enabled check, so unmanaged members of many organizations paid one query per organization on every request. - Add OrganizationProvider#hasDisabledOrganizations, backed by a single query in JPA and cached in the realm cache by the Infinispan provider. The cached entry is invalidated when an organization is created, updated or removed. - Return early when organizations are enabled and none is disabled, as no member can be read-only in that case. - Otherwise check the organization's enabled state before the managed membership check. Closes keycloak#46681 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Vivek Shanmughan <6814487+vivekksk@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
A critical cache race can expose an uncommitted organization enablement to concurrent requests, incorrectly treating managed members as enabled even if the transaction rolls back.
1 open issue
What changed in this PR
Addresses #46681 by skipping per-user organization membership checks when organizations are enabled realm-wide and none is disabled.
Changes:
- Adds a disabled-organization existence check with JPA and cached implementations.
- Updates both read-only member checks to short-circuit and evaluate enabled state before managed membership.
- Adds tests for cache invalidation and managed-user enabled-state transitions.
| File | Description |
|---|---|
tests/base/src/test/java/org/keycloak/tests/organization/member/OrganizationMemberTest.java |
Tests enabled-state transitions and mixed managed/unmanaged memberships. |
tests/base/src/test/java/org/keycloak/tests/organization/cache/OrganizationCacheTest.java |
Tests disabled-organization detection and cache invalidation. |
services/src/main/java/org/keycloak/organization/utils/Organizations.java |
Adds the fast path to shared read-only member detection. |
server-spi/src/main/java/org/keycloak/organization/OrganizationProvider.java |
Adds a default disabled-organization existence check. |
model/storage-private/src/main/java/org/keycloak/storage/UserStorageManager.java |
Applies the same optimization to storage-level user lookups. |
model/jpa/src/main/java/org/keycloak/organization/jpa/JpaOrganizationProvider.java |
Implements a result-limited existence query. |
model/jpa/src/main/java/org/keycloak/models/jpa/entities/OrganizationEntity.java |
Defines the disabled-organization named query. |
model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/organization/InfinispanOrganizationProvider.java |
Caches the realm-level result and registers invalidation on organization changes. |
Review effort: Balanced
If an organization was created, updated or removed in the current transaction, the realm cache invalidation is only applied on transaction completion. Loading the flag on a cold cache in that transaction published a value based on uncommitted changes to other transactions until then. Read it from the database without caching in that case, as getByAlias does. Closes keycloak#46681 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Vivek Shanmughan <6814487+vivekksk@users.noreply.github.com>
|
Tested this PR applied on 26.7.5, on a local node with Postgres 16. One user is an unmanaged member of 1,111 organizations, and later of 3,900. Results with warm caches: Adding a member: 11 ms. Removing one: 40 ms. One row of the member listing: 6 ms. Code-to-token and refresh: about 20 ms. Adding 1,680 memberships: 4.4 s in total. All of these were in the seconds before the fix. One path is still O(N): a login without an organization scope (password grant, login form, SSO authorize). It goes through OrganizationMemberValidator → Organizations.resolveOrganization, whose isManaged loop runs two queries per organization. For the user in 3,900 organizations the password grant takes 16.6 s and the login form POST 51 s. I opened #55257 for it, with a proposed fix and measurements, so this PR can stay as it is. |
Closes #46681
Summary
isReadOnlyOrganizationMemberruns on every user lookup: user cache load and validation,GET /users, organization member listing, and the authorize, token and refresh requests. For each organization the user belongs to, it ran the managed-membership check, a database round trip, before the cheapisEnabled()check. An unmanaged member of N organizations therefore paid N queries on every request, even when every organization was enabled.hasDisabledOrganizations()follows the existinghasOrganizations()pattern:Evidence
Tests added in
tests/base:OrganizationCacheTest#testHasDisabledOrganizations(@DatabaseTest): checks the JPA and cached answers as an organization is disabled, re-enabled, created disabled and removed, and that the cached entry is invalidated after each change.OrganizationMemberTest#testManagedMemberEnabledStateFollowsOrganizationEnabledState: a managed member is reported disabled, enabled and disabled again as the organization is toggled. Each step is checked through organization members, user search and get-by-id, so stale state would be caught.OrganizationMemberTest#testManagedMemberEnabledWhenAnotherOrganizationIsDisabled: a managed member of an enabled organization who is also an unmanaged member of a disabled organization stays enabled. This covers the reordered predicate.Before: the issue reports O(N) queries joining
KEYCLOAK_GROUP.TYPE=1per listed user. A later comment on 26.7.5 reports about 3.3 s per authorize, token and refresh request for a user in about 1,100 organizations.After: the new tests and the existing
testGetAllDisabledOrganization,testGetAllDisabledOrganizationProviderandtestMembersCountpass locally. The fullorg.keycloak.tests.organizationpackage also passes locally (distribution server, H2, JDK 21): 498 tests in 32 classes, 0 failures, 0 errors, 0 skipped.Benchmark against the scenario from the issue (1,101 organizations, unmanaged members, Postgres): for a member of all organizations, adding or removing a member drops from about 10 s to under 50 ms, one member-listing row from about 4.5 s to 6 ms, and authorize, code-to-token and refresh from about 1.1–1.3 s to 9 ms each. Results are similar when one unrelated organization is disabled. Full setup and tables: #46681 (comment)
Merge Danger
Door: two-way
No schema change and no data migration. The new SPI method has a default implementation, so custom
OrganizationProviderimplementations keep compiling and working.Blast Radius: read-only member detection
If the new cache entry were ever stale, managed members of a disabled organization could appear enabled, or the reverse, until the realm cache entry is invalidated. The entry is invalidated in the same places as the existing organization entries (
registerOrganizationInvalidation,registerCountInvalidation), and the tests cover each transition.🤖 Generated with Claude Code