Search in sources :

Example 11 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class SyncDummyFederationProviderCommand method doRunCommand.

@Override
protected void doRunCommand(KeycloakSession session) {
    int waitTime = getIntArg(0);
    int changedSyncPeriod = getIntArg(1);
    RealmModel realm = session.realms().getRealmByName("master");
    UserStorageProviderModel fedProviderModel = KeycloakModelUtils.findUserStorageProviderByName("cluster-dummy", realm);
    if (fedProviderModel == null) {
        MultivaluedHashMap<String, String> cfg = fedProviderModel.getConfig();
        updateConfig(cfg, waitTime);
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setProviderId("sync-dummy");
        model.setPriority(1);
        model.setName("cluster-dummy");
        model.setFullSyncPeriod(-1);
        model.setChangedSyncPeriod(changedSyncPeriod);
        model.setLastSync(-1);
        fedProviderModel = new UserStorageProviderModel(realm.addComponentModel(model));
    } else {
        MultivaluedHashMap<String, String> cfg = fedProviderModel.getConfig();
        updateConfig(cfg, waitTime);
        fedProviderModel.setChangedSyncPeriod(changedSyncPeriod);
        realm.updateComponent(fedProviderModel);
    }
    new UserStorageSyncManager().notifyToRefreshPeriodicSync(session, realm, fedProviderModel, false);
    log.infof("User federation provider created and sync was started", waitTime);
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 12 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class SyncFederationTest method test03ConcurrentSync.

@Test
public void test03ConcurrentSync() throws Exception {
    // Enable timer for SyncDummyUserFederationProvider
    testingClient.server().run(session -> {
        SyncDummyUserFederationProviderFactory.restartLatches();
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setProviderId(SyncDummyUserFederationProviderFactory.SYNC_PROVIDER_ID);
        model.setPriority(1);
        model.setName("test-sync-dummy");
        model.setFullSyncPeriod(-1);
        model.setChangedSyncPeriod(1);
        model.setLastSync(0);
        model.getConfig().putSingle(SyncDummyUserFederationProviderFactory.WAIT_TIME, "2000");
        ComponentModel dummyModel = new UserStorageProviderModel(appRealm.addComponentModel(model));
    });
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        // bootstrap periodic sync
        UserStorageSyncManager usersSyncManager = new UserStorageSyncManager();
        usersSyncManager.bootstrapPeriodic(sessionFactory, session.getProvider(TimerProvider.class));
        // Wait and then trigger sync manually. Assert it will be ignored
        sleep(1800);
        SynchronizationResult syncResult = usersSyncManager.syncChangedUsers(sessionFactory, appRealm.getId(), dummyModel);
        Assert.assertTrue(syncResult.isIgnored());
        // Cancel timer
        usersSyncManager.notifyToRefreshPeriodicSync(session, appRealm, dummyModel, true);
        // Signal to factory to finish waiting
        SyncDummyUserFederationProviderFactory.latch1.countDown();
        try {
            SyncDummyUserFederationProviderFactory.latch2.await(20000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    // remove provider
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        appRealm.removeComponent(dummyModel);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentModel(org.keycloak.component.ComponentModel) TimerProvider(org.keycloak.timer.TimerProvider) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) Test(org.junit.Test) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest)

Example 13 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class LDAPSyncTest method test05MissingLDAPUsernameSync.

// KEYCLOAK-1728
@Test
public void test05MissingLDAPUsernameSync() {
    String origUsernameAttrName = testingClient.server().fetch(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        // Remove all users from model
        session.userLocalStorage().getUsersStream(ctx.getRealm(), true).peek(user -> System.out.println("trying to delete user: " + user.getUsername())).collect(Collectors.toList()).forEach(user -> {
            UserCache userCache = session.userCache();
            if (userCache != null) {
                userCache.evict(ctx.getRealm(), user);
            }
            session.userLocalStorage().removeUser(ctx.getRealm(), user);
        });
        // Add street mapper and add some user including street
        ComponentModel streetMapper = LDAPTestUtils.addUserAttributeMapper(ctx.getRealm(), ctx.getLdapModel(), "streetMapper", "street", LDAPConstants.STREET);
        LDAPObject streetUser = LDAPTestUtils.addLDAPUser(ctx.getLdapProvider(), ctx.getRealm(), "user8", "User8FN", "User8LN", "user8@email.org", "user8street", "126");
        // Change name of username attribute name to street
        String origUsernameAttrNamee = ctx.getLdapModel().get(LDAPConstants.USERNAME_LDAP_ATTRIBUTE);
        ctx.getLdapModel().getConfig().putSingle(LDAPConstants.USERNAME_LDAP_ATTRIBUTE, "street");
        // Need to change this due to ApacheDS pagination bug (For other LDAP servers, pagination works fine) TODO: Remove once ApacheDS upgraded and pagination is fixed
        ctx.getLdapModel().put(LDAPConstants.BATCH_SIZE_FOR_SYNC, "10");
        ctx.getRealm().updateComponent(ctx.getLdapModel());
        return origUsernameAttrNamee;
    }, String.class);
    // Just user8 synced. All others failed to sync
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        SynchronizationResult syncResult = new UserStorageSyncManager().syncAllUsers(sessionFactory, "test", ctx.getLdapModel());
        Assert.assertEquals(1, syncResult.getAdded());
        Assert.assertTrue(syncResult.getFailed() > 0);
    });
    // Revert config changes
    ComponentRepresentation ldapRep = testRealm().components().component(ldapModelId).toRepresentation();
    if (origUsernameAttrName == null) {
        ldapRep.getConfig().remove(LDAPConstants.USERNAME_LDAP_ATTRIBUTE);
    } else {
        ldapRep.getConfig().putSingle(LDAPConstants.USERNAME_LDAP_ATTRIBUTE, origUsernameAttrName);
    }
    testRealm().components().component(ldapModelId).update(ldapRep);
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        // Revert config changes
        ComponentModel streetMapper = LDAPTestUtils.getSubcomponentByName(ctx.getRealm(), ctx.getLdapModel(), "streetMapper");
        ctx.getRealm().removeComponent(streetMapper);
    });
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentModel(org.keycloak.component.ComponentModel) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) UserCache(org.keycloak.models.cache.UserCache) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) Test(org.junit.Test)

