Search in sources :

Example 31 with UserStorageProviderModel

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

the class LDAPMSADFullNameTest method afterImportTestRealm.

@Override
protected void afterImportTestRealm() {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserStorageProviderModel ldapModel = ctx.getLdapModel();
        LDAPTestUtils.addLocalUser(session, appRealm, "marykeycloak", "mary@test.com", "password-app");
        LDAPTestUtils.addZipCodeLDAPMapper(appRealm, ldapModel);
        // Delete all LDAP users and add some new for testing
        LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);
        LDAPTestUtils.removeAllLDAPUsers(ldapFedProvider, appRealm);
        // Remove the mapper for "username-cn" and create new mapper for fullName
        ComponentModel mapperModel = LDAPTestUtils.getSubcomponentByName(appRealm, ldapModel, "username-cn");
        Assert.assertNotNull(mapperModel);
        appRealm.removeComponent(mapperModel);
        mapperModel = KeycloakModelUtils.createComponentModel("fullNameWritable", ldapModel.getId(), FullNameLDAPStorageMapperFactory.PROVIDER_ID, LDAPStorageMapper.class.getName(), FullNameLDAPStorageMapper.LDAP_FULL_NAME_ATTRIBUTE, LDAPConstants.CN, FullNameLDAPStorageMapper.READ_ONLY, "false", FullNameLDAPStorageMapper.WRITE_ONLY, "true");
        appRealm.addComponentModel(mapperModel);
        appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ComponentModel(org.keycloak.component.ComponentModel) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 32 with UserStorageProviderModel

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

the class UserSyncTest method testManyUsersImport.

@Test
public void testManyUsersImport() {
    IntStream.range(0, NUMBER_OF_USERS).parallel().forEach(index -> inComittedTransaction(index, (session, i) -> {
        final RealmModel realm = session.realms().getRealm(realmId);
        ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(realm);
        LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);
        LDAPTestUtils.addLDAPUser(ldapFedProvider, realm, "user" + i, "User" + i + "FN", "User" + i + "LN", "user" + i + "@email.org", null, "12" + i);
        return null;
    }));
    assertThat(withRealm(realmId, (session, realm) -> session.userLocalStorage().getUsersCount(realm)), is(0));
    long start = System.currentTimeMillis();
    SynchronizationResult res = withRealm(realmId, (session, realm) -> {
        UserStorageProviderModel providerModel = new UserStorageProviderModel(realm.getComponent(userFederationId));
        return new UserStorageSyncManager().syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    });
    long end = System.currentTimeMillis();
    long timeNeeded = end - start;
    // The sync shouldn't take more than 18 second per user
    assertThat(String.format("User sync took %f seconds per user, but it should take less than 18 seconds", (float) (timeNeeded) / NUMBER_OF_USERS), timeNeeded, Matchers.lessThan((long) (18 * NUMBER_OF_USERS)));
    assertThat(res.getAdded(), is(NUMBER_OF_USERS));
    assertThat(withRealm(realmId, (session, realm) -> session.userLocalStorage().getUsersCount(realm)), is(NUMBER_OF_USERS));
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) RealmModel(org.keycloak.models.RealmModel) Assume.assumeThat(org.junit.Assume.assumeThat) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) UserStorageProvider(org.keycloak.storage.UserStorageProvider) LDAPTestUtils(org.keycloak.testsuite.util.LDAPTestUtils) Constants(org.keycloak.models.Constants) Matchers(org.hamcrest.Matchers) KeycloakSession(org.keycloak.models.KeycloakSession) Test(org.junit.Test) LDAPStorageProviderFactory(org.keycloak.storage.ldap.LDAPStorageProviderFactory) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ClusterProvider(org.keycloak.cluster.ClusterProvider) UserProvider(org.keycloak.models.UserProvider) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) RealmProvider(org.keycloak.models.RealmProvider) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) RealmModel(org.keycloak.models.RealmModel) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentModel(org.keycloak.component.ComponentModel) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) Test(org.junit.Test)

Example 33 with UserStorageProviderModel

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

the class TestsuiteUserMapStorage method getParameters.

