Skip to content

Commit

Permalink
KEYCLOAK-5688 Add externalizers for session entities
Browse files Browse the repository at this point in the history
and remove unused events
  • Loading branch information
hmlnarik committed Oct 18, 2017
1 parent d01be82 commit 6d18ba4
Show file tree
Hide file tree
Showing 28 changed files with 288 additions and 253 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,33 @@
package org.keycloak.models.cache.infinispan.events;

import org.keycloak.cluster.ClusterEvent;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.infinispan.commons.marshall.Externalizer;
import org.infinispan.commons.marshall.MarshallUtil;
import org.infinispan.commons.marshall.SerializeWith;

/**
*
* @author hmlnarik
*/
@SerializeWith(AuthenticationSessionAuthNoteUpdateEvent.ExternalizerImpl.class)
public class AuthenticationSessionAuthNoteUpdateEvent implements ClusterEvent {

private String authSessionId;

private Map<String, String> authNotesFragment;

/**
* Creates an instance of the event.
* @param authSessionId
* @param authNotesFragment
* @return Event. Note that {@code authNotesFragment} property is not thread safe which is fine for now.
*/
public static AuthenticationSessionAuthNoteUpdateEvent create(String authSessionId, Map<String, String> authNotesFragment) {
AuthenticationSessionAuthNoteUpdateEvent event = new AuthenticationSessionAuthNoteUpdateEvent();
event.authSessionId = authSessionId;
Expand All @@ -50,4 +64,34 @@ public String toString() {
return String.format("AuthenticationSessionAuthNoteUpdateEvent [ authSessionId=%s, authNotesFragment=%s ]", authSessionId, authNotesFragment);
}

public static class ExternalizerImpl implements Externalizer<AuthenticationSessionAuthNoteUpdateEvent> {

private static final int VERSION_1 = 1;

@Override
public void writeObject(ObjectOutput output, AuthenticationSessionAuthNoteUpdateEvent value) throws IOException {
output.write(VERSION_1);

MarshallUtil.marshallString(value.authSessionId, output);
MarshallUtil.marshallMap(value.authNotesFragment, output);
}

@Override
public AuthenticationSessionAuthNoteUpdateEvent readObject(ObjectInput input) throws IOException, ClassNotFoundException {
switch (input.readByte()) {
case VERSION_1:
return readObjectVersion1(input);
default:
throw new IOException("Unknown version");
}
}

public AuthenticationSessionAuthNoteUpdateEvent readObjectVersion1(ObjectInput input) throws IOException, ClassNotFoundException {
return create(
MarshallUtil.unmarshallString(input),
MarshallUtil.unmarshallMap(input, (size) -> new HashMap<>(size))
);
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void restartSession(RealmModel realm, ClientModel client) {
String id = entity.getId();
entity = new AuthenticationSessionEntity();
entity.setId(id);
entity.setRealm(realm.getId());
entity.setRealmId(realm.getId());
entity.setClientUuid(client.getId());
entity.setTimestamp(Time.currentTime());
update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
package org.keycloak.models.sessions.infinispan;

import org.jboss.logging.Logger;
import org.keycloak.cluster.ClusterProvider;
import org.keycloak.common.util.Time;
import org.keycloak.models.*;

import org.keycloak.models.cache.infinispan.events.RemoveActionTokensSpecificEvent;
import org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity;
import org.keycloak.models.sessions.infinispan.entities.ActionTokenReducedKey;
import java.util.*;
Expand Down Expand Up @@ -99,14 +97,4 @@ public ActionTokenValueModel remove(ActionTokenKeyModel actionTokenKey) {

return value;
}

@Override
public void removeAll(String userId, String actionId) {
if (userId == null || actionId == null) {
return;
}

ClusterProvider cluster = session.getProvider(ClusterProvider.class);
this.tx.notify(cluster, InfinispanActionTokenStoreProviderFactory.ACTION_TOKEN_EVENTS, new RemoveActionTokensSpecificEvent(userId, actionId), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,23 @@

import org.keycloak.Config;
import org.keycloak.Config.Scope;
import org.keycloak.cluster.ClusterProvider;
import org.keycloak.connections.infinispan.InfinispanConnectionProvider;
import org.keycloak.models.*;

import org.keycloak.models.cache.infinispan.events.RemoveActionTokensSpecificEvent;
import org.keycloak.models.sessions.infinispan.entities.ActionTokenValueEntity;
import org.keycloak.models.sessions.infinispan.entities.ActionTokenReducedKey;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.context.Flag;
import org.infinispan.remoting.transport.Address;
import org.jboss.logging.Logger;

/**
*
* @author hmlnarik
*/
public class InfinispanActionTokenStoreProviderFactory implements ActionTokenStoreProviderFactory {

private static final Logger LOG = Logger.getLogger(InfinispanActionTokenStoreProviderFactory.class);

private volatile Cache<ActionTokenReducedKey, ActionTokenValueEntity> actionTokenCache;

public static final String ACTION_TOKEN_EVENTS = "ACTION_TOKEN_EVENTS";

/**
* If expiration is set to this value, no expiration is set on the corresponding cache entry (hence cache default is honored)
*/
private static final int DEFAULT_CACHE_EXPIRATION = 0;

private Config.Scope config;

@Override
Expand All @@ -66,32 +50,6 @@ public void init(Scope config) {
private static Cache<ActionTokenReducedKey, ActionTokenValueEntity> initActionTokenCache(KeycloakSession session) {
InfinispanConnectionProvider connections = session.getProvider(InfinispanConnectionProvider.class);
Cache<ActionTokenReducedKey, ActionTokenValueEntity> cache = connections.getCache(InfinispanConnectionProvider.ACTION_TOKEN_CACHE);
final Address cacheAddress = cache.getCacheManager().getAddress();

ClusterProvider cluster = session.getProvider(ClusterProvider.class);

cluster.registerListener(ACTION_TOKEN_EVENTS, event -> {

RemoveActionTokensSpecificEvent e = (RemoveActionTokensSpecificEvent) event;

LOG.debugf("[%s] Removing token invalidation for user+action: userId=%s, actionId=%s", cacheAddress, e.getUserId(), e.getActionId());

AdvancedCache<ActionTokenReducedKey, ActionTokenValueEntity> localCache = cache
.getAdvancedCache()
.withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD);

List<ActionTokenReducedKey> toRemove = localCache
.keySet()
.stream()
.filter(k -> Objects.equals(k.getUserId(), e.getUserId()) && Objects.equals(k.getActionId(), e.getActionId()))
.collect(Collectors.toList());

toRemove.forEach(localCache::remove);

});

LOG.debugf("[%s] Registered cluster listeners", cacheAddress);

return cache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AuthenticationSessionModel createAuthenticationSession(RealmModel realm,
public AuthenticationSessionModel createAuthenticationSession(String id, RealmModel realm, ClientModel client) {
AuthenticationSessionEntity entity = new AuthenticationSessionEntity();
entity.setId(id);
entity.setRealm(realm.getId());
entity.setRealmId(realm.getId());
entity.setTimestamp(Time.currentTime());
entity.setClientUuid(client.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public CrossDCMessageStatus getCrossDCMessageStatus(SessionEntityWrapper<UserSes
}

void updateSessionEntity(UserSessionEntity entity, RealmModel realm, UserModel user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId) {
entity.setRealm(realm.getId());
entity.setRealmId(realm.getId());
entity.setUser(user.getId());
entity.setLoginUsername(loginUsername);
entity.setIpAddress(ipAddress);
Expand Down Expand Up @@ -525,7 +525,7 @@ private LoginFailureEntity getLoginFailureEntity(LoginFailureKey key) {
public UserLoginFailureModel addUserLoginFailure(RealmModel realm, String userId) {
LoginFailureKey key = new LoginFailureKey(realm.getId(), userId);
LoginFailureEntity entity = new LoginFailureEntity();
entity.setRealm(realm.getId());
entity.setRealmId(realm.getId());
entity.setUserId(userId);

SessionUpdateTask<LoginFailureEntity> createLoginFailureTask = new SessionUpdateTask<LoginFailureEntity>() {
Expand Down Expand Up @@ -768,7 +768,7 @@ public List<UserSessionModel> getOfflineUserSessions(RealmModel realm, ClientMod
public UserSessionAdapter importUserSession(UserSessionModel userSession, boolean offline, boolean importAuthenticatedClientSessions) {
UserSessionEntity entity = new UserSessionEntity();
entity.setId(userSession.getId());
entity.setRealm(userSession.getRealm().getId());
entity.setRealmId(userSession.getRealm().getId());

entity.setAuthMethod(userSession.getAuthMethod());
entity.setBrokerSessionId(userSession.getBrokerSessionId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void addTask(K key, SessionUpdateTask<V> task) {
return;
}

RealmModel realm = kcSession.realms().getRealm(wrappedEntity.getEntity().getRealm());
RealmModel realm = kcSession.realms().getRealm(wrappedEntity.getEntity().getRealmId());

myUpdates = new SessionUpdatesList<>(realm, wrappedEntity);
updates.put(key, myUpdates);
Expand All @@ -81,7 +81,7 @@ public void addTask(K key, SessionUpdateTask<V> task, V entity) {
throw new IllegalArgumentException("Null entity not allowed");
}

RealmModel realm = kcSession.realms().getRealm(entity.getRealm());
RealmModel realm = kcSession.realms().getRealm(entity.getRealmId());
SessionEntityWrapper<V> wrappedEntity = new SessionEntityWrapper<>(entity);
SessionUpdatesList<V> myUpdates = new SessionUpdatesList<>(realm, wrappedEntity);
updates.put(key, myUpdates);
Expand Down Expand Up @@ -121,7 +121,7 @@ public SessionEntityWrapper<V> get(K key) {
return null;
}

RealmModel realm = kcSession.realms().getRealm(wrappedEntity.getEntity().getRealm());
RealmModel realm = kcSession.realms().getRealm(wrappedEntity.getEntity().getRealmId());

myUpdates = new SessionUpdatesList<>(realm, wrappedEntity);
updates.put(key, myUpdates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AuthenticationSessionEntity extends SessionEntity {
private Set<String> roles;
private Set<String> protocolMappers;

private Map<String, AuthenticationSessionModel.ExecutionStatus> executionStatus = new HashMap<>();;
private Map<String, AuthenticationSessionModel.ExecutionStatus> executionStatus = new HashMap<>();
private String protocol;

private Map<String, String> clientNotes;
Expand Down Expand Up @@ -179,6 +179,6 @@ public int hashCode() {

@Override
public String toString() {
return String.format("AuthenticationSessionEntity [id=%s, realm=%s, clientUuid=%s ]", getId(), getRealm(), getClientUuid());
return String.format("AuthenticationSessionEntity [id=%s, realm=%s, clientUuid=%s ]", getId(), getRealmId(), getClientUuid());
}
}
Loading

0 comments on commit 6d18ba4

Please sign in to comment.