Search in sources :

Example 16 with ClusterProvider

use of org.keycloak.cluster.ClusterProvider in project keycloak by keycloak.

the class UserStorageSyncManager method syncAllUsers.

public SynchronizationResult syncAllUsers(final KeycloakSessionFactory sessionFactory, final String realmId, final UserStorageProviderModel provider) {
    UserStorageProviderFactory factory = (UserStorageProviderFactory) sessionFactory.getProviderFactory(UserStorageProvider.class, provider.getProviderId());
    if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled() || !provider.isEnabled()) {
        return SynchronizationResult.ignored();
    }
    final Holder holder = new Holder();
    // Ensure not executed concurrently on this or any other cluster node
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            ClusterProvider clusterProvider = session.getProvider(ClusterProvider.class);
            // shared key for "full" and "changed" . Improve if needed
            String taskKey = provider.getId() + "::sync";
            // 30 seconds minimal timeout for now
            int timeout = Math.max(30, provider.getFullSyncPeriod());
            holder.result = clusterProvider.executeIfNotExecuted(taskKey, timeout, new Callable<SynchronizationResult>() {

                @Override
                public SynchronizationResult call() throws Exception {
                    updateLastSyncInterval(sessionFactory, provider, realmId);
                    return ((ImportSynchronization) factory).sync(sessionFactory, realmId, provider);
                }
            });
        }
    });
    if (holder.result == null || !holder.result.isExecuted()) {
        logger.debugf("syncAllUsers for federation provider %s was ignored as it's already in progress", provider.getName());
        return SynchronizationResult.ignored();
    } else {
        return holder.result.getResult();
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) KeycloakSession(org.keycloak.models.KeycloakSession) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult)

Example 17 with ClusterProvider

use of org.keycloak.cluster.ClusterProvider in project keycloak by keycloak.

the class MapRootAuthenticationSessionProvider method updateNonlocalSessionAuthNotes.

@Override
public void updateNonlocalSessionAuthNotes(AuthenticationSessionCompoundId compoundId, Map<String, String> authNotesFragment) {
    if (compoundId == null) {
        return;
    }
    Objects.requireNonNull(authNotesFragment, "The provided authentication's notes map can't be null!");
    ClusterProvider cluster = session.getProvider(ClusterProvider.class);
    cluster.notify(AUTHENTICATION_SESSION_EVENTS, MapAuthenticationSessionAuthNoteUpdateEvent.create(compoundId.getRootSessionId(), compoundId.getTabId(), compoundId.getClientUUID(), authNotesFragment), true, ClusterProvider.DCNotify.ALL_BUT_LOCAL_DC);
}
Also used : ClusterProvider(org.keycloak.cluster.ClusterProvider)

Example 18 with ClusterProvider

use of org.keycloak.cluster.ClusterProvider in project keycloak by keycloak.

the class InfinispanAuthenticationSessionProviderFactory method lazyInit.

private void lazyInit(KeycloakSession session) {
    if (authSessionsCache == null) {
        synchronized (this) {
            if (authSessionsCache == null) {
                InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
                authSessionsCache = connections.getCache(InfinispanConnectionProvider.AUTHENTICATION_SESSIONS_CACHE_NAME);
                keyGenerator = new InfinispanKeyGenerator();
                ClusterProvider cluster = session.getProvider(ClusterProvider.class);
                cluster.registerListener(AUTHENTICATION_SESSION_EVENTS, this::updateAuthNotes);
                log.debugf("[%s] Registered cluster listeners", authSessionsCache.getCacheManager().getAddress());
            }
        }
    }
}
Also used : ClusterProvider(org.keycloak.cluster.ClusterProvider) InfinispanKeyGenerator(org.keycloak.models.sessions.infinispan.util.InfinispanKeyGenerator) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider)

Example 19 with ClusterProvider

use of org.keycloak.cluster.ClusterProvider in project keycloak by keycloak.

the class InfinispanAuthenticationSessionProviderFactory method registerClusterListeners.

