Remove static ScimDispatcher map

This commit is contained in:
Alex Morel 2024-06-21 11:58:56 +02:00
parent 1e46d584c8
commit 55730a8021
3 changed files with 6 additions and 15 deletions

View file

@ -6,11 +6,8 @@ import org.keycloak.models.KeycloakSession;
import sh.libre.scim.storage.ScimEndpointConfigurationStorageProviderFactory; import sh.libre.scim.storage.ScimEndpointConfigurationStorageProviderFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@ -20,20 +17,13 @@ public class ScimDispatcher {
private static final Logger logger = Logger.getLogger(ScimDispatcher.class); private static final Logger logger = Logger.getLogger(ScimDispatcher.class);
private static final Map<KeycloakSession, ScimDispatcher> sessionToScimDispatcher = new ConcurrentHashMap<>();
private final KeycloakSession session; private final KeycloakSession session;
private boolean clientsInitialized = false; private boolean clientsInitialized = false;
private final List<UserScimService> userScimServices = new ArrayList<>(); private final List<UserScimService> userScimServices = new ArrayList<>();
private final List<GroupScimService> groupScimServices = new ArrayList<>(); private final List<GroupScimService> groupScimServices = new ArrayList<>();
public static ScimDispatcher createForSession(KeycloakSession session) { public ScimDispatcher(KeycloakSession session) {
// Only create a scim dispatcher if there is none already created for session
sessionToScimDispatcher.computeIfAbsent(session, ScimDispatcher::new);
return sessionToScimDispatcher.get(session);
}
private ScimDispatcher(KeycloakSession session) {
this.session = session; this.session = session;
} }
@ -117,7 +107,6 @@ public class ScimDispatcher {
} }
public void close() { public void close() {
sessionToScimDispatcher.remove(session);
for (GroupScimService c : groupScimServices) { for (GroupScimService c : groupScimServices) {
c.close(); c.close();
} }

View file

@ -46,7 +46,7 @@ public class ScimEventListenerProvider implements EventListenerProvider {
public ScimEventListenerProvider(KeycloakSession session) { public ScimEventListenerProvider(KeycloakSession session) {
this.session = session; this.session = session;
this.keycloackDao = new KeycloakDao(session); this.keycloackDao = new KeycloakDao(session);
this.dispatcher = ScimDispatcher.createForSession(session); this.dispatcher = new ScimDispatcher(session);
} }
@Override @Override

View file

@ -48,13 +48,14 @@ public class ScimEndpointConfigurationStorageProviderFactory
KeycloakModelUtils.runJobInTransaction(sessionFactory, session -> { KeycloakModelUtils.runJobInTransaction(sessionFactory, session -> {
RealmModel realm = session.realms().getRealm(realmId); RealmModel realm = session.realms().getRealm(realmId);
session.getContext().setRealm(realm); session.getContext().setRealm(realm);
ScimDispatcher dispatcher = ScimDispatcher.createForSession(session); ScimDispatcher dispatcher = new ScimDispatcher(session);
if (BooleanUtils.TRUE.equals(model.get("propagation-user"))) { if (BooleanUtils.TRUE.equals(model.get("propagation-user"))) {
dispatcher.dispatchUserModificationToOne(model, client -> client.sync(result)); dispatcher.dispatchUserModificationToOne(model, client -> client.sync(result));
} }
if (BooleanUtils.TRUE.equals(model.get("propagation-group"))) { if (BooleanUtils.TRUE.equals(model.get("propagation-group"))) {
dispatcher.dispatchGroupModificationToOne(model, client -> client.sync(result)); dispatcher.dispatchGroupModificationToOne(model, client -> client.sync(result));
} }
dispatcher.close();
}); });
return result; return result;
} }
@ -74,13 +75,14 @@ public class ScimEndpointConfigurationStorageProviderFactory
for (RealmModel realm : taskSession.realms().getRealmsStream().toList()) { for (RealmModel realm : taskSession.realms().getRealmsStream().toList()) {
KeycloakModelUtils.runJobInTransaction(factory, session -> { KeycloakModelUtils.runJobInTransaction(factory, session -> {
session.getContext().setRealm(realm); session.getContext().setRealm(realm);
ScimDispatcher dispatcher = ScimDispatcher.createForSession(session); ScimDispatcher dispatcher = new ScimDispatcher(session);
for (GroupModel group : session.groups().getGroupsStream(realm) for (GroupModel group : session.groups().getGroupsStream(realm)
.filter(x -> BooleanUtils.TRUE.equals(x.getFirstAttribute("scim-dirty"))).toList()) { .filter(x -> BooleanUtils.TRUE.equals(x.getFirstAttribute("scim-dirty"))).toList()) {
logger.infof("[SCIM] Dirty group : %s", group.getName()); logger.infof("[SCIM] Dirty group : %s", group.getName());
dispatcher.dispatchGroupModificationToAll(client -> client.replace(group)); dispatcher.dispatchGroupModificationToAll(client -> client.replace(group));
group.removeAttribute("scim-dirty"); group.removeAttribute("scim-dirty");
} }
dispatcher.close();
}); });
} }
}, Duration.ofSeconds(30).toMillis(), "scim-background"); }, Duration.ofSeconds(30).toMillis(), "scim-background");