From cbb118c0132a90f5ea31b0b13c0cd846f8c12384 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Wed, 17 Mar 2021 10:45:04 -0300 Subject: [PATCH] [KEYCLOAK-17442] - Upgrade to Quarkus 1.12.2 --- .../quarkus/deployment/KeycloakProcessor.java | 3 +- quarkus/pom.xml | 12 ++--- .../ClientConnectionContextInjector.java | 47 +++++++++++++++++++ .../quarkus/KeycloakContextInjector.java | 47 +++++++++++++++++++ .../quarkus/QuarkusRequestFilter.java | 16 +++++-- ...ovider.java => ResteasyVertxProvider.java} | 14 +++++- .../org.keycloak.common.util.ResteasyProvider | 2 +- 7 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ClientConnectionContextInjector.java create mode 100644 quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/KeycloakContextInjector.java rename quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/{Resteasy4Provider.java => ResteasyVertxProvider.java} (76%) diff --git a/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java b/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java index 5a1c51b126..333042da02 100644 --- a/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java +++ b/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java @@ -17,6 +17,7 @@ package org.keycloak.quarkus.deployment; +import static java.util.Collections.emptyList; import static org.keycloak.configuration.Configuration.getPropertyNames; import static org.keycloak.configuration.Configuration.getRawValue; import static org.keycloak.representations.provider.ScriptProviderDescriptor.AUTHENTICATORS; @@ -147,7 +148,7 @@ class KeycloakProcessor { @Record(ExecutionTime.STATIC_INIT) @BuildStep void configureHibernate(KeycloakRecorder recorder, HibernateOrmConfig config, List descriptors) { - PersistenceUnitDescriptor unit = descriptors.get(0).asOutputPersistenceUnitDefinition().getActualHibernateDescriptor(); + PersistenceUnitDescriptor unit = descriptors.get(0).asOutputPersistenceUnitDefinition(emptyList()).getActualHibernateDescriptor(); unit.getProperties().setProperty(AvailableSettings.DIALECT, config.defaultPersistenceUnit.dialect.dialect.orElse(null)); unit.getProperties().setProperty(AvailableSettings.JPA_TRANSACTION_TYPE, PersistenceUnitTransactionType.JTA.name()); diff --git a/quarkus/pom.xml b/quarkus/pom.xml index b007a54d2f..62c9b1ee1d 100755 --- a/quarkus/pom.xml +++ b/quarkus/pom.xml @@ -31,13 +31,13 @@ pom - 1.10.0.CR1 - 4.5.8.Final - 2.11.3 + 1.12.2.Final + 4.5.9.Final + 2.12.1 ${jackson.version} - 5.4.23.Final - 8.0.22 - 4.5.2 + 5.4.28.Final + 8.0.23 + 4.6.1 1.27 3.0.0-M5 1.5.4.Final-format-001 diff --git a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ClientConnectionContextInjector.java b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ClientConnectionContextInjector.java new file mode 100644 index 0000000000..bec6e2788b --- /dev/null +++ b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ClientConnectionContextInjector.java @@ -0,0 +1,47 @@ +/* + * + * * Copyright 2021 Red Hat, Inc. and/or its affiliates + * * and other contributors as indicated by the @author tags. + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package org.keycloak.provider.quarkus; + +import javax.ws.rs.ext.Provider; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import org.jboss.resteasy.spi.ContextInjector; +import org.keycloak.common.ClientConnection; +import org.keycloak.common.util.Resteasy; + +/** + *

This {@link ContextInjector} allows injecting {@link ClientConnection} to JAX-RS resources. + * + *

Due to the latest changes in Quarkus, the context map is cleared prior to dispatching to JAX-RS resources, so we need + * to delegate to the {@link ResteasyVertxProvider} provider the lookup of Keycloak contextual objects. + * + * @see QuarkusRequestFilter + * @see ResteasyVertxProvider + * + * @author Pedro Igor + */ +@Provider +public class ClientConnectionContextInjector implements ContextInjector { + @Override + public ClientConnection resolve(Class rawType, Type genericType, Annotation[] annotations) { + return Resteasy.getContextData(ClientConnection.class); + } +} diff --git a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/KeycloakContextInjector.java b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/KeycloakContextInjector.java new file mode 100644 index 0000000000..35029f7425 --- /dev/null +++ b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/KeycloakContextInjector.java @@ -0,0 +1,47 @@ +/* + * + * * Copyright 2021 Red Hat, Inc. and/or its affiliates + * * and other contributors as indicated by the @author tags. + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package org.keycloak.provider.quarkus; + +import javax.ws.rs.ext.Provider; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import org.jboss.resteasy.spi.ContextInjector; +import org.keycloak.common.util.Resteasy; +import org.keycloak.models.KeycloakSession; + +/** + *

This {@link ContextInjector} allows injecting {@link KeycloakSession} to JAX-RS resources. + * + *

Due to the latest changes in Quarkus, the context map is cleared prior to dispatching to JAX-RS resources, so we need + * to delegate to the {@link ResteasyVertxProvider} provider the lookup of Keycloak contextual objects. + * + * @see QuarkusRequestFilter + * @see ResteasyVertxProvider + * + * @author Pedro Igor + */ +@Provider +public class KeycloakContextInjector implements ContextInjector { + @Override + public KeycloakSession resolve(Class rawType, Type genericType, Annotation[] annotations) { + return Resteasy.getContextData(KeycloakSession.class); + } +} diff --git a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/QuarkusRequestFilter.java b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/QuarkusRequestFilter.java index dbe032afba..ac874f1ab2 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/QuarkusRequestFilter.java +++ b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/QuarkusRequestFilter.java @@ -17,9 +17,8 @@ package org.keycloak.provider.quarkus; -import java.util.function.Predicate; - import org.keycloak.common.ClientConnection; +import org.keycloak.models.KeycloakSession; import org.keycloak.services.filters.AbstractRequestFilter; import io.vertx.core.AsyncResult; @@ -45,8 +44,12 @@ public class QuarkusRequestFilter extends AbstractRequestFilter implements Handl // our code should always be run as blocking until we don't provide a better support for running non-blocking code // in the event loop context.vertx().executeBlocking(promise -> { - filter(createClientConnection(context.request()), (session) -> { + ClientConnection connection = createClientConnection(context.request()); + + filter(connection, (session) -> { try { + configureContextualData(context, connection, session); + // we need to close the session before response is sent to the client, otherwise subsequent requests could // not get the latest state because the session from the previous request is still being closed // other methods from Vert.x to add a handler to the response works asynchronously @@ -62,6 +65,13 @@ public class QuarkusRequestFilter extends AbstractRequestFilter implements Handl }, false, EMPTY_RESULT); } + private void configureContextualData(RoutingContext context, ClientConnection connection, KeycloakSession session) { + // quarkus-resteasy changed and clears the context map before dispatching + // need to push keycloak contextual objects into the routing context for retrieving it later + context.data().put(KeycloakSession.class.getName(), session); + context.data().put(ClientConnection.class.getName(), connection); + } + @Override protected boolean isAutoClose() { return false; diff --git a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/Resteasy4Provider.java b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ResteasyVertxProvider.java similarity index 76% rename from quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/Resteasy4Provider.java rename to quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ResteasyVertxProvider.java index c7d7f290cb..d4310f91ec 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/Resteasy4Provider.java +++ b/quarkus/runtime/src/main/java/org/keycloak/provider/quarkus/ResteasyVertxProvider.java @@ -17,14 +17,24 @@ package org.keycloak.provider.quarkus; +import io.vertx.ext.web.RoutingContext; import org.jboss.resteasy.core.ResteasyContext; import org.keycloak.common.util.ResteasyProvider; -public class Resteasy4Provider implements ResteasyProvider { +/** + * TODO: we should probably rely on the vert.x routing context instead of resteasy context data + */ +public class ResteasyVertxProvider implements ResteasyProvider { @Override public R getContextData(Class type) { - return ResteasyContext.getContextData(type); + R data = ResteasyContext.getContextData(type); + + if (data == null) { + return (R) ResteasyContext.getContextData(RoutingContext.class).data().get(type.getName()); + } + + return data; } @Override diff --git a/quarkus/runtime/src/main/resources/META-INF/services/org.keycloak.common.util.ResteasyProvider b/quarkus/runtime/src/main/resources/META-INF/services/org.keycloak.common.util.ResteasyProvider index c28f7809e0..4e8e03fbe9 100644 --- a/quarkus/runtime/src/main/resources/META-INF/services/org.keycloak.common.util.ResteasyProvider +++ b/quarkus/runtime/src/main/resources/META-INF/services/org.keycloak.common.util.ResteasyProvider @@ -1 +1 @@ -org.keycloak.provider.quarkus.Resteasy4Provider \ No newline at end of file +org.keycloak.provider.quarkus.ResteasyVertxProvider \ No newline at end of file