protected void registerClusterListeners(KeycloakSession session) {
    KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
    ClusterProvider cluster = session.getProvider(ClusterProvider.class);
    cluster.registerListener(REALM_REMOVED_AUTHSESSION_EVENT, new AbstractAuthSessionClusterListener<RealmRemovedSessionEvent>(sessionFactory) {

        @Override
        protected void eventReceived(KeycloakSession session, InfinispanAuthenticationSessionProvider provider, RealmRemovedSessionEvent sessionEvent) {
            provider.onRealmRemovedEvent(sessionEvent.getRealmId());
        }
    });
    cluster.registerListener(CLIENT_REMOVED_AUTHSESSION_EVENT, new AbstractAuthSessionClusterListener<ClientRemovedSessionEvent>(sessionFactory) {

        @Override
        protected void eventReceived(KeycloakSession session, InfinispanAuthenticationSessionProvider provider, ClientRemovedSessionEvent sessionEvent) {
            provider.onClientRemovedEvent(sessionEvent.getRealmId(), sessionEvent.getClientUuid());
        }
    });
    log.debug("Registered cluster listeners");
}
Also used : ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSession(org.keycloak.models.KeycloakSession) ClientRemovedSessionEvent(org.keycloak.models.sessions.infinispan.events.ClientRemovedSessionEvent) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) RealmRemovedSessionEvent(org.keycloak.models.sessions.infinispan.events.RealmRemovedSessionEvent)

Example 20 with ClusterProvider

use of org.keycloak.cluster.ClusterProvider in project keycloak by keycloak.

the class InfinispanAuthenticationSessionProvider method updateNonlocalSessionAuthNotes.

@Override
public void updateNonlocalSessionAuthNotes(AuthenticationSessionCompoundId compoundId, Map<String, String> authNotesFragment) {
    if (compoundId == null) {
        return;
    }
    ClusterProvider cluster = session.getProvider(ClusterProvider.class);
    cluster.notify(InfinispanAuthenticationSessionProviderFactory.AUTHENTICATION_SESSION_EVENTS, AuthenticationSessionAuthNoteUpdateEvent.create(compoundId.getRootSessionId(), compoundId.getTabId(), compoundId.getClientUUID(), authNotesFragment), true, ClusterProvider.DCNotify.ALL_BUT_LOCAL_DC);
}
Also used : ClusterProvider(org.keycloak.cluster.ClusterProvider)

Aggregations

ClusterProvider (org.keycloak.cluster.ClusterProvider)22 ClusterEvent (org.keycloak.cluster.ClusterEvent)5 KeycloakSession (org.keycloak.models.KeycloakSession)5 InfinispanConnectionProvider (org.keycloak.connections.infinispan.InfinispanConnectionProvider)4 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)4 Revisioned (org.keycloak.models.cache.infinispan.entities.Revisioned)3 InvalidationEvent (org.keycloak.models.cache.infinispan.events.InvalidationEvent)3 RealmRemovedSessionEvent (org.keycloak.models.sessions.infinispan.events.RealmRemovedSessionEvent)3 UserStorageProvider (org.keycloak.storage.UserStorageProvider)3 UserStorageProviderFactory (org.keycloak.storage.UserStorageProviderFactory)3 ImportSynchronization (org.keycloak.storage.user.ImportSynchronization)3 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)2 ClearCacheEvent (org.keycloak.models.cache.infinispan.ClearCacheEvent)2 ClientRemovedSessionEvent (org.keycloak.models.sessions.infinispan.events.ClientRemovedSessionEvent)2 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)2 Future (java.util.concurrent.Future)1 UserLoginFailureProvider (org.keycloak.models.UserLoginFailureProvider)1 UserSessionProvider (org.keycloak.models.UserSessionProvider)1 RealmRemovedEvent (org.keycloak.models.cache.infinispan.events.RealmRemovedEvent)1 RealmUpdatedEvent (org.keycloak.models.cache.infinispan.events.RealmUpdatedEvent)1