grpc icon indicating copy to clipboard operation
grpc copied to clipboard

C++ server-side interceptor ability to cancel a call

Open Thalhammer opened this issue 4 years ago • 22 comments

Is your feature request related to a problem? Please describe.

I want to do rate limiting in the grpc server for fine grained control. The only way in C++ to block a request before the application callback is invoked is using an AuthMetadataProcessor, however those only have access to the call metadata but no info about the client. Interceptors do have access to the server context (and thus the peer info), however those can not cancel an rpc and are invoked after the metadata processor is done, so passing info using the metadata fields doesn't work either.

Describe the solution you'd like

Ideally the channel/call information (which seems to be already available at the call site of AuthMedataProcessor) is passed into the processor similar to how its done for Interceptors. However since the interface of Process() doesn't contain any info class and we can't change it since its public, the best option is probably to add peer info to the auth_metadata similar to :path and :authority.

Describe alternatives you've considered

I currently rate limit based on user information which is extracted from the jwt token sent with each rpc. However verifying a jwt has a considerable performance cost and my hope was to rate limit already before that base on peer ip. In addition it would be usefull to log the peer info in case authentication fails, which is simply not possible right now.

Additional context

Facilities for authentication/authorization in grpc seem to be quite lacking in general for everything except mTLS, which in many cases is not something you need. In particular in my case encryption is not needed since all connections are established via an encrypted vpn anyway. While you obviously dont want to authenticate a client purely based on their IP, having information about the client has a multitude of uses for authentication.

Thalhammer avatar Feb 08 '22 01:02 Thalhammer

I don't think AuthMetadataProcessor is the right way to implement rate-limiting. I think it would make much more sense to do this via an interceptor. An interceptor should be able to cancel the call.

@yashykt, can you please advise as to the best way to implement something like this via an interceptor?

markdroth avatar Feb 08 '22 16:02 markdroth

I think it would make much more sense to do this via an interceptor.

I agree, however an Interceptor can't cancel calls in any way. AuthMetadataProcessor can and (according to the spare docs about it) is supposed to be used for access controls. Even if you only do real authentication (e.g. jwt auth) it might still require peer infos (e.g. if a token is only valid for a specific host).

Thalhammer avatar Feb 08 '22 17:02 Thalhammer

Interceptors should be able to cancel a call. Yash can probably tell you how to do it. Or, if there's not a way for them to do that already, then we can fix that.

I don't think the right answer is to do rate limiting via AuthMetadataProcessor. That's not what that mechanism was designed to do.

markdroth avatar Feb 08 '22 19:02 markdroth

If the question is simply about cancelling a call from an interceptor, we can do that using the TryCancel method on ServerContext. The tricky thing here is that the usage of TryCancel requires some co-ordination with the application callback so that both methods return the same status, and that kind of defeats the purpose of a clean rate limiting interceptor where the application does not see the callback being invoked at all (in my opinion).

This could be achieved if we add Hijack abilities to the server side interceptors similar to the client side. At the time, we had designed this API, there was an explicit decision to not add Hijack abilities to server side interceptors so as to avoid the ability to write the entire server logic in server interceptors. The thinking was that the logic of a server side interceptor wanting to hijack could be pushed to the application layer. We might want to revisit that decision.

yashykt avatar Feb 14 '22 23:02 yashykt

For rate limiting, I assume that it doesn't have to be an explicit cancellation. Can't the interceptor just return whatever status it wants directly?

markdroth avatar Feb 15 '22 15:02 markdroth

there was an explicit decision to not add Hijack abilities to server side interceptors so as to avoid the ability to write the entire server logic in server interceptors.

@yashykt I can't think of a reason why anyone would wan't to write entire logic there when theres an easy application callback to do the work. That aside, the design of having Middlewares (or Interceptors, Layers, whatever you wanna call it) the get executed before and after the actual piece doing work is used in almost every successful framework out there and has proven itself good multiple times. GRPC in its current state has no such option. One thing is AuthMetadataProcessor that is capable of doing access controls, but can't communicate anything to the application and is only executed before a call. The other is Interceptors which fit a lot better into the scheme of Middlewares, but have no way of terminating the call (or sending a response). While having advanced mTLS auth and an authorization framework is pretty cool, this is often not what you need.

The thinking was that the logic of a server side interceptor wanting to hijack could be pushed to the application layer.

Of course it can, but I don't think its the right place for it. I don't wanna have to put a block of identical code in each and every rpc function (in at least two variants for async and sync). Thats simply a maintainance burden thats completely avoidable. Sure you can abstract it away into a single call and/or wrap it in a macro (shudder) to reduce the impact, but thats just hiding the problem, not solving it.

