From 81bfabaf43b6c13c0ca3e9c5a5c1c9ea3d0f23c3 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 19 Nov 2026 08:25:06 +0000 Subject: [PATCH] sww: wire the daemons with fx (closes #414) go.uber.org/fx builds the parts of waf and rproxy in internal/sww, one provider per part. Each background loop is a lifecycle hook added where its part is built. waf.Params takes every part built, and waf.New builds none. GeoJS calls back into waf, so waf exports AddLookup and GeoJSFailed, and clientGroup and banNetblock take the settings. newServer, loadStateFiles, the inBackground list and serveRProxy are gone. serve is the one hook that serves both daemons, and reports a failure to run on a channel of its own; fx's signal handling is not used. Once fx has stopped, run takes each daemon's last step: waf writes the state files, and rproxy waits for its reports. Model: opus-5-5 --- README.md | 2 + SPEC.md | 4 +- go.mod | 4 + go.sum | 8 + internal/sww/lifecycle.go | 198 ++++++++++++ internal/sww/rproxy.go | 105 +++---- internal/sww/sww.go | 397 +----------------------- internal/sww/waf.go | 480 ++++++++++++++++++++++++++++++ internal/waf/abuseipdb.go | 2 +- internal/waf/admin.go | 4 +- internal/waf/answertime_test.go | 19 +- internal/waf/bans.go | 15 +- internal/waf/besidelookup_test.go | 2 +- internal/waf/bytelimits.go | 2 +- internal/waf/client.go | 6 +- internal/waf/coreruleset.go | 2 +- internal/waf/lookup.go | 38 ++- internal/waf/proxy.go | 165 +++------- internal/waf/proxy_test.go | 156 +++++++++- internal/waf/ratelimits.go | 9 +- internal/waf/refusallimit.go | 7 +- internal/waf/reputation.go | 2 +- internal/waf/request.go | 10 +- internal/waf/routewatchtags.go | 2 +- 24 files changed, 995 insertions(+), 644 deletions(-) create mode 100644 internal/sww/lifecycle.go create mode 100644 internal/sww/waf.go diff --git a/README.md b/README.md index c947ac5..5b2c999 100644 --- a/README.md +++ b/README.md @@ -750,6 +750,8 @@ stops the running process (see [`docs/state-files.md`](docs/state-files.md)). - `cmd/sww`: the binary, which only calls `internal/sww`. - `internal/sww`: the process, as the command it is given, and the health check. + It is the only package that imports `go.uber.org/fx`, which builds the parts + of `waf` and `rproxy`. - `internal/config`: reads the settings, the one place they are read. - `internal/rproxy`: the reverse proxy in front of one app. - `internal/waf`: `waf`'s server, which runs the checks and answers its own diff --git a/SPEC.md b/SPEC.md index ca67a0a..3651477 100644 --- a/SPEC.md +++ b/SPEC.md @@ -69,7 +69,9 @@ never as a time. that command's settings, and runs `internal/rproxy` until `SIGTERM` or `SIGINT` (see "`rproxy`"). `sww healthcheck` is the image's health check. With no command, or one it does not know, the binary prints the commands - and exits with status 2. + and exits with status 2. It is the only package that imports + `go.uber.org/fx`. fx builds the parts of `waf` and `rproxy`, and starts + and stops their background work. - `internal/config`: reads the settings, the one place they are read. - `internal/waf`: `waf`'s server. It runs the checks in `check`, and holds `waf`'s question, answer and report. It answers `waf`'s own endpoints in diff --git a/go.mod b/go.mod index 5f24224..959cd68 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/oschwald/maxminddb-golang/v2 v2.7.0 github.com/prometheus/client_golang v1.24.1 github.com/spf13/viper v1.21.0 + go.uber.org/fx v1.24.0 golang.org/x/sys v0.48.0 ) @@ -44,6 +45,9 @@ require ( github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/valllabh/ocsf-schema-golang v1.0.3 // indirect + go.uber.org/dig v1.19.0 // indirect + go.uber.org/multierr v1.10.0 // indirect + go.uber.org/zap v1.26.0 // indirect go.yaml.in/yaml/v3 v3.0.5 // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect golang.org/x/net v0.60.0 // indirect diff --git a/go.sum b/go.sum index 0f8ac76..f9328ba 100644 --- a/go.sum +++ b/go.sum @@ -97,8 +97,16 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/valllabh/ocsf-schema-golang v1.0.3 h1:eR8k/3jP/OOqB8LRCtdJ4U+vlgd/gk5y3KMXoodrsrw= github.com/valllabh/ocsf-schema-golang v1.0.3/go.mod h1:sZ3as9xqm1SSK5feFWIR2CuGeGRhsM7TR1MbpBctzPk= +go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4= +go.uber.org/dig v1.19.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= +go.uber.org/fx v1.24.0 h1:wE8mruvpg2kiiL1Vqd0CC+tr0/24XIB10Iwp2lLWzkg= +go.uber.org/fx v1.24.0/go.mod h1:AmDeGyS+ZARGKM4tlH4FY2Jr63VjbEDJHtqXTGP5hbo= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ= go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= diff --git a/internal/sww/lifecycle.go b/internal/sww/lifecycle.go new file mode 100644 index 0000000..bea8d3f --- /dev/null +++ b/internal/sww/lifecycle.go @@ -0,0 +1,198 @@ +package sww + +import ( + "context" + "errors" + "io" + "log/slog" + "net" + "net/http" + "time" + + "go.uber.org/fx" + "go.uber.org/fx/fxevent" +) + +// shutdownTimeout is how long requests in progress may take to finish +// once sww is told to stop, before their connections are closed. +// runit and docker wait a little longer before they kill the process. +const shutdownTimeout = 5 * time.Second + +// requestLog is where a daemon's server writes the request log: stdout, and +// the remote log sender while there is one. fx tells parts apart by type, so +// it has a type of its own. +type requestLog struct { + io.Writer +} + +// servingFailed gets why serving failed, for run, which then stops the +// daemon. fx tells parts apart by type, so it has a type of its own. +type servingFailed chan error + +// utcNow is the time the state files, the alerts and the rate limits go by. +func utcNow() time.Time { + return time.Now().UTC() +} + +// startError is why a daemon cannot start: the message its process log gives, +// and the error it logs with it. +type startError struct { + message string + err error +} + +func (e startError) Error() string { + return e.message + ": " + e.err.Error() +} + +func (e startError) Unwrap() error { + return e.err +} + +// cannotStart logs why a daemon cannot start, with the message of the +// startError in err, and returns its exit status. +func cannotStart(processLog *slog.Logger, err error) int { + message, cause := "cannot start", err + + failed, ok := errors.AsType[startError](err) + if ok { + message, cause = failed.message, failed.err + } + + processLog.Error(message, "error", cause.Error()) + + return 1 +} + +// listen listens on address, which the setting names. +func listen(address, setting string) (net.Listener, error) { + listener, err := (&net.ListenConfig{}).Listen( + context.Background(), "tcp", address) + if err != nil { + return nil, startError{"cannot listen on " + setting, err} + } + + return listener, nil +} + +// background returns the hook that runs loop on a goroutine of its own from +// the start, and at the stop ends it and waits for it to return. +func background(loop func(context.Context)) fx.Hook { + running, stop := context.WithCancel(context.Background()) + ended := make(chan struct{}) + + return fx.StartStopHook( + func() { + go func() { + loop(running) + close(ended) + }() + }, + func() { + stop() + <-ended + }, + ) +} + +// serve serves requests on listener from the start. At the stop it gives the +// requests in progress shutdownTimeout to finish, then closes their +// connections. When serving fails, it logs why and sends the error to +// failed. +func serve( + lc fx.Lifecycle, server *http.Server, listener net.Listener, + processLog *slog.Logger, failed servingFailed, +) { + served := make(chan error, 1) + + lc.Append(fx.Hook{ + OnStart: func(context.Context) error { + go func() { + err := server.Serve(listener) + if !errors.Is(err, http.ErrServerClosed) { + processLog.Error("serving failed", "error", err.Error()) + + failed <- err + } + + served <- err + }() + + return nil + }, + OnStop: func(ctx context.Context) error { + processLog.Info("stopping") + + shutdownCtx, cancel := context.WithTimeout(ctx, shutdownTimeout) + defer cancel() + + err := server.Shutdown(shutdownCtx) + if err != nil { + processLog.Warn("requests still in progress were cut off", + "error", err.Error()) + + _ = server.Close() + } + + err = <-served + if !errors.Is(err, http.ErrServerClosed) { + return err + } + + return nil + }, + }) +} + +// run starts app, which serves until ctx is done or serving fails, as +// failed tells. Then it stops app and takes the daemon's last step. It +// returns the process's exit status, 1 when serving failed, a part could +// not stop in order or the last step failed; each logs why. +func run( + ctx context.Context, app *fx.App, failed servingFailed, + processLog *slog.Logger, lastStep func() error, +) int { + // The hooks only start goroutines, and end them at the stop, so neither + // is cut short by a deadline or by ctx. + err := app.Start(context.WithoutCancel(ctx)) + if err != nil { + return cannotStart(processLog, err) + } + + status := 0 + + select { + case <-ctx.Done(): + case <-failed: + status = 1 + } + + err = app.Stop(context.WithoutCancel(ctx)) + if err != nil { + status = 1 + } + + err = lastStep() + if err != nil { + status = 1 + } + + if status == 0 { + processLog.Info("stopped") + } + + return status +} + +// fxEventLogger returns what fx.WithLogger takes to write fx's own events to +// processLog at debug, which the daemons' log levels normally hold back. Its +// errors are at debug too: the daemon logs each one itself. +func fxEventLogger(processLog *slog.Logger) func() fxevent.Logger { + return func() fxevent.Logger { + logger := &fxevent.SlogLogger{Logger: processLog} + logger.UseLogLevel(slog.LevelDebug) + logger.UseErrorLevel(slog.LevelDebug) + + return logger + } +} diff --git a/internal/sww/rproxy.go b/internal/sww/rproxy.go index f3f4dd3..1f886fa 100644 --- a/internal/sww/rproxy.go +++ b/internal/sww/rproxy.go @@ -2,12 +2,13 @@ package sww import ( "context" - "errors" "log/slog" "net" "net/http" "os" + "go.uber.org/fx" + "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/rproxy" @@ -30,26 +31,31 @@ func RunRProxy(ctx context.Context, params Params) int { cfg, err := config.RProxyFromEnvironment(params.Viper) if err != nil { - processLog.Error("invalid setting", "error", err.Error()) - - return 1 + return cannotStart(processLog, startError{"invalid setting", err}) } - server := rproxy.New(rproxy.Params{ - Config: cfg, - RequestLog: params.Stdout, - ProcessLog: processLog, - }) + var ( + server *rproxy.Server + listener net.Listener + ) - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr) + failed := make(servingFailed, 1) + + app := fx.New( + fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed), + fx.Provide( + newRProxyServer, + func(server *rproxy.Server) *http.Server { return server.Server }, + newRProxyListener, + ), + fx.Invoke(serve), + fx.Populate(&server, &listener), + fx.WithLogger(fxEventLogger(processLog)), + ) + + err = app.Err() if err != nil { - processLog.Error( - "cannot listen on SWW_RPROXY_LISTEN_ADDR", - "error", - err.Error(), - ) - - return 1 + return cannotStart(processLog, err) } processLog.Info("starting", @@ -57,52 +63,25 @@ func RunRProxy(ctx context.Context, params Params) int { "address", listener.Addr().String(), "settings", cfg) - return serveRProxy(ctx, server, listener, processLog) + return run(ctx, app, failed, processLog, func() error { + server.WaitForReports() + + return nil + }) } -// serveRProxy serves requests on listener until ctx is done, and then stops as -// RunRProxy says. -func serveRProxy( - ctx context.Context, server *rproxy.Server, listener net.Listener, - processLog *slog.Logger, -) int { - served := make(chan error, 1) - - go func() { - served <- server.Serve(listener) - }() - - select { - case err := <-served: - processLog.Error("serving failed", "error", err.Error()) - - return 1 - case <-ctx.Done(): - } - - processLog.Info("stopping") - - shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), - shutdownTimeout) - defer cancel() - - err := server.Shutdown(shutdownCtx) - if err != nil { - processLog.Warn("requests still in progress were cut off", - "error", err.Error()) - - _ = server.Close() - } - - err = <-served - if !errors.Is(err, http.ErrServerClosed) { - processLog.Error("serving failed", "error", err.Error()) - - return 1 - } - - server.WaitForReports() - processLog.Info("stopped") - - return 0 +// newRProxyServer returns the server rproxy runs. +func newRProxyServer( + cfg *config.RProxy, out requestLog, processLog *slog.Logger, +) *rproxy.Server { + return rproxy.New(rproxy.Params{ + Config: cfg, + RequestLog: out, + ProcessLog: processLog, + }) +} + +// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR. +func newRProxyListener(cfg *config.RProxy) (net.Listener, error) { + return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR") } diff --git a/internal/sww/sww.go b/internal/sww/sww.go index 7befcde..604f8d6 100644 --- a/internal/sww/sww.go +++ b/internal/sww/sww.go @@ -4,47 +4,23 @@ // stop, and then stops in an orderly way, writing the state and cache files. // `sww rproxy` reads rproxy's settings alone, and runs the reverse proxy in // front of one app that asks waf about each request. `sww healthcheck` is the -// container's health check. +// container's health check. fx builds the parts of each daemon, starts their +// background work and stops it. package sww import ( "context" - "errors" "fmt" "io" - "log/slog" - "net" - "net/http" "os" "os/signal" "syscall" - "time" "github.com/spf13/viper" - "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/config" - "sneak.berlin/go/smallwebwaf/internal/coreruleset" - "sneak.berlin/go/smallwebwaf/internal/geojs" - "sneak.berlin/go/smallwebwaf/internal/lookup" - "sneak.berlin/go/smallwebwaf/internal/remotelog" - "sneak.berlin/go/smallwebwaf/internal/reputation" - "sneak.berlin/go/smallwebwaf/internal/requestlog" - "sneak.berlin/go/smallwebwaf/internal/rules" - "sneak.berlin/go/smallwebwaf/internal/state" - "sneak.berlin/go/smallwebwaf/internal/waf" ) -// shutdownTimeout is how long requests in progress may take to finish -// once sww is told to stop, before their connections are closed. -// runit and docker wait a little longer before they kill the process. -const shutdownTimeout = 5 * time.Second - -// remoteLogStopTimeout is how long, as waf stops, the log lines still -// waiting are sent to SWW_WAF_LOG_REMOTE_URL before they are given up. stdout has -// carried them. -const remoteLogStopTimeout = 2 * time.Second - // usageStatus is the exit status with no command, or one sww does not // know. const usageStatus = 2 @@ -107,372 +83,3 @@ func printUsage(w io.Writer) int { return usageStatus } - -// RunWAF is `sww waf`: it reads the settings, the rule files, -// the lookup database and the state and cache files, then serves requests until -// ctx is done. It returns the process's exit status, 1 when waf cannot start. -func RunWAF(ctx context.Context, params Params) int { - // Until the settings are read, the one message is an invalid setting's - // error, which every SWW_WAF_LOG_LEVEL lets through. - processLog := requestlog.NewProcessLogger(params.Stdout, - config.InstanceName(params.Viper), slog.LevelError) - - cfg, err := config.WAFFromEnvironment(params.Viper) - if err != nil { - return cannotStart(processLog, "invalid setting", err) - } - - // While SWW_WAF_LOG_REMOTE_URL is set, every line on stdout from here on - // is sent there too. - stdout := params.Stdout - - var remote *remotelog.Sender - - if cfg.LogRemoteURL != nil { - remote = newRemoteLogSender(cfg) - stdout = io.MultiWriter(params.Stdout, remote) - } - - processLog = requestlog.NewProcessLogger( - stdout, - cfg.InstanceName, - cfg.LogLevel, - ) - - if remote != nil { - stopSending := startSending(ctx, remote, processLog) - defer stopSending() - } - - return runWAFWithSettings( - ctx, - params.Version, - cfg, - stdout, - remote, - processLog, - ) -} - -// runWAFWithSettings is the rest of RunWAF once the settings are read and -// the process log goes where they say. remote is nil unless -// SWW_WAF_LOG_REMOTE_URL is set. -func runWAFWithSettings( - ctx context.Context, - version string, - cfg *config.WAF, - stdout io.Writer, - remote *remotelog.Sender, - processLog *slog.Logger, -) int { - // The state files and the alerts give times in UTC. - now := func() time.Time { return time.Now().UTC() } - - alertQueue := newAlertQueue(cfg, now, processLog) - - ruleFiles, err := rules.Load(rules.Params{ - Dir: cfg.RuleFilesDir, - Enabled: cfg.RuleFilesEnabled, - ProcessLog: processLog, - Alerts: alertQueue, - }) - if err != nil { - return cannotStart(processLog, "cannot use the rule files", err) - } - - crs, err := waf.NewCoreRuleSet(cfg) - if err != nil { - return cannotStart(processLog, "cannot use the Core Rule Set", err) - } - - server, err := newServer( - cfg, - stdout, - processLog, - now, - ruleFiles, - alertQueue, - crs, - ) - if err != nil { - return cannotStart(processLog, "cannot use the lookup database", err) - } - - if remote != nil { - server.Metrics.AddRemoteLog(remote) - } - - files, err := loadStateFiles(cfg, server, alertQueue, now, processLog) - if err != nil { - return cannotStart( - processLog, - "cannot use the state and cache files", - err, - ) - } - - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr) - if err != nil { - return cannotStart( - processLog, - "cannot listen on SWW_WAF_LISTEN_ADDR", - err, - ) - } - - processLog.Info("starting", - "version", version, - "address", listener.Addr().String(), - "settings", cfg) - - return serve( - ctx, - server, - listener, - files, - ruleFiles, - alertQueue, - processLog, - ) -} - -// cannotStart logs why waf cannot start, and returns its exit status. -func cannotStart(processLog *slog.Logger, message string, err error) int { - processLog.Error(message, "error", err.Error()) - - return 1 -} - -// newServer returns the server waf runs, with the metrics of the alerts, -// after reading the lookup database while SWW_WAF_LOOKUP_SOURCE is file. A lookup -// database that cannot be read is an error. -func newServer( - cfg *config.WAF, stdout io.Writer, processLog *slog.Logger, - now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue, - crs *coreruleset.CoreRuleSet, -) (*waf.Server, error) { - var lookupFile *lookup.File - - if cfg.LookupSource == "file" { - var err error - - lookupFile, err = lookup.OpenFile(lookup.FileParams{ - Path: cfg.LookupDBPath, - Now: now, - ProcessLog: processLog, - Alerts: alertQueue, - }) - if err != nil { - return nil, err - } - } - - server := waf.New(waf.Params{ - Config: cfg, - RequestLog: stdout, - ProcessLog: processLog, - GeoJSURL: geojs.URL, - AbuseIPDBURL: reputation.AbuseIPDBURL, - LookupFile: lookupFile, - CoreRuleSet: crs, - Now: now, - Rules: ruleFiles, - Alerts: alertQueue, - }) - server.Metrics.AddAlerts(alertQueue) - - if lookupFile != nil { - server.Metrics.AddLookupFile( - lookupFile.LastRead, - lookupFile.ReadFailures, - ) - } - - return server, nil -} - -// loadStateFiles reads the state and cache files into the parts of server and -// into alertQueue, as state.Load does. -func loadStateFiles( - cfg *config.WAF, server *waf.Server, alertQueue *alerts.Queue, - now func() time.Time, processLog *slog.Logger, -) (*state.Files, error) { - return state.Load(state.Params{ - Dir: cfg.StateDir, - CacheDir: cfg.CacheDir, - BansWriteDelay: cfg.BansWriteDelay, - WriteInterval: cfg.StateWriteInterval, - Ledger: server.Ledger, - Limiter: server.Limiter, - GeoJS: server.GeoJS, - Lists: server.Lists, - DNSBL: server.DNSBL, - AbuseIPDB: server.AbuseIPDB, - RouteWatch: server.RouteWatch, - Alerts: alertQueue, - Anomalies: server.Anomalies, - Now: now, - ProcessLog: processLog, - Metrics: server.Metrics, - }) -} - -// newAlertQueue returns the queue of the alerts to the webhook, Slack and -// ntfy, with the settings for them. -func newAlertQueue( - cfg *config.WAF, now func() time.Time, processLog *slog.Logger, -) *alerts.Queue { - return alerts.New(alerts.Params{ - WebhookURL: cfg.AlertWebhookURL, - WebhookHeaders: cfg.AlertWebhookHeaders, - SlackURL: cfg.AlertSlackWebhookURL, - NtfyURL: cfg.AlertNtfyURL, - NtfyToken: cfg.AlertNtfyToken, - Events: cfg.AlertEvents, - Cooldown: cfg.AlertCooldown, - MaxPerHour: cfg.AlertMaxPerHour, - Instance: cfg.InstanceName, - Now: now, - ProcessLog: processLog, - }) -} - -// newRemoteLogSender returns a sender of the log lines to -// SWW_WAF_LOG_REMOTE_URL, with the settings for it. -func newRemoteLogSender(cfg *config.WAF) *remotelog.Sender { - return remotelog.New(remotelog.Params{ - URL: cfg.LogRemoteURL, - RootCAs: cfg.LogRemoteTLSCAs, - BufferLines: cfg.LogRemoteBufferLines, - Facility: cfg.LogRemoteFacility, - AppName: cfg.LogRemoteAppName, - }) -} - -// startSending runs remote until the function it returns is called, which -// then waits at most remoteLogStopTimeout for the lines still waiting to -// be sent. Sending goes on after ctx is done, so that the lines written -// while sww stops are sent too. -func startSending( - ctx context.Context, remote *remotelog.Sender, processLog *slog.Logger, -) func() { - sending, stop := context.WithCancel(context.WithoutCancel(ctx)) - sent := make(chan struct{}) - - go func() { - remote.Run(sending, processLog) - close(sent) - }() - - return func() { - stop() - - select { - case <-sent: - case <-time.After(remoteLogStopTimeout): - } - } -} - -// serve serves requests on listener, writes the state and cache files as they -// are due, takes in an admin's edits of them, reads the rule files again as -// they change, and the lookup database when it is replaced, fetches the -// lists the settings name by URL as they are due, and sends the alerts, -// until ctx is done. Then it gives the requests in progress -// shutdownTimeout to finish, and writes every state and cache file, -// alerts.json with the alerts still waiting. -func serve( - ctx context.Context, server *waf.Server, listener net.Listener, - files *state.Files, ruleFiles *rules.Files, alertQueue *alerts.Queue, - processLog *slog.Logger, -) int { - served := make(chan error, 1) - - go func() { - served <- server.Serve(listener) - }() - - writing, stopWriting := context.WithCancel(ctx) - defer stopWriting() - - written := inBackground(func() { files.Run(writing) }) - watched := inBackground(func() { files.Watch(writing) }) - rulesWatched := inBackground(func() { ruleFiles.Watch(writing) }) - lookupFileWatched := inBackground(func() { - if server.LookupFile != nil { - server.LookupFile.Watch(writing) - } - }) - listsFetched := inBackground(func() { server.Lists.Run(writing) }) - alertsSent := inBackground(func() { alertQueue.Run(writing) }) - - select { - case err := <-served: - processLog.Error("serving failed", "error", err.Error()) - - return 1 - case <-ctx.Done(): - } - - processLog.Info("stopping") - - shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), - shutdownTimeout) - defer cancel() - - err := server.Shutdown(shutdownCtx) - if err != nil { - processLog.Warn("requests still in progress were cut off", - "error", err.Error()) - - _ = server.Close() - } - - err = <-served - if !errors.Is(err, http.ErrServerClosed) { - processLog.Error("serving failed", "error", err.Error()) - - return 1 - } - - // Run and Watch have ended, so nothing else reads or writes the - // files, and no alert is being sent, so that alerts.json keeps every - // alert not yet sent. Every request has ended too, but for one cut off - // because Shutdown timed out, which adds to its client's history only as - // it ends. That can be after this write, and then that request is missing - // from clients.json. - <-written - <-watched - <-rulesWatched - <-lookupFileWatched - <-listsFetched - <-alertsSent - - err = files.WriteAll() - if err != nil { - processLog.Error( - "writing the state and cache files failed", - "error", - err.Error(), - ) - - return 1 - } - - processLog.Info("stopped") - - return 0 -} - -// inBackground runs task on a goroutine of its own, and returns a channel -// that is closed once task has returned. -func inBackground(task func()) <-chan struct{} { - done := make(chan struct{}) - - go func() { - task() - close(done) - }() - - return done -} diff --git a/internal/sww/waf.go b/internal/sww/waf.go new file mode 100644 index 0000000..359fe48 --- /dev/null +++ b/internal/sww/waf.go @@ -0,0 +1,480 @@ +package sww + +import ( + "context" + "io" + "log/slog" + "net" + "net/http" + "time" + + "go.uber.org/fx" + + "sneak.berlin/go/smallwebwaf/internal/alerts" + "sneak.berlin/go/smallwebwaf/internal/anomaly" + "sneak.berlin/go/smallwebwaf/internal/bans" + "sneak.berlin/go/smallwebwaf/internal/config" + "sneak.berlin/go/smallwebwaf/internal/coreruleset" + "sneak.berlin/go/smallwebwaf/internal/geojs" + "sneak.berlin/go/smallwebwaf/internal/lookup" + "sneak.berlin/go/smallwebwaf/internal/metrics" + "sneak.berlin/go/smallwebwaf/internal/ratelimit" + "sneak.berlin/go/smallwebwaf/internal/remotelog" + "sneak.berlin/go/smallwebwaf/internal/reputation" + "sneak.berlin/go/smallwebwaf/internal/requestlog" + "sneak.berlin/go/smallwebwaf/internal/routewatch" + "sneak.berlin/go/smallwebwaf/internal/rules" + "sneak.berlin/go/smallwebwaf/internal/state" + "sneak.berlin/go/smallwebwaf/internal/waf" +) + +// remoteLogStopTimeout is how long, as waf stops, the log lines still +// waiting are sent to SWW_WAF_LOG_REMOTE_URL before they are given up. stdout has +// carried them. +const remoteLogStopTimeout = 2 * time.Second + +// RunWAF is `sww waf`: it reads the settings, the rule files, +// the lookup database and the state and cache files, then serves requests until +// ctx is done. Then it gives the requests in progress shutdownTimeout to +// finish, ends its background work and writes every state and cache file, +// alerts.json with the alerts still waiting. It returns the process's exit +// status, 1 when waf cannot start or stop in order. +func RunWAF(ctx context.Context, params Params) int { + // Until the settings are read, the one message is an invalid setting's + // error, which every SWW_WAF_LOG_LEVEL lets through. + processLog := requestlog.NewProcessLogger(params.Stdout, + config.InstanceName(params.Viper), slog.LevelError) + + cfg, err := config.WAFFromEnvironment(params.Viper) + if err != nil { + return cannotStart(processLog, startError{"invalid setting", err}) + } + + // While SWW_WAF_LOG_REMOTE_URL is set, every line on stdout from here on + // is sent there too. The sender runs before any part is built, so that it + // sends why waf cannot start. + stdout := params.Stdout + + var remote *remotelog.Sender + + if cfg.LogRemoteURL != nil { + remote = newRemoteLogSender(cfg) + stdout = io.MultiWriter(params.Stdout, remote) + } + + processLog = requestlog.NewProcessLogger( + stdout, + cfg.InstanceName, + cfg.LogLevel, + ) + + if remote != nil { + stopSending := startSending(ctx, remote, processLog) + defer stopSending() + } + + var ( + files *state.Files + listener net.Listener + ) + + failed := make(servingFailed, 1) + + app := fx.New( + fx.Supply(cfg, processLog, requestLog{stdout}, remote, failed), + fx.Provide( + newAlertQueue, newRuleFiles, newCoreRuleSet, newLookupFile, + newLimiter, newLedger, newAnomalyCounters, + newLists, newDNSBL, newAbuseIPDB, newRouteWatch, + newMetrics, newGeoJS, newWAFServer, + func(server *waf.Server) *http.Server { return server.Server }, + newWAFListener, newStateFiles, + ), + // fx stops hooks in the reverse of the order they were added in. The + // state files are read before serving is set up, so that serving + // stops before their loops do. + fx.Populate(&files), + fx.Invoke(serve), + fx.Populate(&listener), + fx.WithLogger(fxEventLogger(processLog)), + ) + + err = app.Err() + if err != nil { + return cannotStart(processLog, err) + } + + processLog.Info("starting", + "version", params.Version, + "address", listener.Addr().String(), + "settings", cfg) + + // The last write follows the stop of every hook, and so the end of every + // loop that changes the state or raises an alert. A hook of the state + // files would stop before the loops of the parts built ahead of them. + return run(ctx, app, failed, processLog, func() error { + err := files.WriteAll() + if err != nil { + processLog.Error("writing the state and cache files failed", + "error", err.Error()) + } + + return err + }) +} + +// newAlertQueue returns the queue of the alerts to the webhook, Slack and +// ntfy, with the settings for them. Until waf stops, it sends them. +func newAlertQueue( + lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger, +) *alerts.Queue { + alertQueue := alerts.New(alerts.Params{ + WebhookURL: cfg.AlertWebhookURL, + WebhookHeaders: cfg.AlertWebhookHeaders, + SlackURL: cfg.AlertSlackWebhookURL, + NtfyURL: cfg.AlertNtfyURL, + NtfyToken: cfg.AlertNtfyToken, + Events: cfg.AlertEvents, + Cooldown: cfg.AlertCooldown, + MaxPerHour: cfg.AlertMaxPerHour, + Instance: cfg.InstanceName, + Now: utcNow, + ProcessLog: processLog, + }) + + lc.Append(background(alertQueue.Run)) + + return alertQueue +} + +// newRuleFiles reads the rule files, and reads them again as they change +// until waf stops. +func newRuleFiles( + lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger, + alertQueue *alerts.Queue, +) (*rules.Files, error) { + ruleFiles, err := rules.Load(rules.Params{ + Dir: cfg.RuleFilesDir, + Enabled: cfg.RuleFilesEnabled, + ProcessLog: processLog, + Alerts: alertQueue, + }) + if err != nil { + return nil, startError{"cannot use the rule files", err} + } + + lc.Append(background(ruleFiles.Watch)) + + return ruleFiles, nil +} + +// newCoreRuleSet returns the Core Rule Set, as waf.NewCoreRuleSet does. +func newCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) { + crs, err := waf.NewCoreRuleSet(cfg) + if err != nil { + return nil, startError{"cannot use the Core Rule Set", err} + } + + return crs, nil +} + +// newLookupFile reads the lookup database while SWW_WAF_LOOKUP_SOURCE is +// file, and reads it again when it is replaced until waf stops. It returns +// nil while the source is anything else. +func newLookupFile( + lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger, + alertQueue *alerts.Queue, +) (*lookup.File, error) { + if cfg.LookupSource != "file" { + return nil, nil //nolint:nilnil // no lookup database, and no error + } + + lookupFile, err := lookup.OpenFile(lookup.FileParams{ + Path: cfg.LookupDBPath, + Now: utcNow, + ProcessLog: processLog, + Alerts: alertQueue, + }) + if err != nil { + return nil, startError{"cannot use the lookup database", err} + } + + lc.Append(background(lookupFile.Watch)) + + return lookupFile, nil +} + +// newLimiter returns the table of clients, with the rate and byte limits. +func newLimiter(cfg *config.WAF) *ratelimit.Limiter { + return ratelimit.New(ratelimit.Limits{ + PerMinute: cfg.RateLimitPerMinute, + PerHour: cfg.RateLimitPerHour, + PerDay: cfg.RateLimitPerDay, + BytesPerMinute: cfg.BytesLimitPerMinute, + BytesPerHour: cfg.BytesLimitPerHour, + BytesPerDay: cfg.BytesLimitPerDay, + }, cfg.MaxTrackedClients) +} + +// newLedger returns the ledger of the bans, with the settings for them. +func newLedger(cfg *config.WAF) *bans.Ledger { + return bans.New(bans.Params{ + LimitBanDuration: cfg.LimitBanDuration, + LimitBanRepeatWindow: cfg.LimitBanRepeatWindow, + LimitBanPermanentAbove: cfg.LimitBanPermanentAbove, + AttackBanDuration: cfg.AttackBanDuration, + MaxBans: cfg.MaxBans, + }) +} + +// newAnomalyCounters returns the counters of the anomaly thresholds. +func newAnomalyCounters( + cfg *config.WAF, alertQueue *alerts.Queue, +) *anomaly.Counters { + return anomaly.New(anomaly.Params{ + Client: cfg.AnomalyClient, + Net: cfg.AnomalyNet, + ASN: cfg.AnomalyASN, + Total: cfg.AnomalyTotal, + WatchedNet: cfg.AnomalyWatchedNet, + NetV4Prefix: cfg.AnomalyNetV4Prefix, + NetV6Prefix: cfg.AnomalyNetV6Prefix, + NamedNetblocks: cfg.AnomalyWatchedNets, + Alerts: alertQueue, + }) +} + +// newLists returns the lists the settings name by URL, none fetched yet. +// Until waf stops, it fetches them as they are due. +func newLists( + lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger, + alertQueue *alerts.Queue, +) *reputation.Lists { + lists := reputation.New(reputation.Params{ + BlocklistURLs: cfg.BlocklistURLs, + Refresh: cfg.BlocklistRefresh, + ASNLimitPercentURL: cfg.ASNLimitPercentURL, + TagLists: cfg.TagLists, + Now: utcNow, + ProcessLog: processLog, + Alerts: alertQueue, + }) + + lc.Append(background(lists.Run)) + + return lists +} + +// newDNSBL returns what asks the DNSBL zones, none asked yet. +func newDNSBL( + cfg *config.WAF, processLog *slog.Logger, alertQueue *alerts.Queue, +) *reputation.DNSBL { + return reputation.NewDNSBL(reputation.DNSBLParams{ + Zones: cfg.DNSBLZones, + Resolver: cfg.DNSBLResolver, + CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, + Now: utcNow, + ProcessLog: processLog, + Alerts: alertQueue, + }) +} + +// newAbuseIPDB returns what checks clients with AbuseIPDB while +// SWW_WAF_ABUSEIPDB_KEY is set, none checked yet. +func newAbuseIPDB( + cfg *config.WAF, processLog *slog.Logger, alertQueue *alerts.Queue, +) *reputation.AbuseIPDB { + return reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{ + URL: reputation.AbuseIPDBURL, + Key: cfg.AbuseIPDBKey, + MinScore: cfg.AbuseIPDBMinScore, + DailyBudget: cfg.AbuseIPDBDailyBudget, + CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, + Now: utcNow, + ProcessLog: processLog, + Alerts: alertQueue, + }) +} + +// newRouteWatch returns what asks routewatch about clients, or nil while +// SWW_WAF_ROUTEWATCH_URL is unset. +func newRouteWatch( + cfg *config.WAF, processLog *slog.Logger, +) *routewatch.Client { + if cfg.RouteWatchURL == "" { + return nil + } + + return routewatch.New(routewatch.Params{ + URL: cfg.RouteWatchURL, + Now: utcNow, + ProcessLog: processLog, + }) +} + +// newMetrics returns the metrics, with those read from the other parts as +// the metrics are asked for: AbuseIPDB's while SWW_WAF_ABUSEIPDB_KEY is set, +// the lookup database's while there is one, and the remote log sender's +// while there is one. +func newMetrics( + cfg *config.WAF, ledger *bans.Ledger, limiter *ratelimit.Limiter, + ruleFiles *rules.Files, lists *reputation.Lists, + dnsbl *reputation.DNSBL, abuseIPDB *reputation.AbuseIPDB, + alertQueue *alerts.Queue, lookupFile *lookup.File, + remote *remotelog.Sender, +) *metrics.Metrics { + m := metrics.New(cfg.MetricsBusiestCountriesAndASNs, cfg.InstanceName) + m.AddBansAndClients(ledger, limiter, utcNow) + m.AddRules(ruleFiles) + m.AddReputation(lists, dnsbl) + + if cfg.AbuseIPDBKey != "" { + m.AddAbuseIPDB(abuseIPDB) + } + + m.AddAlerts(alertQueue) + + if lookupFile != nil { + m.AddLookupFile(lookupFile.LastRead, lookupFile.ReadFailures) + } + + if remote != nil { + m.AddRemoteLog(remote) + } + + return m +} + +// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each +// answer goes to the limiter and the ledger, as waf.AddLookup says, and each +// failure is handled as waf.GeoJSFailed says. +func newGeoJS( + cfg *config.WAF, m *metrics.Metrics, limiter *ratelimit.Limiter, + ledger *bans.Ledger, alertQueue *alerts.Queue, processLog *slog.Logger, +) *geojs.GeoJS { + return geojs.New(geojs.Params{ + URL: geojs.URL, + Answered: func(answer geojs.Answer) { + waf.AddLookup(cfg, limiter, ledger, answer) + }, + Now: utcNow, + Asked: m.GeoJSRequests.Inc, + Failed: func(err error, askingAgainIn time.Duration) { + waf.GeoJSFailed(processLog, alertQueue, m, err, askingAgainIn) + }, + WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc, + }) +} + +// newWAFServer returns the server waf runs, with its parts. +func newWAFServer( + cfg *config.WAF, out requestLog, processLog *slog.Logger, + m *metrics.Metrics, limiter *ratelimit.Limiter, ledger *bans.Ledger, + anomalies *anomaly.Counters, lists *reputation.Lists, + dnsbl *reputation.DNSBL, abuseIPDB *reputation.AbuseIPDB, + routeWatch *routewatch.Client, geoJS *geojs.GeoJS, + lookupFile *lookup.File, crs *coreruleset.CoreRuleSet, + ruleFiles *rules.Files, alertQueue *alerts.Queue, +) *waf.Server { + return waf.New(waf.Params{ + Config: cfg, + RequestLog: out, + ProcessLog: processLog, + Metrics: m, + Limiter: limiter, + Ledger: ledger, + Anomalies: anomalies, + Lists: lists, + DNSBL: dnsbl, + AbuseIPDB: abuseIPDB, + RouteWatch: routeWatch, + GeoJS: geoJS, + LookupFile: lookupFile, + CoreRuleSet: crs, + Now: utcNow, + Rules: ruleFiles, + Alerts: alertQueue, + }) +} + +// newWAFListener listens on SWW_WAF_LISTEN_ADDR. +func newWAFListener(cfg *config.WAF) (net.Listener, error) { + return listen(cfg.ListenAddr, "SWW_WAF_LISTEN_ADDR") +} + +// newStateFiles reads the state and cache files into the parts and the +// alert queue, as state.Load does. Until waf stops, it writes them as they +// are due and takes in an admin's edits of them. +func newStateFiles( + lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger, + m *metrics.Metrics, ledger *bans.Ledger, limiter *ratelimit.Limiter, + geoJS *geojs.GeoJS, lists *reputation.Lists, dnsbl *reputation.DNSBL, + abuseIPDB *reputation.AbuseIPDB, routeWatch *routewatch.Client, + alertQueue *alerts.Queue, anomalies *anomaly.Counters, +) (*state.Files, error) { + files, err := state.Load(state.Params{ + Dir: cfg.StateDir, + CacheDir: cfg.CacheDir, + BansWriteDelay: cfg.BansWriteDelay, + WriteInterval: cfg.StateWriteInterval, + Ledger: ledger, + Limiter: limiter, + GeoJS: geoJS, + Lists: lists, + DNSBL: dnsbl, + AbuseIPDB: abuseIPDB, + RouteWatch: routeWatch, + Alerts: alertQueue, + Anomalies: anomalies, + Now: utcNow, + ProcessLog: processLog, + Metrics: m, + }) + if err != nil { + return nil, startError{"cannot use the state and cache files", err} + } + + lc.Append(background(files.Run)) + lc.Append(background(files.Watch)) + + return files, nil +} + +// newRemoteLogSender returns a sender of the log lines to +// SWW_WAF_LOG_REMOTE_URL, with the settings for it. +func newRemoteLogSender(cfg *config.WAF) *remotelog.Sender { + return remotelog.New(remotelog.Params{ + URL: cfg.LogRemoteURL, + RootCAs: cfg.LogRemoteTLSCAs, + BufferLines: cfg.LogRemoteBufferLines, + Facility: cfg.LogRemoteFacility, + AppName: cfg.LogRemoteAppName, + }) +} + +// startSending runs remote until the function it returns is called, which +// then waits at most remoteLogStopTimeout for the lines still waiting to +// be sent. Sending goes on after ctx is done, so that the lines written +// while sww stops are sent too. +func startSending( + ctx context.Context, remote *remotelog.Sender, processLog *slog.Logger, +) func() { + sending, stop := context.WithCancel(context.WithoutCancel(ctx)) + sent := make(chan struct{}) + + go func() { + remote.Run(sending, processLog) + close(sent) + }() + + return func() { + stop() + + select { + case <-sent: + case <-time.After(remoteLogStopTimeout): + } + } +} diff --git a/internal/waf/abuseipdb.go b/internal/waf/abuseipdb.go index 50f64a0..0802ee2 100644 --- a/internal/waf/abuseipdb.go +++ b/internal/waf/abuseipdb.go @@ -35,7 +35,7 @@ func (abuseIPDB) check( return nil } - client := rq.h.clientGroup(rq.client) + client := clientGroup(rq.h.config, rq.client) held, _ := rq.h.limiter.Client(client) offender := held.History.Offences != ratelimit.Offences{} diff --git a/internal/waf/admin.go b/internal/waf/admin.go index d1c9278..1842b37 100644 --- a/internal/waf/admin.go +++ b/internal/waf/admin.go @@ -224,7 +224,7 @@ func (h *handler) banNetblock(value string) (netip.Prefix, error) { return netip.Prefix{}, fmt.Errorf("netblock %q %w", value, errZone) } - return h.netblock(addr), nil + return banNetblock(h.config, addr), nil } // expiry returns when a ban made at now for duration ends: duration @@ -295,7 +295,7 @@ func (rq *request) showClient() { Bans: state.BanEntries(rq.h.ledger.Covering(addr)), } - client, seen := rq.h.limiter.Client(rq.h.clientGroup(addr)) + client, seen := rq.h.limiter.Client(clientGroup(rq.h.config, addr)) if seen { answer.Client = &client } diff --git a/internal/waf/answertime_test.go b/internal/waf/answertime_test.go index 3e184a0..c3e16b5 100644 --- a/internal/waf/answertime_test.go +++ b/internal/waf/answertime_test.go @@ -877,18 +877,13 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs( held, _, _ := startHeldGeoJS(t) resolver, releaseResolver := startHeldResolver(t) - server, out, _ := newWAFWith(t, func( - params waf.Params, - ) *waf.Server { - params.AbuseIPDBURL = silentAbuseIPDBURL - - return waf.New(params) - }, silentGeoJSURL, time.Now, map[string]string{ - tagDeny: "country=KP", coreRuleSetMode: block, - dnsblZones: dnsblZone, dnsblResolver: resolver, - abuseIPDBKey: accountKey, - blocklistURLs: held + "/drop.txt", asnLimitPercentURL: held + "/asn.txt", - }) + server, out, _ := newWAFWith(t, waf.New, + silentGeoJSURL, silentAbuseIPDBURL, time.Now, map[string]string{ + tagDeny: "country=KP", coreRuleSetMode: block, + dnsblZones: dnsblZone, dnsblResolver: resolver, + abuseIPDBKey: accountKey, + blocklistURLs: held + "/drop.txt", asnLimitPercentURL: held + "/asn.txt", + }) // The lists are fetched outside the synctest bubble below: a wait on the // network inside it would keep its clock from moving on. diff --git a/internal/waf/bans.go b/internal/waf/bans.go index 2da3361..bf19d96 100644 --- a/internal/waf/bans.go +++ b/internal/waf/bans.go @@ -10,6 +10,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/bans" + "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/ratelimit" "sneak.berlin/go/smallwebwaf/internal/requestlog" ) @@ -121,7 +122,7 @@ func (rq *request) banForLimit( rq.line.Offence = requestlog.OffenceLimit rq.h.metrics.LimitHit(hit) - netblock := rq.h.netblock(rq.client) + netblock := banNetblock(rq.h.config, rq.client) if rq.h.config.Observe && !rq.wouldAlertBan(netblock, now, bans.CauseLimit) { return @@ -157,7 +158,7 @@ func (rq *request) banForLimit( } ban, made := rq.h.ledger.BanForLimit(netblock, now, notes) - rq.h.limiter.Reset(rq.h.clientGroup(rq.client)) + rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client)) rq.line.BanExpires = banExpires(ban) if made { @@ -171,7 +172,7 @@ func (rq *request) banForLimit( // no ban, and raises the alert for the ban it would have made, if that // alert would be sent. It returns the action for that ban. func (rq *request) banForAttack(now time.Time, notes bans.Notes) string { - netblock := rq.h.netblock(rq.client) + netblock := banNetblock(rq.h.config, rq.client) if rq.h.config.Observe && !rq.wouldAlertBan(netblock, now, bans.CauseAttack) { return banAction( @@ -273,16 +274,16 @@ func (rq *request) netblockRequests(netblock netip.Prefix) int64 { return rq.h.limiter.Requests(netblock) + 1 } -// netblock is the netblock a ban on client covers: its IPv4 address, +// banNetblock is the netblock a ban on client covers: its IPv4 address, // widened to SWW_WAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group clientGroup // counts it in. -func (h *handler) netblock(client netip.Addr) netip.Prefix { +func banNetblock(cfg *config.WAF, client netip.Addr) netip.Prefix { addr := client.Unmap() if addr.Is4() { - return netip.PrefixFrom(addr, h.config.BanScopeV4Prefix).Masked() + return netip.PrefixFrom(addr, cfg.BanScopeV4Prefix).Masked() } - return h.clientGroup(addr) + return clientGroup(cfg, addr) } // banExpires is when ban ends, as the log line gives it: a time, or diff --git a/internal/waf/besidelookup_test.go b/internal/waf/besidelookup_test.go index 7ecb49e..9ee516c 100644 --- a/internal/waf/besidelookup_test.go +++ b/internal/waf/besidelookup_test.go @@ -75,7 +75,7 @@ func newWAFHoldingTheCoreRuleSet( params.CoreRuleSet = crs return waf.New(params) - }, silentGeoJSURL, time.Now, settings) + }, silentGeoJSURL, abuseIPDBURL, time.Now, settings) server.SetBesideLookupTimeLimit(waf.BesideLookupTimeLimit) return server, out, queue diff --git a/internal/waf/bytelimits.go b/internal/waf/bytelimits.go index 7ad1ca4..c1f3c32 100644 --- a/internal/waf/bytelimits.go +++ b/internal/waf/bytelimits.go @@ -30,7 +30,7 @@ func (byteLimits) check( } counts, hit, over := rq.h.limiter.CountBytes( - rq.h.clientGroup(rq.client), + clientGroup(rq.h.config, rq.client), now, rq.countedBytes(), rq.bytesLimitPercent.percent, diff --git a/internal/waf/client.go b/internal/waf/client.go index 9b02334..8381f0a 100644 --- a/internal/waf/client.go +++ b/internal/waf/client.go @@ -5,6 +5,8 @@ import ( "net/http" "net/netip" "slices" + + "sneak.berlin/go/smallwebwaf/internal/config" ) // peerAddress is the address of the request's TCP peer. @@ -45,10 +47,10 @@ func scheme(r *http.Request, isQuestion bool) string { // address, or its IPv6 group, the netblock its IPv6 address is in of the // length SWW_WAF_IPV6_GROUP_PREFIX sets, a /64 by default, since one abuser // usually holds a whole /64. An IPv4 address in IPv6 form counts as IPv4. -func (h *handler) clientGroup(addr netip.Addr) netip.Prefix { +func clientGroup(cfg *config.WAF, addr netip.Addr) netip.Prefix { addr = addr.Unmap() if addr.Is6() { - return netip.PrefixFrom(addr, h.config.IPv6GroupPrefix).Masked() + return netip.PrefixFrom(addr, cfg.IPv6GroupPrefix).Masked() } return netip.PrefixFrom(addr, addr.BitLen()) diff --git a/internal/waf/coreruleset.go b/internal/waf/coreruleset.go index 4acc2cc..049bc6c 100644 --- a/internal/waf/coreruleset.go +++ b/internal/waf/coreruleset.go @@ -108,7 +108,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) { Event: alerts.EventCoreRuleSetMatch, Host: rq.site, Client: rq.client, - Netblock: rq.h.clientGroup(rq.client), + Netblock: clientGroup(rq.h.config, rq.client), ASN: rq.line.ASN, ASName: rq.line.ASName, Country: rq.line.Country, diff --git a/internal/waf/lookup.go b/internal/waf/lookup.go index 8250667..a6fc5e4 100644 --- a/internal/waf/lookup.go +++ b/internal/waf/lookup.go @@ -2,14 +2,18 @@ package waf import ( "context" + "log/slog" "net/http" "net/netip" "strings" "time" "sneak.berlin/go/smallwebwaf/internal/alerts" + "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/geojs" + "sneak.berlin/go/smallwebwaf/internal/metrics" + "sneak.berlin/go/smallwebwaf/internal/ratelimit" ) // The headers in which an answer of allow gives rproxy the client's AS number @@ -51,7 +55,7 @@ func (rq *request) startLookUp( return result } - client := rq.h.clientGroup(rq.client) + client := clientGroup(rq.h.config, rq.client) if rq.h.config.LookupSource == "file" { result <- lookupResult{ @@ -128,22 +132,30 @@ func countryTag(country string) string { return config.CountryTagPrefix + country } -// addLookup adds answer, an answer about a client from the lookup -// database or GeoJS, to the client's history, and to the notes of the bans -// on its netblock that have no AS number, AS name or country yet. -func (h *handler) addLookup(answer geojs.Answer) { - h.limiter.AddLookup(answer.Client, answer.Answered, +// AddLookup adds answer, an answer about a client from the lookup database +// or GeoJS, to the client's history in limiter, and to the notes of the bans +// in ledger on its netblock that have no AS number, AS name or country yet. +// cfg sets the netblock's size. +func AddLookup( + cfg *config.WAF, limiter *ratelimit.Limiter, ledger *bans.Ledger, + answer geojs.Answer, +) { + limiter.AddLookup(answer.Client, answer.Answered, answer.ASN, answer.ASName, answer.Country) - h.ledger.AddLookup(h.netblock(answer.Client.Addr()), + ledger.AddLookup(banNetblock(cfg, answer.Client.Addr()), answer.ASN, answer.ASName, answer.Country) } -// geojsFailed logs a failure of GeoJS, raises a source_failure alert for it -// and counts it. askingAgainIn is how long GeoJS is left alone after it. -func (h *handler) geojsFailed(err error, askingAgainIn time.Duration) { - h.processLog.Warn("asking GeoJS failed", +// GeoJSFailed logs a failure of GeoJS to processLog, raises a +// source_failure alert for it on alertQueue and counts it in m. +// askingAgainIn is how long GeoJS is left alone after it. +func GeoJSFailed( + processLog *slog.Logger, alertQueue *alerts.Queue, m *metrics.Metrics, + err error, askingAgainIn time.Duration, +) { + processLog.Warn("asking GeoJS failed", "error", err.Error(), "asking_again_in", askingAgainIn.String()) - h.alerts.Raise(alerts.Alert{ + alertQueue.Raise(alerts.Alert{ Event: alerts.EventSourceFailure, Reason: "asking GeoJS failed", Detail: map[string]any{ @@ -151,7 +163,7 @@ func (h *handler) geojsFailed(err error, askingAgainIn time.Duration) { "asking_again_in": askingAgainIn.String(), }, }) - h.metrics.GeoJSFailures.Inc() + m.GeoJSFailures.Inc() } // setLookupHeaders sets the headers that give the client's AS number and diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index 9179ae4..50c4f71 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -56,12 +56,28 @@ type Params struct { RequestLog io.Writer // ProcessLog receives the process's own messages. ProcessLog *slog.Logger - // GeoJSURL is where clients' AS numbers and countries are looked up - // while SWW_WAF_LOOKUP_SOURCE is geojs, normally geojs.URL. - GeoJSURL string - // AbuseIPDBURL is where clients are checked with AbuseIPDB while - // SWW_WAF_ABUSEIPDB_KEY is set, normally reputation.AbuseIPDBURL. - AbuseIPDBURL string + // Metrics count what waf does. Whoever builds them adds the metrics + // read from the other parts. + Metrics *metrics.Metrics + // Limiter is the table of clients the rate limits count, with each + // client's history. + Limiter *ratelimit.Limiter + // Ledger holds the bans. + Ledger *bans.Ledger + // Anomalies are the counters of the anomaly thresholds. + Anomalies *anomaly.Counters + // Lists are the lists fetched from URLs, DNSBL the DNSBL zones' + // verdicts, and AbuseIPDB AbuseIPDB's scores and the checks spent. + Lists *reputation.Lists + DNSBL *reputation.DNSBL + AbuseIPDB *reputation.AbuseIPDB + // RouteWatch asks routewatch about clients, and is nil while + // SWW_WAF_ROUTEWATCH_URL is unset. + RouteWatch *routewatch.Client + // GeoJS looks clients' AS numbers and countries up while + // SWW_WAF_LOOKUP_SOURCE is geojs. It is given AddLookup as its Answered + // and GeoJSFailed as its Failed. + GeoJS *geojs.GeoJS // LookupFile is the lookup database they are looked up in while // SWW_WAF_LOOKUP_SOURCE is file, and nil otherwise. LookupFile *lookup.File @@ -93,11 +109,8 @@ type CoreRuleSetInspector interface { ) (coreruleset.Result, []byte, error) } -// Server is the server waf runs, with the parts whose state the state -// files keep, the lookup database, nil unless SWW_WAF_LOOKUP_SOURCE is file, the -// lists fetched from URLs, which its Run fetches, the DNSBL zones' verdicts, -// AbuseIPDB's scores and checks spent, routewatch's answers, and the metrics. -// RouteWatch is nil while SWW_WAF_ROUTEWATCH_URL is unset. +// Server is the server waf runs, with the parts of its Params. The tests +// read them. type Server struct { *http.Server @@ -113,126 +126,42 @@ type Server struct { Metrics *metrics.Metrics } -// newHandler returns the handler with what every request shares, as the -// settings in params make it. +// newHandler returns the handler with what every request shares, the parts +// in params among it. func newHandler(params Params) *handler { - m := metrics.New( - params.Config.MetricsBusiestCountriesAndASNs, - params.Config.InstanceName, - ) - lists, dnsbl, abuseIPDB := newReputation(params, m) - awaitingReport, err := lru.New[string, *decision](maxAwaitingReport) if err != nil { panic(err) // New fails only for a size below one } - h := &handler{ + return &handler{ config: params.Config, requestLog: params.RequestLog, processLog: params.ProcessLog, now: params.Now, - metrics: m, + metrics: params.Metrics, awaitingReport: awaitingReport, - limiter: ratelimit.New(ratelimit.Limits{ - PerMinute: params.Config.RateLimitPerMinute, - PerHour: params.Config.RateLimitPerHour, - PerDay: params.Config.RateLimitPerDay, - BytesPerMinute: params.Config.BytesLimitPerMinute, - BytesPerHour: params.Config.BytesLimitPerHour, - BytesPerDay: params.Config.BytesLimitPerDay, - }, params.Config.MaxTrackedClients), - ledger: bans.New(bans.Params{ - LimitBanDuration: params.Config.LimitBanDuration, - LimitBanRepeatWindow: params.Config.LimitBanRepeatWindow, - LimitBanPermanentAbove: params.Config.LimitBanPermanentAbove, - AttackBanDuration: params.Config.AttackBanDuration, - MaxBans: params.Config.MaxBans, - }), - anomalies: anomaly.New(anomaly.Params{ - Client: params.Config.AnomalyClient, - Net: params.Config.AnomalyNet, - ASN: params.Config.AnomalyASN, - Total: params.Config.AnomalyTotal, - WatchedNet: params.Config.AnomalyWatchedNet, - NetV4Prefix: params.Config.AnomalyNetV4Prefix, - NetV6Prefix: params.Config.AnomalyNetV6Prefix, - NamedNetblocks: params.Config.AnomalyWatchedNets, - Alerts: params.Alerts, - }), - lookupFile: params.LookupFile, - lists: lists, - dnsbl: dnsbl, - abuseIPDB: abuseIPDB, - rules: params.Rules, - coreRuleSet: params.CoreRuleSet, - alerts: params.Alerts, - + limiter: params.Limiter, + ledger: params.Ledger, + geojs: params.GeoJS, + anomalies: params.Anomalies, + lookupFile: params.LookupFile, + lists: params.Lists, + dnsbl: params.DNSBL, + abuseIPDB: params.AbuseIPDB, + routeWatch: params.RouteWatch, + rules: params.Rules, + coreRuleSet: params.CoreRuleSet, + alerts: params.Alerts, + // The country list, the headers, the tag settings that name an asn= + // or country= tag and the biased thresholds act on the answer before + // the request goes on. + waitForGeoJS: len(params.Config.ExclusivelyAllowedCountries) > 0 || + params.Config.AddLookupHeaders || + params.Config.TagsNeedLookup() || + biasedThresholdsSet(params.Config), besideLookupLimit: besideLookupTimeLimit, } - if params.Config.RouteWatchURL != "" { - h.routeWatch = routewatch.New(routewatch.Params{ - URL: params.Config.RouteWatchURL, Now: params.Now, ProcessLog: params.ProcessLog, - }) - } - - // The country list, the headers, the tag settings that name an asn= or - // country= tag and the biased thresholds act on the answer before the - // request goes on. - h.waitForGeoJS = len(params.Config.ExclusivelyAllowedCountries) > 0 || - params.Config.AddLookupHeaders || params.Config.TagsNeedLookup() || - biasedThresholdsSet(params.Config) - h.geojs = geojs.New(geojs.Params{ - URL: params.GeoJSURL, - Answered: h.addLookup, - Now: params.Now, - Asked: m.GeoJSRequests.Inc, - Failed: h.geojsFailed, - WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc, - }) - m.AddBansAndClients(h.ledger, h.limiter, params.Now) - m.AddRules(params.Rules) - - return h -} - -// newReputation returns the lists fetched from URLs, the DNSBL zones' -// verdicts and AbuseIPDB's scores, as the settings in params name them, -// with none fetched, asked for or checked yet, and adds their metrics to -// m, AbuseIPDB's while SWW_WAF_ABUSEIPDB_KEY is set. -func newReputation( - params Params, m *metrics.Metrics, -) (*reputation.Lists, *reputation.DNSBL, *reputation.AbuseIPDB) { - cfg := params.Config - lists := reputation.New(reputation.Params{ - BlocklistURLs: cfg.BlocklistURLs, - Refresh: cfg.BlocklistRefresh, - ASNLimitPercentURL: cfg.ASNLimitPercentURL, - TagLists: cfg.TagLists, - Now: params.Now, - ProcessLog: params.ProcessLog, - Alerts: params.Alerts, - }) - dnsbl := reputation.NewDNSBL(reputation.DNSBLParams{ - Zones: cfg.DNSBLZones, Resolver: cfg.DNSBLResolver, - CacheTTL: cfg.IPReputationCacheTTL, - Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog, - Alerts: params.Alerts, - }) - abuseIPDB := reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{ - URL: params.AbuseIPDBURL, Key: cfg.AbuseIPDBKey, MinScore: cfg.AbuseIPDBMinScore, - DailyBudget: cfg.AbuseIPDBDailyBudget, CacheTTL: cfg.IPReputationCacheTTL, - Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog, - Alerts: params.Alerts, - }) - - m.AddReputation(lists, dnsbl) - - if cfg.AbuseIPDBKey != "" { - m.AddAbuseIPDB(abuseIPDB) - } - - return lists, dnsbl, abuseIPDB } // NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL, diff --git a/internal/waf/proxy_test.go b/internal/waf/proxy_test.go index 054b46b..44ccdc4 100644 --- a/internal/waf/proxy_test.go +++ b/internal/waf/proxy_test.go @@ -16,10 +16,17 @@ import ( "time" "sneak.berlin/go/smallwebwaf/internal/alerts" + "sneak.berlin/go/smallwebwaf/internal/anomaly" + "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/config/configtest" + "sneak.berlin/go/smallwebwaf/internal/geojs" "sneak.berlin/go/smallwebwaf/internal/lookup" + "sneak.berlin/go/smallwebwaf/internal/metrics" + "sneak.berlin/go/smallwebwaf/internal/ratelimit" + "sneak.berlin/go/smallwebwaf/internal/reputation" "sneak.berlin/go/smallwebwaf/internal/requestlog" + "sneak.berlin/go/smallwebwaf/internal/routewatch" "sneak.berlin/go/smallwebwaf/internal/rproxy" "sneak.berlin/go/smallwebwaf/internal/rules" "sneak.berlin/go/smallwebwaf/internal/waf" @@ -435,13 +442,15 @@ func newWAF( ) (*waf.Server, *output, *alerts.Queue) { t.Helper() - return newWAFWith(t, waf.New, geojsURL, now, env) + return newWAFWith(t, waf.New, geojsURL, abuseIPDBURL, now, env) } -// newWAFWith is newWAF for the server build makes. +// newWAFWith is newWAF for the server build makes, with clients checked with +// AbuseIPDB at abuseIPDBCheckURL. func newWAFWith( t *testing.T, build func(waf.Params) *waf.Server, - geojsURL string, now func() time.Time, env map[string]string, + geojsURL, abuseIPDBCheckURL string, now func() time.Time, + env map[string]string, ) (*waf.Server, *output, *alerts.Queue) { t.Helper() @@ -499,18 +508,20 @@ func newWAFWith( t.Fatalf("Core Rule Set: %v", err) } - server := build(waf.Params{ - Config: cfg, - RequestLog: out, - ProcessLog: processLog, - GeoJSURL: geojsURL, - AbuseIPDBURL: abuseIPDBURL, - LookupFile: lookupFile, - CoreRuleSet: coreRuleSet, - Now: now, - Rules: ruleFiles, - Alerts: alertQueue, - }) + params := waf.Params{ + Config: cfg, + RequestLog: out, + ProcessLog: processLog, + LookupFile: lookupFile, + CoreRuleSet: coreRuleSet, + Now: now, + Rules: ruleFiles, + Alerts: alertQueue, + } + addCounters(¶ms) + addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL) + + server := build(params) // The race detector and a busy host can slow the Core Rule Set's // inspections past the time limit. The tests of the limit set it back, and @@ -520,6 +531,121 @@ func newWAFWith( return server, out, alertQueue } +// addCounters adds to params the metrics, the table of clients, the ledger +// and the anomaly counters, as sww builds them from params' settings. The +// metrics read the ledger, the table of clients and the rules. +func addCounters(params *waf.Params) { + cfg := params.Config + + params.Metrics = metrics.New( + cfg.MetricsBusiestCountriesAndASNs, + cfg.InstanceName, + ) + params.Limiter = ratelimit.New(ratelimit.Limits{ + PerMinute: cfg.RateLimitPerMinute, + PerHour: cfg.RateLimitPerHour, + PerDay: cfg.RateLimitPerDay, + BytesPerMinute: cfg.BytesLimitPerMinute, + BytesPerHour: cfg.BytesLimitPerHour, + BytesPerDay: cfg.BytesLimitPerDay, + }, cfg.MaxTrackedClients) + params.Ledger = bans.New(bans.Params{ + LimitBanDuration: cfg.LimitBanDuration, + LimitBanRepeatWindow: cfg.LimitBanRepeatWindow, + LimitBanPermanentAbove: cfg.LimitBanPermanentAbove, + AttackBanDuration: cfg.AttackBanDuration, + MaxBans: cfg.MaxBans, + }) + params.Anomalies = anomaly.New(anomaly.Params{ + Client: cfg.AnomalyClient, + Net: cfg.AnomalyNet, + ASN: cfg.AnomalyASN, + Total: cfg.AnomalyTotal, + WatchedNet: cfg.AnomalyWatchedNet, + NetV4Prefix: cfg.AnomalyNetV4Prefix, + NetV6Prefix: cfg.AnomalyNetV6Prefix, + NamedNetblocks: cfg.AnomalyWatchedNets, + Alerts: params.Alerts, + }) + + params.Metrics.AddBansAndClients( + params.Ledger, + params.Limiter, + params.Now, + ) + params.Metrics.AddRules(params.Rules) +} + +// addOutsideSources adds to params the lists fetched from URLs, the DNSBL +// zones' verdicts, AbuseIPDB's scores from abuseIPDBCheckURL, routewatch's +// answers while its setting is set, and GeoJS at geojsURL, as sww builds +// them from params' settings and the parts addCounters added. The metrics +// read the lists, the zones' verdicts, and AbuseIPDB's while its key is set. +func addOutsideSources( + params *waf.Params, geojsURL, abuseIPDBCheckURL string, +) { + cfg := params.Config + m, limiter, ledger := params.Metrics, params.Limiter, params.Ledger + processLog, alertQueue := params.ProcessLog, params.Alerts + + params.Lists = reputation.New(reputation.Params{ + BlocklistURLs: cfg.BlocklistURLs, + Refresh: cfg.BlocklistRefresh, + ASNLimitPercentURL: cfg.ASNLimitPercentURL, + TagLists: cfg.TagLists, + Now: params.Now, + ProcessLog: processLog, + Alerts: alertQueue, + }) + params.DNSBL = reputation.NewDNSBL(reputation.DNSBLParams{ + Zones: cfg.DNSBLZones, + Resolver: cfg.DNSBLResolver, + CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, + Now: params.Now, + ProcessLog: processLog, + Alerts: alertQueue, + }) + params.AbuseIPDB = reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{ + URL: abuseIPDBCheckURL, + Key: cfg.AbuseIPDBKey, + MinScore: cfg.AbuseIPDBMinScore, + DailyBudget: cfg.AbuseIPDBDailyBudget, + CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, + Now: params.Now, + ProcessLog: processLog, + Alerts: alertQueue, + }) + + if cfg.RouteWatchURL != "" { + params.RouteWatch = routewatch.New(routewatch.Params{ + URL: cfg.RouteWatchURL, + Now: params.Now, + ProcessLog: processLog, + }) + } + + params.GeoJS = geojs.New(geojs.Params{ + URL: geojsURL, + Answered: func(answer geojs.Answer) { + waf.AddLookup(cfg, limiter, ledger, answer) + }, + Now: params.Now, + Asked: m.GeoJSRequests.Inc, + Failed: func(err error, askingAgainIn time.Duration) { + waf.GeoJSFailed(processLog, alertQueue, m, err, askingAgainIn) + }, + WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc, + }) + + m.AddReputation(params.Lists, params.DNSBL) + + if cfg.AbuseIPDBKey != "" { + m.AddAbuseIPDB(params.AbuseIPDB) + } +} + // newClient returns an HTTP client that sends requests as they are made, // with no compression of its own. func newClient(t *testing.T) *http.Client { diff --git a/internal/waf/ratelimits.go b/internal/waf/ratelimits.go index 286cf1f..28ba812 100644 --- a/internal/waf/ratelimits.go +++ b/internal/waf/ratelimits.go @@ -38,8 +38,11 @@ func (rateLimits) check( rq.line.BytesLimitPercent, rq.line.BytesLimitPercentSetting = rq.bytesLimitPercent.logged() - counts, hit, over := rq.h.limiter.Count(rq.h.clientGroup(rq.client), now, - rq.rateLimitPercent.percent) + counts, hit, over := rq.h.limiter.Count( + clientGroup(rq.h.config, rq.client), + now, + rq.rateLimitPercent.percent, + ) rq.line.Counts = counts if !over { @@ -59,7 +62,7 @@ func (rateLimits) check( // limit. It counts nothing. func (rq *request) overAWholeRateLimit(now time.Time) bool { return rq.countedByRateLimits() && - rq.h.limiter.OverARateLimit(rq.h.clientGroup(rq.client), now) + rq.h.limiter.OverARateLimit(clientGroup(rq.h.config, rq.client), now) } // countedByRateLimits reports whether the rate limits count the request: its diff --git a/internal/waf/refusallimit.go b/internal/waf/refusallimit.go index cb30c2f..1c8a4ee 100644 --- a/internal/waf/refusallimit.go +++ b/internal/waf/refusallimit.go @@ -36,8 +36,11 @@ func (refusalLimit) check( return nil } - hit, over := rq.h.limiter.CountRefusal(rq.h.clientGroup(rq.client), now, - cfg.RefusalsLimitPerMinute) + hit, over := rq.h.limiter.CountRefusal( + clientGroup(rq.h.config, rq.client), + now, + cfg.RefusalsLimitPerMinute, + ) if !over { return nil } diff --git a/internal/waf/reputation.go b/internal/waf/reputation.go index 06bd4f5..b760666 100644 --- a/internal/waf/reputation.go +++ b/internal/waf/reputation.go @@ -37,7 +37,7 @@ func (rq *request) noteHit(hit bans.ReputationHit, reason string) { Event: alerts.EventReputationHit, Host: rq.site, Client: rq.client, - Netblock: rq.h.clientGroup(rq.client), + Netblock: clientGroup(rq.h.config, rq.client), ASN: rq.line.ASN, ASName: rq.line.ASName, Country: rq.line.Country, diff --git a/internal/waf/request.go b/internal/waf/request.go index 06bec96..9c6dbc8 100644 --- a/internal/waf/request.go +++ b/internal/waf/request.go @@ -122,7 +122,7 @@ func (h *handler) startRequest( RequestID: requestID(r, isQuestion), PeerIP: peer.String(), ForwardedFor: strings.Join(forwardedFor, ", "), - ClientGroup: h.clientGroup(client).String(), + ClientGroup: clientGroup(h.config, client).String(), ContentType: r.Header.Get("Content-Type"), RequestHeaders: requestHeaders( r, @@ -365,7 +365,7 @@ func (rq *request) addToHistory() { } rq.h.limiter.AddToHistory( - rq.h.clientGroup(rq.client), + clientGroup(rq.h.config, rq.client), rq.h.now(), request, ) @@ -373,7 +373,7 @@ func (rq *request) addToHistory() { answer, found := rq.answerAtTheEnd() if found { - rq.h.addLookup(answer) + AddLookup(rq.h.config, rq.h.limiter, rq.h.ledger, answer) } } @@ -394,7 +394,7 @@ func (rq *request) countAnomalies() { rq.h.anomalies.Count(rq.h.now(), anomaly.Request{ Host: rq.site, Client: rq.client, - ClientGroup: rq.h.clientGroup(rq.client), + ClientGroup: clientGroup(rq.h.config, rq.client), ASN: answer.ASN, ASName: answer.ASName, Country: answer.Country, @@ -425,7 +425,7 @@ func (rq *request) answerAtTheEnd() (geojs.Answer, bool) { return rq.lookupAnswer, true } - return rq.h.geojs.Kept(rq.h.clientGroup(rq.client)) + return rq.h.geojs.Kept(clientGroup(rq.h.config, rq.client)) } // responseWriter is waf's answer to a request. It notes the status and diff --git a/internal/waf/routewatchtags.go b/internal/waf/routewatchtags.go index 65af428..d1a528c 100644 --- a/internal/waf/routewatchtags.go +++ b/internal/waf/routewatchtags.go @@ -32,7 +32,7 @@ func (routeWatchTags) check( route, missing := rq.h.routeWatch.Route( ctx, - rq.h.clientGroup(rq.client), + clientGroup(rq.h.config, rq.client), rq.client, ) if missing != "" {