@Override
public <T> Stream<T> getParameters(Class<T> clazz) {
    if (UserStorageProviderModel.class.isAssignableFrom(clazz)) {
        UserStorageProviderModel federatedStorage = new UserStorageProviderModel();
        federatedStorage.setName(UserMapStorageFactory.PROVIDER_ID + ":" + counter.getAndIncrement());
        federatedStorage.setProviderId(UserMapStorageFactory.PROVIDER_ID);
        federatedStorage.setProviderType(UserStorageProvider.class.getName());
        return Stream.of((T) federatedStorage);
    } else {
        return super.getParameters(clazz);
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 34 with UserStorageProviderModel

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

the class BackwardsCompatibilityUserStorage method getParameters.

@Override
public <T> Stream<T> getParameters(Class<T> clazz) {
    if (UserStorageProviderModel.class.isAssignableFrom(clazz)) {
        UserStorageProviderModel federatedStorage = new UserStorageProviderModel();
        federatedStorage.setName(BackwardsCompatibilityUserStorageFactory.PROVIDER_ID + ":" + counter.getAndIncrement());
        federatedStorage.setProviderId(BackwardsCompatibilityUserStorageFactory.PROVIDER_ID);
        federatedStorage.setProviderType(UserStorageProvider.class.getName());
        return Stream.of((T) federatedStorage);
    } else {
        return super.getParameters(clazz);
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 35 with UserStorageProviderModel

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

the class LDAPStorageProviderFactory method validateConfiguration.

@Override
public void validateConfiguration(KeycloakSession session, RealmModel realm, ComponentModel config) throws ComponentValidationException {
    LDAPConfig cfg = new LDAPConfig(config.getConfig());
    UserStorageProviderModel userStorageModel = new UserStorageProviderModel(config);
    String customFilter = cfg.getCustomUserSearchFilter();
    LDAPUtils.validateCustomLdapFilter(customFilter);
    String connectionTimeout = cfg.getConnectionTimeout();
    if (connectionTimeout != null && !connectionTimeout.isEmpty()) {
        try {
            Long.parseLong(connectionTimeout);
        } catch (NumberFormatException nfe) {
            throw new ComponentValidationException("ldapErrorConnectionTimeoutNotNumber");
        }
    }
    String readTimeout = cfg.getReadTimeout();
    if (readTimeout != null && !readTimeout.isEmpty()) {
        try {
            Long.parseLong(readTimeout);
        } catch (NumberFormatException nfe) {
            throw new ComponentValidationException("ldapErrorReadTimeoutNotNumber");
        }
    }
    if (cfg.isStartTls() && cfg.getConnectionPooling() != null) {
        throw new ComponentValidationException("ldapErrorCantEnableStartTlsAndConnectionPooling");
    }
    // editMode is mandatory
    if (config.get(LDAPConstants.EDIT_MODE) == null) {
        throw new ComponentValidationException("ldapErrorEditModeMandatory");
    }
    // validatePasswordPolicy applicable only for WRITABLE mode
    if (cfg.getEditMode() != UserStorageProvider.EditMode.WRITABLE) {
        if (cfg.isValidatePasswordPolicy()) {
            throw new ComponentValidationException("ldapErrorValidatePasswordPolicyAvailableForWritableOnly");
        }
    }
    if (!userStorageModel.isImportEnabled() && cfg.getEditMode() == UserStorageProvider.EditMode.UNSYNCED) {
        throw new ComponentValidationException("ldapErrorCantEnableUnsyncedAndImportOff");
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Aggregations

UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)40 ComponentModel (org.keycloak.component.ComponentModel)19 RealmModel (org.keycloak.models.RealmModel)17 UserStorageProvider (org.keycloak.storage.UserStorageProvider)12 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)8 Test (org.junit.Test)7 UserStorageSyncManager (org.keycloak.services.managers.UserStorageSyncManager)7 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)6 LinkedList (java.util.LinkedList)5 UserModel (org.keycloak.models.UserModel)5 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)5 HashMap (java.util.HashMap)4 KeycloakSession (org.keycloak.models.KeycloakSession)4 UserStorageProviderFactory (org.keycloak.storage.UserStorageProviderFactory)4 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)4 Path (javax.ws.rs.Path)3 CachedUserModel (org.keycloak.models.cache.CachedUserModel)3 ProviderConfigurationBuilder (org.keycloak.provider.ProviderConfigurationBuilder)3 Arrays (java.util.Arrays)2 List (java.util.List)2