Example 14 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class UserStorageManager method onCreate.

@Override
public void onCreate(KeycloakSession session, RealmModel realm, ComponentModel model) {
    ComponentFactory factory = ComponentUtil.getComponentFactory(session, model);
    if (!(factory instanceof UserStorageProviderFactory))
        return;
    new UserStorageSyncManager().notifyToRefreshPeriodicSync(session, realm, new UserStorageProviderModel(model), false);
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentFactory(org.keycloak.component.ComponentFactory)

Example 15 with UserStorageSyncManager

use of org.keycloak.services.managers.UserStorageSyncManager in project keycloak by keycloak.

the class UserStorageManager method preRemove.

@Override
public void preRemove(RealmModel realm, ComponentModel component) {
    if (component.getProviderType().equals(ClientStorageProvider.class.getName())) {
        localStorage().preRemove(realm, component);
        if (getFederatedStorage() != null)
            getFederatedStorage().preRemove(realm, component);
        return;
    }
    if (!component.getProviderType().equals(UserStorageProvider.class.getName()))
        return;
    localStorage().preRemove(realm, component);
    if (getFederatedStorage() != null)
        getFederatedStorage().preRemove(realm, component);
    new UserStorageSyncManager().notifyToRefreshPeriodicSync(session, realm, new UserStorageProviderModel(component), true);
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ClientStorageProvider(org.keycloak.storage.client.ClientStorageProvider)

Aggregations

UserStorageSyncManager (org.keycloak.services.managers.UserStorageSyncManager)18 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)12 Test (org.junit.Test)11 RealmModel (org.keycloak.models.RealmModel)10 ComponentModel (org.keycloak.component.ComponentModel)8 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)8 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)7 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)6 HashMap (java.util.HashMap)4 UserModel (org.keycloak.models.UserModel)4 UserProvider (org.keycloak.models.UserProvider)4 BadRequestException (javax.ws.rs.BadRequestException)3 UserCache (org.keycloak.models.cache.UserCache)3 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)3 UserStorageProvider (org.keycloak.storage.UserStorageProvider)3 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)3 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)3 NotFoundException (javax.ws.rs.NotFoundException)2 Matchers (org.hamcrest.Matchers)2 ComponentFactory (org.keycloak.component.ComponentFactory)2