-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log key when rate limit has been exceeded. #57
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,10 @@ type Handler struct { | |
// the global or default storage configuration will be used. | ||
StorageRaw json.RawMessage `json:"storage,omitempty" caddy:"namespace=caddy.storage inline_key=module"` | ||
|
||
// LogKey, if true, will log the key used for rate limiting. | ||
// Defaults to `false` because keys can contain sensitive information. | ||
LogKey bool `json:"log_key,omitempty"` | ||
|
||
rateLimits []*RateLimit | ||
storage certmagic.Storage | ||
random *weakrand.Rand | ||
|
@@ -170,7 +174,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt | |
if h.Distributed == nil { | ||
// internal rate limiter only | ||
if dur := limiter.When(); dur > 0 { | ||
return h.rateLimitExceeded(w, r, repl, rl.zoneName, dur) | ||
return h.rateLimitExceeded(w, r, repl, rl.zoneName, key, dur) | ||
} | ||
} else { | ||
// distributed rate limiting; add last known state of other instances | ||
|
@@ -183,7 +187,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhtt | |
return next.ServeHTTP(w, r) | ||
} | ||
|
||
func (h *Handler) rateLimitExceeded(w http.ResponseWriter, r *http.Request, repl *caddy.Replacer, zoneName string, wait time.Duration) error { | ||
func (h *Handler) rateLimitExceeded(w http.ResponseWriter, r *http.Request, repl *caddy.Replacer, zoneName string, key string, wait time.Duration) error { | ||
// add jitter, if configured | ||
if h.random != nil { | ||
jitter := h.randomFloatInRange(0, float64(wait)*h.Jitter) | ||
|
@@ -198,11 +202,21 @@ func (h *Handler) rateLimitExceeded(w http.ResponseWriter, r *http.Request, repl | |
if err != nil { | ||
remoteIP = r.RemoteAddr // assume there was no port, I guess | ||
} | ||
h.logger.Info("rate limit exceeded", | ||
zap.String("zone", zoneName), | ||
zap.Duration("wait", wait), | ||
zap.String("remote_ip", remoteIP), | ||
) | ||
|
||
if h.LogKey { | ||
h.logger.Info("rate limit exceeded", | ||
zap.String("zone", zoneName), | ||
zap.String("key", key), | ||
zap.Duration("wait", wait), | ||
zap.String("remote_ip", remoteIP), | ||
) | ||
} else { | ||
h.logger.Info("rate limit exceeded", | ||
zap.String("zone", zoneName), | ||
zap.Duration("wait", wait), | ||
zap.String("remote_ip", remoteIP), | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you can do here instead to avoid duplication is You could alternatively build a slice of zap fields instead of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't know about that. Should be fixed now. Let me know if there's anything else. |
||
|
||
// make some information about this rate limit available | ||
repl.Set("http.rate_limit.exceeded.name", zoneName) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is in the wrong place.