Search in sources :

Example 1 with PostMigrationEvent

use of org.keycloak.models.utils.PostMigrationEvent in project keycloak by keycloak.

the class InfinispanUserLoginFailureProviderFactory method postInit.

@Override
public void postInit(final KeycloakSessionFactory factory) {
    this.remoteCacheInvoker = new RemoteCacheInvoker();
    factory.register(event -> {
        if (event instanceof PostMigrationEvent) {
            KeycloakModelUtils.runJobInTransaction(factory, (KeycloakSession session) -> {
                checkRemoteCaches(session);
                registerClusterListeners(session);
                loadLoginFailuresFromRemoteCaches(session);
            });
        } else if (event instanceof UserModel.UserRemovedEvent) {
            UserModel.UserRemovedEvent userRemovedEvent = (UserModel.UserRemovedEvent) event;
            UserLoginFailureProvider provider = userRemovedEvent.getKeycloakSession().getProvider(UserLoginFailureProvider.class, getId());
            provider.removeUserLoginFailure(userRemovedEvent.getRealm(), userRemovedEvent.getUser().getId());
        }
    });
}
Also used : UserModel(org.keycloak.models.UserModel) RemoteCacheInvoker(org.keycloak.models.sessions.infinispan.remotestore.RemoteCacheInvoker) KeycloakSession(org.keycloak.models.KeycloakSession) UserLoginFailureProvider(org.keycloak.models.UserLoginFailureProvider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 2 with PostMigrationEvent

use of org.keycloak.models.utils.PostMigrationEvent in project keycloak by keycloak.

the class InfinispanUserSessionProviderFactory method postInit.

@Override
public void postInit(final KeycloakSessionFactory factory) {
    factory.register(new ProviderEventListener() {

        @Override
        public void onEvent(ProviderEvent event) {
            if (event instanceof PostMigrationEvent) {
                int preloadTransactionTimeout = getTimeoutForPreloadingSessionsSeconds();
                log.debugf("Will preload sessions with transaction timeout %d seconds", preloadTransactionTimeout);
                KeycloakModelUtils.runJobInTransactionWithTimeout(factory, (KeycloakSession session) -> {
                    keyGenerator = new InfinispanKeyGenerator();
                    checkRemoteCaches(session);
                    loadPersistentSessions(factory, getMaxErrors(), getSessionsPerSegment());
                    registerClusterListeners(session);
                    loadSessionsFromRemoteCaches(session);
                }, preloadTransactionTimeout);
            } else if (event instanceof UserModel.UserRemovedEvent) {
                UserModel.UserRemovedEvent userRemovedEvent = (UserModel.UserRemovedEvent) event;
                InfinispanUserSessionProvider provider = (InfinispanUserSessionProvider) userRemovedEvent.getKeycloakSession().getProvider(UserSessionProvider.class, getId());
                provider.onUserRemoved(userRemovedEvent.getRealm(), userRemovedEvent.getUser());
            } else if (event instanceof ResetTimeOffsetEvent) {
                if (persisterLastSessionRefreshStore != null) {
                    persisterLastSessionRefreshStore.reset();
                }
                if (lastSessionRefreshStore != null) {
                    lastSessionRefreshStore.reset();
                }
                if (offlineLastSessionRefreshStore != null) {
                    offlineLastSessionRefreshStore.reset();
                }
            }
        }
    });
}
Also used : ProviderEventListener(org.keycloak.provider.ProviderEventListener) ProviderEvent(org.keycloak.provider.ProviderEvent) UserModel(org.keycloak.models.UserModel) UserSessionProvider(org.keycloak.models.UserSessionProvider) ResetTimeOffsetEvent(org.keycloak.models.utils.ResetTimeOffsetEvent) KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanKeyGenerator(org.keycloak.models.sessions.infinispan.util.InfinispanKeyGenerator) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 3 with PostMigrationEvent

use of org.keycloak.models.utils.PostMigrationEvent in project keycloak by keycloak.

the class KeycloakModelTest method createKeycloakSessionFactory.

/**
 * Creates a fresh initialized {@link KeycloakSessionFactory}. The returned factory uses configuration
 * local to the thread that calls this method, allowing for per-thread customization. This in turn allows
 * testing of several parallel session factories which can be used to simulate several servers
 * running in parallel.
 * @return
 */
public static KeycloakSessionFactory createKeycloakSessionFactory() {
    int factoryIndex = FACTORY_COUNT.incrementAndGet();
    String threadName = Thread.currentThread().getName();
    CONFIG.reset();
    CONFIG.spi(ComponentFactorySpi.NAME).provider(DefaultComponentFactoryProviderFactory.PROVIDER_ID).config("cachingForced", "true");
    MODEL_PARAMETERS.forEach(m -> m.updateConfig(CONFIG));
    LOG.debugf("Creating factory %d in %s using the following configuration:\n    %s", factoryIndex, threadName, CONFIG);
    DefaultKeycloakSessionFactory res = new DefaultKeycloakSessionFactory() {

        @Override
        protected boolean isEnabled(ProviderFactory factory, Scope scope) {
            return super.isEnabled(factory, scope) && isFactoryAllowed(factory);
        }

        @Override
        protected Map<Class<? extends Provider>, Map<String, ProviderFactory>> loadFactories(ProviderManager pm) {
            spis.removeIf(s -> !isSpiAllowed(s));
            return super.loadFactories(pm);
        }

        private boolean isSpiAllowed(Spi s) {
            return MODEL_PARAMETERS.stream().anyMatch(p -> p.isSpiAllowed(s));
        }

        private boolean isFactoryAllowed(ProviderFactory factory) {
            return MODEL_PARAMETERS.stream().anyMatch(p -> p.isFactoryAllowed(factory));
        }

        @Override
        public String toString() {
            return "KeycloakSessionFactory " + factoryIndex + " (from " + threadName + " thread)";
        }
    };
    res.init();
    res.publish(new PostMigrationEvent());
    return res;
}
Also used : DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory) Scope(org.keycloak.Config.Scope) DefaultExecutorsProviderFactory(org.keycloak.executors.DefaultExecutorsProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) DefaultAuthorizationProviderFactory(org.keycloak.authorization.DefaultAuthorizationProviderFactory) DefaultComponentFactoryProviderFactory(org.keycloak.services.DefaultComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) DeploymentStateProviderFactory(org.keycloak.models.DeploymentStateProviderFactory) ProviderManager(org.keycloak.provider.ProviderManager) BeforeClass(org.junit.BeforeClass) Map(java.util.Map) ComponentFactorySpi(org.keycloak.component.ComponentFactorySpi) ClientScopeSpi(org.keycloak.models.ClientScopeSpi) ExecutorsSpi(org.keycloak.executors.ExecutorsSpi) DBLockSpi(org.keycloak.models.dblock.DBLockSpi) ClusterSpi(org.keycloak.cluster.ClusterSpi) UserSpi(org.keycloak.models.UserSpi) Spi(org.keycloak.provider.Spi) UserLoginFailureSpi(org.keycloak.models.UserLoginFailureSpi) TimerSpi(org.keycloak.timer.TimerSpi) ClientSpi(org.keycloak.models.ClientSpi) StoreFactorySpi(org.keycloak.authorization.store.StoreFactorySpi) RealmSpi(org.keycloak.models.RealmSpi) GroupSpi(org.keycloak.models.GroupSpi) UserSessionSpi(org.keycloak.models.UserSessionSpi) DeploymentStateSpi(org.keycloak.models.DeploymentStateSpi) EventStoreSpi(org.keycloak.events.EventStoreSpi) AuthorizationSpi(org.keycloak.authorization.AuthorizationSpi) RoleSpi(org.keycloak.models.RoleSpi) Provider(org.keycloak.provider.Provider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 4 with PostMigrationEvent

use of org.keycloak.models.utils.PostMigrationEvent in project keycloak by keycloak.

the class KeycloakApplication method startup.

protected void startup() {
    KeycloakApplication.sessionFactory = createSessionFactory();
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            DBLockManager dbLockManager = new DBLockManager(session);
            dbLockManager.checkForcedUnlock();
            DBLockProvider dbLock = dbLockManager.getDBLock();
            dbLock.waitForLock(DBLockProvider.Namespace.KEYCLOAK_BOOT);
            try {
                exportImportManager[0] = bootstrap();
            } finally {
                dbLock.releaseLock();
            }
        }
    });
    if (exportImportManager[0].isRunExport()) {
        exportImportManager[0].runExport();
    }
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
            BOOTSTRAP_ADMIN_USER.set(shouldBootstrapAdmin);
        }
    });
    sessionFactory.publish(new PostMigrationEvent());
    setupScheduledTasks(sessionFactory);
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 5 with PostMigrationEvent

