Quarkus: Environment property is not used in ContainerRequestFilter

Created on 8 Nov 2019  Â·  11Comments  Â·  Source: quarkusio/quarkus

Describe the bug
I want to use a Configuration Property in my Request Interceptor but when i try to overwrite it with an environmental variable it still uses the one from the application.properties.
This behavior only appears when i am running it as a native image.

In this repository you can find the code.
https://github.com/niedch/quarkus-env-property

Running in dev mode (mvn compile quarkus:dev)
test.property in Interceptor: Injected from environment

Running in container using native-image
test.property in Interceptor: Injected from application.properties (even if TEST_PROPERTY is present)
One more thing is that everything is fine when using this Configproperty in the RestController Class

Expected
Even if application is running as native image property should be injected from env

Actual behavior
application is using the property from application.properties

To Reproduce
Steps to reproduce the behavior:

  1. checkout repository
  2. mvn package -Pnative -Dquarkus.native.container-build=true
  3. docker build -t envinjection -f src/main/docker/Dockerfile.native .
  4. docker run -p 8081:8081 --env TEST_PROPERTY=test-from-env envinjection

Environment (please complete the following information):

  • uname: 18.04.1-Ubuntu
  • Output of java -version:
    ```openjdk version "1.8.0_232"
    OpenJDK Runtime Environment (build 1.8.0_232-20191008104205.buildslave.jdk8u-src-tar--b07)
    OpenJDK 64-Bit GraalVM CE 19.2.1 (build 25.232-b07-jvmci-19.2-b03, mixed mode)

 - GraalVM version (if different from Java): GraalVM Version 19.2.1 CE
 - Quarkus version or git rev: 0.28.1

**Additional context**
Logs from application:
**mvn compile quarkus:dev**

2019-11-08 11:38:16,395 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) test property: test-from-env
2019-11-08 11:38:16,395 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) interceptor:
2019-11-08 11:38:16,396 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) SysPropConfigSource
2019-11-08 11:38:16,400 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) EnvConfigSource
2019-11-08 11:38:16,402 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) TEST_PROPERTY : test-from-env
2019-11-08 11:38:16,412 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,413 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,414 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) test.property : test-property in application.properties
2019-11-08 11:38:16,415 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,415 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) test.property : test-property in application.properties
2019-11-08 11:38:16,415 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) PropertiesConfigSource[source=Build system]
2019-11-08 11:38:16,416 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) null:null:ServletConfigSource
2019-11-08 11:38:16,416 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) null:null:FilterConfigSource
2019-11-08 11:38:16,416 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) null:ServletContextConfigSource
2019-11-08 11:38:16,416 INFO [tes.hom.Interceptor] (vert.x-worker-thread-1) default values
2019-11-08 11:38:16,418 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) test property: test-from-env
2019-11-08 11:38:16,418 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) endpoint:
2019-11-08 11:38:16,418 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) SysPropConfigSource
2019-11-08 11:38:16,419 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) EnvConfigSource
2019-11-08 11:38:16,420 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) TEST_PROPERTY : test-from-env
2019-11-08 11:38:16,426 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,426 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,426 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) test.property : test-property in application.properties
2019-11-08 11:38:16,426 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) PropertiesConfigSource[source=application.properties]
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) test.property : test-property in application.properties
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) PropertiesConfigSource[source=Build system]
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) null:null:ServletConfigSource
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) null:null:FilterConfigSource
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) null:ServletContextConfigSource
2019-11-08 11:38:16,427 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-1) default values


**docker run -p 8081:8081 --env TEST_PROPERTY=test-from-env envinjection**

2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) test property: test-property in application.properties
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) interceptor:
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) SysPropConfigSource
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) EnvConfigSource
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) TEST_PROPERTY : test-from-env
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) PropertiesConfigSource[source=Default configuration values]
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) test.property : test-from-env
2019-11-08 10:43:57,947 INFO [tes.hom.Interceptor] (vert.x-worker-thread-0) default values
2019-11-08 10:43:57,947 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) test property: test-from-env
2019-11-08 10:43:57,947 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) endpoint:
2019-11-08 10:43:57,947 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) SysPropConfigSource
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) EnvConfigSource
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) TEST_PROPERTY : test-from-env
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) PropertiesConfigSource[source=Default configuration values]
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) test.property : test-from-env
2019-11-08 10:43:57,948 INFO [tes.hom.ExampleResource] (vert.x-worker-thread-0) default values
```

