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

View File

@ -777,6 +777,12 @@ func (r *InterceptorChain) middlewareUnaryServerInterceptor() grpc.UnaryServerIn
return nil, err 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( msg, err := NewMessageInterceptionRequest(
ctx, TypeRequest, false, info.FullMethod, req, ctx, TypeRequest, false, info.FullMethod, req,
) )
@ -822,6 +828,12 @@ func (r *InterceptorChain) middlewareStreamServerInterceptor() grpc.StreamServer
return err 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 // To give the middleware a chance to accept or reject the
// establishment of the stream itself (and not only when the // establishment of the stream itself (and not only when the
// first message is sent on the stream), we send an intercept // first message is sent on the stream), we send an intercept
@ -875,6 +887,15 @@ func (r *InterceptorChain) checkMandatoryMiddleware(fullMethod string) error {
return nil 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 // acceptRequest sends an intercept request to all middlewares that have
// registered for it. This means either a middleware has requested read-only // 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 // access or the request actually has a macaroon which a caveat the middleware