use of org.keycloak.models.utils.PostMigrationEvent in project keycloak by keycloak.

the class QuarkusKeycloakApplication method initializeKeycloakSessionFactory.

private void initializeKeycloakSessionFactory() {
    QuarkusKeycloakSessionFactory instance = QuarkusKeycloakSessionFactory.getInstance();
    sessionFactory = instance;
    instance.init();
    sessionFactory.publish(new PostMigrationEvent());
}
Also used : QuarkusKeycloakSessionFactory(org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Aggregations

PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)5 KeycloakSession (org.keycloak.models.KeycloakSession)3 UserModel (org.keycloak.models.UserModel)2 Map (java.util.Map)1 BeforeClass (org.junit.BeforeClass)1 Scope (org.keycloak.Config.Scope)1 AuthorizationSpi (org.keycloak.authorization.AuthorizationSpi)1 DefaultAuthorizationProviderFactory (org.keycloak.authorization.DefaultAuthorizationProviderFactory)1 StoreFactorySpi (org.keycloak.authorization.store.StoreFactorySpi)1 ClusterSpi (org.keycloak.cluster.ClusterSpi)1 ComponentFactoryProviderFactory (org.keycloak.component.ComponentFactoryProviderFactory)1 ComponentFactorySpi (org.keycloak.component.ComponentFactorySpi)1 EventStoreSpi (org.keycloak.events.EventStoreSpi)1 DefaultExecutorsProviderFactory (org.keycloak.executors.DefaultExecutorsProviderFactory)1 ExecutorsSpi (org.keycloak.executors.ExecutorsSpi)1 ExportImportManager (org.keycloak.exportimport.ExportImportManager)1 ClientScopeSpi (org.keycloak.models.ClientScopeSpi)1 ClientSpi (org.keycloak.models.ClientSpi)1 DeploymentStateProviderFactory (org.keycloak.models.DeploymentStateProviderFactory)1 DeploymentStateSpi (org.keycloak.models.DeploymentStateSpi)1