Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing unnecessary code paths during startup #10131

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions quarkus/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 78,6 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-metrics-deployment</artifactId>
</dependency>
<!-- Cross-DC and ISPN client dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-infinispan-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-sasl-gssapi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-sasl-gs2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.vault</groupId>
<artifactId>quarkus-vault-deployment</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions quarkus/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 80,8 @@
<artifactId>quarkus-smallrye-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-infinispan-client</artifactId>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.vault</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,42 @@
/*
* 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.quarkus.runtime;

import java.io.IOException;
import java.net.URL;
import java.sql.Driver;
import java.util.Collections;
import java.util.Enumeration;

public class KeycloakClassLoader extends ClassLoader {

KeycloakClassLoader() {
super(Thread.currentThread().getContextClassLoader());
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
// drivers are going to be loaded lazily, and we avoid loading all available drivers
// see https://github.com/quarkusio/quarkus/pull/7089
if (name.contains(Driver.class.getName())) {
return Collections.emptyEnumeration();
}

return super.getResources(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 50,6 @@
@ApplicationScoped
public class KeycloakMain implements QuarkusApplication {

private static final Logger LOGGER = Logger.getLogger(KeycloakMain.class);

public static void main(String[] args) {
System.setProperty("kc.version", Version.VERSION_KEYCLOAK);
List<String> cliArgs = Picocli.parseArgs(args);
Expand Down Expand Up @@ -80,7 78,11 @@ public static void main(String[] args) {
}

public static void start(ExecutionExceptionHandler errorHandler, PrintWriter errStream) {
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();

try {
Thread.currentThread().setContextClassLoader(new KeycloakClassLoader());

Quarkus.run(KeycloakMain.class, (exitCode, cause) -> {
if (cause != null) {
errorHandler.error(errStream,
Expand All @@ -98,6 100,8 @@ public static void start(ExecutionExceptionHandler errorHandler, PrintWriter err
errorHandler.error(errStream,
String.format("Unexpected error when starting the server in (%s) mode", getKeycloakModeFromProfile(getProfileOrDefault("prod"))),
cause.getCause());
} finally {
Thread.currentThread().setContextClassLoader(originalCl);
}
}

Expand All @@ -107,7 111,7 @@ public static void start(ExecutionExceptionHandler errorHandler, PrintWriter err
@Override
public int run(String... args) throws Exception {
if (isDevProfile()) {
LOGGER.warnf("Running the server in development mode. DO NOT use this configuration in production.");
Logger.getLogger(KeycloakMain.class).warnf("Running the server in development mode. DO NOT use this configuration in production.");
}

int exitCode = ApplicationLifecycleManager.getExitCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 62,7 @@ public void configSessionFactory(

public RuntimeValue<CacheManagerFactory> createCacheInitializer(String config, ShutdownContext shutdownContext) {
try {
ConfigurationBuilderHolder builder = new ParserRegistry().parse(config);

if (builder.getNamedConfigurationBuilders().get("sessions").clustering().cacheMode().isClustered()) {
configureTransportStack(builder);
}

// For Infinispan 10, we go with the JBoss marshalling.
// TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
// See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details
builder.getGlobalConfigurationBuilder().serialization().marshaller(new JBossUserMarshaller());
CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(builder);
CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config);

shutdownContext.addShutdownTask(new Runnable() {
@Override
Expand All @@ -91,15 81,6 @@ public void run() {
}
}

private void configureTransportStack(ConfigurationBuilderHolder builder) {
String transportStack = Configuration.getRawValue("kc.cache-stack");

if (transportStack != null) {
builder.getGlobalConfigurationBuilder().transport().defaultTransport()
.addProperty("configurationFile", "default-configs/default-jgroups-" transportStack ".xml");
}
}

public void registerShutdownHook(ShutdownContext shutdownContext) {
shutdownContext.addShutdownTask(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 122,11 @@ private void addSpecificNamedQueries(KeycloakSession session, Connection connect
try {
Map<String, Object> unitProperties = emf.getProperties();

unitProperties.entrySet().stream()
.filter(entry -> entry.getKey().startsWith(QUERY_PROPERTY_PREFIX))
.forEach(entry -> configureNamedQuery(entry.getKey().substring(QUERY_PROPERTY_PREFIX.length()), entry.getValue().toString(), em));
for (Map.Entry<String, Object> entry : unitProperties.entrySet()) {
if (entry.getKey().startsWith(QUERY_PROPERTY_PREFIX)) {
configureNamedQuery(entry.getKey().substring(QUERY_PROPERTY_PREFIX.length()), entry.getValue().toString(), em);
}
}
} finally {
JpaUtils.closeEntityManager(em);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 23,21 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.jboss.marshalling.core.JBossUserMarshaller;
import org.infinispan.manager.DefaultCacheManager;
import org.jboss.logging.Logger;
import org.keycloak.quarkus.runtime.configuration.Configuration;

public class CacheManagerFactory {

private ConfigurationBuilderHolder config;
private String config;
private DefaultCacheManager cacheManager;
private Future<DefaultCacheManager> cacheManagerFuture;
private ExecutorService executor;
private boolean initialized;

public CacheManagerFactory(ConfigurationBuilderHolder config) {
public CacheManagerFactory(String config) {
this.config = config;
this.executor = createThreadPool();
this.cacheManagerFuture = executor.submit(this::startCacheManager);
Expand Down Expand Up @@ -69,7 72,18 @@ public Thread newThread(Runnable r) {
}

private DefaultCacheManager startCacheManager() {
return new DefaultCacheManager(config, isStartEagerly());
ConfigurationBuilderHolder builder = new ParserRegistry().parse(config);

if (builder.getNamedConfigurationBuilders().get("sessions").clustering().cacheMode().isClustered()) {
configureTransportStack(builder);
}

// For Infinispan 10, we go with the JBoss marshalling.
// TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
// See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details
builder.getGlobalConfigurationBuilder().serialization().marshaller(new JBossUserMarshaller());

return new DefaultCacheManager(builder, isStartEagerly());
}

private boolean isStartEagerly() {
Expand Down Expand Up @@ -101,4 115,13 @@ private void shutdownThreadPool() {
}
}
}

private void configureTransportStack(ConfigurationBuilderHolder builder) {
String transportStack = Configuration.getRawValue("kc.cache-stack");

if (transportStack != null) {
builder.getGlobalConfigurationBuilder().transport().defaultTransport()
.addProperty("configurationFile", "default-configs/default-jgroups-" transportStack ".xml");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 16,30 @@

package org.keycloak.credential;

import org.keycloak.Config;
import org.keycloak.common.Profile;
import org.keycloak.models.KeycloakSession;

import com.webauthn4j.converter.util.ObjectConverter;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.provider.EnvironmentDependentProviderFactory;

public class WebAuthnCredentialProviderFactory implements CredentialProviderFactory<WebAuthnCredentialProvider>, EnvironmentDependentProviderFactory {

public static final String PROVIDER_ID = "keycloak-webauthn";

private static ObjectConverter converter = new ObjectConverter();
private ObjectConverter converter;

@Override
public CredentialProvider create(KeycloakSession session) {
return new WebAuthnCredentialProvider(session, converter);
}

@Override
public void init(Config.Scope config) {
converter = new ObjectConverter();
}

@Override
public String getId() {
return PROVIDER_ID;
Expand Down