From 3bc60510ce3521f3669389f75658d4e70b31b0a7 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Thu, 12 Aug 2021 17:14:41 +0200 Subject: [PATCH] use go-chi middlewares go-chi already delivers the middlewares we need so we don't need to import other libraries for that. --- accounts/pkg/server/http/server.go | 5 +++-- go.mod | 2 -- go.sum | 6 ------ graph-explorer/pkg/server/http/server.go | 5 +++-- graph/pkg/server/http/server.go | 3 ++- graph/pkg/service/v0/errorcode/errorcode.go | 11 +++++------ idp/pkg/server/http/server.go | 5 +++-- ocis-pkg/middleware/realip.go | 18 ------------------ ocis-pkg/middleware/requestid.go | 12 ------------ ocis-pkg/service/debug/service.go | 5 +++-- ocs/pkg/server/http/server.go | 5 +++-- onlyoffice/pkg/server/http/server.go | 5 +++-- proxy/pkg/command/server.go | 6 +++--- settings/pkg/server/http/server.go | 5 +++-- web/pkg/server/http/server.go | 5 +++-- webdav/pkg/server/http/server.go | 5 +++-- 16 files changed, 37 insertions(+), 66 deletions(-) delete mode 100644 ocis-pkg/middleware/realip.go delete mode 100644 ocis-pkg/middleware/requestid.go diff --git a/accounts/pkg/server/http/server.go b/accounts/pkg/server/http/server.go index a71ca6f8f..78de9d928 100644 --- a/accounts/pkg/server/http/server.go +++ b/accounts/pkg/server/http/server.go @@ -3,6 +3,7 @@ package http import ( "github.com/asim/go-micro/v3" "github.com/go-chi/chi" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/accounts/pkg/assets" "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/owncloud/ocis/accounts/pkg/version" @@ -28,8 +29,8 @@ func Server(opts ...Option) http.Service { mux := chi.NewMux() - mux.Use(middleware.RealIP) - mux.Use(middleware.RequestID) + mux.Use(chimiddleware.RealIP) + mux.Use(chimiddleware.RequestID) mux.Use(middleware.NoCache) mux.Use(middleware.Cors) mux.Use(middleware.Secure) diff --git a/go.mod b/go.mod index 134f67d7c..c92771161 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( contrib.go.opencensus.io/exporter/zipkin v0.1.2 github.com/CiscoM31/godata v0.0.0-20201003040028-eadcd34e7f06 github.com/GeertJohan/yubigo v0.0.0-20190917122436-175bc097e60e - github.com/ascarter/requestid v0.0.0-20170313220838-5b76ab3d4aee github.com/asim/go-micro/plugins/client/grpc/v3 v3.0.0-20210408173139-0d57213d3f5c github.com/asim/go-micro/plugins/logger/zerolog/v3 v3.0.0-20210217182006-0f0ace1a44a9 github.com/asim/go-micro/plugins/registry/etcd/v3 v3.0.0-20210408173139-0d57213d3f5c @@ -63,7 +62,6 @@ require ( github.com/stretchr/testify v1.7.0 github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect github.com/thejerf/suture/v4 v4.0.1 - github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce github.com/yaegashi/msgraph.go v0.1.4 go.etcd.io/etcd/pkg/v3 v3.5.0-pre // indirect go.opencensus.io v0.23.0 diff --git a/go.sum b/go.sum index f7c126ad9..beb2ec4d4 100644 --- a/go.sum +++ b/go.sum @@ -126,8 +126,6 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0S6Vi7/lbWECcX0j45yZReDZ56BQsrVBOEEY= github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= -github.com/ascarter/requestid v0.0.0-20170313220838-5b76ab3d4aee h1:3T/l+vMotQ7cDSLWNAn2Vg1SAQ3mdyLgBWWBitSS3uU= -github.com/ascarter/requestid v0.0.0-20170313220838-5b76ab3d4aee/go.mod h1:u7Wtt4WATGGgae9mURNGQQqxAudPKrxfsbSDSGOso+g= github.com/asim/go-micro/plugins/broker/memory/v3 v3.0.0-20210202145831-070250155285 h1:DUDMXvS9LfkgDYBRnKEJh7yPfV+tXOvLqIszqNg4DYc= github.com/asim/go-micro/plugins/broker/memory/v3 v3.0.0-20210202145831-070250155285/go.mod h1:IJrBUDk0XAbi0iDg+sKgkY588m311TkSoEAjU9xu/mc= github.com/asim/go-micro/plugins/client/grpc/v3 v3.0.0-20210205090925-e8167a8b79ed/go.mod h1:Mx1EwGdIq7wl5IkKLaGbHtoIC4yvqUNxJtZu6wxsqiU= @@ -288,8 +286,6 @@ github.com/crewjam/saml v0.4.5/go.mod h1:qCJQpUtZte9R1ZjUBcW8qtCNlinbO363ooNl02S github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4= github.com/cs3org/go-cs3apis v0.0.0-20210802070913-970eec344e59 h1:cj9HxIbmbGn+HPpFP8nZ8oaNUsoFa0+cheCO8FUNoMc= github.com/cs3org/go-cs3apis v0.0.0-20210802070913-970eec344e59/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY= -github.com/cs3org/reva v1.11.1-0.20210811142901-ec4099da830e h1:/gJanj5PuKzY51Z2Cp01DjHT2U1HLnEUSDrMf9kC7b8= -github.com/cs3org/reva v1.11.1-0.20210811142901-ec4099da830e/go.mod h1:9DgwsoB4cqlPaEC3hT7/NCdgJO7T1bY3kpxvWXc1DkA= github.com/cs3org/reva v1.11.1-0.20210812105259-756bdced1d22 h1:RZxu/fWJiTmu8fT6pWfu0fdKq7ZUXAZI4TndUfiUIYI= github.com/cs3org/reva v1.11.1-0.20210812105259-756bdced1d22/go.mod h1:9DgwsoB4cqlPaEC3hT7/NCdgJO7T1bY3kpxvWXc1DkA= github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d h1:SwD98825d6bdB+pEuTxWOXiSjBrHdOl/UVp75eI7JT8= @@ -1110,8 +1106,6 @@ github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU= github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= -github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY= github.com/tus/tusd v1.1.0/go.mod h1:3DWPOdeCnjBwKtv98y5dSws3itPqfce5TVa0s59LRiA= github.com/tus/tusd v1.1.1-0.20200416115059-9deabf9d80c2 h1:rcji4q9wMuSrz0tZt3kgIr/3WsB5kUqFja6RrgeCGEo= diff --git a/graph-explorer/pkg/server/http/server.go b/graph-explorer/pkg/server/http/server.go index 5650948af..677b8c1af 100644 --- a/graph-explorer/pkg/server/http/server.go +++ b/graph-explorer/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" svc "github.com/owncloud/ocis/graph-explorer/pkg/service/v0" "github.com/owncloud/ocis/graph-explorer/pkg/version" "github.com/owncloud/ocis/ocis-pkg/middleware" @@ -26,8 +27,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/graph/pkg/server/http/server.go b/graph/pkg/server/http/server.go index 0526ce0ef..a4025857a 100644 --- a/graph/pkg/server/http/server.go +++ b/graph/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" graphMiddleware "github.com/owncloud/ocis/graph/pkg/middleware" svc "github.com/owncloud/ocis/graph/pkg/service/v0" "github.com/owncloud/ocis/graph/pkg/version" @@ -28,7 +29,7 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RequestID, + chimiddleware.RequestID, middleware.Version( "graph", version.String, diff --git a/graph/pkg/service/v0/errorcode/errorcode.go b/graph/pkg/service/v0/errorcode/errorcode.go index 068e68478..ce20e834c 100644 --- a/graph/pkg/service/v0/errorcode/errorcode.go +++ b/graph/pkg/service/v0/errorcode/errorcode.go @@ -4,8 +4,8 @@ import ( "net/http" "time" + "github.com/go-chi/chi/middleware" "github.com/go-chi/render" - "github.com/ascarter/requestid" msgraph "github.com/owncloud/open-graph-api-go" ) @@ -72,13 +72,12 @@ func (e ErrorCode) Render(w http.ResponseWriter, r *http.Request, status int, ms "date": time.Now().UTC().Format(time.RFC3339), // TODO return client-request-id? } - if id, ok := requestid.FromContext(r.Context()); ok { - innererror["request-id"] = id - } + + innererror["request-id"] = middleware.GetReqID(r.Context()) resp := &msgraph.OdataError{ Error: msgraph.OdataErrorMain{ - Code: e.String(), - Message: msg, + Code: e.String(), + Message: msg, Innererror: &innererror, }, } diff --git a/idp/pkg/server/http/server.go b/idp/pkg/server/http/server.go index 72fab91c8..b86bde7a7 100644 --- a/idp/pkg/server/http/server.go +++ b/idp/pkg/server/http/server.go @@ -5,6 +5,7 @@ import ( "os" "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" svc "github.com/owncloud/ocis/idp/pkg/service/v0" pkgcrypto "github.com/owncloud/ocis/ocis-pkg/crypto" "github.com/owncloud/ocis/ocis-pkg/middleware" @@ -57,8 +58,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/ocis-pkg/middleware/realip.go b/ocis-pkg/middleware/realip.go deleted file mode 100644 index f68b17399..000000000 --- a/ocis-pkg/middleware/realip.go +++ /dev/null @@ -1,18 +0,0 @@ -package middleware - -import ( - "net/http" - - "github.com/tomasen/realip" -) - -// RealIP is a middleware that sets a http.Request RemoteAddr. -func RealIP(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if ip := realip.RealIP(r); ip != "" { - r.RemoteAddr = ip - } - - next.ServeHTTP(w, r) - }) -} diff --git a/ocis-pkg/middleware/requestid.go b/ocis-pkg/middleware/requestid.go deleted file mode 100644 index b647464c8..000000000 --- a/ocis-pkg/middleware/requestid.go +++ /dev/null @@ -1,12 +0,0 @@ -package middleware - -import ( - "net/http" - - "github.com/ascarter/requestid" -) - -// RequestID is a convenient middleware to inject a request id. -func RequestID(next http.Handler) http.Handler { - return requestid.RequestIDHandler(next) -} diff --git a/ocis-pkg/service/debug/service.go b/ocis-pkg/service/debug/service.go index 1771fb074..a3b1761ca 100644 --- a/ocis-pkg/service/debug/service.go +++ b/ocis-pkg/service/debug/service.go @@ -4,6 +4,7 @@ import ( "net/http" "net/http/pprof" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/justinas/alice" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -47,8 +48,8 @@ func NewService(opts ...Option) *http.Server { return &http.Server{ Addr: dopts.Address, Handler: alice.New( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/ocs/pkg/server/http/server.go b/ocs/pkg/server/http/server.go index d19dfcc72..2f1b792bb 100644 --- a/ocs/pkg/server/http/server.go +++ b/ocs/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" svc "github.com/owncloud/ocis/ocs/pkg/service/v0" @@ -25,8 +26,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/onlyoffice/pkg/server/http/server.go b/onlyoffice/pkg/server/http/server.go index 9d3a1de24..20717f039 100644 --- a/onlyoffice/pkg/server/http/server.go +++ b/onlyoffice/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" "github.com/owncloud/ocis/onlyoffice/pkg/assets" @@ -27,8 +28,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/proxy/pkg/command/server.go b/proxy/pkg/command/server.go index 9546b92ab..addd0a187 100644 --- a/proxy/pkg/command/server.go +++ b/proxy/pkg/command/server.go @@ -9,13 +9,13 @@ import ( "time" "github.com/coreos/go-oidc" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/justinas/alice" "github.com/micro/cli/v2" "github.com/oklog/run" acc "github.com/owncloud/ocis/accounts/pkg/proto/v0" "github.com/owncloud/ocis/ocis-pkg/conversions" "github.com/owncloud/ocis/ocis-pkg/log" - pkgmiddleware "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/grpc" "github.com/owncloud/ocis/ocis-pkg/sync" "github.com/owncloud/ocis/proxy/pkg/config" @@ -178,8 +178,8 @@ func loadMiddlewares(ctx context.Context, l log.Logger, cfg *config.Config) alic return alice.New( // first make sure we log all requests and redirect to https if necessary - pkgmiddleware.RealIP, - pkgmiddleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.AccessLog(l), middleware.HTTPSRedirect, diff --git a/settings/pkg/server/http/server.go b/settings/pkg/server/http/server.go index cbdd8ccea..e643a40aa 100644 --- a/settings/pkg/server/http/server.go +++ b/settings/pkg/server/http/server.go @@ -3,6 +3,7 @@ package http import ( "github.com/asim/go-micro/v3" "github.com/go-chi/chi" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/ocis-pkg/account" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" @@ -36,8 +37,8 @@ func Server(opts ...Option) http.Service { mux := chi.NewMux() - mux.Use(middleware.RealIP) - mux.Use(middleware.RequestID) + mux.Use(chimiddleware.RealIP) + mux.Use(chimiddleware.RequestID) mux.Use(middleware.NoCache) mux.Use(middleware.Cors) mux.Use(middleware.Secure) diff --git a/web/pkg/server/http/server.go b/web/pkg/server/http/server.go index d569d6faa..a7b325d51 100644 --- a/web/pkg/server/http/server.go +++ b/web/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" webmid "github.com/owncloud/ocis/web/pkg/middleware" @@ -27,8 +28,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure, diff --git a/webdav/pkg/server/http/server.go b/webdav/pkg/server/http/server.go index f4b016b2a..0f7802a02 100644 --- a/webdav/pkg/server/http/server.go +++ b/webdav/pkg/server/http/server.go @@ -2,6 +2,7 @@ package http import ( "github.com/asim/go-micro/v3" + chimiddleware "github.com/go-chi/chi/middleware" "github.com/owncloud/ocis/ocis-pkg/middleware" "github.com/owncloud/ocis/ocis-pkg/service/http" svc "github.com/owncloud/ocis/webdav/pkg/service/v0" @@ -25,8 +26,8 @@ func Server(opts ...Option) (http.Service, error) { svc.Logger(options.Logger), svc.Config(options.Config), svc.Middleware( - middleware.RealIP, - middleware.RequestID, + chimiddleware.RealIP, + chimiddleware.RequestID, middleware.NoCache, middleware.Cors, middleware.Secure,