// Package sww runs the sww process, as the command it is given. // `sww waf` reads the settings, the rule files, the lookup database // and the state and cache files, answers rproxy's questions until it is told to // 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. fx builds the parts of each daemon, starts their // background work and stops it. package sww import ( "context" "fmt" "io" "log/slog" "net" "net/http" "os" "os/signal" "syscall" "time" "github.com/spf13/viper" "go.uber.org/fx" "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" ) // 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 // usage is what sww prints then. const usage = `usage: sww commands: rproxy the reverse proxy in front of one app waf decides for rproxy, and keeps bans, counters and history healthcheck the container's health check ` // Params are what RunWAF and RunRProxy need from the process. type Params struct { // Version is the version of the binary, set when it is built. Version string // Viper reads the settings, normally the viper config.NewViper returns // for the process's environment. Viper *viper.Viper // Stdout receives the request log and the process's own messages. Stdout io.Writer } // Main runs the command os.Args names, and returns the process's exit status. // `sww rproxy` and `sww waf` run until SIGTERM or SIGINT. // With no command, or one it does not know, it prints the commands and returns // usageStatus. func Main(version string) int { args := os.Args[1:] if len(args) == 0 { return printUsage(os.Stderr) } ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt) defer stop() params := Params{ Version: version, Viper: config.NewViper(), Stdout: os.Stdout, } switch args[0] { case "rproxy": return RunRProxy(ctx, params) case "waf": return RunWAF(ctx, params) case "healthcheck": return HealthCheck(ctx, args[1:], params.Viper, os.Stderr) default: return printUsage(os.Stderr) } } // printUsage writes the commands to w, and returns usageStatus. func printUsage(w io.Writer) int { _, _ = fmt.Fprint(w, usage) 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. 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 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) } // 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, 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 } // 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: out, ProcessLog: processLog, GeoJSURL: geojs.URL, AbuseIPDBURL: reputation.AbuseIPDBURL, LookupFile: lookupFile, CoreRuleSet: crs, Now: utcNow, Rules: ruleFiles, Alerts: alertQueue, }) server.Metrics.AddAlerts(alertQueue) if lookupFile != nil { server.Metrics.AddLookupFile( lookupFile.LastRead, lookupFile.ReadFailures, ) } 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. 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( 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, 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: 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 } // 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 // 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): } } }