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

Security interceptor doesn't propagate the original exception #357

Closed
trolle4 opened this issue May 23, 2023 · 13 comments
Closed

Security interceptor doesn't propagate the original exception #357

trolle4 opened this issue May 23, 2023 · 13 comments
Labels
bug Auto-generates notes
Milestone

Comments

@trolle4
Copy link

trolle4 commented May 23, 2023

Using the starter v 5.1.1, with bearer token authentication enabled like described:

builder.authorizeRequests().anyMethod().authenticated().and()
                    .authenticationProvider(JwtAuthProviderFactory.forAuthorities(jwtDecoder));

This all works gr8. However, when i throw a simple exception extenting runtime exception from the onNext method, the following error occurs

Caught exception while handling exception using method public ....

java.lang.IllegalArgumentException: argument type mismatch

So it does find the correct method I have specified with @GRpcExceptionHandler but invokes it with the wrong parameters.

What I have been able to figure out, is that it is wrapped in a AuthenticationException on this line
Thus, it does fint the correct method, when searching for available handlers, but it is not invoked with the cause, which is the exception i throw that also mathes the execption handler.

Does this have something to do with the fact that it is caught in the SecurityInterceptor and I may have set things up the wrong way?

Any pointers would be greatly appreciated.

I can work around it, by creating an exception handler that takes RuntimeException and then just check the type of the cause in there, but that doesn't seem like the intended way to do it :)

@jvmlet
Copy link
Collaborator

jvmlet commented May 23, 2023

Please read this section carefully

@jvmlet
Copy link
Collaborator

jvmlet commented May 23, 2023

btw, what is the order of your interceptors ?

@trolle4
Copy link
Author

trolle4 commented May 23, 2023

Thank you for the swift response.
I feel I have read this, but I may be missing something.

The mapped interceptors resolve to the following:

lyngsoesystems.logistics.products.rtls.kvasir.v1.UploadDataService=org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver@7436364d
grpc.health.v1.Health=org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver@1a8e9ed9

It is not a checked exception but an unchecked exception. I read the documentation like I should not do anything special about that right?
In the onNext i simply call throw new MyRuntimeException("test");

I have tried with a checked exception and calling the throw new GRpcRuntimeExceptionWrapper(new MyRuntimeException("test")); but same result

This is my handler method:

    @GRpcExceptionHandler
    public Status tokenExpiredExceptionHandler(MyRuntimeException e, GRpcExceptionScope scope) {
        log.info("My Error Handler");
        return Status.ABORTED;
    }

The method is added to the available error handlers, and it is also selected but not invoked with the right exception.
The logs state:

2023-05-23T14:23:35.945+02:00 ERROR 42553 --- [ault-executor-1] o.l.s.grpc.FailureHandlingSupport        : Caught exception while handling exception  using method public io.grpc.Status com.lyngsoesystems.esp.kvasir.grpc.endpoint.GrpcDataEndpoint.tokenExpiredExceptionHandler(com.lyngsoesystems.esp.kvasir.grpc.endpoint.MyRuntimeException,org.lognet.springboot.grpc.recovery.GRpcExceptionScope), closing with Status{code=INTERNAL, description=null, cause=null}.

java.lang.IllegalArgumentException: argument type mismatch

@jvmlet
Copy link
Collaborator

jvmlet commented May 23, 2023

The error handling interceptor should be executed before authentication interceptor, have you set your custom order of interceptors ?

@jvmlet
Copy link
Collaborator

jvmlet commented May 23, 2023

when i throw a simple exception extenting runtime exception from the onNext method

Do you have your own custom interceptor ? Where do you throw the exception from ?

@trolle4
Copy link
Author

trolle4 commented May 23, 2023

Have not specified the order so it's using default. I did try to specify with this:

grpc:
  recovery:
    interceptor-order: 0
  security:
    auth:
      interceptor-order: 10

But same result.

The server code is stripped down to this now:

@GRpcService
@Slf4j
public class GrpcDataEndpoint extends UploadDataServiceGrpc.UploadDataServiceImplBase {


    static class MyRuntimeException extends RuntimeException {
        public MyRuntimeException(String message) {
            super(message);
        }
    }

    @GRpcExceptionHandler
    public Status tokenExpiredExceptionHandler(MyRuntimeException npe, GRpcExceptionScope scope) {
        log.info("My Error Handler 2");
        return Status.ABORTED;
    }

    @Override
    public StreamObserver<UploadDataRequest> uploadData(StreamObserver<UploadDataResponse> responseObserver) {

        return new StreamObserver<>() {
            @Override
            public void onNext(UploadDataRequest value) {

                throw new MyRuntimeException("test");
            }

            @Override
            public void onError(Throwable t) {
                log.error("Error: {}", t.getMessage());
            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}

@jvmlet
Copy link
Collaborator

jvmlet commented May 23, 2023

I'll try to reproduce bidi-stream call in test

@trolle4
Copy link
Author

trolle4 commented May 24, 2023

I will try to create a minimal test project for you to see

@jvmlet
Copy link
Collaborator

jvmlet commented May 24, 2023

Was able to reproduce, will be fixed (soon).
Thanks for reporting.

@jvmlet jvmlet added the bug Auto-generates notes label May 24, 2023
@jvmlet jvmlet added this to the 5.1.2 milestone May 24, 2023
@jvmlet jvmlet changed the title Cannot get Exceptionhandling to work properly Security interceptor doesn't propagate the original exception May 24, 2023
@trolle4
Copy link
Author

trolle4 commented May 24, 2023

Sounds great. Thank you

jvmlet added a commit that referenced this issue May 24, 2023
@jvmlet
Copy link
Collaborator

jvmlet commented May 24, 2023

@trolle4 , please try with latest 5.1.2-SNAPSHOT once this build finishes.

@trolle4
Copy link
Author

trolle4 commented May 24, 2023

Yup it's working with the snapshot version.

@jvmlet jvmlet closed this as completed May 24, 2023
@jvmlet
Copy link
Collaborator

jvmlet commented May 24, 2023

5.1.2 is out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Auto-generates notes
Projects
None yet
Development

No branches or pull requests

2 participants