Search in sources :

Example 11 with ClusterProvider

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

the class InfinispanCacheRealmProviderFactory method lazyInit.

private void lazyInit(KeycloakSession session) {
    if (realmCache == null) {
        synchronized (this) {
            if (realmCache == null) {
                Cache<String, Revisioned> cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_CACHE_NAME);
                Cache<String, Long> revisions = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.REALM_REVISIONS_CACHE_NAME);
                realmCache = new RealmCacheManager(cache, revisions);
                ClusterProvider cluster = session.getProvider(ClusterProvider.class);
                cluster.registerListener(REALM_INVALIDATION_EVENTS, (ClusterEvent event) -> {
                    InvalidationEvent invalidationEvent = (InvalidationEvent) event;
                    realmCache.invalidationEventReceived(invalidationEvent);
                });
                cluster.registerListener(REALM_CLEAR_CACHE_EVENTS, (ClusterEvent event) -> {
                    realmCache.clear();
                });
                log.debug("Registered cluster listeners");
            }
        }
    }
}
Also used : ClusterEvent(org.keycloak.cluster.ClusterEvent) Revisioned(org.keycloak.models.cache.infinispan.entities.Revisioned) ClusterProvider(org.keycloak.cluster.ClusterProvider) InfinispanConnectionProvider(org.keycloak.connections.infinispan.InfinispanConnectionProvider) InvalidationEvent(org.keycloak.models.cache.infinispan.events.InvalidationEvent)

Example 12 with ClusterProvider

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

the class RealmCacheSession method clear.

@Override
public void clear() {
    ClusterProvider cluster = session.getProvider(ClusterProvider.class);
    cluster.notify(InfinispanCacheRealmProviderFactory.REALM_CLEAR_CACHE_EVENTS, new ClearCacheEvent(), false, ClusterProvider.DCNotify.ALL_DCS);
}
Also used : ClusterProvider(org.keycloak.cluster.ClusterProvider)

Example 13 with ClusterProvider

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

the class InfinispanUserLoginFailureProviderFactory method registerClusterListeners.

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

        @Override
        protected void eventReceived(KeycloakSession session, UserLoginFailureProvider provider, RealmRemovedSessionEvent sessionEvent) {
            if (provider instanceof InfinispanUserLoginFailureProvider) {
                ((InfinispanUserLoginFailureProvider) provider).removeAllLocalUserLoginFailuresEvent(sessionEvent.getRealmId());
            }
        }
    });
    cluster.registerListener(REMOVE_ALL_LOGIN_FAILURES_EVENT, new AbstractUserSessionClusterListener<RemoveAllUserLoginFailuresEvent, UserLoginFailureProvider>(sessionFactory, UserLoginFailureProvider.class) {

        @Override
        protected void eventReceived(KeycloakSession session, UserLoginFailureProvider provider, RemoveAllUserLoginFailuresEvent sessionEvent) {
            if (provider instanceof InfinispanUserLoginFailureProvider) {
                ((InfinispanUserLoginFailureProvider) provider).removeAllLocalUserLoginFailuresEvent(sessionEvent.getRealmId());
            }
        }
    });
    log.debug("Registered cluster listeners");
}
Also used : RemoveAllUserLoginFailuresEvent(org.keycloak.models.sessions.infinispan.events.RemoveAllUserLoginFailuresEvent) ClusterProvider(org.keycloak.cluster.ClusterProvider) KeycloakSession(org.keycloak.models.KeycloakSession) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) RealmRemovedSessionEvent(org.keycloak.models.sessions.infinispan.events.RealmRemovedSessionEvent) UserLoginFailureProvider(org.keycloak.models.UserLoginFailureProvider)

Example 14 with ClusterProvider

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

the class UserStorageSyncManager method notifyToRefreshPeriodicSync.

// Ensure all cluster nodes are notified
public void notifyToRefreshPeriodicSync(KeycloakSession session, RealmModel realm, UserStorageProviderModel provider, boolean removed) {
    UserStorageProviderFactory factory = (UserStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, provider.getProviderId());
    if (!(factory instanceof ImportSynchronization) || !provider.isImportEnabled()) {
        return;
    }
    final ClusterProvider cp = session.getProvider(ClusterProvider.class);
    if (cp != null) {
        UserStorageProviderClusterEvent event = UserStorageProviderClusterEvent.createEvent(removed, realm.getId(), provider);
        cp.notify(USER_STORAGE_TASK_KEY, event, false, ClusterProvider.DCNotify.ALL_DCS);
    }
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClusterProvider(org.keycloak.cluster.ClusterProvider) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization)

Example 15 with ClusterProvider

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

the class UserStorageSyncManager method syncChangedUsers.

public SynchronizationResult syncChangedUsers(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.getChangedSyncPeriod());
            holder.result = clusterProvider.executeIfNotExecuted(taskKey, timeout, new Callable<SynchronizationResult>() {

                @Override
                public SynchronizationResult call() throws Exception {
                    // See when we did last sync.
                    int oldLastSync = provider.getLastSync();
                    updateLastSyncInterval(sessionFactory, provider, realmId);
                    return ((ImportSynchronization) factory).syncSince(Time.toDate(oldLastSync), sessionFactory, realmId, provider);
                }
            });
        }
    });
    if (holder.result == null || !holder.result.isExecuted()) {
        logger.debugf("syncChangedUsers 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)

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