Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContextNotActiveException Issue in ResteasyReactiveClientResponseFilter when using @ActivateRequestContext and OidcClientRequestReactiveFilter #29960

Open
npoirey opened this issue Dec 19, 2022 · 12 comments
Labels
area/oidc kind/bug Something isn't working

Comments

@npoirey
Copy link

npoirey commented Dec 19, 2022

Describe the bug

Hi,

This one is weird and I gave up trying to find the root cause.
I made a small project that reproduces the issue here : https://github.com/npoirey/quarkus-repro-oidc-requestcontextnotactive

Basically, I have the following :

  • a RequestScoped bean (RequestContextBean), that I use to propagate some context for logging purpose mainly
  • a kafka consumer (TotoConsumer in the repo) annotated with @Blocking, and @ActivateRequestContext in order to use my RequestScopedBean
  • 2 HTTP clients, both identical and having a logging filter (ClientLoggingFilter), but one of them also has the provider OidcClientRequestReactiveFilter

the idea is

  • consume data
  • init RequestScopedBean from data
  • call some API
  • have the request/response logged (system.out in my example)
  • ack the message

If I use the HTTP client WITHOUT OIDC, everything works fine.
You can see it by running the test RequestContextBeanPropagationTest.testConsumesWithoutOidc() :
image
(don't pay attention to the 404, I didn't bother mocking the server)
In this test, I get traces from my filter

However, it doesnt work when using OIDC
You can see it by running the test RequestContextBeanPropagationTest.testConsumesWithOidc() :
image
in this test, I dont get the logs from my filter. Instead it throws a javax.enterprise.context.ContextNotActiveException: RequestScoped context was not active when trying to obtain a bean instance for a client proxy of CLASS bean [class=org.acme.security.openid.connect.client.RequestContextBean, id=e4da94214e7ddc6f068456b5228496f1268add1a]

As a workaround, I added @ActivateRequestContext annotation on the filter itself, but obviously this only solves the exception - I still don't have the correct bean/context so my log is incomplete

As I had trouble understanding where the issue comes from, please let me know if I should create this issue on another project.

Thank you

Expected behavior

The requestContext should be active in ResteasyReactiveClientResponseFilter, even if it was activated manually beforehand via @ActivateRequestContext and the client is using OIDC.

In my example, RequestContextBeanPropagationTest.testConsumesWithOidc() should have the same output than RequestContextBeanPropagationTest.testConsumesWithoutOidc()

Actual behavior

throws a javax.enterprise.context.ContextNotActiveException: RequestScoped context was not active when trying to obtain a bean instance for a client proxy of CLASS bean [class=org.acme.security.openid.connect.client.RequestContextBean, id=e4da94214e7ddc6f068456b5228496f1268add1a]

How to Reproduce?

pull this repo
run RequestContextBeanPropagationTest.testConsumesWithOidc()

Output of uname -a or ver

No response

Output of java -version

No response

GraalVM version (if different from Java)

No response

Quarkus version or git rev

No response

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@npoirey npoirey added the kind/bug Something isn't working label Dec 19, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Dec 19, 2022

/cc @pedroigor(oidc), @sberyozkin(oidc)

@sberyozkin
Copy link
Member

@geoand
Copy link
Contributor

geoand commented Dec 20, 2022

I am not really sure how this could happen, so I would like to hear from @mkouba

@mkouba
Copy link
Contributor

mkouba commented Dec 20, 2022

I think that we'll need someone with Reactive Messaging experience. In this particular case, a reactive client (implemented by quarkus-rest-client-reactive) is called from within a processor method.

CC @ozangunalp @cescoffier

Also #21367 (comment) and #27802 might be related.

FTR I've tried to disable SmallRye Context Propagation for CDI contexts completely via quarkus.arc.context-propagation.enabled=false and both test methods fail consistently with ContextNotActiveException. Which indicates that the vertx duplicated context created by the Reactive Messaging per the processor invocation is not used in the ClientLoggingFilter.

@sberyozkin
Copy link
Member

Indeed, it is a typical idea AFAIK in RestEasy reactive filters as otherwise it would have to block until the token is retrieved

@mkouba
Copy link
Contributor

mkouba commented Dec 20, 2022

I see that the request context is suspended during token retrieval in

Yes, but that's not the CDI request context but resteasy-reactive request context. @geoand Could it be related?

@geoand
Copy link
Contributor

geoand commented Dec 20, 2022

Likely, but I don't really know where to look

@ozangunalp
Copy link
Contributor

I don't think the rest client supports propagating the CDI request scope:

It is debatable but I think it makes sense. The request scope is for incoming server requests, an outgoing REST client request doesn't require the CDI Request scope.

@sberyozkin
Copy link
Member

an outgoing REST client request doesn't require the CDI Request scope.

FYI, OIDC token propagation reactive client filter (though it is not used in this reproducer) has a current request token injected

@ozangunalp
Copy link
Contributor

FYI, OIDC token propagation reactive client filter (though it is not used in this reproducer) has a current request token injected

I see that now, HeaderContainer is also provided as request-scoped.
But suppose somehow the rest client is called without a request context, or the request context gets deactivated as in the case of OidcClientRequestReactiveFilter. In that case, it won't be reactivated during handler calls.

@geoand
Copy link
Contributor

geoand commented Dec 20, 2022

Ideally we would have the rest client keep the request scope when it's executed as part of an active request, but I am not sure how possible that will be...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/oidc kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants