feat(ops): protect metrics, rate-limit auth, agent secret timing, e2e smoke
Bearer для /metrics (EVOBGP_METRICS_TOKEN); rate limit /v1/auth/config; constant-time agent secret; OTel stub; Playwright smoke; HTTP_PROXY note; checklist обновлён. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// authRateLimiter is a simple per-IP token bucket for public auth-ish endpoints.
|
||||
type authRateLimiter struct {
|
||||
mu sync.Mutex
|
||||
hits map[string][]time.Time
|
||||
limit int
|
||||
window time.Duration
|
||||
}
|
||||
|
||||
func newAuthRateLimiter() *authRateLimiter {
|
||||
limit := 60
|
||||
if n, err := strconv.Atoi(strings.TrimSpace(os.Getenv("EVOBGP_AUTH_RATE_LIMIT"))); err == nil && n > 0 {
|
||||
limit = n
|
||||
}
|
||||
return &authRateLimiter{
|
||||
hits: make(map[string][]time.Time),
|
||||
limit: limit,
|
||||
window: time.Minute,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *authRateLimiter) allow(ip string) bool {
|
||||
if l == nil {
|
||||
return true
|
||||
}
|
||||
now := time.Now()
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
cut := now.Add(-l.window)
|
||||
arr := l.hits[ip]
|
||||
kept := arr[:0]
|
||||
for _, t := range arr {
|
||||
if t.After(cut) {
|
||||
kept = append(kept, t)
|
||||
}
|
||||
}
|
||||
if len(kept) >= l.limit {
|
||||
l.hits[ip] = kept
|
||||
return false
|
||||
}
|
||||
kept = append(kept, now)
|
||||
l.hits[ip] = kept
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) withAuthRateLimit(next http.HandlerFunc) http.HandlerFunc {
|
||||
if s.authLimiter == nil {
|
||||
s.authLimiter = newAuthRateLimiter()
|
||||
}
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ip := clientIP(r)
|
||||
if !s.authLimiter.allow(ip) {
|
||||
writeProblem(w, http.StatusTooManyRequests, "Too Many Requests", "auth rate limit exceeded")
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
@@ -30,12 +30,12 @@ func (s *Server) Handler() http.Handler {
|
||||
s.registerV1(v1)
|
||||
wrappedV1 := http.StripPrefix("/v1", v1)
|
||||
|
||||
s.mux.Handle("GET /metrics", observability.MetricsHandler())
|
||||
s.mux.Handle("GET /metrics", observability.ProtectMetrics(observability.MetricsHandler()))
|
||||
s.mux.HandleFunc("GET /version", s.handleVersion)
|
||||
s.mux.HandleFunc("GET /v1/health", s.handleHealth)
|
||||
s.mux.HandleFunc("GET /v1/ready", s.handleReady)
|
||||
s.mux.HandleFunc("GET /v1/version", s.handleVersion)
|
||||
s.mux.HandleFunc("GET /v1/auth/config", s.handleAuthConfigPublic)
|
||||
s.mux.HandleFunc("GET /v1/auth/config", s.withAuthRateLimit(s.handleAuthConfigPublic))
|
||||
// Firewall subsystem moved to the standalone EvoFirewall service; see docs/firewall.md.
|
||||
// Registered on the public mux so it wins over the "/v1/" subtree below regardless of auth.
|
||||
s.mux.HandleFunc("/v1/firewall/", s.handleFirewallGone)
|
||||
|
||||
@@ -29,6 +29,7 @@ type Server struct {
|
||||
maintConfig *maintenance.ConfigProvider
|
||||
maintStats *maintenance.DBStatsProvider
|
||||
jobs *jobs.Registry
|
||||
authLimiter *authRateLimiter
|
||||
bundlePriv ed25519.PrivateKey
|
||||
keyResolver *apiKeyResolver
|
||||
firewallResolver *firewallTokenResolver
|
||||
|
||||
Reference in New Issue
Block a user