8000 consolidating profile logic and moving reaug related logic by shawkins · Pull Request #38514 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

import org.keycloak.common.Profile;
import org.keycloak.common.util.NetworkUtils;
import org.keycloak.quarkus.runtime.cli.command.AbstractCommand;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource;

public final class Environment {

Expand All @@ -48,8 +46,6 @@ public final class Environment {
public static final String PROD_PROFILE_VALUE = "prod";
public static final String LAUNCH_MODE = "kc.launch.mode";

private static volatile AbstractCommand parsedCommand;

private Environment() {}

public static Boolean isRebuild() {
Expand Down Expand Up @@ -109,21 +105,6 @@ public static void setProfile(String profile) {
}
}

/**
* Update the profile settings based upon what was set in the system, environment, or optionally persistent values
*/
public static String updateProfile(boolean usePersistent) {
String profile = org.keycloak.common.util.Environment.getProfile();
if(profile == null && usePersistent) {
profile = PersistedConfigSource.getInstance().getValue(org.keycloak.common.util.Environment.PROFILE);
}
if (profile == null) {
profile = Environment.PROD_PROFILE_VALUE;
}
setProfile(profile);
return profile;
}

public static boolean isDevMode() {
if (org.keycloak.common.util.Environment.isDevMode()) {
return true;
Expand Down Expand Up @@ -247,21 +228,6 @@ public synchronized static Profile getCurrentOrCreateFeatureProfile() {
return profile;
}

/**
* Get parsed AbstractCommand we obtained from the CLI
*/
public static Optional<AbstractCommand> getParsedCommand() {
return Optional.ofNullable(parsedCommand);
}

public static boolean isParsedCommand(String commandName) {
return getParsedCommand().filter(f -> f.getName().equals(commandName)).isPresent();
}

public static void setParsedCommand(AbstractCommand command) {
Environment.parsedCommand = command;
}

public static void removeHomeDir() {
System.getProperties().remove(KC_HOME_DIR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@

import org.keycloak.infinispan.util.InfinispanUtils;
import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource;
import org.keycloak.quarkus.runtime.integration.jaxrs.QuarkusKeycloakApplication;

import io.quarkus.bootstrap.runner.RunnerClassLoader;
import io.quarkus.arc.Arc;
import io.quarkus.runtime.ApplicationLifecycleManager;
import io.quarkus.runtime.Quarkus;

import org.jboss.logging.Logger;
import org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler;
import org.keycloak.quarkus.runtime.cli.PropertyException;
import org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler;
import org.keycloak.quarkus.runtime.cli.Picocli;
import org.keycloak.common.Version;
import org.keycloak.quarkus.runtime.cli.command.AbstractNonServerCommand;
import org.keycloak.quarkus.runtime.cli.command.DryRunMixin;
import org.keycloak.quarkus.runtime.cli.command.Start;

Expand All @@ -54,6 +57,8 @@
@ApplicationScoped
public class KeycloakMain implements QuarkusApplication {

private static AbstractNonServerCommand COMMAND;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it needs to be AbstractNonServerCommand and not AbstractCommand?

What is the reason we have this only for non server commands?

public void onStart(QuarkusKeycloakApplication application) {}

We could at least improve the javadoc for that with some comments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it needs to be AbstractNonServerCommand and not AbstractCommand?

It's not something that AbstractCommand needs in general, but if you want to avoid the cast, it can be moved there.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave it as is, but I was wondering why we need to keep the reference to the AbstractNonServerCommand in the KeycloakMain - like why it needs to be the non-server command.

But if it's not suitable for all commands, I'm ok to have it like this.


static {
InfinispanUtils.configureVirtualThreads();
}
Expand Down Expand Up @@ -92,6 +97,9 @@ public static void main(String[] args, Picocli picocli) {
}

if (cliArgs.isEmpty()) {
if (Environment.isRebuildCheck()) {
return; // nothing to do - not currently caught by the shell scripts
}
cliArgs = new ArrayList<>(cliArgs);
// default to show help message
cliArgs.add("-h");
Expand Down Expand Up @@ -128,7 +136,8 @@ private static boolean isFastStart(List<String> cliArgs) {
return cliArgs.size() == 2 && cliArgs.get(0).equals(Start.NAME) && cliArgs.stream().anyMatch(OPTIMIZED_BUILD_OPTION_LONG::equals);
}

public static void start(Picocli picocli, ExecutionExceptionHandler errorHandler) {
public static void start(Picocli picocli, AbstractNonServerCommand command, ExecutionExceptionHandler errorHandler) {
COMMAND = command; // it would be nice to not do this statically - start quarkus with an instance of KeycloakMain, rather than a class for example
try {
Quarkus.run(KeycloakMain.class, (exitCode, cause) -> {
if (cause != null) {
Expand All @@ -151,6 +160,10 @@ public static void start(Picocli picocli, ExecutionExceptionHandler errorHandler
*/
@Override
public int run(String... args) throws Exception {
if (COMMAND != null) {
QuarkusKeycloakApplication application = Arc.container().instance(QuarkusKeycloakApplication.class).get();
COMMAND.onStart(application);
}
if (isTestLaunchMode() || isNonServerMode()) {
// in test mode we exit immediately
// we should be managing this behavior more dynamically depending on the tests requirements (short/long lived)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@

import static java.lang.String.format;
import static org.keycloak.quarkus.runtime.Environment.getProviderFiles;
import static org.keycloak.quarkus.runtime.Environment.isDevMode;
import static org.keycloak.quarkus.runtime.Environment.isRebuild;
import static org.keycloak.quarkus.runtime.Environment.isRebuildCheck;
import static org.keycloak.quarkus.runtime.Environment.isRebuilt;
import static org.keycloak.quarkus.runtime.cli.OptionRenderer.decorateDuplicitOptionName;
import static org.keycloak.quarkus.runtime.cli.command.AbstractStartCommand.OPTIMIZED_BUILD_OPTION_LONG;
import static org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource.parseConfigArgs;
import static org.keycloak.quarkus.runtime.configuration.Configuration.isUserModifiable;
import static org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX;
import static org.keycloak.quarkus.runtime.configuration.mappers.PropertyMappers.maskValue;
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_COMMAND_LIST;

import java.io.File;
Expand Down Expand Up @@ -57,12 +54,9 @@
import org.keycloak.quarkus.runtime.KeycloakMain;
import org.keycloak.quarkus.runtime.Messages;
import org.keycloak.quarkus.runtime.cli.command.AbstractCommand;
import org.keycloak.quarkus.runtime.cli.command.AbstractNonServerCommand;
import org.keycloak.quarkus.runtime.cli.command.Build;
import org.keycloak.quarkus.runtime.cli.command.Completion;
import org.keycloak.quarkus.runtime.cli.command.Main;
import org.keycloak.quarkus.runtime.cli.command.ShowConfig;
import org.keycloak.quarkus.runtime.cli.command.StartDev;
import org.keycloak.quarkus.runtime.cli.command.UpdateCompatibility;
import org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.DisabledMappersInterceptor;
Expand Down Expand Up @@ -105,6 +99,7 @@ private static class IncludeOptions {
private final ExecutionExceptionHandler errorHandler = new ExecutionExceptionHandler();
private Set<PropertyMapper<?>> allowedMappers;
private final List<String> unrecognizedArgs = new ArrayList<>();
private Optional<AbstractCommand> parsedCommand = Optional.empty();

public void parseAndRun(List<String> cliArgs) {
// perform two passes over the cli args. First without option validation to determine the current command, then with option validation enabled
Expand All @@ -118,17 +113,7 @@ public void parseAndRun(List<String> cliArgs) {
// recreate the command specifically for the current
cmd = createCommandLineForCommand(cliArgs, commandLineList);

int exitCode;
if (isRebuildCheck()) {
CommandLine currentCommand = null;
if (commandLineList.size() > 1) {
currentCommand = commandLineList.get(commandLineList.size() - 1);
}
exitCode = runReAugmentationIfNeeded(cliArgs, cmd, currentCommand);
} else {
PropertyMappers.sanitizeDisabledMappers();
exitCode = cmd.execute(argArray);
}
int exitCode = cmd.execute(argArray);

exit(exitCode);
} catch (ParameterException parEx) {
Expand Down Expand Up @@ -168,15 +153,16 @@ public <T> T set(T value) {
addHelp(currentSpec);
}

AbstractCommand currentCommand = null;
if (currentSpec != null) {
CommandLine commandLine = currentSpec.commandLine();
addCommandOptions(cliArgs, commandLine);

if (commandLine != null && commandLine.getCommand() instanceof AbstractCommand ac) {
// set current parsed command
Environment.setParsedCommand(ac);
currentCommand = ac;
}
}
initConfig(currentCommand);

if (isRebuildCheck()) {
// build command should be available when running re-aug
Expand All @@ -185,6 +171,10 @@ public <T> T set(T value) {
});
}

public Optional<AbstractCommand> getParsedCommand() {
return parsedCommand;
}

private void catchParameterException(ParameterException parEx, CommandLine cmd, String[] args) {
int exitCode;
try {
Expand All @@ -205,122 +195,6 @@ public void exit(int exitCode) {
System.exit(exitCode);
}

private int runReAugmentationIfNeeded(List<String> cliArgs, CommandLine cmd, CommandLine currentCommand) {
int exitCode = 0;

if (currentCommand == null) {
return exitCode; // possible if using --version or the user made a mistake
}

String currentCommandName = currentCommand.getCommandName();

if (shouldSkipRebuild(cliArgs, currentCommandName)) {
return exitCode;
}

// TODO: ensure that the config has not yet been initialized
// - there's currently no good way to do that directly on ConfigProviderResolver
initProfile(cliArgs, currentCommandName);

if (requiresReAugmentation(currentCommand)) {
PropertyMappers.sanitizeDisabledMappers();
exitCode = runReAugmentation(cliArgs, cmd);
}

return exitCode;
}

protected void initProfile(List<String> cliArgs, String currentCommandName) {
if (currentCommandName.equals(StartDev.NAME)) {
// force the server image to be set with the dev profile
Environment.forceDevProfile();
} else {
Environment.updateProfile(false);

// override from the cli if specified
parseConfigArgs(cliArgs, (k, v) -> {
if (k.equals(Main.PROFILE_SHORT_NAME) || k.equals(Main.PROFILE_LONG_NAME)) {
Environment.setProfile(v);
}
}, ignored -> {});
}
}

private static boolean shouldSkipRebuild(List<String> cliArgs, String currentCommandName) {
return cliArgs.contains("--help")
|| cliArgs.contains("-h")
|| cliArgs.contains("--help-all")
|| currentCommandName.equals(Build.NAME)
|| currentCommandName.equals(ShowConfig.NAME)
|| currentCommandName.equals(Completion.NAME)
|| currentCommandName.equals(UpdateCompatibility.NAME);
}

private static boolean requiresReAugmentation(CommandLine cmdCommand) {
Map<String, String> rawPersistedProperties = Configuration.getRawPersistedProperties();
if (rawPersistedProperties.isEmpty()) {
return true; // no build yet
}
var current = getNonPersistedBuildTimeOptions();

// everything but the optimized value must match
String key = Configuration.KC_OPTIMIZED;
Optional.ofNullable(rawPersistedProperties.get(key)).ifPresentOrElse(value -> current.put(key, value), () -> current.remove(key));
return !rawPersistedProperties.equals(current);
}

/**
* checks the raw cli input for possible credentials / properties which should be masked,
* and masks them.
* @return a list of potentially masked properties in CLI format, e.g. `--db-password=*******`
* instead of the actual passwords value.
*/
private static List<String> getSanitizedRuntimeCliOptions() {
List<String> properties = new ArrayList<>();

parseConfigArgs(ConfigArgsConfigSource.getAllCliArgs(), (key, value) -> {
PropertyMapper<?> mapper = PropertyMappers.getMapperByCliKey(key);

if (mapper == null || mapper.isRunTime()) {
properties.add(key + "=" + maskValue(value, mapper));
}
}, properties::add);

return properties;
}

private static int runReAugmentation(List<String> cliArgs, CommandLine cmd) {
if (cmd == null) {
throw new IllegalStateException("CommandLine is null when trying to run re-augmentation. (CLI args: '%s')".formatted(String.join(", ", cliArgs)));
}

if (!isDevMode()) {
cmd.getOut().println("Changes detected in configuration. Updating the server image.");
if (Configuration.isOptimized()) {
checkChangesInBuildOptionsDuringAutoBuild(cmd.getOut());
}
}

List<String> configArgsList = new ArrayList<>();
configArgsList.add(Build.NAME);
parseConfigArgs(cliArgs, (k, v) -> {
PropertyMapper<?> mapper = PropertyMappers.getMapperByCliKey(k);

if (mapper != null && mapper.isBuildTime()) {
configArgsList.add(k + "=" + v);
}
}, ignored -> {});

cmd = cmd.setUnmatchedArgumentsAllowed(true);
int exitCode = cmd.execute(configArgsList.toArray(new String[0]));

if(!isDevMode() && exitCode == cmd.getCommandSpec().exitCodeOnSuccess()) {
cmd.getOut().printf("Next time you run the server, just run:%n%n\t%s %s %s%n%n", Environment.getCommand(), String.join(" ", getSanitizedRuntimeCliOptions()), OPTIMIZED_BUILD_OPTION_LONG);
}

return exitCode;
}

private static boolean wasBuildEverRun() {
return !Configuration.getRawPersistedProperties().isEmpty();
}
Expand Down Expand Up @@ -623,6 +497,11 @@ public void info(String text) {
getOutWriter().println(defaultColorScheme.apply("INFO: ", Arrays.asList(Style.fg_green, Style.bold)) + text);
}

public void error(String text) {
ColorScheme defaultColorScheme = picocli.CommandLine.Help.defaultColorScheme(Help.Ansi.AUTO);
getErrWriter().println(defaultColorScheme.apply(text, Arrays.asList(Style.fg_red, Style.bold)));
}

private static void warn(String text, PrintWriter outwriter) {
ColorScheme defaultColorScheme = picocli.CommandLine.Help.defaultColorScheme(Help.Ansi.AUTO);
outwriter.println(defaultColorScheme.apply("WARNING: ", Arrays.asList(Style.fg_yellow, Style.bold)) + text);
Expand Down Expand Up @@ -922,8 +801,8 @@ private static String getDecoratedOptionDescription(PropertyMapper<?> mapper) {
return transformedDesc.toString();
}

public static void println(CommandLine cmd, String message) {
cmd.getOut().println(message);
public void println(String message) {
getOutWriter().println(message);
}

public static List<String> parseArgs(String[] rawArgs) throws PropertyException {
Expand Down Expand Up @@ -953,7 +832,7 @@ public static List<String> parseArgs(String[] rawArgs) throws PropertyException
return args;
}

private static void checkChangesInBuildOptionsDuringAutoBuild(PrintWriter out) {
public static void checkChangesInBuildOptionsDuringAutoBuild(PrintWriter out) {
StringBuilder options = new StringBuilder();

checkChangesInBuildOptions((key, oldValue, newValue) -> optionChanged(options, key, oldValue, newValue));
Expand Down Expand Up @@ -1011,11 +890,26 @@ private static boolean isIgnoredPersistedOption(String key) {
}

public void start() {
KeycloakMain.start(this, errorHandler);
KeycloakMain.start(this, (AbstractNonServerCommand) this.getParsedCommand()
.filter(AbstractNonServerCommand.class::isInstance).orElse(null), this.errorHandler);
}

public void build() throws Throwable {
QuarkusEntryPoint.main();
}

public void initConfig(AbstractCommand command) {
if (Configuration.isInitialized()) {
throw new IllegalStateException("Config should not be initialized until profile is determined");
}
this.parsedCommand = Optional.ofNullable(command);

String profile = parsedCommand.map(AbstractCommand::getInitProfile)
.orElseGet(() -> Optional.ofNullable(org.keycloak.common.util.Environment.getProfile())
.orElse(Environment.PROD_PROFILE_VALUE));

Environment.setProfile(profile);
parsedCommand.ifPresent(PropertyMappers::sanitizeDisabledMappers);
}

}
Loading
0