-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config: detect injected config value mismatch during static init
- record the values injected during static intialization phase - if the runtime value differs from the injected value the app startup fails - also introduce ExecutionMode to easily detect the STATIC_INIT bootstrap phase
- Loading branch information
Showing
17 changed files
with
501 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/runtime/src/main/java/io/quarkus/runtime/ExecutionMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.runtime; | ||
|
||
/** | ||
* The runtime execution mode. | ||
*/ | ||
public enum ExecutionMode { | ||
|
||
/** | ||
* Static initializiation. | ||
*/ | ||
STATIC_INIT, | ||
|
||
/** | ||
* Runtime initialization. | ||
*/ | ||
RUNTIME_INIT, | ||
|
||
/** | ||
* The application is running. | ||
*/ | ||
RUNNING, | ||
|
||
UNSET, | ||
; | ||
|
||
public static ExecutionMode current() { | ||
return ExecutionModeManager.getExecutionMode(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
core/runtime/src/main/java/io/quarkus/runtime/ExecutionModeManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.runtime; | ||
|
||
public final class ExecutionModeManager { | ||
|
||
private static volatile ExecutionMode executionMode = ExecutionMode.UNSET; | ||
|
||
public static void staticInit() { | ||
executionMode = ExecutionMode.STATIC_INIT; | ||
} | ||
|
||
public static void runtimeInit() { | ||
executionMode = ExecutionMode.RUNTIME_INIT; | ||
} | ||
|
||
public static void running() { | ||
executionMode = ExecutionMode.RUNNING; | ||
} | ||
|
||
public static ExecutionMode getExecutionMode() { | ||
return executionMode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ns/arc/deployment/src/main/java/io/quarkus/arc/deployment/ConfigStaticInitBuildSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.arc.deployment; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.arc.processor.AnnotationsTransformer; | ||
import io.quarkus.arc.processor.DotNames; | ||
import io.quarkus.arc.runtime.ConfigStaticInitCheck; | ||
import io.quarkus.arc.runtime.ConfigStaticInitCheckInterceptor; | ||
import io.quarkus.arc.runtime.ConfigStaticInitValues; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
|
||
public class ConfigStaticInitBuildSteps { | ||
|
||
@BuildStep | ||
AdditionalBeanBuildItem registerBeans() { | ||
return AdditionalBeanBuildItem.builder() | ||
.addBeanClasses(ConfigStaticInitCheckInterceptor.class, ConfigStaticInitValues.class, | ||
ConfigStaticInitCheck.class) | ||
.build(); | ||
} | ||
|
||
@BuildStep | ||
AnnotationsTransformerBuildItem transformConfigProducer() { | ||
DotName configProducerName = DotName.createSimple("io.smallrye.config.inject.ConfigProducer"); | ||
|
||
return new AnnotationsTransformerBuildItem(AnnotationsTransformer.appliedToMethod().whenMethod(m -> { | ||
// Apply to all producer methods declared on io.smallrye.config.inject.ConfigProducer | ||
return m.declaringClass().name().equals(configProducerName) | ||
&& m.hasAnnotation(DotNames.PRODUCES) | ||
&& m.hasAnnotation(ConfigBuildStep.MP_CONFIG_PROPERTY_NAME); | ||
}).thenTransform(t -> { | ||
t.add(ConfigStaticInitCheck.class); | ||
})); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ns/arc/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class StaticInitBean { | ||
|
||
@ConfigProperty(name = "apfelstrudel") | ||
String value; | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...test/java/io/quarkus/arc/test/config/staticinit/StaticInitConfigInjectionFailureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StaticInitConfigInjectionFailureTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(StaticInitBean.class, StaticInitEagerBean.class, UnsafeConfigSource.class) | ||
.addAsServiceProvider(ConfigSource.class, UnsafeConfigSource.class) | ||
// the value from application.properties should be injected during STATIC_INIT | ||
.addAsResource(new StringAsset("apfelstrudel=jandex"), "application.properties")) | ||
.assertException(t -> { | ||
assertThat(t).isInstanceOf(IllegalStateException.class) | ||
.hasMessageContainingAll("Injected config property value mismatch detected", | ||
"the runtime value of 'apfelstrudel' is [gizmo] but the value [jandex] was injected into io.quarkus.arc.test.config.staticinit.StaticInitBean#value", | ||
"the runtime value of 'apfelstrudel' is [gizmo] but the value [jandex] was injected into io.quarkus.arc.test.config.staticinit.StaticInitEagerBean#value"); | ||
}); | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...c/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitEagerBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class StaticInitEagerBean { | ||
|
||
@ConfigProperty(name = "apfelstrudel") | ||
Instance<String> value; | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
// this should trigger the failure | ||
value.get(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...rc/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitLazyBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@Singleton | ||
public class StaticInitLazyBean { | ||
|
||
@ConfigProperty(name = "apfelstrudel") | ||
Instance<String> value; | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
// value is not obtained... | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...rc/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/StaticInitSafeBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Initialized; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Singleton; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
import io.quarkus.runtime.annotations.StaticInitSafe; | ||
|
||
@Singleton | ||
public class StaticInitSafeBean { | ||
|
||
String value; | ||
|
||
public StaticInitSafeBean(@StaticInitSafe @ConfigProperty(name = "apfelstrudel") String value) { | ||
this.value = value; | ||
} | ||
|
||
// bean is instantiated during STATIC_INIT | ||
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...rc/test/java/io/quarkus/arc/test/config/staticinit/StaticInitSafeConfigInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class StaticInitSafeConfigInjectionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addClasses(StaticInitSafeBean.class, StaticInitLazyBean.class, UnsafeConfigSource.class) | ||
.addAsServiceProvider(ConfigSource.class, UnsafeConfigSource.class) | ||
// the value from application.properties should be injected during STATIC_INIT | ||
.addAsResource(new StringAsset("apfelstrudel=jandex"), "application.properties")); | ||
|
||
@Inject | ||
StaticInitSafeBean bean; | ||
|
||
@Test | ||
public void test() { | ||
assertEquals("jandex", bean.value); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...rc/deployment/src/test/java/io/quarkus/arc/test/config/staticinit/UnsafeConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.arc.test.config.staticinit; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
// Intentionally not annotated with @StaticInitSafe so that it's not considered durin the STATIC_INIT | ||
public class UnsafeConfigSource implements ConfigSource { | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
return Set.of("apfelstrudel"); | ||
} | ||
|
||
@Override | ||
public String getValue(String propertyName) { | ||
return propertyName.equals("apfelstrudel") ? "gizmo" : null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Unsafe Test"; | ||
} | ||
|
||
@Override | ||
public int getOrdinal() { | ||
return 500; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.