go-grpc-middleware icon indicating copy to clipboard operation
go-grpc-middleware copied to clipboard

Exposing the Request to Streaming Server Interceptors

Open SSSOCPaulCote opened this issue 3 years ago • 2 comments

For unary interceptors, the req interface{} is exposed to validate any request parameters. It would be very helpful if the request interface was also exposed for streaming interceptors, but for my specific use case, the streaming server interceptor.

I read somewhere that I could implement the grpc.ServerStream interface and grab the request from RecvMsg(m interface{}) function. I tried that and when I would switch request := m.(type){} and print out the value of the type asserted request, it would always be blank. I've had to resort to duplicating some code and moving authentication to the actual implementation of the streaming RPC function which feels wrong considering I have auth middleware for unary commands.

I'm open to alternatives, I probably missed something and this change isn't needed but figured I'd ask.

SSSOCPaulCote avatar Jul 21 '22 21:07 SSSOCPaulCote

To be clear, the reason why I don't want to do this in the actual function implementation is because I extract a specific parameter from the request, insert it into the context and pass that along to my auth function. Basically, I'm creating an API authentication system where credentials can be valid for specific commands with specific arguments. Which is why doing this in middleware is ideal.

SSSOCPaulCote avatar Jul 21 '22 21:07 SSSOCPaulCote

Hi, I believe this isn't something that we can control in this repo, it's dictated by the interceptor interface exposed by the grpc-go library. Am I misunderstanding?

johanbrandhorst avatar Jul 24 '22 04:07 johanbrandhorst

I think you could try using our v2 interceptor with reporter interface https://github.com/grpc-ecosystem/go-grpc-middleware/blob/v2/interceptors/reporter.go#L90, let us know if this work!

bwplotka avatar Mar 19 '23 01:03 bwplotka

This looks really interesting. Essentially, I had to move my authentication for any streaming endpoints to the RPC function implementation as opposed to doing it in middleware. With these changes, it seems I may be able to implement something using the ServerReportable interface.

SSSOCPaulCote avatar Mar 19 '23 19:03 SSSOCPaulCote

Sounds like this is solved, feel free to visit our Slack channel or we can reopen this issue, thanks!

bwplotka avatar Apr 13 '23 10:04 bwplotka