Search in sources :

Example 16 with UserStorageProviderModel

use of org.keycloak.storage.UserStorageProviderModel 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 17 with UserStorageProviderModel

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

the class LDAPRoleMappingsNoImportTest method afterImportTestRealm.

@Override
protected void afterImportTestRealm() {
    // Disable pagination
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        ctx.getLdapModel().put(LDAPConstants.PAGINATION, "false");
        appRealm.updateComponent(ctx.getLdapModel());
    });
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserStorageProviderModel ldapModel = ctx.getLdapModel();
        LDAPTestUtils.addLocalUser(session, appRealm, "mary", "mary@test.com", "password-app");
        // Delete all LDAP users
        LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);
        LDAPTestUtils.removeAllLDAPUsers(ldapFedProvider, appRealm);
        // Add sample application
        ClientModel finance = appRealm.addClient("finance");
        // Delete all LDAP roles
        LDAPTestUtils.addOrUpdateRoleLDAPMappers(appRealm, ldapModel, LDAPGroupMapperMode.LDAP_ONLY);
        LDAPTestUtils.removeAllLDAPRoles(session, appRealm, ldapModel, "realmRolesMapper");
        LDAPTestUtils.removeAllLDAPRoles(session, appRealm, ldapModel, "financeRolesMapper");
        // Add some users for testing
        LDAPObject john = LDAPTestUtils.addLDAPUser(ldapFedProvider, appRealm, "johnkeycloak", "John", "Doe", "john@email.org", null, "1234");
        LDAPTestUtils.updateLDAPPassword(ldapFedProvider, john, "Password1");
        LDAPObject mary = LDAPTestUtils.addLDAPUser(ldapFedProvider, appRealm, "marykeycloak", "Mary", "Kelly", "mary@email.org", null, "5678");
        LDAPTestUtils.updateLDAPPassword(ldapFedProvider, mary, "Password1");
        LDAPObject rob = LDAPTestUtils.addLDAPUser(ldapFedProvider, appRealm, "robkeycloak", "Rob", "Brown", "rob@email.org", null, "8910");
        LDAPTestUtils.updateLDAPPassword(ldapFedProvider, rob, "Password1");
        // Add some roles for testing
        LDAPTestUtils.createLDAPRole(session, appRealm, ldapModel, "realmRolesMapper", "realmRole1");
        LDAPTestUtils.createLDAPRole(session, appRealm, ldapModel, "realmRolesMapper", "realmRole2");
        LDAPTestUtils.createLDAPRole(session, appRealm, ldapModel, "financeRolesMapper", "financeRole1");
        // Sync LDAP roles to Keycloak DB
        LDAPTestUtils.syncRolesFromLDAP(appRealm, ldapFedProvider, ldapModel);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) LDAPTestContext(org.keycloak.testsuite.federation.ldap.LDAPTestContext) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Example 18 with UserStorageProviderModel

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

the class BrokenUserStorageTest method testBootWithBadProviderId.

@Test
public void testBootWithBadProviderId() throws Exception {
    testingClient.server().run(session -> {
        // set this system property
        System.setProperty(RealmAdapter.COMPONENT_PROVIDER_EXISTS_DISABLED, "true");
        RealmModel realm = session.realms().getRealmByName("master");
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setName("bad-provider-id");
        model.setPriority(2);
        model.setParentId(realm.getId());
        model.setProviderId("error");
        ComponentModel component = realm.importComponentModel(model);
    });
    controller.stop(suiteContext.getAuthServerInfo().getQualifier());
    controller.start(suiteContext.getAuthServerInfo().getQualifier());
    reconnectAdminClient();
    loginSuccessAndLogout("test-user@localhost", "password");
    // make sure we can list components and delete provider as this is an admin console operation
    RealmResource master = adminClient.realms().realm("master");
    String masterId = master.toRepresentation().getId();
    List<ComponentRepresentation> components = master.components().query(masterId, UserStorageProvider.class.getName());
    ComponentRepresentation found = null;
    for (ComponentRepresentation rep : components) {
        if (rep.getName().equals("bad-provider-id")) {
            found = rep;
        }
    }
    Assert.assertNotNull(found);
    master.components().component(found.getId()).remove();
    List<ComponentRepresentation> components2 = master.components().query(masterId, UserStorageProvider.class.getName());
    Assert.assertEquals(components.size() - 1, components2.size());
}
Also used : RealmModel(org.keycloak.models.RealmModel) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageProvider(org.keycloak.storage.UserStorageProvider) RealmResource(org.keycloak.admin.client.resource.RealmResource) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 19 with UserStorageProviderModel

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

