Fix NPE in LDAPUtils.loadAllLDAPObjects when batch size is set to value <= 0

Signed-off-by: Stefan Guilhen <sguilhen@redhat.com>

Closes #39022
This commit is contained in:
Stefan Guilhen 2025-04-15 18:28:42 -03:00 committed by Pedro Igor
parent 636fffe0bc
commit 9976f9380c
2 changed files with 36 additions and 2 deletions

View File

@ -310,8 +310,8 @@ public class LDAPUtils {
* @return
*/
public static List<LDAPObject> loadAllLDAPObjects(LDAPQuery ldapQuery, LDAPConfig ldapConfig) {
boolean pagination = ldapConfig.isPagination();
if (pagination) {
if (ldapConfig.isPagination() && ldapConfig.getBatchSizeForSync() > 0) {
// For now reuse globally configured batch size in LDAP provider page
int pageSize = ldapConfig.getBatchSizeForSync();

View File

@ -952,5 +952,39 @@ public class LDAPGroupMapperTest extends AbstractLDAPTest {
Assert.assertEquals("localuser", localGroupMembers.get(0));
});
}
@Test
public void test11_fetchUsersGroupsWithPaginationAndInvalidBatchSize() {
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
RealmModel appRealm = ctx.getRealm();
// batch size 0 effectively disables pagination, code must fall back to non-paginated search
ComponentModel mapperModel = LDAPTestUtils.getLdapProviderModel(appRealm);
LDAPTestUtils.updateConfigOptions(mapperModel, LDAPConstants.BATCH_SIZE_FOR_SYNC, "0");
appRealm.updateComponent(mapperModel);
});
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
RealmModel appRealm = ctx.getRealm();
// verify that getting the user's groups still works
UserModel john = session.users().getUserByUsername(appRealm, "johnkeycloak");
Set<GroupModel> johnGroups = john.getGroupsStream().collect(Collectors.toSet());
Assert.assertEquals(4, johnGroups.size());
});
String configuredBatchSize = ldapRule.getConfig().get(LDAPConstants.BATCH_SIZE_FOR_SYNC);
testingClient.server().run(session -> {
LDAPTestContext ctx = LDAPTestContext.init(session);
RealmModel appRealm = ctx.getRealm();
// revert batch size change
ComponentModel mapperModel = LDAPTestUtils.getLdapProviderModel(appRealm);
LDAPTestUtils.updateConfigOptions(mapperModel, LDAPConstants.BATCH_SIZE_FOR_SYNC, configuredBatchSize);
appRealm.updateComponent(mapperModel);
});
}
}