From 08a7f08d6eb91a882f27595ce80815a2d42de831 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 4 Jan 2022 09:48:16 +0100 Subject: [PATCH] httpd: switch back to chi Recoverer now that the required patch is merged Signed-off-by: Nicola Murino --- go.mod | 4 ++-- go.sum | 4 ++++ httpd/internal_test.go | 3 ++- httpd/middleware.go | 27 --------------------------- httpd/server.go | 2 +- util/util.go | 2 +- 6 files changed, 10 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index ae545191..6cb4a5f1 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,12 @@ require ( github.com/Azure/azure-storage-blob-go v0.14.0 github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 github.com/alexedwards/argon2id v0.0.0-20211130144151-3585854a6387 - github.com/aws/aws-sdk-go v1.42.25 + github.com/aws/aws-sdk-go v1.42.26 github.com/cockroachdb/cockroach-go/v2 v2.2.5 github.com/eikenb/pipeat v0.0.0-20210603033007-44fc3ffce52b github.com/fclairamb/ftpserverlib v0.17.0 github.com/fclairamb/go-log v0.2.0 - github.com/go-chi/chi/v5 v5.0.7 + github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f github.com/go-chi/jwtauth/v5 v5.0.2 github.com/go-chi/render v1.0.1 github.com/go-sql-driver/mysql v1.6.0 diff --git a/go.sum b/go.sum index ff25396c..9c247f5b 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,8 @@ github.com/aws/aws-sdk-go v1.38.68/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2z github.com/aws/aws-sdk-go v1.40.34/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= github.com/aws/aws-sdk-go v1.42.25 h1:BbdvHAi+t9LRiaYUyd53noq9jcaAcfzOhSVbKfr6Avs= github.com/aws/aws-sdk-go v1.42.25/go.mod h1:gyRszuZ/icHmHAVE4gc/r+cfCmhA1AD+vqfWbgI+eHs= +github.com/aws/aws-sdk-go v1.42.26 h1:3+GcxzyI+kvqoASDNeeLZfqGkvyNMrE9IDuErxPRtCA= +github.com/aws/aws-sdk-go v1.42.26/go.mod h1:gyRszuZ/icHmHAVE4gc/r+cfCmhA1AD+vqfWbgI+eHs= github.com/aws/aws-sdk-go-v2 v1.7.0/go.mod h1:tb9wi5s61kTDA5qCkcDbt3KRVV74GGslQkl/DRdX/P4= github.com/aws/aws-sdk-go-v2 v1.9.0/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= github.com/aws/aws-sdk-go-v2/config v1.7.0/go.mod h1:w9+nMZ7soXCe5nT46Ri354SNhXDQ6v+V5wqDjnZE+GY= @@ -271,6 +273,8 @@ github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwv github.com/go-chi/chi/v5 v5.0.4/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f h1:6kLofhLkWj7lgCc+mvcVLnwhTzQYgL/yW/Y0e/JYwjg= +github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/jwtauth/v5 v5.0.2 h1:CSKtr+b6Jnfy5T27sMaiBPxaVE/bjnjS3ramFQ0526w= github.com/go-chi/jwtauth/v5 v5.0.2/go.mod h1:TeA7vmPe3uYThvHw8O8W13HOOpOd4MTgToxL41gZyjs= github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8= diff --git a/httpd/internal_test.go b/httpd/internal_test.go index 81326adb..549fb96a 100644 --- a/httpd/internal_test.go +++ b/httpd/internal_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/jwtauth/v5" "github.com/klauspost/compress/zip" "github.com/lestrrat-go/jwx/jwa" @@ -1529,7 +1530,7 @@ func TestRecoverer(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, rr.Code, rr.Body.String()) server.router = chi.NewRouter() - server.router.Use(recoverer) + server.router.Use(middleware.Recoverer) server.router.Get(recoveryPath, func(w http.ResponseWriter, r *http.Request) { panic("panic") }) diff --git a/httpd/middleware.go b/httpd/middleware.go index c56872a4..82480f66 100644 --- a/httpd/middleware.go +++ b/httpd/middleware.go @@ -4,11 +4,9 @@ import ( "errors" "fmt" "net/http" - "runtime/debug" "strings" "time" - "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/jwtauth/v5" "github.com/lestrrat-go/jwx/jwt" "github.com/rs/xid" @@ -332,31 +330,6 @@ func forbidAPIKeyAuthentication(next http.Handler) http.Handler { }) } -func recoverer(next http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - defer func() { - if rvr := recover(); rvr != nil { - if rvr == http.ErrAbortHandler { - panic(rvr) - } - - logEntry := middleware.GetLogEntry(r) - if logEntry != nil { - logEntry.Panic(rvr, debug.Stack()) - } else { - middleware.PrintPrettyStack(rvr) - } - - w.WriteHeader(http.StatusInternalServerError) - } - }() - - next.ServeHTTP(w, r) - } - - return http.HandlerFunc(fn) -} - func authenticateAdminWithAPIKey(username, keyID string, tokenAuth *jwtauth.JWTAuth, r *http.Request) error { if username == "" { return errors.New("the provided key is not associated with any admin and no username was provided") diff --git a/httpd/server.go b/httpd/server.go index 0f2c1371..337d1f08 100644 --- a/httpd/server.go +++ b/httpd/server.go @@ -969,7 +969,7 @@ func (s *httpdServer) initializeRouter() { s.router.Use(middleware.RequestID) s.router.Use(s.checkConnection) s.router.Use(logger.NewStructuredLogger(logger.GetLogger())) - s.router.Use(recoverer) + s.router.Use(middleware.Recoverer) if s.cors.Enabled { c := cors.New(cors.Options{ AllowedOrigins: s.cors.AllowedOrigins, diff --git a/util/util.go b/util/util.go index 87479d0c..dfaa9f21 100644 --- a/util/util.go +++ b/util/util.go @@ -498,7 +498,7 @@ func GetRealIP(r *http.Request) string { } ip = strings.TrimSpace(xff[:i]) } - if net.ParseIP(ip) == nil { + if ip == "" || net.ParseIP(ip) == nil { return "" }