Skip to content

Commit

Permalink
Merge pull request #405 from hellofresh/feature/improve_ratelimit_log…
Browse files Browse the repository at this point in the history
…ging

Log correct IP in ratelimiter if we trust forward headers
  • Loading branch information
Seklfreak authored Aug 2, 2019
2 parents f37b43b + f9bb3b4 commit 7f40345
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Unreleased
- None
- Rate limiter logs correct (forwarded) IP if configuration is set to respect `X-Forwarded-For` and `X-Real-IP` headers


# 3.8.9
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/rate/rate_limit_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ const (
)

// NewRateLimitLogger logs the IP of blocked users with rate limit
func NewRateLimitLogger(lmt *limiter.Limiter, statsClient client.Client) func(handler http.Handler) http.Handler {
func NewRateLimitLogger(lmt *limiter.Limiter, statsClient client.Client, trustForwardHeaders bool) func(handler http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Debug("Starting RateLimitLogger.WriterWrapper middleware")

m := httpsnoop.CaptureMetrics(handler, w, r)

limiterIP := limiter.GetIP(r)
limiterIP := limiter.GetIP(r, trustForwardHeaders)
if m.Code == http.StatusTooManyRequests {
log.WithFields(log.Fields{
"ip_address": limiterIP.String(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/rate/rate_limit_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSuccessfulRateLimitLog(t *testing.T) {
rate, _ := limiter.NewRateFromFormatted("100-M")
limiterInstance := limiter.New(limiterStore, rate)

mw := NewRateLimitLogger(limiterInstance, statsClient)
mw := NewRateLimitLogger(limiterInstance, statsClient, false)
w, err := test.Record(
"GET",
"/",
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/rate/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func setupRateLimit(def *proxy.RouterDefinition, rawConfig plugin.Config) error
}

limiterInstance := limiter.New(limiterStore, rate)
def.AddMiddleware(NewRateLimitLogger(limiterInstance, statsClient))
def.AddMiddleware(NewRateLimitLogger(limiterInstance, statsClient, config.TrustForwardHeaders))
def.AddMiddleware(stdlib.NewMiddleware(limiterInstance, stdlib.WithForwardHeader(config.TrustForwardHeaders)).Handler)

return nil
Expand Down

0 comments on commit 7f40345

Please sign in to comment.