8000 fix: ensuring persisted state does not leak into builds (#54494) by shawkins · Pull Request #54585 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
8000
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 @@ -12,6 +12,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import jakarta.ws.rs.core.MultivaluedHashMap;
Expand Down Expand Up @@ -313,20 +314,28 @@ public Set<WildcardPropertyMapper<?>> getWildcardMappers() {
}

public void sanitizeDisabledMappers(AbstractCommand command) {
// Initialize profile in order to check state of features. Disable Persisted CS for re-augmentation
if (isRebuildCheck()) {
PersistedConfigSource.getInstance().runWithDisabled(Environment::getCurrentOrCreateFeatureProfile);
} else {
// For a build (explicit, or a potential auto-build) the feature profile and the enabled state of the
// mappers must not be influenced by the persisted values, so disable the Persisted CS
boolean build = isRebuildCheck() || Build.NAME.equals(command.getName());

Supplier<Void> sanitize = () -> {
// Initialize profile in order to check state of features
Environment.getCurrentOrCreateFeatureProfile();
if (!command.shouldStart() && !Build.NAME.equals(command.getName())) {
if (!build && !command.shouldStart()) {
// this will use the deferred logger, which means it may not be seen in some circumstances
Profile.getInstance().logUnsupportedFeatures();
}

sanitizeMappers(buildTimeMappers, disabledBuildTimeMappers, command);
sanitizeMappers(runtimeTimeMappers, disabledRuntimeMappers, command);
return null;
};
if (build) {
PersistedConfigSource.getInstance().runWithDisabled(sanitize);
} else {
sanitize.get();
}

sanitizeMappers(buildTimeMappers, disabledBuildTimeMappers, command);
sanitizeMappers(runtimeTimeMappers, disabledRuntimeMappers, command);

// enforce single mappings - by dropping multiple mapping synthetics
entrySet().stream().forEach(e -> {
if (e.getValue().size() <= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.quarkus.runtime.KeycloakMain;
import org.keycloak.quarkus.runtime.cli.command.AbstractAutoBuildCommand;
import org.keycloak.quarkus.runtime.cli.command.Build;
import org.keycloak.quarkus.runtime.configuration.AbstractConfigurationTest;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource;
Expand Down Expand Up @@ -533,6 +534,37 @@ public void testReaugFromProdToDev() {
assertEquals("dev", nonRunningPicocli.getBuildProps().getProperty(org.keycloak.common.util.Environment.PROFILE));
}

@Test
public void testBuildIgnoresPersistedOptionsWhenEnablingMappers() {
build("build", "--db=dev-file", "--metrics-enabled=true");

// metrics are not enabled for this build, so the options that depend on them must not be enabled by the persisted value
NonRunningPicocli nonRunningPicocli = pseudoLaunch("build", "--db=dev-file", "--http-metrics-histograms-enabled=true");
assertError(nonRunningPicocli, "Disabled option: '--http-metrics-histograms-enabled'. Available only when metrics are enabled");
}

@Test
public void testAutoBuildIgnoresPersistedOptionsWhenEnablingMappers() {
build("build", "--db=dev-file", "--metrics-enabled=true");

// the implicit build will not have metrics enabled, so the dependent option must not be enabled by the persisted value
NonRunningPicocli nonRunningPicocli = pseudoLaunch("start", "--db=dev-file", "--http-enabled=true", "--hostname-strict=false", "--http-metrics-histograms-enabled=true");
assertError(nonRunningPicocli, "Disabled option: '--http-metrics-histograms-enabled'. Available only when metrics are enabled");
}

@Test
public void testBuildIgnoresPersistedFeatures() {
build("build", "--db=dev-file", "--features=token-exchange");

// the persisted features must not influence the artifacts selected for the new build
NonRunningPicocli nonRunningPicocli = pseudoLaunch("build", "--db=dev-file", "--features=token-exchange,client-admin-api:v2");
assertEquals(nonRunningPicocli.getErrString(), CommandLine.ExitCode.OK, nonRunningPicocli.exitCode);
assertTrue(nonRunningPicocli.reaug);
assertTrue(Profile.isFeatureEnabled(Profile.Feature.CLIENT_ADMIN_API_V2));
assertThat(System.getProperty(Build.QUARKUS_REMOVED_ARTIFACTS_PROPERTY),
not(containsString("io.quarkus:quarkus-hibernate-validator")));
}

/**
* Runs a fake build to setup the state of the persisted build properties
*/
Expand Down
Loading
0