Skip to content

Commit

Permalink
Merge pull request #1395 from authzed/grpc-middleware-order-assertion
Browse files Browse the repository at this point in the history
grpc middleware order assertion
  • Loading branch information
vroldanbet authored Jun 19, 2023
2 parents d1c9c83 + bd65f9b commit d2560c7
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 102 deletions.
216 changes: 120 additions & 96 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,109 +139,133 @@ const (
// DefaultUnaryMiddleware generates the default middleware chain used for the public SpiceDB Unary gRPC methods
func DefaultUnaryMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc, enableVersionResponse bool, dispatcher dispatch.Dispatcher, ds datastore.Datastore) (*MiddlewareChain[grpc.UnaryServerInterceptor], error) {
chain, err := NewMiddlewareChain([]ReferenceableMiddleware[grpc.UnaryServerInterceptor]{
{
Name: DefaultMiddlewareRequestID,
Middleware: requestid.UnaryServerInterceptor(requestid.GenerateIfMissing(true)),
},
{
Name: DefaultMiddlewareLog,
Middleware: logmw.UnaryServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
},
{
Name: DefaultMiddlewareGRPCLog,
Middleware: grpclog.UnaryServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...),
},
{
Name: DefaultMiddlewareOTelGRPC,
Middleware: otelgrpc.UnaryServerInterceptor(),
},
{
Name: DefaultMiddlewareGRPCProm,
Middleware: grpcprom.UnaryServerInterceptor,
},
{
Name: DefaultMiddlewareGRPCAuth,
Middleware: grpcauth.UnaryServerInterceptor(authFunc),
},
{
Name: DefaultMiddlewareServerVersion,
Middleware: serverversion.UnaryServerInterceptor(enableVersionResponse),
},
{
Name: DefaultInternalMiddlewareDispatch,
Internal: true,
Middleware: dispatchmw.UnaryServerInterceptor(dispatcher),
},
{
Name: DefaultInternalMiddlewareDatastore,
Internal: true,
Middleware: datastoremw.UnaryServerInterceptor(ds),
},
{
Name: DefaultInternalMiddlewareConsistency,
Internal: true,
Middleware: consistencymw.UnaryServerInterceptor(),
},
{
Name: DefaultInternalMiddlewareServerSpecific,
Internal: true,
Middleware: servicespecific.UnaryServerInterceptor,
},
NewUnaryMiddleware().
WithName(DefaultMiddlewareRequestID).
WithInterceptor(requestid.UnaryServerInterceptor(requestid.GenerateIfMissing(true))).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareLog).
WithInterceptor(logmw.UnaryServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID"))).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareGRPCLog).
WithInterceptor(grpclog.UnaryServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...)).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareOTelGRPC).
WithInterceptor(otelgrpc.UnaryServerInterceptor()).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareGRPCProm).
WithInterceptor(grpcprom.UnaryServerInterceptor).
EnsureNotExecuted(DefaultMiddlewareGRPCAuth).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareGRPCAuth).
WithInterceptor(grpcauth.UnaryServerInterceptor(authFunc)).
EnsureAlreadyExecuted(DefaultMiddlewareGRPCProm).
Done(),

NewUnaryMiddleware().
WithName(DefaultMiddlewareServerVersion).
WithInterceptor(serverversion.UnaryServerInterceptor(enableVersionResponse)).
Done(),

NewUnaryMiddleware().
WithName(DefaultInternalMiddlewareDispatch).
WithInternal(true).
WithInterceptor(dispatchmw.UnaryServerInterceptor(dispatcher)).
Done(),

NewUnaryMiddleware().
WithName(DefaultInternalMiddlewareDatastore).
WithInternal(true).
WithInterceptor(datastoremw.UnaryServerInterceptor(ds)).
Done(),

NewUnaryMiddleware().
WithName(DefaultInternalMiddlewareConsistency).
WithInternal(true).
WithInterceptor(consistencymw.UnaryServerInterceptor()).
Done(),

