Skip to content

Commit

Permalink
[KEYCLOAK-19306] - Automatic re-augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor committed Sep 29, 2021
1 parent 15b3af7 commit 36706c7
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 240 deletions.
13 changes: 11 additions & 2 deletions distribution/server-x-dist/src/main/content/bin/kc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DEBUG_MODE="${DEBUG:-false}"
DEBUG_PORT="${DEBUG_PORT:-8787}"

CONFIG_ARGS=${CONFIG_ARGS:-""}
IS_CONFIGURE="false"
IS_DEV_MODE="false"

while [ "$#" -gt 0 ]
do
Expand All @@ -47,6 +47,9 @@ do
;;
*)
if [[ $1 = --* || ! $1 =~ ^-.* ]]; then
if [ "$1" = "start-dev" ]; then
IS_DEV_MODE=true
fi
CONFIG_ARGS="$CONFIG_ARGS $1"
else
SERVER_OPTS="$SERVER_OPTS $1"
Expand Down Expand Up @@ -77,4 +80,10 @@ fi

CLASSPATH_OPTS="$DIRNAME/../lib/quarkus-run.jar"

exec java $JAVA_OPTS $SERVER_OPTS -cp $CLASSPATH_OPTS io.quarkus.bootstrap.runner.QuarkusEntryPoint ${CONFIG_ARGS#?}
JAVA_RUN_OPTS="$JAVA_OPTS $SERVER_OPTS -cp $CLASSPATH_OPTS io.quarkus.bootstrap.runner.QuarkusEntryPoint ${CONFIG_ARGS#?}"

if [ "$IS_DEV_MODE" = "true" ]; then
java -Dkc.dev.rebuild=true $JAVA_RUN_OPTS
fi

exec java $JAVA_RUN_OPTS

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import static java.util.Collections.emptyList;
import static org.keycloak.configuration.Configuration.getPropertyNames;
import static org.keycloak.configuration.Configuration.getRawValue;
import static org.keycloak.connections.jpa.QuarkusJpaConnectionProviderFactory.QUERY_PROPERTY_PREFIX;
import static org.keycloak.connections.jpa.util.JpaUtils.loadSpecificNamedQueries;
import static org.keycloak.configuration.MicroProfileConfigProvider.NS_KEYCLOAK;
import static org.keycloak.representations.provider.ScriptProviderDescriptor.AUTHENTICATORS;
import static org.keycloak.representations.provider.ScriptProviderDescriptor.MAPPERS;
import static org.keycloak.representations.provider.ScriptProviderDescriptor.POLICIES;
import static org.keycloak.util.Environment.CLI_ARGS;
import static org.keycloak.util.Environment.getProviderFiles;

import javax.persistence.spi.PersistenceUnitTransactionType;
import java.io.File;
Expand Down Expand Up @@ -78,10 +80,8 @@
import org.keycloak.configuration.KeycloakConfigSourceProvider;
import org.keycloak.configuration.MicroProfileConfigProvider;
import org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory;
import org.keycloak.connections.jpa.QuarkusJpaConnectionProviderFactory;
import org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProviderFactory;
import org.keycloak.connections.jpa.updater.liquibase.conn.DefaultLiquibaseConnectionProvider;
import org.keycloak.connections.jpa.util.JpaUtils;
import org.keycloak.protocol.ProtocolMapperSpi;
import org.keycloak.protocol.oidc.mappers.DeployedScriptOIDCProtocolMapper;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
Expand All @@ -106,7 +106,6 @@
import org.keycloak.services.NotFoundHandler;
import org.keycloak.services.ServicesLogger;
import org.keycloak.services.health.KeycloakMetricsHandler;
import org.keycloak.services.resources.KeycloakApplication;
import org.keycloak.transaction.JBossJtaTransactionManagerLookup;
import org.keycloak.util.Environment;
import org.keycloak.util.JsonSerialization;
Expand Down Expand Up @@ -226,7 +225,7 @@ void configureConfigSources(BuildProducer<StaticInitConfigSourceProviderBuildIte
* @param recorder the recorder
*/
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
@BuildStep(onlyIf = isReAugmentation.class)
void setBuildTimeProperties(KeycloakRecorder recorder) {
Properties properties = new Properties();

Expand All @@ -242,6 +241,10 @@ void setBuildTimeProperties(KeycloakRecorder recorder) {
}
}

for (File jar : getProviderFiles()) {
properties.put(String.format("kc.provider.file.%s.last-modified", jar.getName()), String.valueOf(jar.lastModified()));
}

File file = KeycloakConfigSourceProvider.getPersistedConfigFile().toFile();

if (file.exists()) {
Expand All @@ -253,15 +256,11 @@ void setBuildTimeProperties(KeycloakRecorder recorder) {
} catch (Exception e) {
throw new RuntimeException("Failed to generate persisted.properties file", e);
}

recorder.validateAndSetBuildTimeProperties(Environment.isRebuild(), getRawValue("kc.config.args"));

recorder.showConfig();
}

private boolean isNotPersistentProperty(String name) {
// these properties are ignored from the build time properties as they are runtime-specific
return !name.startsWith("kc") || "kc.home.dir".equals(name) || "kc.config.args".equals(name);
return !name.startsWith(NS_KEYCLOAK) || "kc.home.dir".equals(name) || CLI_ARGS.equals(name);
}

/**
Expand Down Expand Up @@ -343,14 +342,14 @@ void configureDevMode(BuildProducer<HotDeploymentWatchedFileBuildItem> hotFiles)
private Map<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> loadFactories(
Map<String, ProviderFactory> preConfiguredProviders) {
Config.init(new MicroProfileConfigProvider());
BuildClassLoader providerClassLoader = new BuildClassLoader();
ProviderManager pm = new ProviderManager(KeycloakDeploymentInfo.create().services(), providerClassLoader);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ProviderManager pm = new ProviderManager(KeycloakDeploymentInfo.create().services(), classLoader);
Map<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> factories = new HashMap<>();

for (Spi spi : pm.loadSpis()) {
Map<Class<? extends Provider>, Map<String, ProviderFactory>> providers = new HashMap<>();
List<ProviderFactory> loadedFactories = new ArrayList<>(pm.load(spi));
Map<String, ProviderFactory> deployedScriptProviders = loadDeployedScriptProviders(providerClassLoader, spi);
Map<String, ProviderFactory> deployedScriptProviders = loadDeployedScriptProviders(classLoader, spi);

loadedFactories.addAll(deployedScriptProviders.values());
preConfiguredProviders.putAll(deployedScriptProviders);
Expand Down Expand Up @@ -384,12 +383,12 @@ private Map<Spi, Map<Class<? extends Provider>, Map<String, ProviderFactory>>> l
return factories;
}

private Map<String, ProviderFactory> loadDeployedScriptProviders(BuildClassLoader providerClassLoader, Spi spi) {
private Map<String, ProviderFactory> loadDeployedScriptProviders(ClassLoader classLoader, Spi spi) {
Map<String, ProviderFactory> providers = new HashMap<>();

if (supportsDeployeableScripts(spi)) {
try {
Enumeration<URL> urls = providerClassLoader.getResources(KEYCLOAK_SCRIPTS_JSON_PATH);
Enumeration<URL> urls = classLoader.getResources(KEYCLOAK_SCRIPTS_JSON_PATH);

while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.deployment;

import java.util.function.BooleanSupplier;
import org.keycloak.util.Environment;

public class isReAugmentation implements BooleanSupplier {

@Override
public boolean getAsBoolean() {
return Environment.isRebuild();
}

}
Loading

0 comments on commit 36706c7

Please sign in to comment.