Search in sources :

Example 31 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class LDAPQuery method getResultList.

public List<LDAPObject> getResultList() {
    // Apply mappers now
    LDAPMappersComparator ldapMappersComparator = new LDAPMappersComparator(ldapFedProvider.getLdapIdentityStore().getConfig());
    Collections.sort(mappers, ldapMappersComparator.sortAsc());
    for (ComponentModel mapperModel : mappers) {
        LDAPStorageMapper fedMapper = ldapFedProvider.getMapperManager().getMapper(mapperModel);
        fedMapper.beforeLDAPQuery(this);
    }
    List<LDAPObject> result = new ArrayList<LDAPObject>();
    try {
        for (LDAPObject ldapObject : ldapFedProvider.getLdapIdentityStore().fetchQueryResults(this)) {
            result.add(ldapObject);
        }
    } catch (Exception e) {
        throw new ModelException("LDAP Query failed", e);
    }
    return result;
}
Also used : LDAPStorageMapper(org.keycloak.storage.ldap.mappers.LDAPStorageMapper) LDAPMappersComparator(org.keycloak.storage.ldap.mappers.LDAPMappersComparator) ModelException(org.keycloak.models.ModelException) ComponentModel(org.keycloak.component.ComponentModel) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) NamingException(javax.naming.NamingException) ModelException(org.keycloak.models.ModelException) ModelDuplicateException(org.keycloak.models.ModelDuplicateException)

Example 32 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class MapComponentEntity method toModel.

static ComponentModel toModel(MapComponentEntity entity) {
    ComponentModel model = new ComponentModel();
    model.setId(entity.getId());
    model.setName(entity.getName());
    model.setProviderId(entity.getProviderId());
    model.setProviderType(entity.getProviderType());
    model.setSubType(entity.getSubType());
    model.setParentId(entity.getParentId());
    Map<String, List<String>> config = entity.getConfig();
    model.setConfig(config == null ? null : new MultivaluedHashMap<>(config));
    return model;
}
Also used : MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ComponentModel(org.keycloak.component.ComponentModel) List(java.util.List)

Example 33 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class UserStorageFailureTest method toggleProviderEnabled.

protected void toggleProviderEnabled(final boolean toggle) {
    final String failureProviderId = this.failureProviderId;
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName(AuthRealm.TEST);
        ComponentModel memoryProvider = realm.getComponent(failureProviderId);
        UserStorageProviderModel model = new UserStorageProviderModel(memoryProvider);
        model.setEnabled(toggle);
        realm.updateComponent(model);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 34 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class SyncFederationTest method test01PeriodicSyncOnCreate.

/**
 * Test that period sync is triggered when creating a synchronized User Storage Provider
 */
@Test
public void test01PeriodicSyncOnCreate() {
    final Map<String, Integer> state = testingClient.server().fetch(session -> {
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        DummyUserFederationProviderFactory dummyFedFactory = (DummyUserFederationProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, DummyUserFederationProviderFactory.PROVIDER_NAME);
        int full = dummyFedFactory.getFullSyncCounter();
        int changed = dummyFedFactory.getChangedSyncCounter();
        Map<String, Integer> state1 = new HashMap<>();
        state1.put("full", full);
        state1.put("changed", changed);
        return state1;
    }, Map.class);
    // Enable timer for SyncDummyUserFederationProvider
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setProviderId(DummyUserFederationProviderFactory.PROVIDER_NAME);
        model.setPriority(1);
        model.setName("test-sync-dummy");
        model.setFullSyncPeriod(-1);
        model.setChangedSyncPeriod(1);
        model.setLastSync(0);
        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();
        DummyUserFederationProviderFactory dummyFedFactory = (DummyUserFederationProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, DummyUserFederationProviderFactory.PROVIDER_NAME);
        // Assert that after some period was DummyUserFederationProvider triggered
        UserStorageSyncManager usersSyncManager = new UserStorageSyncManager();
        sleep(1800);
        // Cancel timer
        usersSyncManager.notifyToRefreshPeriodicSync(session, appRealm, dummyModel, true);
        log.infof("Notified sync manager about cancel periodic sync");
        // This sync is here just to ensure that we have lock (doublecheck that periodic sync, which was possibly triggered before canceling timer is finished too)
        while (true) {
            SynchronizationResult result = usersSyncManager.syncChangedUsers(session.getKeycloakSessionFactory(), appRealm.getId(), dummyModel);
            if (result.isIgnored()) {
                log.infof("Still waiting for lock before periodic sync is finished", result.toString());
                sleep(1000);
            } else {
                break;
            }
        }
        int full = state.get("full");
        int changed = state.get("changed");
        // Assert that DummyUserFederationProviderFactory.syncChangedUsers was invoked at least 2 times (once periodically and once for us)
        int newChanged = dummyFedFactory.getChangedSyncCounter();
        Assert.assertEquals(full, dummyFedFactory.getFullSyncCounter());
        Assert.assertTrue("Assertion failed. newChanged=" + newChanged + ", changed=" + changed, newChanged > (changed + 1));
        // Assert that dummy provider won't be invoked anymore
        sleep(1800);
        Assert.assertEquals(full, dummyFedFactory.getFullSyncCounter());
        int newestChanged = dummyFedFactory.getChangedSyncCounter();
        Assert.assertEquals("Assertion failed. newChanged=" + newChanged + ", newestChanged=" + newestChanged, newChanged, newestChanged);
    });
    // remove dummyProvider
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        appRealm.removeComponent(dummyModel);
    });
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) HashMap(java.util.HashMap) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) DummyUserFederationProviderFactory(org.keycloak.testsuite.federation.DummyUserFederationProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) Test(org.junit.Test) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest)