NewUnaryMiddleware().
WithName(DefaultInternalMiddlewareServerSpecific).
WithInternal(true).
WithInterceptor(servicespecific.UnaryServerInterceptor).
Done(),
}...)
return &chain, err
}

// DefaultStreamingMiddleware generates the default middleware chain used for the public SpiceDB Streaming gRPC methods
func DefaultStreamingMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc, enableVersionResponse bool, dispatcher dispatch.Dispatcher, ds datastore.Datastore) (*MiddlewareChain[grpc.StreamServerInterceptor], error) {
chain, err := NewMiddlewareChain([]ReferenceableMiddleware[grpc.StreamServerInterceptor]{
{
Name: DefaultMiddlewareRequestID,
Middleware: requestid.StreamServerInterceptor(requestid.GenerateIfMissing(true)),
},
{
Name: DefaultMiddlewareLog,
Middleware: logmw.StreamServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
},
{
Name: DefaultMiddlewareGRPCLog,
Middleware: grpclog.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...),
},
{
Name: DefaultMiddlewareOTelGRPC,
Middleware: otelgrpc.StreamServerInterceptor(),
},
{
Name: DefaultMiddlewareGRPCProm,
Middleware: grpcprom.StreamServerInterceptor,
},
{
Name: DefaultMiddlewareGRPCAuth,
Middleware: grpcauth.StreamServerInterceptor(authFunc),
},
{
Name: DefaultMiddlewareServerVersion,
Middleware: serverversion.StreamServerInterceptor(enableVersionResponse),
},
{
Name: DefaultInternalMiddlewareDispatch,
Internal: true,
Middleware: dispatchmw.StreamServerInterceptor(dispatcher),
},
{
Name: DefaultInternalMiddlewareDatastore,
Internal: true,
Middleware: datastoremw.StreamServerInterceptor(ds),
},
{
Name: DefaultInternalMiddlewareConsistency,
Internal: true,
Middleware: consistencymw.StreamServerInterceptor(),
},
{
Name: DefaultInternalMiddlewareServerSpecific,
Internal: true,
Middleware: servicespecific.StreamServerInterceptor,
},
NewStreamMiddleware().
WithName(DefaultMiddlewareRequestID).
WithInterceptor(requestid.StreamServerInterceptor(requestid.GenerateIfMissing(true))).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareLog).
WithInterceptor(logmw.StreamServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID"))).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareGRPCLog).
WithInterceptor(grpclog.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...)).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareOTelGRPC).
WithInterceptor(grpclog.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...)).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareGRPCProm).
WithInterceptor(grpcprom.StreamServerInterceptor).
EnsureAlreadyExecuted(DefaultMiddlewareGRPCAuth).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareGRPCAuth).
WithInterceptor(grpcauth.StreamServerInterceptor(authFunc)).
EnsureNotExecuted(DefaultMiddlewareGRPCProm).
Done(),

NewStreamMiddleware().
WithName(DefaultMiddlewareServerVersion).
WithInterceptor(serverversion.StreamServerInterceptor(enableVersionResponse)).
Done(),

NewStreamMiddleware().
WithName(DefaultInternalMiddlewareDispatch).
WithInternal(true).
WithInterceptor(dispatchmw.StreamServerInterceptor(dispatcher)).
Done(),

NewStreamMiddleware().
WithName(DefaultInternalMiddlewareDatastore).
WithInternal(true).
WithInterceptor(datastoremw.StreamServerInterceptor(ds)).
Done(),

NewStreamMiddleware().
WithName(DefaultInternalMiddlewareConsistency).
WithInternal(true).
WithInterceptor(consistencymw.StreamServerInterceptor()).
Done(),

NewStreamMiddleware().
WithName(DefaultInternalMiddlewareServerSpecific).
WithInternal(true).
WithInterceptor(servicespecific.StreamServerInterceptor).
Done(),
}...)
return &chain, err
}
Expand Down
Loading

0 comments on commit d2560c7

Please sign in to comment.