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