diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-10-08 08:57:25 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-10-14 09:01:02 +0300 |
commit | 3f78e6ad2b2a99804c979bdd516f8579e8a0d152 (patch) | |
tree | 014344988f6f3f090c89588b0e48189e2844d130 /internal | |
parent | ccfdff303646b86daed2bd9ae7e2f2a5eb4a2c5c (diff) |
chore: use global logger
Diffstat (limited to 'internal')
-rw-r--r-- | internal/ratelimiter/middleware.go | 10 | ||||
-rw-r--r-- | internal/ratelimiter/middleware_test.go | 8 |
2 files changed, 9 insertions, 9 deletions
diff --git a/internal/ratelimiter/middleware.go b/internal/ratelimiter/middleware.go index d0996021..4f7a91e9 100644 --- a/internal/ratelimiter/middleware.go +++ b/internal/ratelimiter/middleware.go @@ -8,6 +8,7 @@ import ( "github.com/sebest/xff" "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/labkit/correlation" + "gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" ) @@ -20,12 +21,11 @@ const ( // SourceIPLimiter middleware ensures that the originating // rate limit. See -rate-limiter -func (rl *RateLimiter) SourceIPLimiter(logger logrus.FieldLogger, handler http.Handler) http.Handler { +func (rl *RateLimiter) SourceIPLimiter(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { host, sourceIP := rl.getReqDetails(r) - if !rl.SourceIPAllowed(sourceIP) { - rl.logSourceIP(logger, r, host, sourceIP) + rl.logSourceIP(r, host, sourceIP) // Only drop requests once FF_ENABLE_RATE_LIMITER is enabled // https://gitlab.com/gitlab-org/gitlab-pages/-/issues/629 @@ -63,8 +63,8 @@ func (rl *RateLimiter) getReqDetails(r *http.Request) (string, string) { return host, ip } -func (rl *RateLimiter) logSourceIP(logger logrus.FieldLogger, r *http.Request, host, sourceIP string) { - logger.WithFields(logrus.Fields{ +func (rl *RateLimiter) logSourceIP(r *http.Request, host, sourceIP string) { + log.WithFields(logrus.Fields{ "handler": "source_ip_rate_limiter", "correlation_id": correlation.ExtractFromContext(r.Context()), "req_scheme": r.URL.Scheme, diff --git a/internal/ratelimiter/middleware_test.go b/internal/ratelimiter/middleware_test.go index eaa199c4..eb431554 100644 --- a/internal/ratelimiter/middleware_test.go +++ b/internal/ratelimiter/middleware_test.go @@ -23,7 +23,7 @@ var next = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { }) func TestSourceIPLimiterWithDifferentLimits(t *testing.T) { - log, hook := testlog.NewNullLogger() + hook := testlog.NewGlobal() testhelpers.EnableRateLimiter(t) for tn, tc := range sharedTestCases { @@ -41,7 +41,7 @@ func TestSourceIPLimiterWithDifferentLimits(t *testing.T) { rr.Header.Set(headerXForwardedFor, xForwardedFor) rr.RemoteAddr = remoteAddr - handler := rl.SourceIPLimiter(log, next) + handler := rl.SourceIPLimiter(next) if tc.proxied { handler = ghandlers.ProxyHeaders(handler) } @@ -69,7 +69,7 @@ func TestSourceIPLimiterWithDifferentLimits(t *testing.T) { } func TestSourceIPLimiterDenyRequestsAfterBurst(t *testing.T) { - log, hook := testlog.NewNullLogger() + hook := testlog.NewGlobal() tcs := map[string]struct { enabled bool @@ -143,7 +143,7 @@ func TestSourceIPLimiterDenyRequestsAfterBurst(t *testing.T) { rr.RemoteAddr = remoteAddr // middleware is evaluated in reverse order - handler := rl.SourceIPLimiter(log, next) + handler := rl.SourceIPLimiter(next) if tc.proxied { handler = ghandlers.ProxyHeaders(handler) } |