diff --git a/go.mod b/go.mod index 41939a501..ccacd9905 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/golang/protobuf v1.4.3 github.com/gorilla/sessions v1.1.3 github.com/gorilla/websocket v1.4.2 + github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 github.com/julienschmidt/httprouter v1.2.0 github.com/markbates/pkger v0.17.1 github.com/ory/cli v0.0.11 @@ -44,6 +45,7 @@ require ( github.com/tidwall/gjson v1.6.0 github.com/tidwall/sjson v1.1.1 github.com/uber/jaeger-lib v2.4.0+incompatible // indirect + github.com/urfave/negroni v1.0.0 go.mongodb.org/mongo-driver v1.3.4 // indirect golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 diff --git a/go.sum b/go.sum index 96a82937c..30073e3bc 100644 --- a/go.sum +++ b/go.sum @@ -642,6 +642,7 @@ github.com/gotestyourself/gotestyourself v1.3.0 h1:9X3T0HDKAY/58/sEPpTkmyOg4wbb1 github.com/gotestyourself/gotestyourself v1.3.0/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI= github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= diff --git a/internal/driver/daemon.go b/internal/driver/daemon.go index 070137941..934eadaec 100644 --- a/internal/driver/daemon.go +++ b/internal/driver/daemon.go @@ -6,7 +6,6 @@ import ( "net/http" "strings" - "github.com/julienschmidt/httprouter" "github.com/ory/graceful" "github.com/pkg/errors" "github.com/soheilhy/cmux" @@ -24,7 +23,7 @@ func (r *RegistryDefault) ServeAll(ctx context.Context) error { } func (r *RegistryDefault) ServeRead(ctx context.Context) func() error { - rt, s := r.ReadRouter().Router, r.ReadGRPCServer() + rt, s := r.ReadRouter(), r.ReadGRPCServer() return func() error { return multiplexPort(ctx, r.Config().ReadAPIListenOn(), rt, s) @@ -32,14 +31,14 @@ func (r *RegistryDefault) ServeRead(ctx context.Context) func() error { } func (r *RegistryDefault) ServeWrite(ctx context.Context) func() error { - rt, s := r.WriteRouter().Router, r.WriteGRPCServer() + rt, s := r.WriteRouter(), r.WriteGRPCServer() return func() error { return multiplexPort(ctx, r.Config().WriteAPIListenOn(), rt, s) } } -func multiplexPort(ctx context.Context, addr string, router *httprouter.Router, grpcS *grpc.Server) error { +func multiplexPort(ctx context.Context, addr string, router http.Handler, grpcS *grpc.Server) error { l, err := net.Listen("tcp", addr) if err != nil { return err diff --git a/internal/driver/registry.go b/internal/driver/registry.go index a30179518..b6d16fdce 100644 --- a/internal/driver/registry.go +++ b/internal/driver/registry.go @@ -2,6 +2,7 @@ package driver import ( "context" + "net/http" "google.golang.org/grpc" @@ -38,8 +39,8 @@ type ( HealthHandler() *healthx.Handler Tracer() *tracing.Tracer - ReadRouter() *x.ReadRouter - WriteRouter() *x.WriteRouter + ReadRouter() http.Handler + WriteRouter() http.Handler ReadGRPCServer() *grpc.Server WriteGRPCServer() *grpc.Server diff --git a/internal/driver/registry_default.go b/internal/driver/registry_default.go index 44bf3ddbb..71643c5b6 100644 --- a/internal/driver/registry_default.go +++ b/internal/driver/registry_default.go @@ -3,8 +3,14 @@ package driver import ( "bytes" "context" + "net/http" "strings" + grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" + "github.com/ory/x/reqlog" + "github.com/urfave/negroni" + + grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware" "github.com/julienschmidt/httprouter" "google.golang.org/grpc" "google.golang.org/grpc/health" @@ -188,7 +194,9 @@ func (r *RegistryDefault) allHandlers() []Handler { return r.handlers } -func (r *RegistryDefault) ReadRouter() *x.ReadRouter { +func (r *RegistryDefault) ReadRouter() http.Handler { + n := negroni.New(reqlog.NewMiddlewareFromLogger(r.l, "write#Ory Keto")) + br := &x.ReadRouter{Router: httprouter.New()} r.HealthHandler().SetRoutes(br.Router, false) @@ -197,10 +205,13 @@ func (r *RegistryDefault) ReadRouter() *x.ReadRouter { h.RegisterReadRoutes(br) } - return br + n.UseHandler(br) + return n } -func (r *RegistryDefault) WriteRouter() *x.WriteRouter { +func (r *RegistryDefault) WriteRouter() http.Handler { + n := negroni.New(reqlog.NewMiddlewareFromLogger(r.l, "write#Ory Keto")) + pr := &x.WriteRouter{Router: httprouter.New()} r.HealthHandler().SetRoutes(pr.Router, false) @@ -209,11 +220,23 @@ func (r *RegistryDefault) WriteRouter() *x.WriteRouter { h.RegisterWriteRoutes(pr) } - return pr + n.UseHandler(pr) + return n } func (r *RegistryDefault) ReadGRPCServer() *grpc.Server { - s := grpc.NewServer() + s := grpc.NewServer( + grpc.StreamInterceptor( + grpcMiddleware.ChainStreamServer( + grpc_logrus.StreamServerInterceptor(r.l.Entry), + ), + ), + grpc.UnaryInterceptor( + grpcMiddleware.ChainUnaryServer( + grpc_logrus.UnaryServerInterceptor(r.l.Entry), + ), + ), + ) grpcHealthV1.RegisterHealthServer(s, r.HealthServer()) @@ -225,7 +248,18 @@ func (r *RegistryDefault) ReadGRPCServer() *grpc.Server { } func (r *RegistryDefault) WriteGRPCServer() *grpc.Server { - s := grpc.NewServer() + s := grpc.NewServer( + grpc.StreamInterceptor( + grpcMiddleware.ChainStreamServer( + grpc_logrus.StreamServerInterceptor(r.l.Entry), + ), + ), + grpc.UnaryInterceptor( + grpcMiddleware.ChainUnaryServer( + grpc_logrus.UnaryServerInterceptor(r.l.Entry), + ), + ), + ) grpcHealthV1.RegisterHealthServer(s, r.HealthServer())