I know my opinion probably doesn't matter much (and tbh I have no idea how google manages this problem when using grpc), but imho there are a two of options:

  • AuthMetadataProcessor needs a way to pass data to Interceptors and the application callback (and result headers). This would allow doing all of the access control related stuff (which usually happens right at the start of an rpc) there and pass the information (user id, scopes, etc) to the rest of the chain using a trusted channel. As it stands right now, there is no (sane) way to pass any data at all (You can't get the call data/servercontext, the authcontext data is not per call and the result metadata not implemented).
  • Interceptor needs a way to do respond to the call (at least returning a specific status, ideally including metadata). We can pass auth data in the initial metadata map, which is not as clean as a per call AuthContext would be, but at least works.

Don't get me wrong, I love gRPC, but as of right now its a lot more complicated to use (if you need authentication) than it would have to be.

Thalhammer avatar Feb 15 '22 18:02 Thalhammer

For rate limiting, I assume that it doesn't have to be an explicit cancellation. Can't the interceptor just return whatever status it wants directly?

We can, but the way the interceptor is structured is that it intercepts the ops that we send through the stack, so we would only be able to change the status on PRE_SEND_STATUS which would get triggered after the application triggers sending the status.

yashykt avatar Feb 15 '22 21:02 yashykt

Of course it can, but I don't think its the right place for it. I don't wanna have to put a block of identical code in each and every rpc function (in at least two variants for async and sync). Thats simply a maintainance burden thats completely avoidable. Sure you can abstract it away into a single call and/or wrap it in a macro (shudder) to reduce the impact, but thats just hiding the problem, not solving it.

This is exactly what I was thinking of as the rationale for providing hijacking semantics with the server side interceptor API too.

yashykt avatar Feb 15 '22 21:02 yashykt

If the question is simply about cancelling a call from an interceptor, we can do that using the TryCancel method on ServerContext. The tricky thing here is that the usage of TryCancel requires some co-ordination with the application callback so that both methods return the same status, and that kind of defeats the purpose of a clean rate limiting interceptor where the application does not see the callback being invoked at all (in my opinion).

This could be achieved if we add Hijack abilities to the server side interceptors similar to the client side. At the time, we had designed this API, there was an explicit decision to not add Hijack abilities to server side interceptors so as to avoid the ability to write the entire server logic in server interceptors. The thinking was that the logic of a server side interceptor wanting to hijack could be pushed to the application layer. We might want to revisit that decision.

Suppose that I want to add Hijack abilities to the server side interceptors only for sync and async request. Is it possible without needing to redesign the whole interceptor structure and APIs(change too much code)?

It seems that I can somehow refer to client side implementation, but since server side process involves in putting and plucking the CQ, there is much more to do. Currently I think that I might set the hijacked flag in the interceptor, then check the hijacked flag before run the actual request handler. If the request is hijacked, I mannually put the response and status returned by interceptor into CQ, bypassing server side logic. I wonder whether this approach possible?

Thanks in advance!

livingshade avatar Mar 09 '23 03:03 livingshade

Hi, any progress on this?

olafmustafar avatar Aug 29 '23 19:08 olafmustafar

Base on the usage of interceptor, it just a hook, you ask too much. LOL

methods->QueryInterceptionHookPoint(grpc::experimental::InterceptionHookPoints::XXX)

I also want to implement a rate limiter in interceptor before understand above code.

gripleaf avatar Sep 01 '23 06:09 gripleaf

The situation is further complicated by the fact that there is no option to use AuthMetadataProcessor with an insecure server. Therefore, authentication needs to be performed within RPC calls. Is there a plan to allow breaking the call at the POST_RECV_INITIAL_METADATA hook provided by the Interceptor and returning an arbitrary status? Currently, I can only interrupt the call at the PRE_SEND_STATUS hook, preventing the client from receiving the response, but the server-side call has already been executed.

kzolti avatar Dec 27 '23 19:12 kzolti

Is there a plan to allow breaking the call at the POST_RECV_INITIAL_METADATA hook provided by the Interceptor and returning an arbitrary status?

I opened this issue ~2 years ago and so far there has been zero intention to change anything about it. Instead the grpc team added countless "features" that might make sense for google, but are fairly useless for anyone outside it and only add bloat (which can not even be disabled at compile time). Not to mention theres now 4 different ways for handling the rpc call, neither of which can be disabled. At this point its probably faster and more efficient to roll your own grpc server/client on top of libnghttp2, than waiting for the grpc team to fix those issues.

Thalhammer avatar Dec 28 '23 08:12 Thalhammer

If the question is simply about cancelling a call from an interceptor, we can do that using the TryCancel method on ServerContext. The tricky thing here is that the usage of TryCancel requires some co-ordination with the application callback so that both methods return the same status, and that kind of defeats the purpose of a clean rate limiting interceptor where the application does not see the callback being invoked at all (in my opinion).

This could be achieved if we add Hijack abilities to the server side interceptors similar to the client side. At the time, we had designed this API, there was an explicit decision to not add Hijack abilities to server side interceptors so as to avoid the ability to write the entire server logic in server interceptors. The thinking was that the logic of a server side interceptor wanting to hijack could be pushed to the application layer. We might want to revisit that decision.

I'm trying to just cancel the rpc call with the interceptor (based on context information I can only get there), so requiring coordination with the service method is not an issue to me. But may I check what exactly are the coordination I need to do? I tried calling context->TryCancel(); and then methods->Proceed() in the interceptor. ~~It seems the subsequent service method is not called at all.~~ I can see the service method is still called. Calling context->IsCancelled() returns false. But the client side receives "not ok" and I can see the error message as "Cancelled on the server side". @yashykt

Update: I managed to get context->IsCancelled() to return true in the service method by checking POST_RECV_INITIAL_METADATA hookpoint in the interceptor instead of PRE_SEND_INITIAL_METADATA. But I would still like to confirm if the behavior is deterministic, i.e. context->IsCancelled() will always return true at the beginning of the service method after TryCancel() is called in the interceptor. I will need to make sure any application logic is not executed.

rdu-weston avatar Mar 28 '24 01:03 rdu-weston

But I would still like to confirm if the behavior is deterministic

PRE_SEND_INITIAL_METADATA is too late in the cycle. Its invoked for the response metadata which (unless you explicitly write submit it earlier) gets sent along the (first) response message. POST_RECV_INITIAL_METADATA is more or less required to be invoked before entering the callback because the initial metadata is part of the server context.

That said if I have to check if the request is cancelled at the start of every call I might as well check the authorisation there. The whole point of interceptors is to decouple logic like rate limiting and authorization/authentication from the application logic and to intercept the call before it hits said application logic.

Thalhammer avatar Mar 28 '24 09:03 Thalhammer

But I would still like to confirm if the behavior is deterministic

PRE_SEND_INITIAL_METADATA is too late in the cycle. Its invoked for the response metadata which (unless you explicitly write submit it earlier) gets sent along the (first) response message. POST_RECV_INITIAL_METADATA is more or less required to be invoked before entering the callback because the initial metadata is part of the server context.

That said if I have to check if the request is cancelled at the start of every call I might as well check the authorisation there. The whole point of interceptors is to decouple logic like rate limiting and authorization/authentication from the application logic and to intercept the call before it hits said application logic.

Totally agree. It's best if the authorization can be completely handled in the interceptor so that I don't have to do any checks in the service method implementation. But since there is no way to cancel the call properly in the interceptor with the current grpc c++ library. What I'm trying to do is more of a workaround. Even so, I'm not sure if there are any possible situations where undefined behavior may happen, by calling TryCancel() in the interceptor first and then check IsCancelled() later in the service method to decide whether to proceed, since TryCancel() is not supposed to be called in an interceptor.

rdu-weston avatar Mar 28 '24 09:03 rdu-weston

Even so, I'm not sure if there are any possible situations where undefined behavior may happen, by calling TryCancel() in the interceptor first and then check IsCancelled() later in the service method to decide whether to proceed, since TryCancel() is not supposed to be called in an interceptor.

Sending TryCancel() in the interceptor is completely meaningless if you still need to inspect isCancelled() in the service functions afterwards. This seems like a unnecessary effort.

kzolti avatar Mar 28 '24 19:03 kzolti

Even so, I'm not sure if there are any possible situations where undefined behavior may happen, by calling TryCancel() in the interceptor first and then check IsCancelled() later in the service method to decide whether to proceed, since TryCancel() is not supposed to be called in an interceptor.

Sending TryCancel() in the interceptor is completely meaningless if you still need to inspect isCancelled() in the service functions afterwards. This seems like a unnecessary effort.

I'm just using it as a way to tell the service method that it should not proceed, since there is no way to cancel the call directly in the interceptor or passing any message from the interceptor to the service method. But I do need the context information only available in the interceptor to do the check first.

rdu-weston avatar Mar 29 '24 01:03 rdu-weston

The Golang version of the interceptor has hijacking semantics, but as a native version, C++does not have this feature. https://github.com/grpc/grpc-go/blob/master/examples/features/interceptor/server/main.go

hatlonely avatar Nov 16 '24 08:11 hatlonely

Hi everyone, just a quick update - We've got plans of revamping the interception API which is also the reason why this feature request is not getting much traction. As part of the revamp, we should be able to add server side hijacking ability as well.

yashykt avatar Jan 30 '25 22:01 yashykt