From 1fe7261a90660978039adb8a8e46b095dad69659 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Mon, 8 May 2023 02:09:10 +0000 Subject: Upgrade golangci-lint to v1.52.2 on CI --- .gitlab/ci/test.yml | 2 +- .golangci.yml | 10 +++-- .tool-versions | 1 - app.go | 6 +-- internal/auth/auth.go | 4 +- internal/serving/disk/symlink/symlink.go | 2 +- internal/uniqueDomain/middleware.go | 74 -------------------------------- internal/uniquedomain/middleware.go | 74 ++++++++++++++++++++++++++++++++ internal/vfs/serving/main_test.go | 2 +- internal/vfs/serving/serving_test.go | 2 +- 10 files changed, 89 insertions(+), 88 deletions(-) delete mode 100644 internal/uniqueDomain/middleware.go create mode 100644 internal/uniquedomain/middleware.go diff --git a/.gitlab/ci/test.yml b/.gitlab/ci/test.yml index ef0fd0d2..7af9d2ca 100644 --- a/.gitlab/ci/test.yml +++ b/.gitlab/ci/test.yml @@ -43,7 +43,7 @@ cover: code_quality: extends: .tests-common - image: golangci/golangci-lint:v1.46.2 + image: golangci/golangci-lint:v1.52.2 variables: REPORT_FILE: gl-code-quality-report.json LINT_FLAGS: "--color never --deadline 15m" diff --git a/.golangci.yml b/.golangci.yml index 7009afd9..dd8bf738 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -81,7 +81,6 @@ linters: disable-all: true enable: - bodyclose - - deadcode - dogsled - errorlint - gci @@ -95,17 +94,16 @@ linters: - misspell - revive - staticcheck - - structcheck - typecheck - unconvert - unparam - unused - - varcheck - whitespace - - nolintlint fast: false issues: + exclude: + - "unused-parameter:" # Excluding configuration per-path, per-linter, per-text and per-source exclude-rules: - path: _test\.go @@ -116,6 +114,10 @@ issues: - path: internal/fixture/fixtures\.go linters: - gosec + - path: app.go + text: "G112" + linters: + - gosec # Maximum issues count per one linter. Set to 0 to disable. Default is 50. max-issues-per-linter: 0 diff --git a/.tool-versions b/.tool-versions index 8e754ce1..f434cb94 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,2 @@ golang 1.19.9 -golangci-lint 1.46.2 ruby 3.0.5 diff --git a/app.go b/app.go index aa63fc7f..4fe08c9b 100644 --- a/app.go +++ b/app.go @@ -40,7 +40,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/source" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab" "gitlab.com/gitlab-org/gitlab-pages/internal/tls" - "gitlab.com/gitlab-org/gitlab-pages/internal/uniqueDomain" + "gitlab.com/gitlab-org/gitlab-pages/internal/uniquedomain" "gitlab.com/gitlab-org/gitlab-pages/internal/urilimiter" "gitlab.com/gitlab-org/gitlab-pages/metrics" ) @@ -134,7 +134,7 @@ func setRequestScheme(r *http.Request) *http.Request { func (a *theApp) buildHandlerPipeline() (http.Handler, error) { // Handlers should be applied in a reverse order handler := a.serveFileOrNotFoundHandler() - handler = uniqueDomain.NewMiddleware(handler) + handler = uniquedomain.NewMiddleware(handler) handler = a.Auth.AuthorizationMiddleware(handler) handler = routing.NewMiddleware(handler, a.source) @@ -183,7 +183,7 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) { return handler, nil } -//nolint: gocyclo // ignore this +// nolint: gocyclo // ignore this func (a *theApp) Run() error { var limiter *netutil.Limiter if a.config.General.MaxConns > 0 { diff --git a/internal/auth/auth.go b/internal/auth/auth.go index c532d5f6..dcc81eee 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -28,7 +28,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/source" ) -//nolint: gosec // auth constants, not credentials +// nolint: gosec // auth constants, not credentials // gosec: G101: Potential hardcoded credentials const ( apiURLUserTemplate = "%s/api/v4/user" @@ -198,7 +198,7 @@ func (a *Auth) domainAllowed(ctx context.Context, name string, domains source.So return (domain != nil && err == nil) } -//nolint: gocyclo // TODO refactor this function https://gitlab.com/gitlab-org/gitlab-pages/-/issues/813 +// nolint: gocyclo // TODO refactor this function https://gitlab.com/gitlab-org/gitlab-pages/-/issues/813 func (a *Auth) handleProxyingAuth(session *hostSession, w http.ResponseWriter, r *http.Request, domains source.Source) bool { // handle auth callback e.g. https://gitlab.io/auth?domain=domain&state=state if shouldProxyAuthToGitlab(r) { diff --git a/internal/serving/disk/symlink/symlink.go b/internal/serving/disk/symlink/symlink.go index 35545a4d..fe26aa63 100644 --- a/internal/serving/disk/symlink/symlink.go +++ b/internal/serving/disk/symlink/symlink.go @@ -14,7 +14,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/vfs" ) -//nolint: gocyclo // this is vendored code +// nolint: gocyclo // this is vendored code func walkSymlinks(ctx context.Context, root vfs.Root, path string) (string, error) { volLen := volumeNameLen(path) pathSeparator := string(os.PathSeparator) diff --git a/internal/uniqueDomain/middleware.go b/internal/uniqueDomain/middleware.go deleted file mode 100644 index b0af761a..00000000 --- a/internal/uniqueDomain/middleware.go +++ /dev/null @@ -1,74 +0,0 @@ -package uniqueDomain - -import ( - "net" - "net/http" - "strings" - - "gitlab.com/gitlab-org/gitlab-pages/internal/domain" - "gitlab.com/gitlab-org/gitlab-pages/internal/logging" -) - -func NewMiddleware(handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - uniqueURL := getUniqueURL(r) - if uniqueURL == "" { - logging. - LogRequest(r). - WithField("uniqueURL", uniqueURL). - Debug("unique domain: doing nothing") - - handler.ServeHTTP(w, r) - return - } - - logging. - LogRequest(r). - WithField("uniqueURL", uniqueURL). - Info("redirecting to unique domain") - - http.Redirect(w, r, uniqueURL, http.StatusPermanentRedirect) - }) -} - -func getUniqueURL(r *http.Request) string { - domain := domain.FromRequest(r) - lookupPath, err := domain.GetLookupPath(r) - if err != nil { - logging. - LogRequest(r). - WithError(err). - Error("uniqueDomain: failed to get lookupPath") - return "" - } - - // No uniqueHost to redirect - if lookupPath.UniqueHost == "" { - return "" - } - - requestHost, port, err := net.SplitHostPort(r.Host) - if err != nil { - requestHost = r.Host - } - - // Already serving the uniqueHost - if lookupPath.UniqueHost == requestHost { - return "" - } - - uniqueURL := *r.URL - if port == "" { - uniqueURL.Host = lookupPath.UniqueHost - } else { - uniqueURL.Host = net.JoinHostPort(lookupPath.UniqueHost, port) - } - - // Ensure to redirect to the same path requested - uniqueURL.Path = strings.TrimPrefix( - r.URL.Path, - strings.TrimSuffix(lookupPath.Prefix, "/"), - ) - - return uniqueURL.String() -} diff --git a/internal/uniquedomain/middleware.go b/internal/uniquedomain/middleware.go new file mode 100644 index 00000000..0028822d --- /dev/null +++ b/internal/uniquedomain/middleware.go @@ -0,0 +1,74 @@ +package uniquedomain + +import ( + "net" + "net/http" + "strings" + + "gitlab.com/gitlab-org/gitlab-pages/internal/domain" + "gitlab.com/gitlab-org/gitlab-pages/internal/logging" +) + +func NewMiddleware(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + uniqueURL := getUniqueURL(r) + if uniqueURL == "" { + logging. + LogRequest(r). + WithField("uniqueURL", uniqueURL). + Debug("unique domain: doing nothing") + + handler.ServeHTTP(w, r) + return + } + + logging. + LogRequest(r). + WithField("uniqueURL", uniqueURL). + Info("redirecting to unique domain") + + http.Redirect(w, r, uniqueURL, http.StatusPermanentRedirect) + }) +} + +func getUniqueURL(r *http.Request) string { + domain := domain.FromRequest(r) + lookupPath, err := domain.GetLookupPath(r) + if err != nil { + logging. + LogRequest(r). + WithError(err). + Error("uniqueDomain: failed to get lookupPath") + return "" + } + + // No uniqueHost to redirect + if lookupPath.UniqueHost == "" { + return "" + } + + requestHost, port, err := net.SplitHostPort(r.Host) + if err != nil { + requestHost = r.Host + } + + // Already serving the uniqueHost + if lookupPath.UniqueHost == requestHost { + return "" + } + + uniqueURL := *r.URL + if port == "" { + uniqueURL.Host = lookupPath.UniqueHost + } else { + uniqueURL.Host = net.JoinHostPort(lookupPath.UniqueHost, port) + } + + // Ensure to redirect to the same path requested + uniqueURL.Path = strings.TrimPrefix( + r.URL.Path, + strings.TrimSuffix(lookupPath.Prefix, "/"), + ) + + return uniqueURL.String() +} diff --git a/internal/vfs/serving/main_test.go b/internal/vfs/serving/main_test.go index 85e7c465..c0c2c29f 100644 --- a/internal/vfs/serving/main_test.go +++ b/internal/vfs/serving/main_test.go @@ -23,7 +23,7 @@ func TestMain(m *testing.M) { os.Exit(v) } -//nolint: gocyclo // this is vendored code +// nolint: gocyclo // this is vendored code func interestingGoroutines() (gs []string) { buf := make([]byte, 2<<20) buf = buf[:runtime.Stack(buf, true)] diff --git a/internal/vfs/serving/serving_test.go b/internal/vfs/serving/serving_test.go index 161fd2e2..f7bfc2e9 100644 --- a/internal/vfs/serving/serving_test.go +++ b/internal/vfs/serving/serving_test.go @@ -22,7 +22,7 @@ var ( lastMod = time.Now() ) -//nolint: gocyclo // this is vendored code +// nolint: gocyclo // this is vendored code func TestServeContent(t *testing.T) { defer afterTest(t) type serveParam struct { -- cgit v1.2.3