From 37098fa9b79442008d962c21e61ea78b7db77ded 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 each daemon's parts in internal/sww from one small provider per part, and runs each background loop as one lifecycle hook. This replaces newServer, loadStateFiles, the inBackground list and both serve functions. One serve hook and one run function serve both daemons. fx's own events go to the process log at debug. Start-up errors keep their messages through startError. RunWAF and RunRProxy use fx's Start and Stop, not its signal handling. The last state write is registered before the loops that feed it, so that it runs after they end. Model: opus-5-5 --- README.md | 2 + SPEC.md | 4 +- go.mod | 4 + go.sum | 8 + internal/sww/lifecycle.go | 183 ++++++++++++++++++ internal/sww/rproxy.go | 95 ++++------ internal/sww/sww.go | 387 ++++++++++++++------------------------ 7 files changed, 381 insertions(+), 302 deletions(-) create mode 100644 internal/sww/lifecycle.go diff --git a/README.md b/README.md index c947ac5..b268c5c 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 builds each command's parts with `go.uber.org/fx`, the one package that + imports it. - `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..53d17e2 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. `go.uber.org/fx` builds each command's parts in + this package alone. It starts their background work, and stops it in the + reverse order. - `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..d6c82c7 --- /dev/null +++ b/internal/sww/lifecycle.go @@ -0,0 +1,183 @@ +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 +} + +// 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 has the daemon stop with +// exit status 1. +func serve( + lc fx.Lifecycle, shutdowner fx.Shutdowner, server *http.Server, + listener net.Listener, processLog *slog.Logger, +) { + 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()) + + _ = shutdowner.Shutdown(fx.ExitCode(1)) + } + + 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, and then +// stops it. It returns the process's exit status, 1 when serving failed or a +// part could not stop in order; each part logs why. +func run(ctx context.Context, app *fx.App, processLog *slog.Logger) 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 signal := <-app.Wait(): + status = signal.ExitCode + } + + err = app.Stop(context.WithoutCancel(ctx)) + if err != nil || status != 0 { + return 1 + } + + processLog.Info("stopped") + + return 0 +} + +// 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..ed722e0 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,26 @@ 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 listener net.Listener - listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", cfg.ListenAddr) + app := fx.New( + fx.Supply(cfg, processLog, requestLog{params.Stdout}), + fx.Provide( + newRProxyServer, + func(server *rproxy.Server) *http.Server { return server.Server }, + newRProxyListener, + ), + fx.Invoke(serve), + fx.Populate(&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 +58,28 @@ func RunRProxy(ctx context.Context, params Params) int { "address", listener.Addr().String(), "settings", cfg) - return serveRProxy(ctx, server, listener, processLog) + return run(ctx, app, processLog) } -// 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, +// newRProxyServer returns the server rproxy runs. Once it has stopped +// serving, the stop waits for the reports being sent. +func newRProxyServer( + lc fx.Lifecycle, cfg *config.RProxy, out requestLog, processLog *slog.Logger, -) int { - served := make(chan error, 1) +) *rproxy.Server { + server := rproxy.New(rproxy.Params{ + Config: cfg, + RequestLog: out, + ProcessLog: processLog, + }) - go func() { - served <- server.Serve(listener) - }() + // fx stops in the reverse order, so this follows the stop of serving. + lc.Append(fx.StopHook(server.WaitForReports)) - select { - case err := <-served: - processLog.Error("serving failed", "error", err.Error()) + return server +} - 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 +// 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..534fad7 100644 --- a/internal/sww/sww.go +++ b/internal/sww/sww.go @@ -4,12 +4,12 @@ // 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" @@ -21,6 +21,7 @@ import ( "time" "github.com/spf13/viper" + "go.uber.org/fx" "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/config" @@ -35,11 +36,6 @@ import ( "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. @@ -110,7 +106,10 @@ func printUsage(w io.Writer) int { // 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. +// 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. @@ -119,11 +118,12 @@ func RunWAF(ctx context.Context, params Params) int { cfg, err := config.WAFFromEnvironment(params.Viper) if err != nil { - return cannotStart(processLog, "invalid setting", err) + 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. + // 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 @@ -144,32 +144,61 @@ func RunWAF(ctx context.Context, params Params) int { defer stopSending() } - return runWAFWithSettings( - ctx, - params.Version, - cfg, - stdout, - remote, - processLog, + var listener net.Listener + + app := fx.New( + fx.Supply(cfg, processLog, requestLog{stdout}, remote), + fx.Provide( + newAlertQueue, + newRuleFiles, + newCoreRuleSet, + newLookupFile, + newWAFServer, + func(server *waf.Server) *http.Server { return server.Server }, + newWAFListener, + ), + fx.Invoke(loadStateFiles, 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) + + return run(ctx, app, 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) +// newAlertQueue returns the queue of the alerts to the webhook, Slack and +// ntfy, with the settings for them. +func newAlertQueue(cfg *config.WAF, 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: utcNow, + ProcessLog: processLog, + }) +} +// 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, @@ -177,105 +206,68 @@ func runWAFWithSettings( Alerts: alertQueue, }) if err != nil { - return cannotStart(processLog, "cannot use the rule files", err) + 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 cannotStart(processLog, "cannot use the Core Rule Set", err) + return nil, startError{"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, - ) + return crs, nil } -// 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 - } +// 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 +} + +// newWAFServer returns the server waf runs, with the metrics of the alerts, +// of the lookup database while there is one, and of the remote log sender +// while there is one. +func newWAFServer( + cfg *config.WAF, out requestLog, processLog *slog.Logger, + ruleFiles *rules.Files, alertQueue *alerts.Queue, + crs *coreruleset.CoreRuleSet, lookupFile *lookup.File, + remote *remotelog.Sender, +) *waf.Server { server := waf.New(waf.Params{ Config: cfg, - RequestLog: stdout, + RequestLog: out, ProcessLog: processLog, GeoJSURL: geojs.URL, AbuseIPDBURL: reputation.AbuseIPDBURL, LookupFile: lookupFile, CoreRuleSet: crs, - Now: now, + Now: utcNow, Rules: ruleFiles, Alerts: alertQueue, }) @@ -288,16 +280,23 @@ func newServer( ) } - return server, nil + if remote != nil { + server.Metrics.AddRemoteLog(remote) + } + + return server } // loadStateFiles reads the state and cache files into the parts of server and -// into alertQueue, as state.Load does. +// into alertQueue, as state.Load does. Until waf stops, it writes them as +// they are due, takes in an admin's edits of them, fetches the lists the +// settings name by URL and sends the alerts. Once all of that has ended, it +// writes every file. 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{ + lc fx.Lifecycle, cfg *config.WAF, server *waf.Server, + alertQueue *alerts.Queue, processLog *slog.Logger, +) error { + files, err := state.Load(state.Params{ Dir: cfg.StateDir, CacheDir: cfg.CacheDir, BansWriteDelay: cfg.BansWriteDelay, @@ -311,30 +310,37 @@ func loadStateFiles( RouteWatch: server.RouteWatch, Alerts: alertQueue, Anomalies: server.Anomalies, - Now: now, + Now: utcNow, ProcessLog: processLog, Metrics: server.Metrics, }) + if err != nil { + return startError{"cannot use the state and cache files", err} + } + + // fx stops in the reverse order, so the last write follows the end of + // the loops below. The lists and the alerts waiting are in the files, so + // their loops are here and not where their parts are built. + lc.Append(fx.StopHook(func() error { + err := files.WriteAll() + if err != nil { + processLog.Error("writing the state and cache files failed", + "error", err.Error()) + } + + return err + })) + lc.Append(background(files.Run)) + lc.Append(background(files.Watch)) + lc.Append(background(server.Lists.Run)) + lc.Append(background(alertQueue.Run)) + + return nil } -// 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, - }) +// newWAFListener listens on SWW_WAF_LISTEN_ADDR. +func newWAFListener(cfg *config.WAF) (net.Listener, error) { + return listen(cfg.ListenAddr, "SWW_WAF_LISTEN_ADDR") } // newRemoteLogSender returns a sender of the log lines to @@ -373,106 +379,3 @@ func startSending( } } } - -// 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 -}