rpcperms: don't intercept if no middleware is registered

If there is no middleware registered, we don't need to intercept any
call and therefore can skip the request and macaroon parsing section.
This commit is contained in:
Oliver Gugger 2021-10-21 20:28:30 +02:00
parent c02bf19fc5
commit af09f11c1c
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -777,6 +777,12 @@ func (r *InterceptorChain) middlewareUnaryServerInterceptor() grpc.UnaryServerIn
return nil, err
}
// If there is no middleware registered, we don't need to
// intercept anything.
if !r.middlewareRegistered() {
return handler(ctx, req)
}
msg, err := NewMessageInterceptionRequest(
ctx, TypeRequest, false, info.FullMethod, req,
)
@ -822,6 +828,12 @@ func (r *InterceptorChain) middlewareStreamServerInterceptor() grpc.StreamServer
return err
}
// If there is no middleware registered, we don't need to
// intercept anything.
if !r.middlewareRegistered() {
return handler(srv, ss)
}
// To give the middleware a chance to accept or reject the
// establishment of the stream itself (and not only when the
// first message is sent on the stream), we send an intercept
@ -875,6 +887,15 @@ func (r *InterceptorChain) checkMandatoryMiddleware(fullMethod string) error {
return nil
}
// middlewareRegistered returns true if there is at least one middleware
// currently registered.
func (r *InterceptorChain) middlewareRegistered() bool {
r.RLock()
defer r.RUnlock()
return len(r.registeredMiddleware) > 0
}
// acceptRequest sends an intercept request to all middlewares that have
// registered for it. This means either a middleware has requested read-only
// access or the request actually has a macaroon which a caveat the middleware