Merge pull request #2389 from owncloud/replace-middlewares

use go-chi middlewares
This commit is contained in:
David Christofas
2021-08-12 17:52:04 +02:00
committed by GitHub
16 changed files with 37 additions and 66 deletions

View File

@@ -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)

2
go.mod
View File

@@ -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

6
go.sum
View File

@@ -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=
@@ -1112,8 +1108,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=

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,
},
}

View File

@@ -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,

View File

@@ -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)
})
}

View File

@@ -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)
}

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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)

View File

@@ -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,

View File

@@ -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,