Added noop event provider to avoid error for not registered event listener and avoid search in master realm if current realm is already the master

This commit is contained in:
Mauro Mugnaini 2021-05-21 19:04:25 +02:00
parent 44a75b2aff
commit 32dd4f7387
2 changed files with 71 additions and 18 deletions

View File

@ -0,0 +1,48 @@
package org.gcube.keycloak.event;
import java.util.EventListener;
import org.gcube.event.publisher.AbstractEventPublisher;
import org.gcube.event.publisher.EventSender;
import org.keycloak.events.Event;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.admin.AdminEvent;
/**
* Added to avoid errors for not configured provider also in realm where event listener is not configured (KC bug?)
*
* @author <a href="mailto:mauro.mugnaini@nubisware.com">Mauro Mugnaini</a>
*
*/
public class NoOpEventPublisherProvider extends AbstractEventPublisher implements EventListenerProvider, EventListener {
public NoOpEventPublisherProvider() {
super();
}
@Override
public void close() {
}
@Override
public void onEvent(Event event) {
// Nothing to do
}
@Override
public void onEvent(AdminEvent event, boolean includeRepresentation) {
// Nothing to do
}
@Override
protected EventSender createEventSender() {
return new EventSender() {
@Override
public void send(org.gcube.event.publisher.Event event) {
// Nothing to do
}
};
}
}

View File

@ -5,6 +5,7 @@ import java.net.URL;
import org.jboss.logging.Logger;
import org.keycloak.Config.Scope;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventListenerProviderFactory;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
@ -30,7 +31,7 @@ public class OrchestratorEventPublisherProviderFactory implements EventListenerP
public static String KEYCLOAK_CLIENT_SECRET;
protected Long lastEndpointCheck = new Long(0);
protected OrchestratorEventPublisherProvider oepp;
protected EventListenerProvider oepp;
public OrchestratorEventPublisherProviderFactory() {
logger.info("New OrchestratorEventPublisherProviderFactory has been created");
@ -41,7 +42,7 @@ public class OrchestratorEventPublisherProviderFactory implements EventListenerP
}
@Override
public synchronized OrchestratorEventPublisherProvider create(KeycloakSession keycloakSession) {
public synchronized EventListenerProvider create(KeycloakSession keycloakSession) {
Long now = System.currentTimeMillis();
Long elapsed = now - lastEndpointCheck;
if (oepp == null || elapsed > CHECK_DELAY) {
@ -55,13 +56,13 @@ public class OrchestratorEventPublisherProviderFactory implements EventListenerP
} catch (MalformedURLException e) {
logger.errorf("Can't create new orchestrator endpoint address: %s", orchestratorClient.getBaseUrl(),
e);
oepp = null;
return null;
oepp = new NoOpEventPublisherProvider();
return oepp;
}
} else {
logger.debugf("Can't go ahead without a configured '%f' client", ORCHESTRATOR_CLIENT_ID);
oepp = null;
return null;
return oepp;
}
ClientModel keycloakClient = getClientInActualOrMasterRealm(keycloakSession, KEYCLOAK_CLIENT_ID);
URL newKeycloakEndpoint;
@ -75,13 +76,13 @@ public class OrchestratorEventPublisherProviderFactory implements EventListenerP
keycloakClientSecret = keycloakClient.getSecret();
} catch (MalformedURLException e) {
logger.errorf("Can't create new keycloak token address: %s", keycloakClient.getBaseUrl(), e);
oepp = null;
return null;
oepp = new NoOpEventPublisherProvider();
return oepp;
}
} else {
logger.debugf("Can't go ahead without a configured '%f' client", KEYCLOAK_CLIENT_ID);
oepp = null;
return null;
oepp = new NoOpEventPublisherProvider();
return oepp;
}
if (oepp == null || !newOrchestratorEndpoint.equals(ORCHESTRATOR_ENDPOINT)
@ -106,17 +107,21 @@ public class OrchestratorEventPublisherProviderFactory implements EventListenerP
protected ClientModel getClientInActualOrMasterRealm(KeycloakSession keycloakSession, String clientId) {
logger.debug("Getting actual realm from session's context");
RealmModel realm = keycloakSession.getContext().getRealm();
logger.debugf("Trying getting '%s' client in current realm '%s'", clientId, realm.getName());
logger.debugf("Trying getting '%s' client in current realm: '%s'", clientId, realm.getName());
ClientModel client = realm.getClientByClientId(clientId);
if (client == null) {
logger.debugf("Not found. Now trying getting '%s' in '%s' realm", clientId, MASTER_REALM_NAME);
realm = keycloakSession.realms().getRealmByName(MASTER_REALM_NAME);
client = realm.getClientByClientId(clientId);
if (client == null) {
logger.warnf("Cannot find '%s' client not even in '%s' realm", clientId, realm.getName(),
MASTER_REALM_NAME);
return null;
if (!MASTER_REALM_NAME.equals(realm.getName())) {
logger.debugf("Not found. Now trying getting '%s' in '%s' realm", clientId, MASTER_REALM_NAME);
realm = keycloakSession.realms().getRealmByName(MASTER_REALM_NAME);
client = realm.getClientByClientId(clientId);
if (client == null) {
logger.warnf("Cannot find '%s' client not even in '%s' realm", clientId, realm.getName(),
MASTER_REALM_NAME);
return null;
}
} else {
logger.trace("Not found.");
}
}
logger.debugf("Client '%f' found", clientId);