kinbug

Most helpful comment

I can confirm that it works if you add the following to the interceptor:

    @ConfigProperty(name = "test.property")
    javax.enterprise.inject.Instance<String> testProperty;

And then read the result using testProperty.get()

All 11 comments

@mkouba is this something ArC (or GraalVM) related?

Weird, I see an exception when using 1.0.0.CR1 and 999-SNAPSHOT in your code:

➜  quarkus-env-property git:(master) ✗ java -jar target/envinjection-1.0.0-SNAPSHOT-runner.jar
2019-11-08 09:02:22,159 INFO  [io.quarkus] (main) envinjection 1.0.0-SNAPSHOT (running on Quarkus 999-SNAPSHOT) started in 0.969s. Listening on: http://0.0.0.0:8081
2019-11-08 09:02:22,179 INFO  [io.quarkus] (main) Profile prod activated. 
2019-11-08 09:02:22,180 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
2019-11-08 09:02:24,552 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) test property: test-property in application.properties
2019-11-08 09:02:24,553 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) interceptor:
2019-11-08 09:02:24,553 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) SysPropConfigSource
2019-11-08 09:02:24,553 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) EnvConfigSource
2019-11-08 09:02:24,554 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 09:02:24,554 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) PropertiesConfigSource[source=application.properties]
2019-11-08 09:02:24,554 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0)    test.property : test-property in application.properties
2019-11-08 09:02:24,558 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (vert.x-worker-thread-0) HTTP Request to /hello failed, error id: cb41b4a8-c288-4989-902b-2fa5c874f340-1: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:381)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:209)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252)
    at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
    at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
    at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:108)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatchRequestContext(VertxRequestHandler.java:83)
    at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.lambda$handle$0(VertxRequestHandler.java:70)
    at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$2(ContextImpl.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at org.jboss.resteasy.microprofile.config.BaseServletConfigSource.getName(BaseServletConfigSource.java:51)
    at test.home.ExampleResource.printConfigSources(ExampleResource.java:38)
    at test.home.Interceptor.filter(Interceptor.java:22)
    at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:311)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391)
    at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365)
    at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
    at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
    at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:110)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477)
    ... 14 more

^C2019-11-08 09:02:49,090 INFO  [io.quarkus] (main) envinjection stopped in 0.079s

It could be also docker-related. Did you try to run the native image directly?

The NPE looks like to be a known bug in Resteasy; https://github.com/quarkusio/quarkus/pull/2897/files#diff-9e5254b805bbc8bfcb28f957cb36efb1R244

Here is a fix for the NPE above: https://github.com/resteasy/Resteasy/pull/2211

@mkouba I tested the native image directly and I can see the bug described above.

2019-11-08 13:29:15,244 INFO  [tes.hom.Interceptor] (vert.x-worker-thread-0) INTERCEPTOR: test-property in application.properties
2019-11-08 13:29:15,244 INFO  [tes.hom.ExampleResource] (vert.x-worker-thread-0) JAX_RS RESOURCE: test-env

The lookup works if the programatic approach is used:

        logger.info("PROGR: "+ ConfigProviderResolver.instance().getConfig().getValue("test.property", String.class));

So it seems to be something in the Resteasy integration that doesn't let @ConfigProperty to be evaluated in run-time in @Provider classes
/cc @patriot1burke

Hm, the question is WHEN are the provider instances created. If it's STATIC_INIT then it makes sense that it can't see the env property.

I need to look, just starting my day now, but, this was refactored so that there were no hard servlet dependencies in resteasy.

I can confirm that it works if you add the following to the interceptor:

    @ConfigProperty(name = "test.property")
    javax.enterprise.inject.Instance<String> testProperty;

And then read the result using testProperty.get()

I can also verify that lazy loading helps to resolve that issue, but its an workaround.
Thank you anyways.

Was this page helpful?
0 / 5 - 0 ratings