Example 35 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class SyncFederationTest method test02PeriodicSyncOnUpdate.

/**
 * Test that period sync is triggered when updating a synchronized User Storage Provider to have a non-negative sync period
 */
@Test
public void test02PeriodicSyncOnUpdate() {
    final Map<String, Integer> state = testingClient.server().fetch(session -> {
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        DummyUserFederationProviderFactory dummyFedFactory = (DummyUserFederationProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, DummyUserFederationProviderFactory.PROVIDER_NAME);
        int full = dummyFedFactory.getFullSyncCounter();
        int changed = dummyFedFactory.getChangedSyncCounter();
        Map<String, Integer> state1 = new HashMap<>();
        state1.put("full", full);
        state1.put("changed", changed);
        return state1;
    }, Map.class);
    // Configure sync without timer for SyncDummyUserFederationProvider
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setProviderId(DummyUserFederationProviderFactory.PROVIDER_NAME);
        model.setPriority(1);
        model.setName("test-sync-dummy");
        model.setFullSyncPeriod(-1);
        model.setChangedSyncPeriod(-1);
        model.setLastSync(0);
        ComponentModel dummyModel = new UserStorageProviderModel(appRealm.addComponentModel(model));
    });
    testingClient.server().run(session -> {
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        DummyUserFederationProviderFactory dummyFedFactory = (DummyUserFederationProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, DummyUserFederationProviderFactory.PROVIDER_NAME);
        // Assert that after some period was DummyUserFederationProvider triggered
        UserStorageSyncManager usersSyncManager = new UserStorageSyncManager();
        // Assert that dummy provider wasn't invoked anymore
        sleep(1800);
        int full = state.get("full");
        int changed = state.get("changed");
        Assert.assertEquals(full, dummyFedFactory.getFullSyncCounter());
        int newChanged = dummyFedFactory.getChangedSyncCounter();
        Assert.assertEquals("Assertion failed. changed=" + changed + ", newChanged=" + newChanged, changed, newChanged);
    });
    // Re-enable periodic sync for changed users
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        dummyModel.setChangedSyncPeriod(1);
        appRealm.updateComponent(dummyModel);
    });
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        DummyUserFederationProviderFactory dummyFedFactory = (DummyUserFederationProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, DummyUserFederationProviderFactory.PROVIDER_NAME);
        // Assert that after some period was DummyUserFederationProvider triggered
        UserStorageSyncManager usersSyncManager = new UserStorageSyncManager();
        sleep(1800);
        // Cancel timer
        usersSyncManager.notifyToRefreshPeriodicSync(session, appRealm, dummyModel, true);
        log.infof("Notified sync manager about cancel periodic sync");
        // This sync is here just to ensure that we have lock (doublecheck that periodic sync, which was possibly triggered before canceling timer is finished too)
        while (true) {
            SynchronizationResult result = usersSyncManager.syncChangedUsers(session.getKeycloakSessionFactory(), appRealm.getId(), dummyModel);
            if (result.isIgnored()) {
                log.infof("Still waiting for lock before periodic sync is finished", result.toString());
                sleep(1000);
            } else {
                break;
            }
        }
        int full = state.get("full");
        int changed = state.get("changed");
        // Assert that DummyUserFederationProviderFactory.syncChangedUsers was invoked at least 1 time
        int newChanged = dummyFedFactory.getChangedSyncCounter();
        Assert.assertEquals(full, dummyFedFactory.getFullSyncCounter());
        log.info("Asserting. newChanged=" + newChanged + " > changed=" + changed);
        Assert.assertTrue("Assertion failed. newChanged=" + newChanged + ", changed=" + changed, newChanged > (changed + 1));
        // Assert that dummy provider won't be invoked anymore
        sleep(1800);
        Assert.assertEquals(full, dummyFedFactory.getFullSyncCounter());
        int newestChanged = dummyFedFactory.getChangedSyncCounter();
        Assert.assertEquals("Assertion failed. newChanged=" + newChanged + ", newestChanged=" + newestChanged, newChanged, newestChanged);
    });
    // remove dummyProvider
    testingClient.server().run(session -> {
        RealmModel appRealm = session.realms().getRealmByName(AuthRealm.TEST);
        UserStorageProviderModel dummyModel = findDummyProviderModel(appRealm);
        appRealm.removeComponent(dummyModel);
    });
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) HashMap(java.util.HashMap) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) DummyUserFederationProviderFactory(org.keycloak.testsuite.federation.DummyUserFederationProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) Test(org.junit.Test) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest)

Aggregations

ComponentModel (org.keycloak.component.ComponentModel)155 RealmModel (org.keycloak.models.RealmModel)74 Test (org.junit.Test)52 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)46 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)34 UserModel (org.keycloak.models.UserModel)29 HashMap (java.util.HashMap)22 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)22 GroupModel (org.keycloak.models.GroupModel)21 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)18 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)18 ComponentValidationException (org.keycloak.component.ComponentValidationException)16 UserStorageProvider (org.keycloak.storage.UserStorageProvider)16 LDAPStorageMapper (org.keycloak.storage.ldap.mappers.LDAPStorageMapper)15 GroupLDAPStorageMapper (org.keycloak.storage.ldap.mappers.membership.group.GroupLDAPStorageMapper)15 Path (javax.ws.rs.Path)14 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)13 ModelException (org.keycloak.models.ModelException)11 UPAttribute (org.keycloak.userprofile.config.UPAttribute)11 UPConfig (org.keycloak.userprofile.config.UPConfig)11