forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not automatically re-import users if they already exist locally wh…
…en searching by attributes (keycloak#32887) Closes keycloak#32870 Signed-off-by: Alexander Schwartz <[email protected]> Co-authored-by: Stefan Guilhen <[email protected]>
- Loading branch information
Showing
2 changed files
with
64 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,11 @@ | |
import org.junit.runners.MethodSorters; | ||
import org.keycloak.models.LDAPConstants; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.models.UserProvider; | ||
import org.keycloak.representations.idm.UserRepresentation; | ||
import org.keycloak.storage.DatastoreProvider; | ||
import org.keycloak.storage.datastore.DefaultDatastoreProvider; | ||
import org.keycloak.testsuite.util.LDAPRule; | ||
import org.keycloak.testsuite.util.LDAPTestUtils; | ||
|
||
|
@@ -180,6 +184,48 @@ public void testSearchLDAPLdapEntryDn() { | |
Assert.assertEquals(Set.of("john"), usernames); | ||
} | ||
|
||
@Test | ||
public void testSearchByUserAttributeDoesNotTriggerUserReimport() { | ||
|
||
testingClient.server().run(session -> { | ||
// add a new user for testing that searching by attributes should not cause the user to be re-imported. | ||
LDAPTestContext ctx = LDAPTestContext.init(session); | ||
RealmModel appRealm = ctx.getRealm(); | ||
LDAPTestUtils.addLDAPUser(ctx.getLdapProvider(), appRealm, "bwayne", "Bruce", "Wayne", "[email protected]", "Gotham Avenue", "666"); | ||
}); | ||
|
||
testingClient.server(TEST_REALM_NAME).run(session -> { | ||
// check the user doesn't yet exist in Keycloak | ||
UserProvider localProvider = ((DefaultDatastoreProvider) session.getProvider(DatastoreProvider.class)).userLocalStorage(); | ||
UserModel user = localProvider.getUserByUsername(session.getContext().getRealm(), "bwayne"); | ||
Assert.assertNull(user); | ||
|
||
// import the user by searching for its username, and check it has the timestamp set by one of the LDAP mappers. | ||
user = session.users().getUserByUsername(session.getContext().getRealm(), "bwayne"); | ||
Assert.assertNotNull(user); | ||
Assert.assertNotNull(user.getAttributes().get("createTimestamp")); | ||
|
||
// remove the create timestamp from the user. | ||
user.removeAttribute("createTimestamp"); | ||
user = localProvider.getUserByUsername(session.getContext().getRealm(), "bwayne"); | ||
Assert.assertNull(user.getAttributes().get("createTimestamp")); | ||
}); | ||
|
||
testingClient.server(TEST_REALM_NAME).run(session -> { | ||
// search users by user attribute - the existing user SHOULD NOT be re-imported (GHI #32870) | ||
List<UserModel> users = session.users().searchForUserByUserAttributeStream(session.getContext().getRealm(), "street", "Gotham Avenue").collect(Collectors.toList()); | ||
Assert.assertEquals(1, users.size()); | ||
UserModel user = users.get(0); | ||
// create timestamp won't be null because it is provided directly from the LDAP mapper, so it should still be visible. | ||
Assert.assertNotNull(user.getAttributes().get("createTimestamp")); | ||
|
||
// however, the local stored attribute should not have been updated (i.e. user should not have been fully re-imported). | ||
UserProvider localProvider = ((DefaultDatastoreProvider) session.getProvider(DatastoreProvider.class)).userLocalStorage(); | ||
user = localProvider.getUserByUsername(session.getContext().getRealm(), "bwayne"); | ||
Assert.assertNull(user.getAttributes().get("createTimestamp")); | ||
}); | ||
} | ||
|
||
private void assertLDAPSearchMatchesLocalDB(String searchString) { | ||
//this call should import some users into local database | ||
List<String> importedUsers = adminClient.realm(TEST_REALM_NAME).users().search(searchString, null, null).stream().map(UserRepresentation::getUsername).collect(Collectors.toList()); | ||
|