Skip to content

Commit

Permalink
Merge pull request #22331 from radcortez/fix-19549
Browse files Browse the repository at this point in the history
Configuration property to use only system sources
  • Loading branch information
radcortez authored Jan 6, 2022
2 parents 3b72229 + 19f7d11 commit 63d29a6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.deployment;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = ConfigItem.PARENT, phase = ConfigPhase.BUILD_TIME)
public class ConfigBuildTimeConfig {
/**
* <p>
* Set this to <code>true</code> to read configuration from system properties and environment variables only. This
* only applies to runtime.
* </p>
*/
@ConfigItem(name = "config.sources.system-only", defaultValue = "false")
public boolean systemOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
import java.util.List;
import java.util.OptionalInt;
import java.util.Set;
import java.util.function.BooleanSupplier;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
import org.eclipse.microprofile.config.spi.Converter;

import io.quarkus.deployment.ConfigBuildTimeConfig;
import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationSourceBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.util.ServiceUtil;
Expand All @@ -25,6 +29,7 @@
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.runtime.configuration.SystemOnlySourcesConfigBuilder;
import io.quarkus.runtime.graal.InetRunTime;
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorFactory;
Expand Down Expand Up @@ -97,4 +102,20 @@ void nativeServiceProviders(
RuntimeInitializedClassBuildItem runtimeInitializedClass() {
return new RuntimeInitializedClassBuildItem(InetRunTime.class.getName());
}

@BuildStep(onlyIf = SystemOnlySources.class)
void systemOnlySources(BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(SystemOnlySourcesConfigBuilder.class.getName()));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(SystemOnlySourcesConfigBuilder.class.getName()));
}

private static class SystemOnlySources implements BooleanSupplier {
ConfigBuildTimeConfig configBuildTimeConfig;

@Override
public boolean getAsBoolean() {
return configBuildTimeConfig.systemOnly;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.runtime.configuration;

import io.smallrye.config.SmallRyeConfigBuilder;

public class SystemOnlySourcesConfigBuilder implements ConfigBuilder {
@Override
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
builder.getSourceProviders().clear();
return builder;
}

@Override
public int priority() {
return Integer.MAX_VALUE;
}
}
3 changes: 2 additions & 1 deletion devtools/cli/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ quarkus.native.resources.includes=quarkus.properties
quarkus.native.additional-build-args=--initialize-at-run-time=org.apache.maven.wagon.shared.http.AbstractHttpClientWagon
# Do not attempt to detect "unused removed beans" false positives during programmatic lookup
# at runtime to conserve some memory
quarkus.arc.detect-unused-false-positives=false
quarkus.arc.detect-unused-false-positives=false
quarkus.config.sources.system-only=true
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,4 @@ class PicocliDeploymentConfiguration {
*/
@ConfigItem(name = "native-image.processing.enable", defaultValue = "true")
boolean nativeImageProcessingEnabled;

/**
* <p>
* Set this to <code>true</code> to disable reading configuration from the default Config Sources. This includes
* `application.properties` and `.env` files.
* </p>
*/
@ConfigItem(defaultValue = "false")
boolean disableDefaultConfigSources;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.QuarkusApplicationClassBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.picocli.runtime.DefaultPicocliCommandLineFactory;
import io.quarkus.picocli.runtime.PicocliConfigBuilder;
import io.quarkus.picocli.runtime.PicocliRunner;
import io.quarkus.picocli.runtime.annotations.TopCommand;
import io.quarkus.runtime.annotations.QuarkusMain;
Expand All @@ -47,17 +44,6 @@ IndexDependencyBuildItem picocliIndexDependency() {
return new IndexDependencyBuildItem("info.picocli", "picocli");
}

@BuildStep
void configBuilder(
PicocliDeploymentConfiguration configuration,
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {
if (configuration.disableDefaultConfigSources) {
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(PicocliConfigBuilder.class.getName()));
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(PicocliConfigBuilder.class.getName()));
}
}

@BuildStep
void picocliRunner(ApplicationIndexBuildItem applicationIndex,
CombinedIndexBuildItem combinedIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class AvailableConfigSourcesTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest().withApplicationRoot(
(jar) -> jar.addAsResource(new StringAsset("quarkus.picocli.disable-default-config-sources=true\n" +
(jar) -> jar.addAsResource(new StringAsset("quarkus.config.sources.system-only=true\n" +
"my.prop=1234\n"), "application.properties"));

@Inject
Expand Down

0 comments on commit 63d29a6

Please sign in to comment.