the class UserStorageProviderResource method syncUsers.

/**
 * Trigger sync of users
 *
 * Action can be "triggerFullSync" or "triggerChangedUsersSync"
 *
 * @param id
 * @param action
 * @return
 */
@POST
@Path("{id}/sync")
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public SynchronizationResult syncUsers(@PathParam("id") String id, @QueryParam("action") String action) {
    auth.users().requireManage();
    ComponentModel model = realm.getComponent(id);
    if (model == null) {
        throw new NotFoundException("Could not find component");
    }
    if (!model.getProviderType().equals(UserStorageProvider.class.getName())) {
        throw new NotFoundException("found, but not a UserStorageProvider");
    }
    UserStorageProviderModel providerModel = new UserStorageProviderModel(model);
    logger.debug("Syncing users");
    UserStorageSyncManager syncManager = new UserStorageSyncManager();
    SynchronizationResult syncResult;
    if ("triggerFullSync".equals(action)) {
        syncResult = syncManager.syncAllUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    } else if ("triggerChangedUsersSync".equals(action)) {
        syncResult = syncManager.syncChangedUsers(session.getKeycloakSessionFactory(), realm.getId(), providerModel);
    } else if (action == null || action == "") {
        logger.debug("Missing action");
        throw new BadRequestException("Missing action");
    } else {
        logger.debug("Unknown action: " + action);
        throw new BadRequestException("Unknown action: " + action);
    }
    Map<String, Object> eventRep = new HashMap<>();
    eventRep.put("action", action);
    eventRep.put("result", syncResult);
    adminEvent.operation(OperationType.ACTION).resourcePath(session.getContext().getUri()).representation(eventRep).success();
    return syncResult;
}
Also used : UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) HashMap(java.util.HashMap) ComponentModel(org.keycloak.component.ComponentModel) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 20 with UserStorageProviderModel

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

the class TestLDAPResource method createLDAPProvider.

/**
 * @param ldapCfg configuration of LDAP provider
 * @param importEnabled specify if LDAP provider will have import enabled
 * @return ID of newly created provider
 */
@POST
@Path("/create-ldap-provider")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String createLDAPProvider(Map<String, String> ldapCfg, @QueryParam("import") boolean importEnabled) {
    MultivaluedHashMap<String, String> ldapConfig = toComponentConfig(ldapCfg);
    ldapConfig.putSingle(LDAPConstants.SYNC_REGISTRATIONS, "true");
    ldapConfig.putSingle(LDAPConstants.EDIT_MODE, UserStorageProvider.EditMode.WRITABLE.toString());
    UserStorageProviderModel model = new UserStorageProviderModel();
    model.setLastSync(0);
    model.setChangedSyncPeriod(-1);
    model.setFullSyncPeriod(-1);
    model.setName("test-ldap");
    model.setPriority(0);
    model.setProviderId(LDAPStorageProviderFactory.PROVIDER_NAME);
    model.setConfig(ldapConfig);
    model.setImportEnabled(importEnabled);
    model.setCachePolicy(UserStorageProviderModel.CachePolicy.MAX_LIFESPAN);
    // Lifetime is 10 minutes
    model.setMaxLifespan(600000);
    ComponentModel ldapModel = realm.addComponentModel(model);
    return ldapModel.getId();
}
Also used : ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

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