8000 fix: warn instead of an error if optimized provider timestamps change by shawkins · Pull Request #41798 · 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 @@ -86,6 +86,8 @@

public class Picocli {

static final String PROVIDER_TIMESTAMP_ERROR = "A provider JAR was updated since the last build, please rebuild for this to be fully utilized.";
static final String PROVIDER_TIMESTAMP_WARNING = "A provider jar has a different timestamp than when the optimized container image was created. If you are changing provider jars after the build, you must run another build to properly account for those modifications.";
static final String KC_PROVIDER_FILE_PREFIX = "kc.provider.file.";
public static final String ARG_PREFIX = "--";
public static final String ARG_SHORT_PREFIX = "-";
Expand All @@ -100,6 +102,7 @@ private static class IncludeOptions {
private Set<PropertyMapper<?>> allowedMappers;
private final List<String> unrecognizedArgs = new ArrayList<>();
private Optional<AbstractCommand> parsedCommand = Optional.empty();
private boolean warnedTimestampChanged;

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 Down Expand Up @@ -234,8 +237,19 @@ public void validateConfig(List<String> cliArgs, AbstractCommand abstractCommand
// we have to ignore things like the profile properties because the commands set them at runtime
checkChangesInBuildOptions((key, oldValue, newValue) -> {
if (key.startsWith(KC_PROVIDER_FILE_PREFIX)) {
if (timestampChanged(oldValue, newValue)) {
throw new PropertyException("A provider JAR was updated since the last build, please rebuild for this to be fully utilized.");
boolean changed = false;
if (newValue == null || oldValue == null) {
changed = true;
} else if (!warnedTimestampChanged && timestampChanged(oldValue, newValue)) {
if (Configuration.getOptionalBooleanKcValue("run-in-container").orElse(false)) {
warnedTimestampChanged = true;
warn(PROVIDER_TIMESTAMP_WARNING);
} else {
changed = true;
}
}
if (changed) {
throw new PropertyException(PROVIDER_TIMESTAMP_ERROR);
}
} else if (newValue != null && !isIgnoredPersistedOption(key)
&& isUserModifiable(Configuration.getConfigValue(key))
Expand Down Expand Up @@ -352,13 +366,10 @@ && isUserModifiable(Configuration.getConfigValue(key))
}

static boolean timestampChanged(String oldValue, String newValue) {
if (newValue != null && oldValue != null) {
long longNewValue = Long.valueOf(newValue);
long longOldValue = Long.valueOf(oldValue);
// docker commonly truncates to the second at runtime, so we'll allow that special case
return ((longNewValue / 1000) * 1000) != longNewValue || ((longOldValue / 1000) * 1000) != longNewValue;
}
return true;
long longNewValue = Long.valueOf(newValue);
long longOldValue = Long.valueOf(oldValue);
// docker commonly truncates to the second at runtime, so we'll allow that special case
return ((longNewValue / 1000) * 1000) != longNewValue || ((longOldValue / 1000) * 1000) != longNewValue;
}

private void validateProperty(AbstractCommand abstractCommand, IncludeOptions options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.keycloak.quarkus.runtime.configuration.AbstractConfigurationTest;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.KeycloakConfigSourceProvider;
import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource;

import io.smallrye.config.SmallRyeConfig;
import picocli.CommandLine;
Expand Down Expand Up @@ -564,9 +565,22 @@ public void providerChanged() {

addPersistedConfigValues(Map.of(Picocli.KC_PROVIDER_FILE_PREFIX + "fake", "value"));

NonRunningPicocli nonRunningPicocli = pseudoLaunch("start", "--optimized");
NonRunningPicocli nonRunningPicocli = pseudoLaunch("start", "--optimized", "--http-enabled=true", "--hostname-strict=false");
assertEquals(CommandLine.ExitCode.USAGE, nonRunningPicocli.exitCode);
assertTrue(nonRunningPicocli.getErrString().contains("A provider JAR was updated since the last build, please rebuild for this to be fully utilized."));
assertTrue(nonRunningPicocli.getErrString().contains(Picocli.PROVIDER_TIMESTAMP_ERROR));
}

@Test
public void warnProviderChanged() {
build("build", "--db=dev-file");

putEnvVar("KC_RUN_IN_CONTAINER", "true");
String key = PersistedConfigSource.getInstance().getConfigValueProperties().keySet().stream().filter(k -> k.startsWith(Picocli.KC_PROVIDER_FILE_PREFIX)).findAny().orElseThrow();
addPersistedConfigValues(Map.of(key, "1")); // change to a fake timestamp

NonRunningPicocli nonRunningPicocli = pseudoLaunch("start", "--optimized", "--http-enabled=true", "--hostname-strict=false");
assertEquals(nonRunningPicocli.getErrString(), CommandLine.ExitCode.OK, nonRunningPicocli.exitCode);
assertTrue(nonRunningPicocli.getOutString().contains(Picocli.PROVIDER_TIMESTAMP_WARNING));
}

@Test
Expand Down Expand Up @@ -800,7 +814,6 @@ public void logAsyncSyslogInvalidValues() {

@Test
public void timestampChanged() {
assertTrue(Picocli.timestampChanged("12345", null));
assertTrue(Picocli.timestampChanged("12345", "12346"));
assertTrue(Picocli.timestampChanged("12000", "12346"));
// new is truncated - should not be a change
Expand Down
Empty file.
0