Repository navigation
Return LDAP users imported by a concurrent request from user search - #55768
ChethankumarGS wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the LDAP search fallback change.
No open issues 🎉
What changed in this PR
Addresses #55749 by recovering LDAP users omitted from search results when another request imports them concurrently.
Changes:
- Adds a fallback username lookup, returning the local user only when its LDAP ID matches.
- Adds four unit tests covering successful imports, concurrent imports, missing users, and mismatched LDAP IDs.
| File | Description |
|---|---|
| federation/ldap/src/test/java/org/keycloak/storage/ldap/LDAPStorageProviderSearchTest.java | Tests search-import fallback behavior. |
| federation/ldap/src/main/java/org/keycloak/storage/ldap/LDAPStorageProvider.java | Recovers matching concurrently imported users during search. |
Review effort: Balanced
There was a problem hiding this comment.
Thanks for picking this up, @ChethankumarGS . I left some detailed comments inline. I built and ran the module locally, and spotless:check is clean.
I also checked the duplicate question, since that is the main risk with a change like this. In UserStorageManager.query the local storage provider is first in the stream and queryProviders consumes providers in order, so local rows are materialized before the LDAP stream starts producing. For your fallback to fire, the concurrent commit has to land after filterLocalUsers, which is after the local query, so the recovered user cannot already be in the result. No duplicates.
Also please squash to a single commit, see CONTRIBUTING.
| * had no local user with that username at that time. If the import still reports an existing user, another | ||
| * request imported the same LDAP user in between and the DB search could not have returned it. Return that | ||
| * user so the search does not drop it. A user that is only known under a different username (for example | ||
| * after a rename in LDAP) is not found by username here and is still skipped, as the DB search returned it. |
There was a problem hiding this comment.
I think the trailing justification is not accurate here. The DB search has not necessarily returned it.
If the LDAP entry was renamed from old to new while the local row still has username old and LDAP_ID=U, a search for "new" does not match the local row on the DB side, the LDAP side matches the entry, filterLocalUsers passes, the import returns null, and the user is silently dropped. That is a pre-existing gap and fine to leave out of scope here, but the comment should describe it as a known separate case rather than assert the behaviour is correct.
The equivalent sentence in the PR description has the same problem.
| } | ||
| String username = LDAPUtils.getUsername(ldapObject, ldapIdentityStore.getConfig()); | ||
| UserModel local = getLocalUserByUsername(realm, username); | ||
| if (local == null || ldapObject.getUuid() == null || !ldapObject.getUuid().equals(local.getFirstAttribute(LDAPConstants.LDAP_ID))) { |
There was a problem hiding this comment.
Nitpick: ldapObject.getUuid() == null cannot be true at this point. importUserFromLDAP has already called LDAPUtils.checkUuid, which throws on a null uuid, so the fallback only runs once the uuid is known to be non-null. Dropping it shortens an already long condition.
| } | ||
|
|
||
| private Predicate<LDAPObject> filterLocalUsers(RealmModel realm) { | ||
| return ldapObject -> UserStoragePrivateUtil.userLocalStorage(session).getUserByUsername(realm, LDAPUtils.getUsername(ldapObject, LDAPStorageProvider.this.ldapIdentityStore.getConfig())) == null; |
There was a problem hiding this comment.
Nit: this is now the second copy of the same lookup. Routing it through getLocalUserByUsername(realm, LDAPUtils.getUsername(ldapObject, ldapIdentityStore.getConfig())) == null would keep the two from drifting, which matters here because the whole fix rests on them agreeing.
| if (local == null || ldapObject.getUuid() == null || !ldapObject.getUuid().equals(local.getFirstAttribute(LDAPConstants.LDAP_ID))) { | ||
| return null; | ||
| } | ||
| return proxy(realm, local, ldapObject, false); |
There was a problem hiding this comment.
Nitpick: a logger.debugf before this return, noting that the entry was recovered because another request imported it, would make this path diagnosable. It is completely invisible otherwise, and it only fires in a case that is hard to reproduce on purpose.
|
|
||
| protected UserModel importUserForSearch(RealmModel realm, LDAPObject ldapObject) { | ||
| UserModel user = importUserFromLDAP(session, realm, ldapObject, ImportType.NOT_FORCED_RETURN_NULL); | ||
| return user != null ? user : findConcurrentlyImportedUser(realm, ldapObject); |
There was a problem hiding this comment.
Just for the record, your reasoning for not reaching for NOT_FORCED_RETURN_EXISTING here is correct: it would convert the rename drop into a duplicate whenever the DB side search did return the user under its old username. Worth moving that paragraph from the PR description into the commit message so it survives in the history.
| /** | ||
| * Tests how a user search treats an LDAP user that another request imported while the search was running. | ||
| */ | ||
| public class LDAPStorageProviderSearchTest { |
There was a problem hiding this comment.
These four tests do not guard the fix. I reverted line 412 back to importUserFromLDAP(session, realm, ldapObject, ImportType.NOT_FORCED_RETURN_NULL), left everything else in place, and all four still passed. They only exercise findConcurrentlyImportedUser in isolation, so the one line that actually fixes the bug has no coverage.
Please add one test that goes through searchForUserStream itself. Two small production changes make that possible:
- route
filterLocalUsersthroughgetLocalUserByUsername(see my other comment in line 1166 of DAPStorageProvider), so a single seam controls both username lookups; - make
searchLDAPprotected, so the subclass can return a fixedLDAPObject.
With those, the race becomes deterministic instead of a matter of timing: have getLocalUserByUsername return null on the first call and the imported user on the second. That is exactly the interleaving the issue describes, the first call being filterLocalUsers and the second your fallback. Keep importUserFromLDAP stubbed to null as you already do, then assert searchForUserStream yields the proxied user.
I tried this on your branch to make sure I was not asking for something that does not work. It passes as written and fails with expected:<1> but was:<0> when line 412 is reverted, which is the guard that is missing today. Add a second case where getLocalUserByUsername returns null both times and assert the result is empty, so the test cannot pass by simply returning everything. Both of those stayed correct for me while the rest of the module stayed green.
Keep the existing four for the LDAP_ID mismatch and no-local-user branches. testImportedUserIsReturnedAsIs becomes largely redundant once the pipeline test exists, since it mostly asserts that your stub was called with the expected ImportType, but no harm in leaving it.
0e57117 to
065b30e
Compare
|
Thanks for the detailed review, @sguilhen. I pushed an update, squashed to one commit.
I ran the |
Unreported flaky test detectedIf the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR. org.keycloak.testsuite.client.FAPI1Test#testFAPIAdvancedClientRegistrationKeycloak CI - FIPS IT (strict) |
searchForUserStream filters out LDAP entries that already have a local user and imports the rest without forcing the import. When another request imported the same user in between, the import returned null and the user was dropped from the result. Look the user up again by username and return it when it is linked to the same LDAP entry and was imported by this provider (matching LDAP_ID and federation link). The search does not use ImportType.NOT_FORCED_RETURN_EXISTING for this. If the DB search already returned a user under its old username, for example after a rename in LDAP, that import type would return the same user a second time as a duplicate. A user whose LDAP entry was renamed while the local user still has the old username is still dropped. That is an existing separate case and is not changed here. The username lookups in the search now go through getLocalUserByUsername and searchLDAP is protected, so a test can control both lookups and check the whole searchForUserStream path. Closes keycloak#55749 Signed-off-by: Chethann <chethankumar0544@gmail.com>
065b30e to
4f5d304
Compare
|
Pushed another update (still one commit) for the Copilot point: it is valid. Another LDAP provider could have imported a user with the same directory-scoped ID, so the fallback now also requires the user's federation link to be this provider before returning it. I added a test for a user of another provider with the same LDAP ID. It fails without the check and passes with it, and the The FIPS IT failure ( |
Yeah, that was a good catch from Copilot |
There was a problem hiding this comment.
Thanks @ChethankumarGS, this addresses all my previous comments, and the squash is in place.
LGTM now

Closes #55749
LDAPStorageProvider#searchForUserStreamdrops LDAP entries that already have a local user, then imports the rest withImportType.NOT_FORCED_RETURN_NULL. If another request imports the same user between those two steps, the import returns null and the user is missing from the search result.This change looks the user up again by username when the import returns null. If the local user is linked to the same LDAP entry (
LDAP_IDmatches) and was imported by this provider (its federation link is this provider), it is returned. If there is no local user, or it is linked to another LDAP entry or another provider, the result is still null as before.Known separate case, not changed here: if an LDAP entry was renamed while the local user still has the old username, the DB search does not match it, the lookup by the new username finds nothing, and the user is still dropped. This was already the case before this change.
To make the whole search path testable, the filter of local users now goes through
getLocalUserByUsernameandsearchLDAPisprotected.Tests:
LDAPStorageProviderSearchTestnow has a test that goes throughsearchForUserStream(first lookup returns null, second returns the imported user) and one where the user stays missing, plus a case for a user of another provider with the sameLDAP_ID. The four earlier cases are kept. The new test fails withexpected:<1> but was:<0>when the fallback is removed. I ran the wholefederation/ldapmodule tests andspotless:check, they pass. I did not run the LDAP integration test suite (ApacheDS based), and there is no test that races two real requests.AI disclosure: I took help from AI.