Search in sources :

Example 1 with UserStorageProvider

use of org.keycloak.storage.UserStorageProvider in project keycloak by keycloak.

the class UserStorageProvidersTestUtils method getStorageProvider.

public static UserStorageProvider getStorageProvider(KeycloakSession session, RealmModel realm, String componentId) {
    ComponentModel model = realm.getComponent(componentId);
    if (model == null)
        return null;
    UserStorageProviderModel storageModel = new UserStorageProviderModel(model);
    UserStorageProviderFactory factory = (UserStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, model.getProviderId());
    if (factory == null) {
        throw new ModelException("Could not find UserStorageProviderFactory for: " + model.getProviderId());
    }
    return getStorageProviderInstance(session, storageModel, factory);
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ModelException(org.keycloak.models.ModelException) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 2 with UserStorageProvider

use of org.keycloak.storage.UserStorageProvider in project keycloak by keycloak.

the class UserModelTest method testAddDirtyRemoveFederationUsersInTheSameGroupConcurrent.

@Test
@RequireProvider(UserStorageProvider.class)
public void testAddDirtyRemoveFederationUsersInTheSameGroupConcurrent() {
    final ConcurrentSkipListSet<String> userIds = new ConcurrentSkipListSet<>();
    String groupId = groupIds.get(0);
    registerUserFederationWithRealm();
    // Create users and let them join first group
    IntStream.range(0, 100).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
        final RealmModel realm = session.realms().getRealm(realmId);
        final UserModel user = session.users().addUser(realm, "user-" + i);
        user.joinGroup(session.groups().getGroupById(realm, groupId));
        log.infof("Created user with id: %s", user.getId());
        userIds.add(user.getId());
        return null;
    }));
    // Remove users _from the federation_, simulates eg. user being removed from LDAP without Keycloak knowing
    withRealm(realmId, (session, realm) -> {
        UserStorageProvider instance = getUserFederationInstance(session, realm);
        log.debugf("Removing selected users from backend");
        IntStream.range(FIRST_DELETED_USER_INDEX, LAST_DELETED_USER_INDEX).forEach(j -> {
            final UserModel user = session.users().getUserByUsername(realm, "user-" + j);
            ((UserRegistrationProvider) instance).removeUser(realm, user);
        });
        return null;
    });
    IntStream.range(0, 7).parallel().forEach(index -> withRealm(realmId, (session, realm) -> {
        final GroupModel group = session.groups().getGroupById(realm, groupId);
        assertThat(session.users().getGroupMembersStream(realm, group).count(), is(100L - DELETED_USER_COUNT));
        return null;
    }));
    inComittedTransaction(session -> {
        // the cache manually.
        if (session.userCache() != null) {
            session.userCache().clear();
        }
        return null;
    });
    // Now delete the users, and count those that were not found to be deleted. This should be equal to the number
    // of users removed directly in the user federation.
    // Some of the transactions may fail due to conflicts as there are many parallel request, so repeat until all users are removed
    AtomicInteger notFoundUsers = new AtomicInteger();
    Set<String> remainingUserIds = new HashSet<>();
    do {
        userIds.stream().parallel().forEach(index -> inComittedTransaction(index, (session, userId) -> {
            final RealmModel realm = session.realms().getRealm(realmId);
            final UserModel user = session.users().getUserById(realm, userId);
            if (user != null) {
                log.debugf("Deleting user: %s", userId);
                session.users().removeUser(realm, user);
            } else {
                log.debugf("Failed deleting user: %s", userId);
                notFoundUsers.incrementAndGet();
            }
            return null;
        }, null, (session, userId) -> {
            log.debugf("Could not delete user %s", userId);
            remainingUserIds.add(userId);
        }));
        userIds.clear();
        userIds.addAll(remainingUserIds);
        remainingUserIds.clear();
    } while (!userIds.isEmpty());
    assertThat(notFoundUsers.get(), is(DELETED_USER_COUNT));
    withRealm(realmId, (session, realm) -> {
        final GroupModel group = session.groups().getGroupById(realm, groupId);
        assertThat(session.users().getGroupMembersStream(realm, group).collect(Collectors.toList()), Matchers.empty());
        return null;
    });
}
Also used : IntStream(java.util.stream.IntStream) Assume.assumeThat(org.junit.Assume.assumeThat) Constants(org.keycloak.models.Constants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UserModel(org.keycloak.models.UserModel) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) RealmProvider(org.keycloak.models.RealmProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComponentModel(org.keycloak.component.ComponentModel) Matchers.hasSize(org.hamcrest.Matchers.hasSize) GroupModel(org.keycloak.models.GroupModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) KeycloakSession(org.keycloak.models.KeycloakSession) Set(java.util.Set) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.is(org.hamcrest.Matchers.is) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GroupModel(org.keycloak.models.GroupModel) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with UserStorageProvider

use of org.keycloak.storage.UserStorageProvider in project keycloak by keycloak.

the class UserModelTest method getUserFederationInstance.

private UserStorageProvider getUserFederationInstance(KeycloakSession session, final RealmModel realm) throws RuntimeException {
    UserStorageProvider instance = (UserStorageProvider) session.getAttribute(userFederationId);
    if (instance == null) {
        ComponentModel model = realm.getComponent(userFederationId);
        UserStorageProviderFactory factory = (UserStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, model.getProviderId());
        instance = factory.create(session, model);
        if (instance == null) {
            throw new RuntimeException("UserStorageProvideFactory (of type " + factory.getClass().getName() + ") produced a null instance");
        }
        session.enlistForClose(instance);
        session.setAttribute(userFederationId, instance);
    }
    return instance;
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ComponentModel(org.keycloak.component.ComponentModel)

Example 4 with UserStorageProvider

use of org.keycloak.storage.UserStorageProvider in project keycloak by keycloak.

the class UserStorageProvidersTestUtils method getStorageProviderInstance.

public static UserStorageProvider getStorageProviderInstance(KeycloakSession session, UserStorageProviderModel model, UserStorageProviderFactory factory) {
    UserStorageProvider instance = (UserStorageProvider) session.getAttribute(model.getId());
    if (instance != null)
        return instance;
    instance = factory.create(session, model);
    if (instance == null) {
        throw new IllegalStateException("UserStorageProvideFactory (of type " + factory.getClass().getName() + ") produced a null instance");
    }
    session.enlistForClose(instance);
    session.setAttribute(model.getId(), instance);
    return instance;
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider)

Example 5 with UserStorageProvider

use of org.keycloak.storage.UserStorageProvider in project keycloak by keycloak.

the class UserModelTest method testAddDirtyRemoveFederationUser.

@Test
@RequireProvider(UserStorageProvider.class)
public void testAddDirtyRemoveFederationUser() {
    registerUserFederationWithRealm();
    withRealm(realmId, (session, realm) -> session.users().addUser(realm, "user-A"));
    // Remove user _from the federation_, simulates eg. user being removed from LDAP without Keycloak knowing
    withRealm(realmId, (session, realm) -> {
        final UserStorageProvider instance = getUserFederationInstance(session, realm);
        log.debugf("Removing selected users from backend");
        final UserModel user = session.users().getUserByUsername(realm, "user-A");
        ((UserRegistrationProvider) instance).removeUser(realm, user);
        return null;
    });
    withRealm(realmId, (session, realm) -> {
        if (session.userCache() != null) {
            session.userCache().clear();
        }
        final UserModel user = session.users().getUserByUsername(realm, "user-A");
        assertThat("User should not be found in the main store", user, Matchers.nullValue());
        return null;
    });
}
Also used : UserModel(org.keycloak.models.UserModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserRegistrationProvider(org.keycloak.storage.user.UserRegistrationProvider) Test(org.junit.Test)

Aggregations

UserStorageProvider (org.keycloak.storage.UserStorageProvider)5 ComponentModel (org.keycloak.component.ComponentModel)3 UserStorageProviderFactory (org.keycloak.storage.UserStorageProviderFactory)3 Test (org.junit.Test)2 UserModel (org.keycloak.models.UserModel)2 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)2 UserRegistrationProvider (org.keycloak.storage.user.UserRegistrationProvider)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers (org.hamcrest.Matchers)1 Matchers.hasItem (org.hamcrest.Matchers.hasItem)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.is (org.hamcrest.Matchers.is)1