metrics: rate limit /metrics per client address before Basic Auth (closes #101)
check / check (push) Successful in 1m18s
check / check (push) Successful in 1m18s
/metrics is behind a password, and REPO_POLICIES.md requires rate limiting on password logins. Each client address may now send it 30 requests a minute, counted by httprate before Basic Auth, so failed logins use up the allowance and a request over it gets 429 without the password being checked. The address is the one the existing trusted-proxy logic in internal/middleware works out, with IPv6 addresses grouped by /64; an IPv4 address a proxy reports in IPv6-mapped form counts as the plain IPv4 address. A Prometheus server scraping every 15 seconds sends 4 requests a minute. Model: opus-5-5
This commit was merged in pull request #180.
This commit is contained in:
@@ -64,9 +64,12 @@ func (s *Server) SetupRoutes() {
|
||||
// Prometheus scraper is not a browser. It is mounted rather than
|
||||
// added with Get so that every method on /metrics, OPTIONS
|
||||
// included, ends here instead of falling through to the public
|
||||
// router and its CORS.
|
||||
// router and its CORS. The rate limit comes before Basic Auth, so
|
||||
// failed logins count against it and a request over the limit
|
||||
// never reaches the password check.
|
||||
if s.params.Config.MetricsUsername != "" {
|
||||
metrics := chi.NewRouter()
|
||||
metrics.Use(s.mw.MetricsRateLimit())
|
||||
metrics.Use(s.mw.MetricsAuth())
|
||||
metrics.Get("/", promhttp.Handler().ServeHTTP)
|
||||
s.router.Mount("/metrics", metrics)
|
||||
|
||||
@@ -219,3 +219,72 @@ func TestPreflightAllowsOnlyWhatPublicRoutesServe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// metricsRequest builds a GET for /metrics from remoteAddr that logs
|
||||
// in with the given password.
|
||||
func metricsRequest(
|
||||
t *testing.T,
|
||||
remoteAddr string,
|
||||
password string,
|
||||
) *http.Request {
|
||||
t.Helper()
|
||||
|
||||
req := httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/metrics", nil,
|
||||
)
|
||||
req.RemoteAddr = remoteAddr
|
||||
req.SetBasicAuth(metricsUsername, password)
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
// TestMetricsRateLimitComesBeforeAuth checks that failed logins to
|
||||
// /metrics count against the rate limit; that once an address is over
|
||||
// it, even the right password gets 429, with the same body as a wrong
|
||||
// one; and that another address still gets in.
|
||||
func TestMetricsRateLimitComesBeforeAuth(t *testing.T) {
|
||||
viper.Reset()
|
||||
t.Setenv("DNSWATCHER_TARGETS", "example.com")
|
||||
t.Setenv("DNSWATCHER_METRICS_USERNAME", metricsUsername)
|
||||
t.Setenv("DNSWATCHER_METRICS_PASSWORD", metricsPassword)
|
||||
|
||||
const (
|
||||
guesser = "198.51.100.1:4000"
|
||||
other = "198.51.100.2:4000"
|
||||
|
||||
// Far more guesses than the rate limit allows.
|
||||
maxGuesses = 1000
|
||||
)
|
||||
|
||||
srv := routedServer(t)
|
||||
|
||||
var guess *httptest.ResponseRecorder
|
||||
|
||||
for range maxGuesses {
|
||||
guess = serve(srv, metricsRequest(t, guesser, "wrong"))
|
||||
if guess.Code != http.StatusUnauthorized {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if guess.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("wrong password: status = %d, want 429", guess.Code)
|
||||
}
|
||||
|
||||
right := serve(srv, metricsRequest(t, guesser, metricsPassword))
|
||||
if right.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("right password: status = %d, want 429", right.Code)
|
||||
}
|
||||
|
||||
if right.Body.String() != guess.Body.String() {
|
||||
t.Errorf(
|
||||
"429 body with right password = %q, with wrong one = %q",
|
||||
right.Body.String(), guess.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
rec := serve(srv, metricsRequest(t, other, metricsPassword))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("another address: status = %d, want 200", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user