Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouples middleware modifications #1391

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/authzed/grpcutil v0.0.0-20230509155820-7a6fedb71dbc
github.com/authzed/spicedb v1.21.0
github.com/brianvoe/gofakeit/v6 v6.15.0
github.com/ecordell/optgen v0.0.9
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698
github.com/jackc/pgx/v5 v5.3.2-0.20230516120255-eab316e200b1
github.com/stretchr/testify v1.8.3
golang.org/x/tools v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ github.com/dave/jennifer v1.6.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ecordell/optgen v0.0.9 h1:kmRMqOkbNsWayOnZSk2m5SeGaOTOc7amfi+MAnaMOeI=
github.com/ecordell/optgen v0.0.9/go.mod h1:+YZ4tk5pNGMoeH+Y4F4HeDDj0SLOlIgMMNae7az4h5g=
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698 h1:Ms2IoxhBTljF7ItN1Oj4yy9DVJHn5L4A8sydRkrxoUE=
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698/go.mod h1:+YZ4tk5pNGMoeH+Y4F4HeDDj0SLOlIgMMNae7az4h5g=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/dalzilio/rudd v1.1.1-0.20220422201445-0a0cd32c7df9
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/dustin/go-humanize v1.0.1
github.com/ecordell/optgen v0.0.9
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698
github.com/emirpasic/gods v1.18.1
github.com/envoyproxy/protoc-gen-validate v1.0.1
github.com/fatih/color v1.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/ecordell/optgen v0.0.9 h1:kmRMqOkbNsWayOnZSk2m5SeGaOTOc7amfi+MAnaMOeI=
github.com/ecordell/optgen v0.0.9/go.mod h1:+YZ4tk5pNGMoeH+Y4F4HeDDj0SLOlIgMMNae7az4h5g=
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698 h1:Ms2IoxhBTljF7ItN1Oj4yy9DVJHn5L4A8sydRkrxoUE=
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698/go.mod h1:+YZ4tk5pNGMoeH+Y4F4HeDDj0SLOlIgMMNae7az4h5g=
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=
Expand Down
38 changes: 27 additions & 11 deletions internal/services/integrationtesting/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,40 @@ func TestCertRotation(t *testing.T) {
server.WithDashboardAPI(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithMetricsAPI(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithDispatchServer(util.GRPCServerConfig{Enabled: false}),
server.SetMiddlewareModification([]server.MiddlewareModification{
server.SetUnaryMiddlewareModification([]server.MiddlewareModification[grpc.UnaryServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware{
Middlewares: []server.ReferenceableMiddleware[grpc.UnaryServerInterceptor]{
{
Name: "datastore",
UnaryMiddleware: datastoremw.UnaryServerInterceptor(ds),
StreamingMiddleware: datastoremw.StreamServerInterceptor(ds),
Name: "datastore",
Middleware: datastoremw.UnaryServerInterceptor(ds),
},
{
Name: "consistency",
UnaryMiddleware: consistency.UnaryServerInterceptor(),
StreamingMiddleware: consistency.StreamServerInterceptor(),
Name: "consistency",
Middleware: consistency.UnaryServerInterceptor(),
},
{
Name: "servicespecific",
UnaryMiddleware: servicespecific.UnaryServerInterceptor,
StreamingMiddleware: servicespecific.StreamServerInterceptor,
Name: "servicespecific",
Middleware: servicespecific.UnaryServerInterceptor,
},
},
},
}),
server.SetStreamingMiddlewareModification([]server.MiddlewareModification[grpc.StreamServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware[grpc.StreamServerInterceptor]{
{
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
Name: "datastore",
Middleware: datastoremw.StreamServerInterceptor(ds),
},
{
Name: "consistency",
Middleware: consistency.StreamServerInterceptor(),
},
{
Name: "servicespecific",
Middleware: servicespecific.StreamServerInterceptor,
},
},
},
Expand Down
47 changes: 33 additions & 14 deletions internal/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,48 @@ func NewTestServerWithConfig(require *require.Assertions,
server.WithDashboardAPI(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithMetricsAPI(util.HTTPServerConfig{HTTPEnabled: false}),
server.WithDispatchServer(util.GRPCServerConfig{Enabled: false}),
server.SetMiddlewareModification([]server.MiddlewareModification{
server.SetUnaryMiddlewareModification([]server.MiddlewareModification[grpc.UnaryServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware{
Middlewares: []server.ReferenceableMiddleware[grpc.UnaryServerInterceptor]{
{
Name: "logging",
UnaryMiddleware: logging.UnaryServerInterceptor(),
StreamingMiddleware: logging.StreamServerInterceptor(),
Name: "logging",
Middleware: logging.UnaryServerInterceptor(),
},
{
Name: "datastore",
UnaryMiddleware: datastoremw.UnaryServerInterceptor(ds),
StreamingMiddleware: datastoremw.StreamServerInterceptor(ds),
Name: "datastore",
Middleware: datastoremw.UnaryServerInterceptor(ds),
},
{
Name: "consistency",
UnaryMiddleware: consistency.UnaryServerInterceptor(),
StreamingMiddleware: consistency.StreamServerInterceptor(),
Name: "consistency",
Middleware: consistency.UnaryServerInterceptor(),
},
{
Name: "servicespecific",
UnaryMiddleware: servicespecific.UnaryServerInterceptor,
StreamingMiddleware: servicespecific.StreamServerInterceptor,
Name: "servicespecific",
Middleware: servicespecific.UnaryServerInterceptor,
},
},
},
}),
server.SetStreamingMiddlewareModification([]server.MiddlewareModification[grpc.StreamServerInterceptor]{
{
Operation: server.OperationReplaceAllUnsafe,
Middlewares: []server.ReferenceableMiddleware[grpc.StreamServerInterceptor]{
{
Name: "logging",
Middleware: logging.StreamServerInterceptor(),
},
{
Name: "datastore",
Middleware: datastoremw.StreamServerInterceptor(ds),
},
{
Name: "consistency",
Middleware: consistency.StreamServerInterceptor(),
},
{
Name: "servicespecific",
Middleware: servicespecific.StreamServerInterceptor,
},
},
},
Expand Down
192 changes: 130 additions & 62 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,68 +136,136 @@ const (
DefaultInternalMiddlewareServerSpecific = "servicespecific"
)

// DefaultMiddleware generates the default middleware chain used for the public SpiceDB gRPC API
func DefaultMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc, enableVersionResponse bool, dispatcher dispatch.Dispatcher, ds datastore.Datastore) (*MiddlewareChain, error) {
chain, err := NewMiddlewareChain([]ReferenceableMiddleware{
{
Name: DefaultMiddlewareRequestID,
UnaryMiddleware: requestid.UnaryServerInterceptor(requestid.GenerateIfMissing(true)),
StreamingMiddleware: requestid.StreamServerInterceptor(requestid.GenerateIfMissing(true)),
},
{
Name: DefaultMiddlewareLog,
UnaryMiddleware: logmw.UnaryServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
StreamingMiddleware: logmw.StreamServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
},
{
Name: DefaultMiddlewareGRPCLog,
UnaryMiddleware: grpclog.UnaryServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...),
StreamingMiddleware: grpclog.StreamServerInterceptor(grpczerolog.InterceptorLogger(logger), defaultGRPCLogOptions...),
},
{
Name: DefaultMiddlewareOTelGRPC,
UnaryMiddleware: otelgrpc.UnaryServerInterceptor(),
StreamingMiddleware: otelgrpc.StreamServerInterceptor(),
},
{
Name: DefaultMiddlewareGRPCProm,
UnaryMiddleware: grpcprom.UnaryServerInterceptor,
StreamingMiddleware: grpcprom.StreamServerInterceptor,
},
{
Name: DefaultMiddlewareGRPCAuth,
UnaryMiddleware: grpcauth.UnaryServerInterceptor(authFunc),
StreamingMiddleware: grpcauth.StreamServerInterceptor(authFunc),
},
{
Name: DefaultMiddlewareServerVersion,
UnaryMiddleware: serverversion.UnaryServerInterceptor(enableVersionResponse),
StreamingMiddleware: serverversion.StreamServerInterceptor(enableVersionResponse),
},
{
Name: DefaultInternalMiddlewareDispatch,
Internal: true,
UnaryMiddleware: dispatchmw.UnaryServerInterceptor(dispatcher),
StreamingMiddleware: dispatchmw.StreamServerInterceptor(dispatcher),
},
{
Name: DefaultInternalMiddlewareDatastore,
Internal: true,
UnaryMiddleware: datastoremw.UnaryServerInterceptor(ds),
StreamingMiddleware: datastoremw.StreamServerInterceptor(ds),
},
{
Name: DefaultInternalMiddlewareConsistency,
Internal: true,
UnaryMiddleware: consistencymw.UnaryServerInterceptor(),
StreamingMiddleware: consistencymw.StreamServerInterceptor(),
},
{
Name: DefaultInternalMiddlewareServerSpecific,
Internal: true,
UnaryMiddleware: servicespecific.UnaryServerInterceptor,
StreamingMiddleware: servicespecific.StreamServerInterceptor,
},
// 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]{
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]{
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