tolerate provider failure

This commit is contained in:
Hugo Renard 2022-05-19 14:13:14 +02:00
parent 6900f4e647
commit 5ab3d960a8
Signed by: hougo
GPG key ID: 3A285FD470209C59
2 changed files with 22 additions and 20 deletions

View file

@ -2,13 +2,14 @@ package sh.libre.scim.core;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import sh.libre.scim.storage.ScimStorageProviderFactory; import sh.libre.scim.storage.ScimStorageProviderFactory;
public class ScimDispatcher { public class ScimDispatcher {
public static final String SCOPE_USER = "user"; public static final String SCOPE_USER = "user";
public static final String SCOPE_GROUP = "group"; public static final String SCOPE_GROUP = "group";
final private KeycloakSession session; final private KeycloakSession session;
final private Logger LOGGER = Logger.getLogger(ScimDispatcher.class); final private Logger LOGGER = Logger.getLogger(ScimDispatcher.class);
@ -20,16 +21,21 @@ public class ScimDispatcher {
public void run(String scope, Consumer<ScimClient> f) { public void run(String scope, Consumer<ScimClient> f) {
session.getContext().getRealm().getComponentsStream() session.getContext().getRealm().getComponentsStream()
.filter((m) -> { .filter((m) -> {
return ScimStorageProviderFactory.ID.equals(m.getProviderId()) && m.get("enabled").equals("true") && m.get("propagation-"+scope).equals("true"); return ScimStorageProviderFactory.ID.equals(m.getProviderId()) && m.get("enabled").equals("true")
&& m.get("propagation-" + scope).equals("true");
}) })
.forEach(m -> { .forEach(m -> runOne(m, f));
LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType()); }
var client = new ScimClient(m, session);
try { public void runOne(ComponentModel m, Consumer<ScimClient> f) {
f.accept(client); LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType());
} finally { var client = new ScimClient(m, session);
client.close(); try {
} f.accept(client);
}); } catch (Exception e) {
LOGGER.error(e);
} finally {
client.close();
}
} }
} }

View file

@ -12,7 +12,6 @@ import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.KeycloakSessionTask; import org.keycloak.models.KeycloakSessionTask;
import org.keycloak.models.RealmModel;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.provider.ProviderConfigurationBuilder; import org.keycloak.provider.ProviderConfigurationBuilder;
@ -22,7 +21,7 @@ import org.keycloak.storage.user.ImportSynchronization;
import org.keycloak.storage.user.SynchronizationResult; import org.keycloak.storage.user.SynchronizationResult;
import sh.libre.scim.core.GroupAdapter; import sh.libre.scim.core.GroupAdapter;
import sh.libre.scim.core.ScimClient; import sh.libre.scim.core.ScimDispatcher;
import sh.libre.scim.core.UserAdapter; import sh.libre.scim.core.UserAdapter;
public class ScimStorageProviderFactory public class ScimStorageProviderFactory
@ -125,16 +124,13 @@ public class ScimStorageProviderFactory
@Override @Override
public void run(KeycloakSession session) { public void run(KeycloakSession session) {
RealmModel realm = session.realms().getRealm(realmId); var dispatcher = new ScimDispatcher(session);
session.getContext().setRealm(realm);
var client = new ScimClient(model, session);
if (model.get("propagation-user").equals("true")) { if (model.get("propagation-user").equals("true")) {
client.sync(UserAdapter.class, result); dispatcher.runOne(model, (client) -> client.sync(UserAdapter.class, result));
} }
if (model.get("propagation-group").equals("true")) { if (model.get("propagation-group").equals("true")) {
client.sync(GroupAdapter.class, result); dispatcher.runOne(model, (client) -> client.sync(GroupAdapter.class, result));
} }
client.close();
} }
}); });