Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb3c8f9351 | ||
|
|
17400f68b8 | ||
|
|
9d008ed31a | ||
|
|
86bb33bf07 | ||
|
|
91444a37b3 | ||
|
|
51989235d6 | ||
|
|
e15edef583 | ||
|
|
06faf0877c | ||
|
|
b2b1716907 |
No files matched your search
@@ -908,6 +908,8 @@ stops the running process (see [`docs/state-files.md`](docs/state-files.md)).
|
||||
`internal/coreruleset`, `internal/geojs`, `internal/lookup`,
|
||||
`internal/reputation`, `internal/routewatch`, `internal/anomaly` and
|
||||
`internal/alerts`: one check or source each.
|
||||
- `internal/dirwatch`: watches a directory, so that `internal/rules` and
|
||||
`internal/lookup` read their files again when they change.
|
||||
- `internal/state`: the state and cache files.
|
||||
- `internal/requestlog`: the request log.
|
||||
- `internal/processlog`: the process's own messages.
|
||||
|
||||
@@ -69,13 +69,14 @@ 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. `internal/sww` is the only package that imports
|
||||
and exits with status 2. `sww help` and `sww --help` print them and exit
|
||||
with status 0. `internal/sww` 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 answers
|
||||
`waf`'s own endpoints in `answerOwnEndpoint`. It counts bytes, refusals
|
||||
and anomalies, and writes the log lines.
|
||||
- `internal/waf`: `waf`'s server. It runs the checks in `check`. Its
|
||||
`routes.go` lists `waf`'s own endpoints. It counts bytes, refusals and
|
||||
anomalies, and writes the log lines.
|
||||
- `internal/rproxy`: the reverse proxy in front of one app that asks `waf`
|
||||
about each request. It passes a request to the app with the standard
|
||||
library's `httputil.ReverseProxy`, within the time and size limits.
|
||||
@@ -96,6 +97,9 @@ never as a time.
|
||||
imports no other package of this repository: `waf` gives it what it needs
|
||||
to count and report failures.
|
||||
- `internal/lookup`: looks clients up in the lookup database.
|
||||
- `internal/dirwatch`: watches a directory, and calls a function once it has
|
||||
gone a short time without a change. `internal/rules` and `internal/lookup`
|
||||
read their files again this way.
|
||||
- `internal/reputation`: fetches the lists, asks the DNS blocklists and
|
||||
AbuseIPDB in the background, and keeps their answers. It also fetches
|
||||
AbuseIPDB's blacklist, which `internal/waf` bans.
|
||||
@@ -124,11 +128,13 @@ never as a time.
|
||||
default `SWW_WAF_CORE_RULE_SET_DISABLED_RULES`, are built for that
|
||||
version. They are checked again before the version changes, since rule
|
||||
ids, and what each rule matches, change between versions;
|
||||
- `github.com/go-chi/chi/v5` to route the requests to `waf`'s own endpoints;
|
||||
- `github.com/hashicorp/golang-lru/v2` for the bounded tables;
|
||||
- `github.com/oschwald/maxminddb-golang/v2` to read the lookup database;
|
||||
- `github.com/fsnotify/fsnotify` to notice edited files;
|
||||
- `github.com/prometheus/client_golang` for the metrics;
|
||||
- `github.com/spf13/viper` to read the settings from the environment;
|
||||
- `github.com/spf13/cobra` for the commands and their help text;
|
||||
- `github.com/maxmind/mmdbwriter`, in the tests, to write lookup databases;
|
||||
- remote log sending written in this project, since the standard library's
|
||||
`log/syslog` is frozen and writes only the older syslog format.
|
||||
|
||||
@@ -7,10 +7,12 @@ require (
|
||||
github.com/corazawaf/coraza-coreruleset/v4 v4.25.0
|
||||
github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68
|
||||
github.com/fsnotify/fsnotify v1.10.1
|
||||
github.com/go-chi/chi/v5 v5.5.0
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7
|
||||
github.com/maxmind/mmdbwriter v1.2.0
|
||||
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
||||
github.com/prometheus/client_golang v1.24.1
|
||||
github.com/spf13/cobra v1.10.2
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.uber.org/fx v1.24.0
|
||||
golang.org/x/net v0.60.0
|
||||
@@ -26,6 +28,7 @@ require (
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/gotnospirit/makeplural v0.0.0-20180622080156-a5f48d94d976 // indirect
|
||||
github.com/gotnospirit/messageformat v0.0.0-20221001023931-dfe49f1eb092 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/kaptinlin/go-i18n v0.1.4 // indirect
|
||||
github.com/kaptinlin/jsonschema v0.4.6 // indirect
|
||||
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||
|
||||
@@ -14,12 +14,15 @@ github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68 h1:KLEjvBBSw
|
||||
github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68/go.mod h1:9XocblQEwK6EYylRth7PrPIifF2l0iSoYo4FAXhXU/8=
|
||||
github.com/corazawaf/libinjection-go v0.3.4 h1:jX2Tit2xZxYJJyVryNXucYBZtQqS7473x9Nc6lgRRbk=
|
||||
github.com/corazawaf/libinjection-go v0.3.4/go.mod h1:Ik/+w3UmTWH9yn366RgS9D95K3y7Atb5m/H/gXzzPCk=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/foxcpp/go-mockdns v1.2.0 h1:omK3OrHRD1IWJz1FuFBCFquhXslXoF17OvBS6JPzZF0=
|
||||
github.com/foxcpp/go-mockdns v1.2.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
|
||||
github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo=
|
||||
github.com/go-chi/chi/v5 v5.5.0 h1:xdKQ1dJmVJsmgahrXSKhUx8NyVPzpwdlyrrQFpMPJgg=
|
||||
github.com/go-chi/chi/v5 v5.5.0/go.mod h1:d1IG7GtKPSjPyBP365PH8sAGVHk3qfB/IhjFNRH1wWM=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
@@ -34,6 +37,8 @@ github.com/gotnospirit/messageformat v0.0.0-20221001023931-dfe49f1eb092 h1:c7gcN
|
||||
github.com/gotnospirit/messageformat v0.0.0-20221001023931-dfe49f1eb092/go.mod h1:ZZAN4fkkful3l1lpJwF8JbW41ZiG9TwJ2ZlqzQovBNU=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/jcchavezs/mergefs v0.1.1 h1:D45R17m6dHnSVZefnhynoeZvcK2Uw0oTrRfoUOQ0S5Y=
|
||||
github.com/jcchavezs/mergefs v0.1.1/go.mod h1:eRLTrsA+vFwQZ48hj8p8gki/5v9C2bFtHH5Mnn4bcGk=
|
||||
github.com/kaptinlin/go-i18n v0.1.4 h1:wCiwAn1LOcvymvWIVAM4m5dUAMiHunTdEubLDk4hTGs=
|
||||
@@ -72,6 +77,7 @@ github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+
|
||||
github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
@@ -80,6 +86,9 @@ github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
@@ -109,6 +118,7 @@ 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.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M=
|
||||
@@ -127,6 +137,7 @@ golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU=
|
||||
golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
rsc.io/binaryregexp v0.2.0 h1:HfqmD5MEmC0zvwBuF187nq9mdnXjXsSivRiXN7SmRkE=
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// Package dirwatch watches a directory, and calls a function once the
|
||||
// directory has gone a short time without a change after one. The rule
|
||||
// files and the lookup database are read again this way.
|
||||
package dirwatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
// quietTime is how long the directory must go without a change before the
|
||||
// function is called, so that a file still being written, such as one
|
||||
// saved in place, appended to or copied in with scp, is read only once
|
||||
// whole.
|
||||
const quietTime = 2 * time.Second
|
||||
|
||||
// Watcher watches one directory. The directory is watched, rather than a
|
||||
// file in it, so that a file replaced by a rename is seen.
|
||||
type Watcher struct {
|
||||
watcher *fsnotify.Watcher
|
||||
}
|
||||
|
||||
// New starts watching dir, or returns why it cannot.
|
||||
func New(dir string) (*Watcher, error) {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = watcher.Add(dir)
|
||||
if err != nil {
|
||||
_ = watcher.Close()
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Watcher{watcher: watcher}, nil
|
||||
}
|
||||
|
||||
// Close stops watching the directory.
|
||||
func (w *Watcher) Close() error {
|
||||
return w.watcher.Close()
|
||||
}
|
||||
|
||||
// CallWhenQuiet calls changed each time the directory has gone quietTime
|
||||
// without a change, after a change to a path for which counts is true, until
|
||||
// ctx is done. It also calls failed with each error the watch reports. The
|
||||
// wait starts at once, as if for a change, so that a change made before the
|
||||
// watch began is taken in too.
|
||||
func (w *Watcher) CallWhenQuiet(
|
||||
ctx context.Context,
|
||||
counts func(path string) bool,
|
||||
changed func(),
|
||||
failed func(error),
|
||||
) {
|
||||
callWhenQuiet(
|
||||
ctx, w.watcher.Events, w.watcher.Errors, counts, changed, failed,
|
||||
)
|
||||
}
|
||||
|
||||
// callWhenQuiet is CallWhenQuiet, for the changes from events and the
|
||||
// errors from errs.
|
||||
func callWhenQuiet(
|
||||
ctx context.Context,
|
||||
events <-chan fsnotify.Event,
|
||||
errs <-chan error,
|
||||
counts func(path string) bool,
|
||||
changed func(),
|
||||
failed func(error),
|
||||
) {
|
||||
quiet := time.NewTimer(quietTime)
|
||||
defer quiet.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case event := <-events:
|
||||
if counts(event.Name) {
|
||||
quiet.Reset(quietTime)
|
||||
}
|
||||
case <-quiet.C:
|
||||
changed()
|
||||
case err := <-errs:
|
||||
failed(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package dirwatch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
// errWatch is an error the watch reports.
|
||||
var errWatch = errors.New("too many changes")
|
||||
|
||||
func TestDirectoryThatCannotBeWatchedIsAnError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := New(filepath.Join(t.TempDir(), "missing"))
|
||||
if err == nil {
|
||||
t.Error("a missing directory is watched, want an error")
|
||||
}
|
||||
}
|
||||
|
||||
// The tests below run callWhenQuiet in a synctest bubble, where time is a
|
||||
// clock of the test's own: time.Sleep moves it on at once, and
|
||||
// synctest.Wait returns once callWhenQuiet waits again, so that every call
|
||||
// due by then is made. The test sends the changes itself, as the watch of
|
||||
// a directory cannot run in a bubble.
|
||||
|
||||
func TestSecondChangeStartsTheWaitAgain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
loop := start(t, everyPath)
|
||||
|
||||
loop.changes <- fsnotify.Event{Name: "50-app.rules", Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 0)
|
||||
|
||||
// The second part starts the wait again.
|
||||
loop.changes <- fsnotify.Event{Name: "50-app.rules", Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 0)
|
||||
|
||||
time.Sleep(time.Nanosecond)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCalledOnceQuietAfterTheStart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
// An edit saved before the watch began sends no change, and is
|
||||
// taken in by this call.
|
||||
loop := start(t, everyPath)
|
||||
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 1)
|
||||
|
||||
// Long after, it has been called only once.
|
||||
time.Sleep(time.Hour)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChangeThatDoesNotCountIsNoChange(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
loop := start(t, func(path string) bool {
|
||||
return path == "ipinfo_lite.mmdb"
|
||||
})
|
||||
|
||||
// The wait that starts with the watch ends with a call.
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
|
||||
loop.changes <- fsnotify.Event{Name: "other.mmdb", Op: fsnotify.Create}
|
||||
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 1)
|
||||
|
||||
loop.changes <- fsnotify.Event{Name: "ipinfo_lite.mmdb", Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
loop.wantCalls(t, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestErrorsOfTheWatchHandedOn(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
loop := start(t, everyPath)
|
||||
|
||||
loop.errs <- errWatch
|
||||
|
||||
synctest.Wait()
|
||||
|
||||
if !errors.Is(loop.failure, errWatch) {
|
||||
t.Errorf("failed with %v, want %v", loop.failure, errWatch)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// loop is a callWhenQuiet that a test runs.
|
||||
type loop struct {
|
||||
// changes and errs send it changes and errors.
|
||||
changes chan fsnotify.Event
|
||||
errs chan error
|
||||
// calls counts its calls of changed, and failure is the last error it
|
||||
// handed to failed.
|
||||
calls atomic.Int32
|
||||
failure error
|
||||
}
|
||||
|
||||
// start runs callWhenQuiet, with counts, until the test ends.
|
||||
func start(t *testing.T, counts func(path string) bool) *loop {
|
||||
t.Helper()
|
||||
|
||||
l := &loop{changes: make(chan fsnotify.Event), errs: make(chan error)}
|
||||
ctx, stop := context.WithCancel(t.Context())
|
||||
stopped := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
callWhenQuiet(ctx, l.changes, l.errs, counts,
|
||||
func() { l.calls.Add(1) },
|
||||
func(err error) { l.failure = err },
|
||||
)
|
||||
close(stopped)
|
||||
}()
|
||||
|
||||
t.Cleanup(func() {
|
||||
stop()
|
||||
<-stopped
|
||||
})
|
||||
|
||||
return l
|
||||
}
|
||||
|
||||
// everyPath counts a change to any path.
|
||||
func everyPath(string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// wantCalls checks how many times changed has been called.
|
||||
func (l *loop) wantCalls(t *testing.T, want int32) {
|
||||
t.Helper()
|
||||
|
||||
got := l.calls.Load()
|
||||
if got != want {
|
||||
t.Errorf("changed called %d times, want %d", got, want)
|
||||
}
|
||||
}
|
||||
+18
-49
@@ -13,18 +13,13 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/oschwald/maxminddb-golang/v2"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/dirwatch"
|
||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||
)
|
||||
|
||||
// quietTime is how long the lookup database must go without a change
|
||||
// before it is read again, so that a file still being copied in is read
|
||||
// only once whole.
|
||||
const quietTime = 2 * time.Second
|
||||
|
||||
// FileParams are what OpenFile needs.
|
||||
type FileParams struct {
|
||||
// Path is the lookup database, the IPinfo Lite file in its .mmdb form
|
||||
@@ -122,20 +117,13 @@ func (f *File) ReadFailures() int {
|
||||
}
|
||||
|
||||
// Watch watches the directory of the lookup database until ctx is done,
|
||||
// and reads the file again once it has gone without a change for
|
||||
// quietTime, after it is replaced, written or removed, and after Watch
|
||||
// starts watching. If the directory cannot be watched, that is logged, and
|
||||
// the database read at start stays in use.
|
||||
// and reads the file again once it has gone without a change for a short
|
||||
// time, after it is replaced, written or removed, and after Watch starts
|
||||
// watching, as dirwatch describes. A change to another file in its
|
||||
// directory does not count. If the directory cannot be watched, that is
|
||||
// logged, and the database read at start stays in use.
|
||||
func (f *File) Watch(ctx context.Context) {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err == nil {
|
||||
defer func() {
|
||||
_ = watcher.Close()
|
||||
}()
|
||||
|
||||
err = watcher.Add(filepath.Dir(f.params.Path))
|
||||
}
|
||||
|
||||
watcher, err := dirwatch.New(filepath.Dir(f.params.Path))
|
||||
if err != nil {
|
||||
f.params.ProcessLog.Error(
|
||||
"cannot watch the lookup database for replacements",
|
||||
@@ -146,41 +134,22 @@ func (f *File) Watch(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = watcher.Close()
|
||||
}()
|
||||
|
||||
f.params.ProcessLog.Info("watching the lookup database for replacements",
|
||||
"file", f.params.Path)
|
||||
|
||||
f.readAfterChanges(ctx, watcher.Events, watcher.Errors)
|
||||
watcher.CallWhenQuiet(ctx, f.isDatabase, f.readAgain, func(err error) {
|
||||
f.params.ProcessLog.Warn("watching the lookup database failed",
|
||||
"error", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
// readAfterChanges reads the lookup database again once quietTime has
|
||||
// passed without a change to it from events, until ctx is done, and logs
|
||||
// the errors from errs. A change to another file in its directory does not
|
||||
// count. The wait starts at once, as if for a change, so that a file
|
||||
// replaced after OpenFile read it, and before its directory was watched,
|
||||
// is read too.
|
||||
func (f *File) readAfterChanges(
|
||||
ctx context.Context, events <-chan fsnotify.Event, errs <-chan error,
|
||||
) {
|
||||
path := filepath.Clean(f.params.Path)
|
||||
|
||||
quiet := time.NewTimer(quietTime)
|
||||
defer quiet.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case event := <-events:
|
||||
if filepath.Clean(event.Name) == path {
|
||||
quiet.Reset(quietTime)
|
||||
}
|
||||
case <-quiet.C:
|
||||
f.readAgain()
|
||||
case err := <-errs:
|
||||
f.params.ProcessLog.Warn("watching the lookup database failed",
|
||||
"error", err.Error())
|
||||
}
|
||||
}
|
||||
// isDatabase reports whether path names the lookup database.
|
||||
func (f *File) isDatabase(path string) bool {
|
||||
return filepath.Clean(path) == filepath.Clean(f.params.Path)
|
||||
}
|
||||
|
||||
// readAgain reads the lookup database again, in place of the one in use,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package lookup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
@@ -12,7 +11,6 @@ import (
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/maxmind/mmdbwriter/mmdbtype"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
@@ -133,66 +131,48 @@ func TestFileThatCannotBeReadIsAnError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The tests below run readAfterChanges in a synctest bubble, where time is
|
||||
// a clock of the test's own: time.Sleep moves it on at once, and
|
||||
// synctest.Wait returns once readAfterChanges waits again, so that every
|
||||
// reading due by then is done. The test sends the changes itself, as the
|
||||
// watch of a directory cannot run in a bubble.
|
||||
func TestChangeOfAnotherFileInTheDirectoryIsNoReplacement(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
func TestReplacementCopiedOverTheFileInTwoPartsIsReadOnlyWhole(
|
||||
t *testing.T,
|
||||
) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "ipinfo_lite.mmdb")
|
||||
writeDatabase(t, path, "DE")
|
||||
|
||||
f := openFile(t, path, newQueue())
|
||||
|
||||
for changed, want := range map[string]bool{
|
||||
path: true,
|
||||
dir + "/./ipinfo_lite.mmdb": true,
|
||||
filepath.Join(dir, "other.mmdb"): false,
|
||||
} {
|
||||
if f.isDatabase(changed) != want {
|
||||
t.Errorf(
|
||||
"a change to %s counts: %t, want %t",
|
||||
changed,
|
||||
!want,
|
||||
want,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The tests below run in a synctest bubble, where time is a clock of the
|
||||
// test's own, so that the time a file is read can be checked. The wait for
|
||||
// the directory to go quiet before a reading is tested in dirwatch.
|
||||
|
||||
func TestReplacementIsReadAgain(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "ipinfo_lite.mmdb")
|
||||
path := filepath.Join(t.TempDir(), "ipinfo_lite.mmdb")
|
||||
writeDatabase(t, path, "DE")
|
||||
|
||||
queue := newQueue()
|
||||
f := openFile(t, path, queue)
|
||||
changes := watch(t, f)
|
||||
|
||||
other := filepath.Join(dir, "replacement.mmdb")
|
||||
writeDatabase(t, other, "KP")
|
||||
|
||||
//nolint:gosec // a file the test wrote
|
||||
replacement, err := os.ReadFile(other)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", other, err)
|
||||
}
|
||||
|
||||
// The file in use is overwritten in place, and keeps giving what
|
||||
// it gave. Its first part alone is not a .mmdb file.
|
||||
file, err := os.Create(path) //nolint:gosec // a file the test wrote
|
||||
if err != nil {
|
||||
t.Fatalf("create %s: %v", path, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
half := len(replacement) / 2
|
||||
write(t, file, replacement[:half])
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
wantCountry(t, f, "DE")
|
||||
|
||||
// The second part starts the wait again.
|
||||
write(t, file, replacement[half:])
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
wantCountry(t, f, "DE")
|
||||
|
||||
time.Sleep(time.Nanosecond)
|
||||
synctest.Wait()
|
||||
writeDatabase(t, path, "KP")
|
||||
time.Sleep(time.Hour)
|
||||
f.readAgain()
|
||||
wantCountry(t, f, "KP")
|
||||
|
||||
if !f.LastRead().Equal(time.Now()) || f.ReadFailures() != 0 {
|
||||
@@ -219,15 +199,10 @@ func TestReplacementThatCannotBeReadLeavesTheFileInUseWithOneAlert(
|
||||
queue := newQueue()
|
||||
f := openFile(t, path, queue)
|
||||
read := f.LastRead()
|
||||
changes := watch(t, f)
|
||||
|
||||
writeFile(t, path, "not a lookup database\n")
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
// Long after, the replacement has been read once.
|
||||
time.Sleep(time.Hour)
|
||||
synctest.Wait()
|
||||
f.readAgain()
|
||||
wantCountry(t, f, "DE")
|
||||
|
||||
if !f.LastRead().Equal(read) || f.ReadFailures() != 1 {
|
||||
@@ -240,7 +215,7 @@ func TestReplacementThatCannotBeReadLeavesTheFileInUseWithOneAlert(
|
||||
}
|
||||
|
||||
wantAlerts(t, queue, alerts.Alert{
|
||||
Time: read.Add(quietTime),
|
||||
Time: read.Add(time.Hour),
|
||||
Event: alerts.EventFileError,
|
||||
Reason: "the lookup database cannot be read, and the one read before stays in use",
|
||||
Detail: map[string]any{
|
||||
@@ -252,55 +227,6 @@ func TestReplacementThatCannotBeReadLeavesTheFileInUseWithOneAlert(
|
||||
})
|
||||
}
|
||||
|
||||
func TestChangeOfAnotherFileInTheDirectoryIsNoReplacement(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "ipinfo_lite.mmdb")
|
||||
writeDatabase(t, path, "DE")
|
||||
|
||||
f := openFile(t, path, newQueue())
|
||||
changes := watch(t, f)
|
||||
|
||||
// The wait that starts with the watch ends with a reading.
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
writeDatabase(t, path, "KP")
|
||||
|
||||
changes <- fsnotify.Event{Name: filepath.Join(dir, "other.mmdb"), Op: fsnotify.Create}
|
||||
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
wantCountry(t, f, "DE")
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
wantCountry(t, f, "KP")
|
||||
})
|
||||
}
|
||||
|
||||
func TestReplacementSavedBeforeTheWatchStartsIsRead(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "ipinfo_lite.mmdb")
|
||||
writeDatabase(t, path, "DE")
|
||||
|
||||
f := openFile(t, path, newQueue())
|
||||
|
||||
// Saved after OpenFile read the file, and before its directory was
|
||||
// watched, so that no change is seen for it.
|
||||
writeDatabase(t, path, "KP")
|
||||
watch(t, f)
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
wantCountry(t, f, "KP")
|
||||
})
|
||||
}
|
||||
|
||||
// newQueue returns a queue of alerts for a webhook that is never sent
|
||||
// them, so that they wait in it for the test to look at.
|
||||
func newQueue() *alerts.Queue {
|
||||
@@ -344,28 +270,6 @@ func openFile(t *testing.T, path string, queue *alerts.Queue) *File {
|
||||
return f
|
||||
}
|
||||
|
||||
// watch runs f's readAfterChanges until the test ends, and returns the
|
||||
// channel that sends it changes.
|
||||
func watch(t *testing.T, f *File) chan<- fsnotify.Event {
|
||||
t.Helper()
|
||||
|
||||
changes := make(chan fsnotify.Event)
|
||||
ctx, stop := context.WithCancel(t.Context())
|
||||
stopped := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
f.readAfterChanges(ctx, changes, nil)
|
||||
close(stopped)
|
||||
}()
|
||||
|
||||
t.Cleanup(func() {
|
||||
stop()
|
||||
<-stopped
|
||||
})
|
||||
|
||||
return changes
|
||||
}
|
||||
|
||||
// wantCountry checks the country f gives testClient.
|
||||
func wantCountry(t *testing.T, f *File, want string) {
|
||||
t.Helper()
|
||||
@@ -401,13 +305,3 @@ func writeFile(t *testing.T, path, content string) {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
// write writes data to the end of file.
|
||||
func write(t *testing.T, file *os.File, data []byte) {
|
||||
t.Helper()
|
||||
|
||||
_, err := file.Write(data)
|
||||
if err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
}
|
||||
+393
-268
@@ -15,12 +15,33 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"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/rules"
|
||||
)
|
||||
|
||||
// Params are what New builds the metrics from. Each part is nil while it is
|
||||
// off, and a part that is nil adds no metric of its own.
|
||||
type Params struct {
|
||||
// BusiestCount is how many countries and how many AS numbers get series
|
||||
// of their own (SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS).
|
||||
BusiestCount int
|
||||
// InstanceName is the label instance every metric carries
|
||||
// (SWW_WAF_INSTANCE_NAME).
|
||||
InstanceName string
|
||||
// Now returns the time at which Bans counts the bans active.
|
||||
Now func() time.Time
|
||||
|
||||
Bans banLedger
|
||||
Clients sized
|
||||
Rules sized
|
||||
Lists fetchedLists
|
||||
DNSBL dnsblZones
|
||||
AbuseIPDB abuseIPDBChecker
|
||||
Alerts alertQueue
|
||||
LookupFile lookupDatabase
|
||||
RemoteLog remoteLogSender
|
||||
}
|
||||
|
||||
// Metrics are sww's metrics. They are safe for concurrent use.
|
||||
type Metrics struct {
|
||||
// registry gives every metric registered with it the label instance.
|
||||
@@ -36,12 +57,10 @@ type Metrics struct {
|
||||
rateLimitHits *prometheus.CounterVec
|
||||
offences *prometheus.CounterVec
|
||||
coreRuleSetMatches *prometheus.CounterVec
|
||||
// ruleMatches are made by AddRules, and reputationHits by
|
||||
// AddReputation.
|
||||
ruleMatches *prometheus.CounterVec
|
||||
reputationHits *prometheus.CounterVec
|
||||
countries *busiest
|
||||
asns *busiest
|
||||
ruleMatches *prometheus.CounterVec
|
||||
reputationHits *prometheus.CounterVec
|
||||
countries *busiest
|
||||
asns *busiest
|
||||
|
||||
// GeoJSRequests are the requests to GeoJS, and GeoJSFailures those
|
||||
// that failed. RequestsWithoutGeoJSAnswer are the requests that needed
|
||||
@@ -59,20 +78,18 @@ type Metrics struct {
|
||||
stateFileEditsSetAside *prometheus.CounterVec
|
||||
}
|
||||
|
||||
// New returns the metrics, with the Go runtime's and the process's own.
|
||||
// busiestCount is how many countries and how many AS numbers get series of
|
||||
// their own (SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS). Every metric
|
||||
// carries instanceName (SWW_WAF_INSTANCE_NAME) as its label instance.
|
||||
// New returns the metrics, with the Go runtime's and the process's own, and
|
||||
// those read from each part in params as the metrics are asked for.
|
||||
//
|
||||
//nolint:funlen // a few lines for each metric, a list that grows with them
|
||||
func New(busiestCount int, instanceName string) *Metrics {
|
||||
func New(params Params) *Metrics {
|
||||
byStatus := []string{"status_class", "action"}
|
||||
byFile := []string{"file"}
|
||||
registry := prometheus.NewRegistry()
|
||||
|
||||
m := &Metrics{
|
||||
registry: prometheus.WrapRegistererWith(
|
||||
prometheus.Labels{"instance": instanceName}, registry),
|
||||
prometheus.Labels{"instance": params.InstanceName}, registry),
|
||||
handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
||||
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "sww_requests_in_flight",
|
||||
@@ -113,8 +130,19 @@ func New(busiestCount int, instanceName string) *Metrics {
|
||||
"and the rule's id.",
|
||||
[]string{"mode", "rule_id"},
|
||||
),
|
||||
countries: newCountries(busiestCount),
|
||||
asns: newASNs(busiestCount),
|
||||
ruleMatches: counterVec(
|
||||
"sww_rule_matches_total",
|
||||
"Requests that matched a rule of the rule files, by its id and action.",
|
||||
[]string{"rule_id", "action"},
|
||||
),
|
||||
reputationHits: counterVec(
|
||||
"sww_reputation_hits_total",
|
||||
"Requests whose client a blocklist, a DNSBL zone or AbuseIPDB lists, "+
|
||||
"by the list's URL, the zone, or abuseipdb.",
|
||||
[]string{sourceLabel},
|
||||
),
|
||||
countries: newCountries(params.BusiestCount),
|
||||
asns: newASNs(params.BusiestCount),
|
||||
GeoJSRequests: prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "sww_geojs_requests_total",
|
||||
Help: "Requests to GeoJS.",
|
||||
@@ -166,6 +194,7 @@ func New(busiestCount int, instanceName string) *Metrics {
|
||||
m.inFlight, m.requests, m.requestBytes, m.responseBytes,
|
||||
m.requestDuration, m.upstreamDuration,
|
||||
m.rateLimitHits, m.offences, m.coreRuleSetMatches,
|
||||
m.ruleMatches, m.reputationHits,
|
||||
m.countries, m.asns,
|
||||
m.GeoJSRequests, m.GeoJSFailures, m.RequestsWithoutGeoJSAnswer,
|
||||
m.stateFileWrites, m.stateFileWriteFailures,
|
||||
@@ -173,261 +202,45 @@ func New(busiestCount int, instanceName string) *Metrics {
|
||||
m.stateFileEditsTakenIn, m.stateFileEditsSetAside,
|
||||
)
|
||||
|
||||
if params.Bans != nil {
|
||||
m.addBans(params.Bans, params.Now)
|
||||
}
|
||||
|
||||
if params.Clients != nil {
|
||||
m.addClients(params.Clients)
|
||||
}
|
||||
|
||||
if params.Rules != nil {
|
||||
m.addRules(params.Rules)
|
||||
}
|
||||
|
||||
if params.Lists != nil {
|
||||
m.addLists(params.Lists)
|
||||
}
|
||||
|
||||
if params.DNSBL != nil {
|
||||
m.addDNSBL(params.DNSBL)
|
||||
}
|
||||
|
||||
if params.AbuseIPDB != nil {
|
||||
m.addAbuseIPDB(params.AbuseIPDB)
|
||||
}
|
||||
|
||||
if params.Alerts != nil {
|
||||
m.addAlerts(params.Alerts)
|
||||
}
|
||||
|
||||
if params.LookupFile != nil {
|
||||
m.addLookupFile(params.LookupFile)
|
||||
}
|
||||
|
||||
if params.RemoteLog != nil {
|
||||
m.addRemoteLog(params.RemoteLog)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// AddBansAndClients adds the metrics read from the ledger and the table
|
||||
// of clients as the metrics are asked for: the bans made since the start,
|
||||
// by cause, the bans active and permanent at now, and the clients in the
|
||||
// table.
|
||||
func (m *Metrics) AddBansAndClients(
|
||||
ledger *bans.Ledger, limiter *ratelimit.Limiter, now func() time.Time,
|
||||
) {
|
||||
for _, cause := range []string{
|
||||
bans.CauseLimit, bans.CauseAttack, bans.CauseAdmin, bans.CauseAbuseIPDB,
|
||||
} {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_bans_made_total",
|
||||
Help: "Bans made, by cause.",
|
||||
ConstLabels: prometheus.Labels{"cause": cause},
|
||||
}, func() float64 {
|
||||
return float64(ledger.Made(cause))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_active_bans",
|
||||
Help: "Bans active now, the permanent ones included.",
|
||||
}, func() float64 {
|
||||
active, _ := ledger.Count(now())
|
||||
|
||||
return float64(active)
|
||||
}),
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_permanent_bans",
|
||||
Help: "Permanent bans not lifted.",
|
||||
}, func() float64 {
|
||||
_, permanent := ledger.Count(now())
|
||||
|
||||
return float64(permanent)
|
||||
}),
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_tracked_clients",
|
||||
Help: "Clients in the table of clients.",
|
||||
}, func() float64 {
|
||||
return float64(limiter.Len())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// AddRules adds the metrics of the rule files: the requests that matched
|
||||
// each rule, which RuleMatched counts, and the rules loaded from
|
||||
// ruleFiles, read as the metrics are asked for. It is called once, before
|
||||
// RuleMatched.
|
||||
func (m *Metrics) AddRules(ruleFiles *rules.Files) {
|
||||
m.ruleMatches = counterVec(
|
||||
"sww_rule_matches_total",
|
||||
"Requests that matched a rule of the rule files, by its id and action.",
|
||||
[]string{"rule_id", "action"},
|
||||
)
|
||||
|
||||
m.registry.MustRegister(m.ruleMatches,
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_rules_loaded",
|
||||
Help: "Rules loaded from the rule files.",
|
||||
}, func() float64 {
|
||||
return float64(ruleFiles.Len())
|
||||
}))
|
||||
}
|
||||
|
||||
// AddRemoteLog adds the metrics of sending the log lines to
|
||||
// SWW_WAF_LOG_REMOTE_URL, read from remote as the metrics are asked for: the
|
||||
// lines sent, those dropped, and those waiting in the buffer.
|
||||
func (m *Metrics) AddRemoteLog(remote *remotelog.Sender) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_remote_log_lines_sent_total",
|
||||
Help: "Log lines sent to SWW_WAF_LOG_REMOTE_URL.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Sent())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_remote_log_lines_dropped_total",
|
||||
Help: "Log lines dropped: the oldest in a full buffer, and those " +
|
||||
"whose sending failed.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Dropped())
|
||||
}),
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_remote_log_lines_waiting",
|
||||
Help: "Log lines in the buffer, waiting to be sent.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Depth())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// AddLookupFile adds the metrics of the lookup database, read as the
|
||||
// metrics are asked for: when the file in use was read, which lastRead
|
||||
// returns, and the replacements of it that could not be read, which
|
||||
// readFailures returns.
|
||||
func (m *Metrics) AddLookupFile(
|
||||
lastRead func() time.Time,
|
||||
readFailures func() int,
|
||||
) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_lookup_database_last_read_timestamp_seconds",
|
||||
Help: "When the lookup database in use was read, in seconds since 1970.",
|
||||
}, func() float64 {
|
||||
return float64(lastRead().Unix())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_lookup_database_read_failures_total",
|
||||
Help: "Replacements of the lookup database that could not be read.",
|
||||
}, func() float64 {
|
||||
return float64(readFailures())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// sourceLabel is the label of the reputation metrics: a list's URL, a
|
||||
// DNSBL zone, its key masked, or abuseipdb.
|
||||
const sourceLabel = "source"
|
||||
|
||||
// AddReputation adds the metrics of the lists fetched from URLs and of the
|
||||
// DNSBL zones, by source, each list's URL or each zone, its key masked as
|
||||
// reputation.MaskZoneKey masks it: the requests whose client a blocklist, a
|
||||
// zone's verdict or AbuseIPDB's score lists, which ReputationHit counts,
|
||||
// and, read from lists and dnsbl as the metrics are asked for, for a list,
|
||||
// the fetches that failed and when the copy in use was fetched, and for a
|
||||
// zone, the queries made and those that failed. It is called once, before
|
||||
// ReputationHit.
|
||||
func (m *Metrics) AddReputation(
|
||||
lists *reputation.Lists,
|
||||
dnsbl *reputation.DNSBL,
|
||||
) {
|
||||
m.reputationHits = counterVec(
|
||||
"sww_reputation_hits_total",
|
||||
"Requests whose client a blocklist, a DNSBL zone or AbuseIPDB lists, "+
|
||||
"by the list's URL, the zone, or abuseipdb.",
|
||||
[]string{sourceLabel},
|
||||
)
|
||||
m.registry.MustRegister(m.reputationHits)
|
||||
|
||||
for _, zone := range dnsbl.Zones() {
|
||||
source := prometheus.Labels{
|
||||
sourceLabel: reputation.MaskZoneKey(zone),
|
||||
}
|
||||
|
||||
m.addReputationQueries(
|
||||
source,
|
||||
func() int { return dnsbl.Queries(zone) },
|
||||
)
|
||||
m.addReputationFailures(
|
||||
source,
|
||||
func() int { return dnsbl.Failures(zone) },
|
||||
)
|
||||
}
|
||||
|
||||
for _, listURL := range lists.URLs() {
|
||||
source := prometheus.Labels{sourceLabel: listURL}
|
||||
|
||||
m.addReputationFailures(
|
||||
source,
|
||||
func() int { return lists.Failures(listURL) },
|
||||
)
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_reputation_last_fetch_timestamp_seconds",
|
||||
Help: "When the copy of the list in use was fetched, in seconds since " +
|
||||
"1970, or 0 while there is none.",
|
||||
ConstLabels: source,
|
||||
}, func() float64 {
|
||||
fetched := lists.Fetched(listURL)
|
||||
if fetched.IsZero() {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(fetched.Unix())
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// AddAbuseIPDB adds the metrics of AbuseIPDB, with the source abuseipdb,
|
||||
// read from abuseIPDB as the metrics are asked for: the checks made, those
|
||||
// that failed, and how many checks the day's budget has left. It is
|
||||
// called once, after AddReputation, while SWW_WAF_ABUSEIPDB_KEY is set.
|
||||
func (m *Metrics) AddAbuseIPDB(abuseIPDB *reputation.AbuseIPDB) {
|
||||
source := prometheus.Labels{sourceLabel: reputation.AbuseIPDBSource}
|
||||
|
||||
m.addReputationQueries(source, abuseIPDB.Checked)
|
||||
m.addReputationFailures(source, abuseIPDB.Failures)
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_reputation_daily_budget_remaining",
|
||||
Help: "Checks of the day's SWW_WAF_ABUSEIPDB_DAILY_BUDGET not yet spent.",
|
||||
ConstLabels: source,
|
||||
}, func() float64 {
|
||||
return float64(abuseIPDB.BudgetLeft())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// ReputationHit counts a request whose client source lists: a blocklist,
|
||||
// by its URL, a DNSBL zone, its key masked, or AbuseIPDB, abuseipdb.
|
||||
func (m *Metrics) ReputationHit(source string) {
|
||||
m.reputationHits.WithLabelValues(source).Inc()
|
||||
}
|
||||
|
||||
// AddAlerts adds the metrics of the alerts sent to each destination set,
|
||||
// read from queue as the metrics are asked for, by destination: the
|
||||
// alerts sent, the requests to the destination that failed, the alerts
|
||||
// held back, which are the same for every destination, and those
|
||||
// dropped. With no destination set, it adds none.
|
||||
func (m *Metrics) AddAlerts(queue *alerts.Queue) {
|
||||
for _, name := range queue.DestinationsSet() {
|
||||
destination := prometheus.Labels{"destination": name}
|
||||
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_sent_total",
|
||||
Help: "Alerts the destination took.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).Sent)
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alert_send_failures_total",
|
||||
Help: "Requests to the destination that failed.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).SendFailures)
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_suppressed_total",
|
||||
Help: "Alerts held back: repeats within SWW_WAF_ALERT_COOLDOWN, and " +
|
||||
"alerts past SWW_WAF_ALERT_MAX_PER_HOUR, for the hour's summary.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Suppressed())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_dropped_total",
|
||||
Help: "Alerts dropped, the oldest first, from a full queue, and " +
|
||||
"alerts given up as the destination refused them.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).Dropped)
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP answers with the metrics in the Prometheus text format.
|
||||
func (m *Metrics) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m.handler.ServeHTTP(w, r)
|
||||
@@ -497,6 +310,12 @@ func (m *Metrics) CoreRuleSetMatched(mode string, id int) {
|
||||
m.coreRuleSetMatches.WithLabelValues(mode, strconv.Itoa(id)).Inc()
|
||||
}
|
||||
|
||||
// ReputationHit counts a request whose client source lists: a blocklist,
|
||||
// by its URL, a DNSBL zone, its key masked, or AbuseIPDB, abuseipdb.
|
||||
func (m *Metrics) ReputationHit(source string) {
|
||||
m.reputationHits.WithLabelValues(source).Inc()
|
||||
}
|
||||
|
||||
// StateFileWritten counts a write of the state or cache file name, of size
|
||||
// bytes, that ended with err.
|
||||
func (m *Metrics) StateFileWritten(name string, size int, err error) {
|
||||
@@ -528,6 +347,312 @@ func (m *Metrics) StateFileEditSetAside(name string) {
|
||||
m.stateFileEditsSetAside.WithLabelValues(name).Inc()
|
||||
}
|
||||
|
||||
// banLedger is what the metrics read from the ledger of bans.
|
||||
type banLedger interface {
|
||||
// Made returns the bans made since the start with cause.
|
||||
Made(cause string) int
|
||||
// Count returns the bans active at now, the permanent ones included,
|
||||
// and the permanent ones.
|
||||
Count(now time.Time) (active, permanent int)
|
||||
}
|
||||
|
||||
// addBans adds the metrics read from ledger as the metrics are asked for:
|
||||
// the bans made since the start, by cause, and the bans active and
|
||||
// permanent at now.
|
||||
func (m *Metrics) addBans(ledger banLedger, now func() time.Time) {
|
||||
for _, cause := range []string{
|
||||
bans.CauseLimit, bans.CauseAttack, bans.CauseAdmin, bans.CauseAbuseIPDB,
|
||||
} {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_bans_made_total",
|
||||
Help: "Bans made, by cause.",
|
||||
ConstLabels: prometheus.Labels{"cause": cause},
|
||||
}, func() float64 {
|
||||
return float64(ledger.Made(cause))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_active_bans",
|
||||
Help: "Bans active now, the permanent ones included.",
|
||||
}, func() float64 {
|
||||
active, _ := ledger.Count(now())
|
||||
|
||||
return float64(active)
|
||||
}),
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_permanent_bans",
|
||||
Help: "Permanent bans not lifted.",
|
||||
}, func() float64 {
|
||||
_, permanent := ledger.Count(now())
|
||||
|
||||
return float64(permanent)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// sized is what the metrics read from the table of clients and from the
|
||||
// rule files.
|
||||
type sized interface {
|
||||
// Len returns how many clients the table holds, or how many rules were
|
||||
// loaded from the rule files.
|
||||
Len() int
|
||||
}
|
||||
|
||||
// addClients adds the metric of the clients in table, read as the metrics
|
||||
// are asked for.
|
||||
func (m *Metrics) addClients(table sized) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_tracked_clients",
|
||||
Help: "Clients in the table of clients.",
|
||||
}, func() float64 {
|
||||
return float64(table.Len())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// addRules adds the metric of the rules loaded from files, read as the
|
||||
// metrics are asked for.
|
||||
func (m *Metrics) addRules(files sized) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_rules_loaded",
|
||||
Help: "Rules loaded from the rule files.",
|
||||
}, func() float64 {
|
||||
return float64(files.Len())
|
||||
}))
|
||||
}
|
||||
|
||||
// remoteLogSender is what the metrics read from what sends the log lines to
|
||||
// SWW_WAF_LOG_REMOTE_URL.
|
||||
type remoteLogSender interface {
|
||||
// Sent returns the lines sent.
|
||||
Sent() int64
|
||||
// Dropped returns the lines dropped.
|
||||
Dropped() int64
|
||||
// Depth returns the lines waiting in the buffer.
|
||||
Depth() int
|
||||
}
|
||||
|
||||
// addRemoteLog adds the metrics of sending the log lines to
|
||||
// SWW_WAF_LOG_REMOTE_URL, read from remote as the metrics are asked for: the
|
||||
// lines sent, those dropped, and those waiting in the buffer.
|
||||
func (m *Metrics) addRemoteLog(remote remoteLogSender) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_remote_log_lines_sent_total",
|
||||
Help: "Log lines sent to SWW_WAF_LOG_REMOTE_URL.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Sent())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_remote_log_lines_dropped_total",
|
||||
Help: "Log lines dropped: the oldest in a full buffer, and those " +
|
||||
"whose sending failed.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Dropped())
|
||||
}),
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_remote_log_lines_waiting",
|
||||
Help: "Log lines in the buffer, waiting to be sent.",
|
||||
}, func() float64 {
|
||||
return float64(remote.Depth())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// lookupDatabase is what the metrics read from the lookup database.
|
||||
type lookupDatabase interface {
|
||||
// LastRead returns when the file in use was read.
|
||||
LastRead() time.Time
|
||||
// ReadFailures returns the replacements of the file that could not be
|
||||
// read.
|
||||
ReadFailures() int
|
||||
}
|
||||
|
||||
// addLookupFile adds the metrics of the lookup database, read from file as
|
||||
// the metrics are asked for: when the file in use was read, and the
|
||||
// replacements of it that could not be read.
|
||||
func (m *Metrics) addLookupFile(file lookupDatabase) {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_lookup_database_last_read_timestamp_seconds",
|
||||
Help: "When the lookup database in use was read, in seconds since 1970.",
|
||||
}, func() float64 {
|
||||
return float64(file.LastRead().Unix())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_lookup_database_read_failures_total",
|
||||
Help: "Replacements of the lookup database that could not be read.",
|
||||
}, func() float64 {
|
||||
return float64(file.ReadFailures())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// sourceLabel is the label of the reputation metrics: a list's URL, a
|
||||
// DNSBL zone, its key masked, or abuseipdb.
|
||||
const sourceLabel = "source"
|
||||
|
||||
// dnsblZones is what the metrics read from what asks the DNSBL zones.
|
||||
type dnsblZones interface {
|
||||
// Zones returns the zones asked, their keys unmasked.
|
||||
Zones() []string
|
||||
// Queries returns the queries made to zone.
|
||||
Queries(zone string) int
|
||||
// Failures returns the queries to zone that failed.
|
||||
Failures(zone string) int
|
||||
}
|
||||
|
||||
// addDNSBL adds the metrics of each zone dnsbl asks, by source, the zone,
|
||||
// its key masked as reputation.MaskZoneKey masks it, read as the metrics are
|
||||
// asked for: the queries made and those that failed.
|
||||
func (m *Metrics) addDNSBL(dnsbl dnsblZones) {
|
||||
for _, zone := range dnsbl.Zones() {
|
||||
source := prometheus.Labels{
|
||||
sourceLabel: reputation.MaskZoneKey(zone),
|
||||
}
|
||||
|
||||
m.addReputationQueries(
|
||||
source,
|
||||
func() int { return dnsbl.Queries(zone) },
|
||||
)
|
||||
m.addReputationFailures(
|
||||
source,
|
||||
func() int { return dnsbl.Failures(zone) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// fetchedLists is what the metrics read from the lists fetched from URLs.
|
||||
type fetchedLists interface {
|
||||
// URLs returns the lists' URLs.
|
||||
URLs() []string
|
||||
// Failures returns the fetches of the list at listURL that failed.
|
||||
Failures(listURL string) int
|
||||
// Fetched returns when the copy in use of the list at listURL was
|
||||
// fetched, or the zero time while there is none.
|
||||
Fetched(listURL string) time.Time
|
||||
}
|
||||
|
||||
// addLists adds the metrics of each list fetched from a URL, by source, the
|
||||
// list's URL, read from lists as the metrics are asked for: the fetches
|
||||
// that failed and when the copy in use was fetched.
|
||||
func (m *Metrics) addLists(lists fetchedLists) {
|
||||
for _, listURL := range lists.URLs() {
|
||||
source := prometheus.Labels{sourceLabel: listURL}
|
||||
|
||||
m.addReputationFailures(
|
||||
source,
|
||||
func() int { return lists.Failures(listURL) },
|
||||
)
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_reputation_last_fetch_timestamp_seconds",
|
||||
Help: "When the copy of the list in use was fetched, in seconds since " +
|
||||
"1970, or 0 while there is none.",
|
||||
ConstLabels: source,
|
||||
}, func() float64 {
|
||||
fetched := lists.Fetched(listURL)
|
||||
if fetched.IsZero() {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(fetched.Unix())
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// abuseIPDBChecker is what the metrics read from what checks clients with
|
||||
// AbuseIPDB.
|
||||
type abuseIPDBChecker interface {
|
||||
// Checked returns the checks made.
|
||||
Checked() int
|
||||
// Failures returns the checks that failed.
|
||||
Failures() int
|
||||
// BudgetLeft returns how many checks the day's budget has left.
|
||||
BudgetLeft() int
|
||||
}
|
||||
|
||||
// addAbuseIPDB adds the metrics of AbuseIPDB, with the source abuseipdb,
|
||||
// read from abuseIPDB as the metrics are asked for: the checks made, those
|
||||
// that failed, and how many checks the day's budget has left.
|
||||
func (m *Metrics) addAbuseIPDB(abuseIPDB abuseIPDBChecker) {
|
||||
source := prometheus.Labels{sourceLabel: reputation.AbuseIPDBSource}
|
||||
|
||||
m.addReputationQueries(source, abuseIPDB.Checked)
|
||||
m.addReputationFailures(source, abuseIPDB.Failures)
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
||||
Name: "sww_reputation_daily_budget_remaining",
|
||||
Help: "Checks of the day's SWW_WAF_ABUSEIPDB_DAILY_BUDGET not yet spent.",
|
||||
ConstLabels: source,
|
||||
}, func() float64 {
|
||||
return float64(abuseIPDB.BudgetLeft())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// alertQueue is what the metrics read from the queue of alerts.
|
||||
type alertQueue interface {
|
||||
// DestinationsSet returns the names of the destinations set.
|
||||
DestinationsSet() []string
|
||||
// Counts returns the alerts sent to the destination name, the requests
|
||||
// to it that failed, and the alerts dropped.
|
||||
Counts(name string) alerts.Counts
|
||||
// Suppressed returns the alerts held back.
|
||||
Suppressed() int64
|
||||
}
|
||||
|
||||
// addAlerts adds the metrics of the alerts sent to each destination set,
|
||||
// read from queue as the metrics are asked for, by destination: the
|
||||
// alerts sent, the requests to the destination that failed, the alerts
|
||||
// held back, which are the same for every destination, and those
|
||||
// dropped. With no destination set, it adds none.
|
||||
func (m *Metrics) addAlerts(queue alertQueue) {
|
||||
for _, name := range queue.DestinationsSet() {
|
||||
destination := prometheus.Labels{"destination": name}
|
||||
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_sent_total",
|
||||
Help: "Alerts the destination took.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).Sent)
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alert_send_failures_total",
|
||||
Help: "Requests to the destination that failed.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).SendFailures)
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_suppressed_total",
|
||||
Help: "Alerts held back: repeats within SWW_WAF_ALERT_COOLDOWN, and " +
|
||||
"alerts past SWW_WAF_ALERT_MAX_PER_HOUR, for the hour's summary.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Suppressed())
|
||||
}),
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_alerts_dropped_total",
|
||||
Help: "Alerts dropped, the oldest first, from a full queue, and " +
|
||||
"alerts given up as the destination refused them.",
|
||||
ConstLabels: destination,
|
||||
}, func() float64 {
|
||||
return float64(queue.Counts(name).Dropped)
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// count counts a request that has ended, as RequestEnded and RequestReported
|
||||
// describe.
|
||||
func (m *Metrics) count(
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package metrics_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
||||
)
|
||||
|
||||
const (
|
||||
listURL = "https://lists.example/block.txt"
|
||||
zone = "dnsbl.example"
|
||||
destination = "webhook"
|
||||
)
|
||||
|
||||
// Each stand-in answers with a number of its own, so that each metric
|
||||
// shows which part it was read from.
|
||||
|
||||
type ledgerStandIn struct{}
|
||||
|
||||
func (ledgerStandIn) Made(string) int { return 1 }
|
||||
|
||||
func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 }
|
||||
|
||||
type clientTableStandIn struct{}
|
||||
|
||||
func (clientTableStandIn) Len() int { return 3 }
|
||||
|
||||
type ruleFilesStandIn struct{}
|
||||
|
||||
func (ruleFilesStandIn) Len() int { return 4 }
|
||||
|
||||
type listsStandIn struct{}
|
||||
|
||||
func (listsStandIn) URLs() []string { return []string{listURL} }
|
||||
|
||||
func (listsStandIn) Failures(string) int { return 5 }
|
||||
|
||||
func (listsStandIn) Fetched(string) time.Time { return time.Time{} }
|
||||
|
||||
type dnsblStandIn struct{}
|
||||
|
||||
func (dnsblStandIn) Zones() []string { return []string{zone} }
|
||||
|
||||
func (dnsblStandIn) Queries(string) int { return 6 }
|
||||
|
||||
func (dnsblStandIn) Failures(string) int { return 0 }
|
||||
|
||||
type abuseIPDBStandIn struct{}
|
||||
|
||||
func (abuseIPDBStandIn) Checked() int { return 0 }
|
||||
|
||||
func (abuseIPDBStandIn) Failures() int { return 0 }
|
||||
|
||||
func (abuseIPDBStandIn) BudgetLeft() int { return 7 }
|
||||
|
||||
type alertQueueStandIn struct{}
|
||||
|
||||
func (alertQueueStandIn) DestinationsSet() []string {
|
||||
return []string{destination}
|
||||
}
|
||||
|
||||
func (alertQueueStandIn) Counts(string) alerts.Counts {
|
||||
return alerts.Counts{Sent: 8}
|
||||
}
|
||||
|
||||
func (alertQueueStandIn) Suppressed() int64 { return 0 }
|
||||
|
||||
type lookupDatabaseStandIn struct{}
|
||||
|
||||
func (lookupDatabaseStandIn) LastRead() time.Time { return time.Time{} }
|
||||
|
||||
func (lookupDatabaseStandIn) ReadFailures() int { return 9 }
|
||||
|
||||
type remoteLogStandIn struct{}
|
||||
|
||||
func (remoteLogStandIn) Sent() int64 { return 10 }
|
||||
|
||||
func (remoteLogStandIn) Dropped() int64 { return 0 }
|
||||
|
||||
func (remoteLogStandIn) Depth() int { return 0 }
|
||||
|
||||
func TestMetricsAreReadFromEachPart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1,
|
||||
InstanceName: "app",
|
||||
Now: func() time.Time { return time.Time{} },
|
||||
Bans: ledgerStandIn{},
|
||||
Clients: clientTableStandIn{},
|
||||
Rules: ruleFilesStandIn{},
|
||||
Lists: listsStandIn{},
|
||||
DNSBL: dnsblStandIn{},
|
||||
AbuseIPDB: abuseIPDBStandIn{},
|
||||
Alerts: alertQueueStandIn{},
|
||||
LookupFile: lookupDatabaseStandIn{},
|
||||
RemoteLog: remoteLogStandIn{},
|
||||
})
|
||||
scraped := scrape(t, m)
|
||||
|
||||
for _, line := range []string{
|
||||
`sww_bans_made_total{cause="limit",instance="app"} 1`,
|
||||
`sww_active_bans{instance="app"} 2`,
|
||||
`sww_tracked_clients{instance="app"} 3`,
|
||||
`sww_rules_loaded{instance="app"} 4`,
|
||||
`sww_reputation_failures_total{instance="app",source="` + listURL + `"} 5`,
|
||||
`sww_reputation_queries_total{instance="app",source="` + zone + `"} 6`,
|
||||
`sww_reputation_daily_budget_remaining{instance="app",source="abuseipdb"} 7`,
|
||||
`sww_alerts_sent_total{destination="` + destination + `",instance="app"} 8`,
|
||||
`sww_lookup_database_read_failures_total{instance="app"} 9`,
|
||||
`sww_remote_log_lines_sent_total{instance="app"} 10`,
|
||||
} {
|
||||
if !strings.Contains(scraped, "\n"+line+"\n") {
|
||||
t.Errorf("metrics\n%s\nwant\n%s", scraped, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartThatIsOffAddsNoMetric(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
scraped := scrape(t, metrics.New(metrics.Params{
|
||||
BusiestCount: 1,
|
||||
InstanceName: "app",
|
||||
}))
|
||||
|
||||
for _, name := range []string{
|
||||
"sww_bans_made_total",
|
||||
"sww_tracked_clients",
|
||||
"sww_rules_loaded",
|
||||
"sww_reputation_",
|
||||
"sww_alert",
|
||||
"sww_lookup_database_",
|
||||
"sww_remote_log_",
|
||||
} {
|
||||
if strings.Contains(scraped, name) {
|
||||
t.Errorf("metrics\n%s\nshow %s", scraped, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// scrape returns the metrics m serves.
|
||||
func scrape(t *testing.T, m *metrics.Metrics) string {
|
||||
t.Helper()
|
||||
|
||||
scraped := httptest.NewRecorder()
|
||||
m.ServeHTTP(
|
||||
scraped,
|
||||
httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/", http.NoBody,
|
||||
),
|
||||
)
|
||||
|
||||
return scraped.Body.String()
|
||||
}
|
||||
@@ -445,12 +445,9 @@ func TestKeyIsSentInTheKeyHeaderAndNeverShown(t *testing.T) {
|
||||
scores: map[string]int64{suspect: 100},
|
||||
}
|
||||
checker := newAbuseIPDB(abuseIPDB, p)
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(
|
||||
reputation.New(params()),
|
||||
reputation.NewDNSBL(dnsblParams()),
|
||||
)
|
||||
m.AddAbuseIPDB(checker)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||
})
|
||||
|
||||
// One check that AbuseIPDB answers, and one it refuses with an answer
|
||||
// that names the key.
|
||||
@@ -503,12 +500,9 @@ func TestMetricsCountTheChecksTheFailuresAndTheBudgetLeft(t *testing.T) {
|
||||
p := abuseIPDBParams()
|
||||
p.DailyBudget = 5
|
||||
checker := newAbuseIPDB(abuseIPDB, p)
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(
|
||||
reputation.New(params()),
|
||||
reputation.NewDNSBL(dnsblParams()),
|
||||
)
|
||||
m.AddAbuseIPDB(checker)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||
})
|
||||
|
||||
// One check that AbuseIPDB answers, and one that fails.
|
||||
hitFrom(t, checker, suspect, true)
|
||||
@@ -811,6 +805,9 @@ func wantBudgetLeft(t *testing.T, checker *reputation.AbuseIPDB, want int) {
|
||||
}
|
||||
}
|
||||
|
||||
// instanceName is the label instance of the metrics the tests build.
|
||||
const instanceName = "app"
|
||||
|
||||
// scrapeMetrics returns the metrics m serves.
|
||||
func scrapeMetrics(t *testing.T, m *metrics.Metrics) string {
|
||||
t.Helper()
|
||||
|
||||
@@ -358,8 +358,9 @@ func TestMetricsCountEachZonesQueriesAndThoseThatFailed(t *testing.T) {
|
||||
"99.2.0.192." + otherZone + ".": {rcode: serverFailure},
|
||||
}}
|
||||
dnsbl := newDNSBL(resolver, dnsblParams(zone, otherZone))
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(reputation.New(params()), dnsbl)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||
})
|
||||
|
||||
wantZones(t, dnsbl, listed)
|
||||
synctest.Wait()
|
||||
@@ -404,8 +405,9 @@ func TestZoneKeyIsMaskedInTheVerdictsTheFailuresAndTheMetrics(t *testing.T) {
|
||||
"99.2.0.192." + keyed + ".": {addrs: []string{listing}},
|
||||
"100.2.0.192." + keyed + ".": {rcode: serverFailure},
|
||||
}}, p)
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(reputation.New(params()), dnsbl)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||
})
|
||||
|
||||
// Both clients are asked about before either answer comes, so that
|
||||
// the failure does not keep the zone from the other query.
|
||||
|
||||
@@ -2,6 +2,7 @@ package rproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
@@ -22,7 +23,9 @@ type requestBody struct {
|
||||
// body is the visitor's body, ending in an *http.MaxBytesError past
|
||||
// SWW_RPROXY_MAX_REQUEST_SIZE.
|
||||
body io.ReadCloser
|
||||
rq *request
|
||||
// refuse refuses the request with a status, as the request's own refuse
|
||||
// does.
|
||||
refuse func(status int)
|
||||
// held is the part read before the request goes to the app, which Read gives
|
||||
// first.
|
||||
held []byte
|
||||
@@ -68,7 +71,7 @@ func (b *requestBody) Read(p []byte) (int, error) {
|
||||
case errors.Is(err, io.EOF):
|
||||
b.received.Store(true)
|
||||
case errors.As(err, &tooLarge):
|
||||
b.rq.refuse(http.StatusRequestEntityTooLarge)
|
||||
b.refuse(http.StatusRequestEntityTooLarge)
|
||||
}
|
||||
|
||||
return n, err
|
||||
@@ -86,7 +89,13 @@ type responseBody struct {
|
||||
// body is the app's body, ending in an *http.MaxBytesError past
|
||||
// SWW_RPROXY_MAX_RESPONSE_SIZE.
|
||||
body io.ReadCloser
|
||||
rq *request
|
||||
// refuse refuses the request with a status, as the request's own refuse
|
||||
// does.
|
||||
refuse func(status int)
|
||||
// responseReceived is called once the app's whole answer has arrived.
|
||||
responseReceived func()
|
||||
// ctx is the visitor's request's context.
|
||||
ctx context.Context //nolint:containedctx // the body ends with the request
|
||||
}
|
||||
|
||||
// Read reads from the app's body.
|
||||
@@ -100,15 +109,15 @@ func (b *responseBody) Read(p []byte) (int, error) {
|
||||
|
||||
switch {
|
||||
case errors.Is(err, io.EOF):
|
||||
b.rq.responseReceived()
|
||||
b.responseReceived()
|
||||
case errors.As(err, &tooLarge):
|
||||
b.rq.refuse(http.StatusBadGateway)
|
||||
b.refuse(http.StatusBadGateway)
|
||||
|
||||
return n, errResponseTooLarge
|
||||
case b.rq.in.Context().Err() == nil:
|
||||
case b.ctx.Err() == nil:
|
||||
// The answer broke off, not because the visitor went away. If a time
|
||||
// limit cut it, that refusal came first and is the one kept.
|
||||
b.rq.refuse(http.StatusBadGateway)
|
||||
b.refuse(http.StatusBadGateway)
|
||||
}
|
||||
|
||||
return n, err
|
||||
|
||||
+37
-164
@@ -12,7 +12,6 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -82,17 +81,9 @@ type request struct {
|
||||
// upgraded is the connection to the app once the app has switched protocols,
|
||||
// as for a WebSocket, and nil otherwise.
|
||||
upgraded *upgradedConn
|
||||
|
||||
// mu guards what follows. The time limits run on goroutines of their own,
|
||||
// and the transport starts and stops them, and notes requestEnd, from its
|
||||
// own; once timersStopped is set, neither time limit acts any more.
|
||||
mu sync.Mutex
|
||||
timersStopped bool
|
||||
requestTimer *time.Timer
|
||||
responseTimer *time.Timer
|
||||
// requestEnd is when the app had been sent the whole request, or the answer
|
||||
// started, if that was sooner.
|
||||
requestEnd time.Time
|
||||
// limits are the request's SWW_RPROXY_MAX_REQUEST_TIME_MS and
|
||||
// SWW_RPROXY_MAX_RESPONSE_TIME_MS.
|
||||
limits *timeLimits
|
||||
}
|
||||
|
||||
// newRequest starts handling r: it notes the time, works out the visitor from
|
||||
@@ -122,17 +113,30 @@ func (h *handler) newRequest(
|
||||
|
||||
if r.Body != http.NoBody {
|
||||
rq.body = &requestBody{
|
||||
body: limitBody(r.Body, h.config.MaxRequestSize),
|
||||
rq: rq,
|
||||
body: limitBody(r.Body, h.config.MaxRequestSize),
|
||||
refuse: rq.refuse,
|
||||
}
|
||||
}
|
||||
|
||||
rq.limits = &timeLimits{
|
||||
maxRequestTime: h.config.MaxRequestTime,
|
||||
maxResponseTime: h.config.MaxResponseTime,
|
||||
start: rq.start,
|
||||
refuse: rq.refuse,
|
||||
waitingForVisitor: func() bool {
|
||||
return rq.body != nil && rq.body.waiting.Load()
|
||||
},
|
||||
endVisitorRead: func() {
|
||||
_ = rq.rc.SetReadDeadline(time.Now())
|
||||
},
|
||||
}
|
||||
|
||||
return rq
|
||||
}
|
||||
|
||||
// answerHealth answers the health check.
|
||||
func (rq *request) answerHealth() {
|
||||
rq.prepareOwnAnswer(rq.requestDeadline())
|
||||
rq.prepareOwnAnswer(rq.limits.requestDeadline())
|
||||
rq.out.Header().Set("Content-Type", plainText)
|
||||
rq.send(http.StatusOK, []byte("ok\n"))
|
||||
}
|
||||
@@ -148,7 +152,7 @@ func (rq *request) readFirstPartOfBody() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
_ = rq.rc.SetReadDeadline(rq.requestDeadline())
|
||||
_ = rq.rc.SetReadDeadline(rq.limits.requestDeadline())
|
||||
held, err := io.ReadAll(
|
||||
io.LimitReader(rq.body, wafprotocol.QuestionBodyMaxBytes),
|
||||
)
|
||||
@@ -188,7 +192,7 @@ func (rq *request) forward(ctx context.Context) {
|
||||
|
||||
rq.cancel = cancel
|
||||
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
|
||||
WroteRequest: rq.wroteRequest,
|
||||
WroteRequest: rq.limits.wroteRequest,
|
||||
})
|
||||
|
||||
out := rq.in.WithContext(ctx)
|
||||
@@ -205,7 +209,7 @@ func (rq *request) forward(ctx context.Context) {
|
||||
ErrorHandler: rq.answerError,
|
||||
}
|
||||
|
||||
rq.startRequestTimer()
|
||||
rq.limits.startRequestTimer()
|
||||
rq.upstreamStart = time.Now()
|
||||
reverseProxy.ServeHTTP(rq.out, out)
|
||||
}
|
||||
@@ -248,7 +252,7 @@ func (rq *request) modifyResponse(res *http.Response) error {
|
||||
// switch, as when the app's Upgrade is not the visitor's, so the status
|
||||
// is noted only once it has taken over the connection, by rq.out's
|
||||
// Hijack.
|
||||
rq.stopTimers()
|
||||
rq.limits.stop()
|
||||
|
||||
conn, ok := res.Body.(io.ReadWriteCloser)
|
||||
if ok {
|
||||
@@ -259,7 +263,7 @@ func (rq *request) modifyResponse(res *http.Response) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
requestEnd := rq.endRequest()
|
||||
requestEnd := rq.limits.endRequest()
|
||||
|
||||
// An answer that arrives once SWW_RPROXY_MAX_RESPONSE_TIME_MS has run out is not
|
||||
// passed on: the visitor is answered 504.
|
||||
@@ -289,9 +293,14 @@ func (rq *request) modifyResponse(res *http.Response) error {
|
||||
|
||||
// An answer that arrives before the app has been sent the whole request ends
|
||||
// the request, and SWW_RPROXY_MAX_RESPONSE_TIME_MS starts.
|
||||
rq.startResponseTimer(requestEnd)
|
||||
rq.limits.startResponseTimer(requestEnd)
|
||||
|
||||
res.Body = &responseBody{body: limitBody(res.Body, maxBytes), rq: rq}
|
||||
res.Body = &responseBody{
|
||||
body: limitBody(res.Body, maxBytes),
|
||||
refuse: rq.refuse,
|
||||
responseReceived: rq.limits.stop,
|
||||
ctx: rq.in.Context(),
|
||||
}
|
||||
rq.out.afterStatus = rq.setResponseDeadline
|
||||
|
||||
return nil
|
||||
@@ -342,7 +351,7 @@ func (rq *request) answer(status int) {
|
||||
// A visitor found too slow is read no more; any other may go on sending until
|
||||
// its time is up, so that the rest of the body is read and the request ends
|
||||
// cleanly: by Go's server, or by readRestOfBody.
|
||||
deadline := rq.requestDeadline()
|
||||
deadline := rq.limits.requestDeadline()
|
||||
if status == http.StatusRequestTimeout {
|
||||
deadline = time.Now()
|
||||
}
|
||||
@@ -388,7 +397,7 @@ func (rq *request) closeAfterAnswer() {
|
||||
// used that time up first, and at least minSendTime.
|
||||
func (rq *request) prepareOwnAnswer(deadline time.Time) {
|
||||
rq.stopReadingBody(deadline)
|
||||
rq.endRequest()
|
||||
rq.limits.endRequest()
|
||||
|
||||
limit := rq.h.config.MaxResponseTime
|
||||
if limit > 0 {
|
||||
@@ -409,7 +418,7 @@ func (rq *request) refuse(status int) {
|
||||
// finish ends the request's time limits, writes its log line and, if waf
|
||||
// answered the question, sends the report. ctx is the request's own context.
|
||||
func (rq *request) finish(ctx context.Context) {
|
||||
rq.stopTimers()
|
||||
rq.limits.stop()
|
||||
|
||||
// After a cut-off answer, the reading of a body that has not arrived whole
|
||||
// ends now. Go's server would otherwise keep the connection open while it
|
||||
@@ -427,10 +436,10 @@ func (rq *request) finish(ctx context.Context) {
|
||||
errors.Is(rq.rc.Flush(), os.ErrDeadlineExceeded):
|
||||
rq.stopPassingOnBody(time.Now())
|
||||
case rq.refused.Load() == 0:
|
||||
rq.stopPassingOnBody(rq.requestDeadline())
|
||||
rq.stopPassingOnBody(rq.limits.requestDeadline())
|
||||
}
|
||||
|
||||
requestEnd := rq.endRequest()
|
||||
requestEnd := rq.limits.endRequest()
|
||||
end := time.Now()
|
||||
requestBytes, responseBytes := rq.bytesEachWay()
|
||||
|
||||
@@ -504,17 +513,6 @@ func (rq *request) bytesEachWay() (int64, int64) {
|
||||
return requestBytes, responseBytes
|
||||
}
|
||||
|
||||
// requestDeadline is when the visitor must have sent its whole request, or zero
|
||||
// when SWW_RPROXY_MAX_REQUEST_TIME_MS is off.
|
||||
func (rq *request) requestDeadline() time.Time {
|
||||
limit := rq.h.config.MaxRequestTime
|
||||
if limit == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
return rq.start.Add(limit)
|
||||
}
|
||||
|
||||
// stopReadingBody ends, at deadline, the reading of a visitor's body that has not
|
||||
// arrived whole: Go's server then reads no more of it, and closes the connection
|
||||
// after the answer.
|
||||
@@ -571,100 +569,6 @@ func (rq *request) stopPassingOnBody(deadline time.Time) {
|
||||
rq.stopReadingBody(deadline)
|
||||
}
|
||||
|
||||
// startRequestTimer starts SWW_RPROXY_MAX_REQUEST_TIME_MS, which runs from the
|
||||
// request's headers being read until the app has been sent the whole request.
|
||||
func (rq *request) startRequestTimer() {
|
||||
deadline := rq.requestDeadline()
|
||||
if deadline.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
rq.requestTimer = time.AfterFunc(
|
||||
time.Until(deadline),
|
||||
rq.requestTimedOut,
|
||||
)
|
||||
}
|
||||
|
||||
// requestTimedOut is called when SWW_RPROXY_MAX_REQUEST_TIME_MS runs out while the
|
||||
// request is still on its way to the app. The answer names the side rproxy was
|
||||
// waiting on at that moment: 408 when it was waiting for the visitor to send more
|
||||
// of its body, 504 when it was waiting for the app to be reached or to take what
|
||||
// it had.
|
||||
func (rq *request) requestTimedOut() {
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
if rq.timersStopped {
|
||||
return
|
||||
}
|
||||
|
||||
if rq.body == nil || !rq.body.waiting.Load() {
|
||||
rq.refuse(http.StatusGatewayTimeout)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.refuse(http.StatusRequestTimeout)
|
||||
// The transport gives up on the app only once its Read of the visitor's body
|
||||
// returns, so that Read is ended now. The lock keeps this from reaching the
|
||||
// connection after the request is handled.
|
||||
_ = rq.rc.SetReadDeadline(time.Now())
|
||||
}
|
||||
|
||||
// wroteRequest is called once the app has been sent the whole request:
|
||||
// SWW_RPROXY_MAX_REQUEST_TIME_MS ends and SWW_RPROXY_MAX_RESPONSE_TIME_MS starts.
|
||||
func (rq *request) wroteRequest(info httptrace.WroteRequestInfo) {
|
||||
if info.Err != nil {
|
||||
return // the transport gives up, or tries again
|
||||
}
|
||||
|
||||
requestEnd := rq.endRequest()
|
||||
|
||||
rq.mu.Lock()
|
||||
stopTimer(rq.requestTimer)
|
||||
rq.mu.Unlock()
|
||||
|
||||
rq.startResponseTimer(requestEnd)
|
||||
}
|
||||
|
||||
// startResponseTimer starts SWW_RPROXY_MAX_RESPONSE_TIME_MS, which runs from
|
||||
// requestEnd, unless it has started already.
|
||||
func (rq *request) startResponseTimer(requestEnd time.Time) {
|
||||
limit := rq.h.config.MaxResponseTime
|
||||
if limit == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
if rq.timersStopped || rq.responseTimer != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rq.responseTimer = time.AfterFunc(time.Until(requestEnd.Add(limit)),
|
||||
rq.responseTimedOut)
|
||||
}
|
||||
|
||||
// responseTimedOut is called when SWW_RPROXY_MAX_RESPONSE_TIME_MS runs out before the
|
||||
// app has sent its whole answer.
|
||||
func (rq *request) responseTimedOut() {
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
if !rq.timersStopped {
|
||||
rq.refuse(http.StatusGatewayTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// responseReceived is called once the app has sent its whole answer.
|
||||
func (rq *request) responseReceived() {
|
||||
rq.stopTimers()
|
||||
}
|
||||
|
||||
// setResponseDeadline sends the status and headers of the app's answer, with
|
||||
// at least minSendTime, before ReverseProxy reads the answer's body, and then
|
||||
// the rest has until SWW_RPROXY_MAX_RESPONSE_TIME_MS after the end of the request.
|
||||
@@ -674,7 +578,7 @@ func (rq *request) setResponseDeadline() {
|
||||
return
|
||||
}
|
||||
|
||||
deadline := rq.endRequest().Add(limit)
|
||||
deadline := rq.limits.endRequest().Add(limit)
|
||||
|
||||
_ = rq.rc.SetWriteDeadline(
|
||||
time.Now().Add(max(time.Until(deadline), minSendTime)),
|
||||
@@ -682,34 +586,3 @@ func (rq *request) setResponseDeadline() {
|
||||
_ = rq.rc.Flush()
|
||||
_ = rq.rc.SetWriteDeadline(deadline)
|
||||
}
|
||||
|
||||
// endRequest notes now as the end of the request, unless an end is noted, and
|
||||
// returns the end noted.
|
||||
func (rq *request) endRequest() time.Time {
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
if rq.requestEnd.IsZero() {
|
||||
rq.requestEnd = time.Now()
|
||||
}
|
||||
|
||||
return rq.requestEnd
|
||||
}
|
||||
|
||||
// stopTimers stops the request's time limits and keeps any from starting later:
|
||||
// the app's answer is complete, the connection upgraded, or the request handled.
|
||||
func (rq *request) stopTimers() {
|
||||
rq.mu.Lock()
|
||||
defer rq.mu.Unlock()
|
||||
|
||||
rq.timersStopped = true
|
||||
stopTimer(rq.requestTimer)
|
||||
stopTimer(rq.responseTimer)
|
||||
}
|
||||
|
||||
// stopTimer stops t, which is nil when its time limit is off.
|
||||
func stopTimer(t *time.Timer) {
|
||||
if t != nil {
|
||||
t.Stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package rproxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// timeLimits are one request's two time limits: SWW_RPROXY_MAX_REQUEST_TIME_MS,
|
||||
// which runs from the request's headers being read until the app has been sent
|
||||
// the whole request, and SWW_RPROXY_MAX_RESPONSE_TIME_MS, which runs from then
|
||||
// until the app has sent its whole answer. A limit of zero is off. Each limit
|
||||
// runs on a goroutine of its own, and the transport starts and stops them from
|
||||
// its own.
|
||||
type timeLimits struct {
|
||||
maxRequestTime time.Duration
|
||||
maxResponseTime time.Duration
|
||||
// start is when the request's headers had been read.
|
||||
start time.Time
|
||||
// refuse refuses the request with 408 or 504 when a limit runs out.
|
||||
refuse func(status int)
|
||||
// waitingForVisitor reports whether a read of the visitor's body is waiting
|
||||
// for the visitor to send more.
|
||||
waitingForVisitor func() bool
|
||||
// endVisitorRead ends at once a read of the visitor's body that is waiting.
|
||||
endVisitorRead func()
|
||||
|
||||
// mu guards what follows. Once stopped is set, neither limit acts any more.
|
||||
mu sync.Mutex
|
||||
stopped bool
|
||||
requestTimer *time.Timer
|
||||
responseTimer *time.Timer
|
||||
// requestEnd is when the app had been sent the whole request, or the answer
|
||||
// started, if that was sooner.
|
||||
requestEnd time.Time
|
||||
}
|
||||
|
||||
// requestDeadline is when the visitor must have sent its whole request, or zero
|
||||
// when SWW_RPROXY_MAX_REQUEST_TIME_MS is off.
|
||||
func (l *timeLimits) requestDeadline() time.Time {
|
||||
if l.maxRequestTime == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
return l.start.Add(l.maxRequestTime)
|
||||
}
|
||||
|
||||
// startRequestTimer starts SWW_RPROXY_MAX_REQUEST_TIME_MS.
|
||||
func (l *timeLimits) startRequestTimer() {
|
||||
deadline := l.requestDeadline()
|
||||
if deadline.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
l.requestTimer = time.AfterFunc(time.Until(deadline), l.requestTimedOut)
|
||||
}
|
||||
|
||||
// requestTimedOut is called when SWW_RPROXY_MAX_REQUEST_TIME_MS runs out while
|
||||
// the request is still on its way to the app. The answer names the side rproxy
|
||||
// was waiting on at that moment: 408 when it was waiting for the visitor to send
|
||||
// more of its body, 504 when it was waiting for the app to be reached or to take
|
||||
// what it had.
|
||||
func (l *timeLimits) requestTimedOut() {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
if l.stopped {
|
||||
return
|
||||
}
|
||||
|
||||
if !l.waitingForVisitor() {
|
||||
l.refuse(http.StatusGatewayTimeout)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
l.refuse(http.StatusRequestTimeout)
|
||||
// The transport gives up on the app only once its Read of the visitor's body
|
||||
// returns, so that Read is ended now. The lock keeps this from reaching the
|
||||
// connection after the request is handled.
|
||||
l.endVisitorRead()
|
||||
}
|
||||
|
||||
// wroteRequest is called once the app has been sent the whole request:
|
||||
// SWW_RPROXY_MAX_REQUEST_TIME_MS ends and SWW_RPROXY_MAX_RESPONSE_TIME_MS
|
||||
// starts.
|
||||
func (l *timeLimits) wroteRequest(info httptrace.WroteRequestInfo) {
|
||||
if info.Err != nil {
|
||||
return // the transport gives up, or tries again
|
||||
}
|
||||
|
||||
requestEnd := l.endRequest()
|
||||
|
||||
l.mu.Lock()
|
||||
stopTimer(l.requestTimer)
|
||||
l.mu.Unlock()
|
||||
|
||||
l.startResponseTimer(requestEnd)
|
||||
}
|
||||
|
||||
// startResponseTimer starts SWW_RPROXY_MAX_RESPONSE_TIME_MS, which runs from
|
||||
// requestEnd, unless it has started already.
|
||||
func (l *timeLimits) startResponseTimer(requestEnd time.Time) {
|
||||
if l.maxResponseTime == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
if l.stopped || l.responseTimer != nil {
|
||||
return
|
||||
}
|
||||
|
||||
l.responseTimer = time.AfterFunc(
|
||||
time.Until(requestEnd.Add(l.maxResponseTime)),
|
||||
l.responseTimedOut,
|
||||
)
|
||||
}
|
||||
|
||||
// responseTimedOut is called when SWW_RPROXY_MAX_RESPONSE_TIME_MS runs out
|
||||
// before the app has sent its whole answer.
|
||||
func (l *timeLimits) responseTimedOut() {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
if !l.stopped {
|
||||
l.refuse(http.StatusGatewayTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// endRequest notes now as the end of the request, unless an end is noted, and
|
||||
// returns the end noted.
|
||||
func (l *timeLimits) endRequest() time.Time {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
if l.requestEnd.IsZero() {
|
||||
l.requestEnd = time.Now()
|
||||
}
|
||||
|
||||
return l.requestEnd
|
||||
}
|
||||
|
||||
// stop stops both limits and keeps either from starting later: the app's
|
||||
// answer is complete, the connection upgraded, or the request handled.
|
||||
func (l *timeLimits) stop() {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
l.stopped = true
|
||||
stopTimer(l.requestTimer)
|
||||
stopTimer(l.responseTimer)
|
||||
}
|
||||
|
||||
// stopTimer stops t, which is nil when its limit is off.
|
||||
func stopTimer(t *time.Timer) {
|
||||
if t != nil {
|
||||
t.Stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package rproxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
)
|
||||
|
||||
// The tests below run timeLimits in a synctest bubble, where time is a clock of
|
||||
// the test's own: time.Sleep moves it on at once, and synctest.Wait returns once
|
||||
// a limit that ran out has refused the request.
|
||||
|
||||
const (
|
||||
testRequestTime = 3 * time.Second
|
||||
testResponseTime = 5 * time.Second
|
||||
)
|
||||
|
||||
// limitsSeen is what a timeLimits under test did to its request.
|
||||
type limitsSeen struct {
|
||||
// refused is the status of the last refusal, or zero.
|
||||
refused atomic.Int64
|
||||
// readEnded is true once the read of the visitor's body was ended.
|
||||
readEnded atomic.Bool
|
||||
// waiting is what waitingForVisitor reports.
|
||||
waiting atomic.Bool
|
||||
}
|
||||
|
||||
// newTestLimits returns time limits that started now, with what they do noted
|
||||
// in seen.
|
||||
func newTestLimits(seen *limitsSeen) *timeLimits {
|
||||
return &timeLimits{
|
||||
maxRequestTime: testRequestTime,
|
||||
maxResponseTime: testResponseTime,
|
||||
start: time.Now(),
|
||||
refuse: func(status int) {
|
||||
seen.refused.Store(int64(status))
|
||||
},
|
||||
waitingForVisitor: seen.waiting.Load,
|
||||
endVisitorRead: func() {
|
||||
seen.readEnded.Store(true)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// sleepThenWait moves the bubble's clock on by d and waits for what is then
|
||||
// due.
|
||||
func sleepThenWait(d time.Duration) {
|
||||
time.Sleep(d)
|
||||
synctest.Wait()
|
||||
}
|
||||
|
||||
func TestRequestTimeRunningOutWhileTheVisitorIsWaitedOnRefuses408(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
seen := &limitsSeen{}
|
||||
seen.waiting.Store(true)
|
||||
|
||||
limits := newTestLimits(seen)
|
||||
limits.startRequestTimer()
|
||||
|
||||
sleepThenWait(testRequestTime - time.Millisecond)
|
||||
|
||||
if got := seen.refused.Load(); got != 0 {
|
||||
t.Fatalf(
|
||||
"refused %d before the limit ran out, want nothing",
|
||||
got,
|
||||
)
|
||||
}
|
||||
|
||||
sleepThenWait(time.Millisecond)
|
||||
|
||||
if got := seen.refused.Load(); got != http.StatusRequestTimeout {
|
||||
t.Errorf("refused %d, want 408", got)
|
||||
}
|
||||
|
||||
if !seen.readEnded.Load() {
|
||||
t.Error("the read of the visitor's body goes on, want it ended")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRequestTimeRunningOutWhileTheAppIsWaitedOnRefuses504(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
seen := &limitsSeen{}
|
||||
|
||||
limits := newTestLimits(seen)
|
||||
limits.startRequestTimer()
|
||||
|
||||
sleepThenWait(testRequestTime)
|
||||
|
||||
if got := seen.refused.Load(); got != http.StatusGatewayTimeout {
|
||||
t.Errorf("refused %d, want 504", got)
|
||||
}
|
||||
|
||||
if seen.readEnded.Load() {
|
||||
t.Error("the read of the visitor's body was ended, want it not")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestResponseTimeRunsFromTheEndOfTheRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
seen := &limitsSeen{}
|
||||
|
||||
limits := newTestLimits(seen)
|
||||
limits.startRequestTimer()
|
||||
|
||||
sleepThenWait(time.Second)
|
||||
|
||||
end := time.Now()
|
||||
|
||||
limits.wroteRequest(httptrace.WroteRequestInfo{})
|
||||
|
||||
if got := limits.endRequest(); !got.Equal(end) {
|
||||
t.Errorf("the request ended at %v, want %v", got, end)
|
||||
}
|
||||
|
||||
// Past SWW_RPROXY_MAX_REQUEST_TIME_MS, which the end of the request
|
||||
// stopped, and just short of SWW_RPROXY_MAX_RESPONSE_TIME_MS.
|
||||
sleepThenWait(testResponseTime - time.Millisecond)
|
||||
|
||||
if got := seen.refused.Load(); got != 0 {
|
||||
t.Fatalf(
|
||||
"refused %d before the limit ran out, want nothing",
|
||||
got,
|
||||
)
|
||||
}
|
||||
|
||||
sleepThenWait(time.Millisecond)
|
||||
|
||||
if got := seen.refused.Load(); got != http.StatusGatewayTimeout {
|
||||
t.Errorf("refused %d, want 504", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestStoppedLimitsRefuseNothing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
seen := &limitsSeen{}
|
||||
seen.waiting.Store(true)
|
||||
|
||||
limits := newTestLimits(seen)
|
||||
limits.startRequestTimer()
|
||||
limits.stop()
|
||||
limits.startResponseTimer(limits.endRequest())
|
||||
|
||||
sleepThenWait(testRequestTime + testResponseTime)
|
||||
|
||||
if got := seen.refused.Load(); got != 0 {
|
||||
t.Errorf("refused %d, want nothing", got)
|
||||
}
|
||||
|
||||
if seen.readEnded.Load() {
|
||||
t.Error("the read of the visitor's body was ended, want it not")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -188,7 +188,7 @@ func canBeAsked(r *http.Request) bool {
|
||||
|
||||
// answerWith gives the visitor waf's answer, less its disposition.
|
||||
func (rq *request) answerWith(answer *wafAnswer) {
|
||||
rq.prepareOwnAnswer(rq.requestDeadline())
|
||||
rq.prepareOwnAnswer(rq.limits.requestDeadline())
|
||||
|
||||
header := rq.out.Header()
|
||||
maps.Copy(header, answer.header)
|
||||
|
||||
+19
-48
@@ -18,12 +18,11 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/dirwatch"
|
||||
)
|
||||
|
||||
// The actions a rule takes when it matches.
|
||||
@@ -40,12 +39,6 @@ const (
|
||||
// extension ends the name of every rule file.
|
||||
const extension = ".rules"
|
||||
|
||||
// quietTime is how long SWW_WAF_RULE_FILES_DIR must go without a change before
|
||||
// the rule files are read again, so that a file still being written, such
|
||||
// as one saved in place, appended to or copied in with scp, is read only
|
||||
// once whole.
|
||||
const quietTime = 2 * time.Second
|
||||
|
||||
// headerTarget starts the target that is one request header,
|
||||
// header:<Name>.
|
||||
const headerTarget = "header:"
|
||||
@@ -176,26 +169,18 @@ func (f *Files) Len() int {
|
||||
}
|
||||
|
||||
// Watch watches Dir until ctx is done, and reads the rule files again
|
||||
// once Dir has had no change for quietTime, after one is edited, added or
|
||||
// removed, and after Watch starts watching. If they then hold an error,
|
||||
// the rules stay as they were, the error is logged with its file and
|
||||
// line, and the files are read again after the next change. If Dir cannot
|
||||
// be watched, that is logged, and the rules stay as they were loaded.
|
||||
// While Enabled is false, Watch returns at once.
|
||||
// once Dir has had no change for a short time, after one is edited, added
|
||||
// or removed, and after Watch starts watching, as dirwatch describes. If
|
||||
// they then hold an error, the rules stay as they were, the error is
|
||||
// logged with its file and line, and the files are read again after the
|
||||
// next change. If Dir cannot be watched, that is logged, and the rules
|
||||
// stay as they were loaded. While Enabled is false, Watch returns at once.
|
||||
func (f *Files) Watch(ctx context.Context) {
|
||||
if !f.params.Enabled {
|
||||
return
|
||||
}
|
||||
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err == nil {
|
||||
defer func() {
|
||||
_ = watcher.Close()
|
||||
}()
|
||||
|
||||
err = watcher.Add(f.params.Dir)
|
||||
}
|
||||
|
||||
watcher, err := dirwatch.New(f.params.Dir)
|
||||
if err != nil {
|
||||
f.params.ProcessLog.Error("cannot watch the rule files for edits",
|
||||
"error", err.Error())
|
||||
@@ -203,36 +188,22 @@ func (f *Files) Watch(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = watcher.Close()
|
||||
}()
|
||||
|
||||
f.params.ProcessLog.Info("watching the rule files for edits",
|
||||
"directory", f.params.Dir)
|
||||
|
||||
f.readAfterChanges(ctx, watcher.Events, watcher.Errors)
|
||||
}
|
||||
|
||||
// readAfterChanges reads the rule files again once quietTime has passed
|
||||
// without a change from events, until ctx is done, and logs the errors
|
||||
// from errs. The wait starts at once, as if for a change, so that an edit
|
||||
// saved after Load read the files, and before Dir was watched, is taken
|
||||
// in too.
|
||||
func (f *Files) readAfterChanges(
|
||||
ctx context.Context, events <-chan fsnotify.Event, errs <-chan error,
|
||||
) {
|
||||
quiet := time.NewTimer(quietTime)
|
||||
defer quiet.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-events:
|
||||
quiet.Reset(quietTime)
|
||||
case <-quiet.C:
|
||||
f.readAgain()
|
||||
case err := <-errs:
|
||||
// A change to any file in Dir counts.
|
||||
watcher.CallWhenQuiet(ctx,
|
||||
func(string) bool { return true },
|
||||
f.readAgain,
|
||||
func(err error) {
|
||||
f.params.ProcessLog.Warn("watching the rule files failed",
|
||||
"error", err.Error())
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// readAgain reads the rule files again, in place of the rules loaded, or
|
||||
|
||||
@@ -1,165 +0,0 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
)
|
||||
|
||||
// The tests below run readAfterChanges in a synctest bubble, where time is
|
||||
// a clock of the test's own: time.Sleep moves it on at once, and
|
||||
// synctest.Wait returns once readAfterChanges waits again, so that every
|
||||
// reading due by then is done. The test sends the changes itself, as the
|
||||
// watch of a directory cannot run in a bubble.
|
||||
|
||||
func TestFileWrittenInTwoPartsTakenInOnlyWhole(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "50-app.rules")
|
||||
writeFile(t, path, "first path block ^/first\n")
|
||||
files := load(t, dir)
|
||||
changes := run(t, files)
|
||||
|
||||
file, err := os.Create(path) //nolint:gosec // a file the test wrote
|
||||
if err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
// The first part ends in the middle of a ban rule's regex, which,
|
||||
// read then, would ban every request.
|
||||
write(t, file, "first path block ^/first\nprobe path ban ^/")
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
wantMatched(t, files, "/anything")
|
||||
|
||||
// The second part starts the wait again.
|
||||
write(t, file, `\.env$`+"\n")
|
||||
|
||||
changes <- fsnotify.Event{Name: path, Op: fsnotify.Write}
|
||||
|
||||
time.Sleep(quietTime - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
wantMatched(t, files, "/.env")
|
||||
|
||||
time.Sleep(time.Nanosecond)
|
||||
synctest.Wait()
|
||||
wantMatched(t, files, "/.env", "probe")
|
||||
wantMatched(t, files, "/anything")
|
||||
})
|
||||
}
|
||||
|
||||
func TestEditSavedBeforeTheWatchStartsTakenIn(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "50-app.rules")
|
||||
writeFile(t, path, "first path block ^/first\n")
|
||||
files := load(t, dir)
|
||||
|
||||
// Saved after Load read the files, and before the directory was
|
||||
// watched, so that no change is seen for it.
|
||||
writeFile(t, path, "first path block ^/edited\n")
|
||||
run(t, files)
|
||||
time.Sleep(quietTime)
|
||||
synctest.Wait()
|
||||
wantMatched(t, files, "/edited", "first")
|
||||
})
|
||||
}
|
||||
|
||||
// load loads the rules in dir.
|
||||
func load(t *testing.T, dir string) *Files {
|
||||
t.Helper()
|
||||
|
||||
files, err := Load(Params{
|
||||
Dir: dir, Enabled: true, ProcessLog: slog.New(slog.DiscardHandler),
|
||||
Alerts: alerts.New(alerts.Params{}),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("load: %v", err)
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
||||
// run runs files' readAfterChanges until the test ends, and returns the
|
||||
// channel that sends it changes.
|
||||
func run(t *testing.T, files *Files) chan<- fsnotify.Event {
|
||||
t.Helper()
|
||||
|
||||
changes := make(chan fsnotify.Event)
|
||||
ctx, stop := context.WithCancel(t.Context())
|
||||
stopped := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
files.readAfterChanges(ctx, changes, nil)
|
||||
close(stopped)
|
||||
}()
|
||||
|
||||
t.Cleanup(func() {
|
||||
stop()
|
||||
<-stopped
|
||||
})
|
||||
|
||||
return changes
|
||||
}
|
||||
|
||||
// writeFile writes content to the file at path.
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
|
||||
err := os.WriteFile(path, []byte(content), 0o600)
|
||||
if err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
// write writes text to the end of file.
|
||||
func write(t *testing.T, file *os.File, text string) {
|
||||
t.Helper()
|
||||
|
||||
_, err := file.WriteString(text)
|
||||
if err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// wantMatched checks the ids of the rules that a GET request for path
|
||||
// matches, in order.
|
||||
func wantMatched(t *testing.T, files *Files, path string, want ...string) {
|
||||
t.Helper()
|
||||
|
||||
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"http://app.example"+path, nil)
|
||||
|
||||
matched := files.Match(r)
|
||||
|
||||
got := make([]string, 0, len(matched))
|
||||
for _, rule := range matched {
|
||||
got = append(got, rule.ID)
|
||||
}
|
||||
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("%s matched %v, want %v", path, got, want)
|
||||
}
|
||||
}
|
||||
@@ -2430,7 +2430,7 @@ func midnight() time.Time {
|
||||
// counts, with thresholds fill does not reach.
|
||||
func newParams(dir string) state.Params {
|
||||
discard := slog.New(slog.DiscardHandler)
|
||||
m := metrics.New(1, "app")
|
||||
m := metrics.New(metrics.Params{BusiestCount: 1, InstanceName: "app"})
|
||||
queue := alerts.New(alerts.Params{
|
||||
WebhookURL: &url.URL{Scheme: "https", Host: "alerts.example"},
|
||||
Events: alerts.Events(),
|
||||
|
||||
+52
-30
@@ -10,13 +10,14 @@ package sww
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
@@ -26,14 +27,7 @@ import (
|
||||
// know.
|
||||
const usageStatus = 2
|
||||
|
||||
// usage is what sww prints then.
|
||||
const usage = `usage: sww <command>
|
||||
|
||||
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
|
||||
`
|
||||
var errNoCommand = errors.New("no command given")
|
||||
|
||||
// Params are what RunWAF and RunRProxy need from the process.
|
||||
type Params struct {
|
||||
@@ -51,14 +45,9 @@ type Params struct {
|
||||
|
||||
// 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.
|
||||
// With no command, or one it does not know, cobra prints the error and the
|
||||
// commands to stderr, and Main 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()
|
||||
@@ -69,21 +58,54 @@ func Main(version string) int {
|
||||
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)
|
||||
// status is what the command that ran returns.
|
||||
status := 0
|
||||
|
||||
root := &cobra.Command{
|
||||
Use: "sww",
|
||||
Short: "a web application firewall",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(*cobra.Command, []string) error {
|
||||
return errNoCommand
|
||||
},
|
||||
}
|
||||
}
|
||||
root.CompletionOptions.DisableDefaultCmd = true
|
||||
|
||||
// printUsage writes the commands to w, and returns usageStatus.
|
||||
func printUsage(w io.Writer) int {
|
||||
_, _ = fmt.Fprint(w, usage)
|
||||
root.AddCommand(
|
||||
&cobra.Command{
|
||||
Use: "rproxy",
|
||||
Short: "the reverse proxy in front of one app",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
status = RunRProxy(cmd.Context(), params)
|
||||
},
|
||||
},
|
||||
&cobra.Command{
|
||||
Use: "waf",
|
||||
Short: "decides for rproxy, and keeps bans, counters and history",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
status = RunWAF(cmd.Context(), params)
|
||||
},
|
||||
},
|
||||
&cobra.Command{
|
||||
Use: "healthcheck",
|
||||
Short: "the container's health check",
|
||||
// HealthCheck refuses every argument itself, flags included.
|
||||
DisableFlagParsing: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
status = HealthCheck(
|
||||
cmd.Context(),
|
||||
args,
|
||||
params.Viper,
|
||||
cmd.ErrOrStderr(),
|
||||
)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
return usageStatus
|
||||
err := root.ExecuteContext(ctx)
|
||||
if err != nil {
|
||||
return usageStatus
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
+18
-11
@@ -392,26 +392,33 @@ func newMetrics(
|
||||
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)
|
||||
params := metrics.Params{
|
||||
BusiestCount: cfg.MetricsBusiestCountriesAndASNs,
|
||||
InstanceName: cfg.InstanceName,
|
||||
Now: utcNow,
|
||||
Bans: ledger,
|
||||
Clients: limiter,
|
||||
Rules: ruleFiles,
|
||||
Lists: lists,
|
||||
DNSBL: dnsbl,
|
||||
Alerts: alertQueue,
|
||||
}
|
||||
|
||||
m.AddAlerts(alertQueue)
|
||||
// A nil pointer in an interface field is not a nil interface, so a part
|
||||
// that is off is left out rather than set to nil.
|
||||
if cfg.AbuseIPDBKey != "" {
|
||||
params.AbuseIPDB = abuseIPDB
|
||||
}
|
||||
|
||||
if lookupFile != nil {
|
||||
m.AddLookupFile(lookupFile.LastRead, lookupFile.ReadFailures)
|
||||
params.LookupFile = lookupFile
|
||||
}
|
||||
|
||||
if remote != nil {
|
||||
m.AddRemoteLog(remote)
|
||||
params.RemoteLog = remote
|
||||
}
|
||||
|
||||
return m
|
||||
return metrics.New(params)
|
||||
}
|
||||
|
||||
// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each
|
||||
|
||||
+88
-29
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -164,46 +165,104 @@ func wantUTC(t *testing.T, where string, value any) {
|
||||
func TestMainWithoutACommandItKnowsPrintsTheCommands(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// completion is cobra's own command, which sww turns off.
|
||||
for _, command := range []string{"", "serve", "completion"} {
|
||||
t.Run("command "+command, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
code, stdout, stderr := runMain(t, command)
|
||||
|
||||
if code != 2 {
|
||||
t.Errorf("exit status %d, want 2", code)
|
||||
}
|
||||
|
||||
wantTheCommands(t, "stderr", stderr)
|
||||
|
||||
if stdout != "" {
|
||||
t.Errorf("stdout %q, want nothing", stdout)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainHelpPrintsTheCommands(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, command := range []string{"help", "--help"} {
|
||||
t.Run("command "+command, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
code, stdout, _ := runMain(t, command)
|
||||
|
||||
if code != 0 {
|
||||
t.Errorf("exit status %d, want 0", code)
|
||||
}
|
||||
|
||||
wantTheCommands(t, "stdout", stdout)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// wantTheCommands checks that text, what Main wrote to where, names each
|
||||
// command.
|
||||
func wantTheCommands(t *testing.T, where, text string) {
|
||||
t.Helper()
|
||||
|
||||
for _, name := range []string{"rproxy", wafCommand, "healthcheck"} {
|
||||
if !strings.Contains(text, name) {
|
||||
t.Errorf("%s %q does not name %s", where, text, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainHealthCheckRefusesAnArgument(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, argument := range []string{"now", "--x", "-h"} {
|
||||
t.Run("argument "+argument, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
code, stdout, stderr := runMain(t, "healthcheck "+argument)
|
||||
|
||||
want := "sww healthcheck: unexpected argument \"" +
|
||||
argument + "\"\n"
|
||||
if code != 1 || stderr != want || stdout != "" {
|
||||
t.Errorf("exit status %d, stdout %q and stderr %q, "+
|
||||
"want 1, nothing and %q", code, stdout, stderr, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// runMain runs Main with command in a process of its own, and returns its
|
||||
// exit status and what it wrote to stdout and stderr.
|
||||
func runMain(t *testing.T, command string) (int, string, string) {
|
||||
t.Helper()
|
||||
|
||||
binary, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatalf("find the test binary: %v", err)
|
||||
}
|
||||
|
||||
for _, command := range []string{"", "serve"} {
|
||||
t.Run("command "+command, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(t.Context(), waitLimit)
|
||||
defer cancel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(t.Context(), waitLimit)
|
||||
defer cancel()
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
//nolint:gosec // this test binary
|
||||
cmd := exec.CommandContext(ctx, binary)
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
|
||||
cmd := exec.CommandContext(ctx, binary)
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
cmd.Env = append(os.Environ(), commandForMain+"="+command)
|
||||
|
||||
cmd.Env = append(os.Environ(), commandForMain+"="+command)
|
||||
err = cmd.Run()
|
||||
|
||||
err := cmd.Run()
|
||||
|
||||
if code := cmd.ProcessState.ExitCode(); code != 2 {
|
||||
t.Errorf("exit status %d (%v), want 2", code, err)
|
||||
}
|
||||
|
||||
for _, name := range []string{"rproxy", wafCommand, "healthcheck"} {
|
||||
if !strings.Contains(stderr.String(), name) {
|
||||
t.Errorf(
|
||||
"stderr %q does not name %s",
|
||||
stderr.String(),
|
||||
name,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if stdout.Len() != 0 {
|
||||
t.Errorf("stdout %q, want nothing", stdout.String())
|
||||
}
|
||||
})
|
||||
var exitErr *exec.ExitError
|
||||
if err != nil && !errors.As(err, &exitErr) {
|
||||
t.Fatalf("run: %v", err)
|
||||
}
|
||||
|
||||
return cmd.ProcessState.ExitCode(), stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
// ask sends waf at addr rproxy's question about a request for path from
|
||||
|
||||
+92
-147
@@ -2,7 +2,6 @@ package waf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -15,7 +14,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||
)
|
||||
|
||||
@@ -41,80 +39,23 @@ var (
|
||||
errNotAddress = errors.New("is not an address, such as 203.0.113.9")
|
||||
)
|
||||
|
||||
// answerHealth answers a request for the health endpoint.
|
||||
func (rq *request) answerHealth() {
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
// Set here rather than left to Go's server, which would set it only
|
||||
// after the log line has taken the response's headers.
|
||||
rq.out.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
_, _ = io.WriteString(rq.out, "ok\n")
|
||||
}
|
||||
|
||||
// answerAdmin answers a request for one of waf's own endpoints. Each
|
||||
// needs a token, sent as Authorization: Bearer <token>: the metrics
|
||||
// SWW_WAF_METRICS_TOKEN, the others SWW_WAF_ADMIN_TOKEN. A request without it is
|
||||
// refused with 401. An endpoint whose token is unset answers 404, as any other
|
||||
// path does.
|
||||
func (rq *request) answerAdmin() {
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
|
||||
token, answer := rq.endpoint()
|
||||
|
||||
switch {
|
||||
case token == "":
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusNotFound),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
case !hasToken(rq.in, token):
|
||||
rq.out.Header().Set("WWW-Authenticate", "Bearer")
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusUnauthorized),
|
||||
http.StatusUnauthorized,
|
||||
)
|
||||
default:
|
||||
answer()
|
||||
// handleHealth answers GET wafprotocol.HealthPath.
|
||||
func handleHealth() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
// Set here rather than left to Go's server, which would set it only
|
||||
// after the log line has taken the response's headers.
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
_, _ = io.WriteString(w, "ok\n")
|
||||
}
|
||||
}
|
||||
|
||||
// endpoint returns the token the request's endpoint needs, and what
|
||||
// answers the request there; "" when there is no such endpoint.
|
||||
func (rq *request) endpoint() (string, func()) {
|
||||
cfg := rq.h.config
|
||||
method, path := rq.in.Method, rq.in.URL.Path
|
||||
|
||||
switch {
|
||||
case method == http.MethodGet && path == MetricsPath:
|
||||
return cfg.MetricsToken, func() { rq.h.metrics.ServeHTTP(rq.out, rq.in) }
|
||||
case method == http.MethodGet && path == BansPath:
|
||||
return cfg.AdminToken, rq.listBans
|
||||
case method == http.MethodPost && path == BansPath:
|
||||
return cfg.AdminToken, rq.addBan
|
||||
case method == http.MethodDelete && strings.HasPrefix(path, BansPath+"/"):
|
||||
return cfg.AdminToken, rq.liftBans
|
||||
case method == http.MethodGet && strings.HasPrefix(path, ClientsPath):
|
||||
return cfg.AdminToken, rq.showClient
|
||||
default:
|
||||
return "", nil
|
||||
// handleListBans answers GET BansPath with every ban held.
|
||||
func (h *handler) handleListBans() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
h.answerBans(w, h.ledger.Snapshot())
|
||||
}
|
||||
}
|
||||
|
||||
// hasToken reports whether r carries token, as Authorization: Bearer
|
||||
// <token>.
|
||||
func hasToken(r *http.Request, token string) bool {
|
||||
scheme, sent, _ := strings.Cut(r.Header.Get("Authorization"), " ")
|
||||
|
||||
return strings.EqualFold(scheme, "Bearer") &&
|
||||
subtle.ConstantTimeCompare([]byte(sent), []byte(token)) == 1
|
||||
}
|
||||
|
||||
// listBans answers GET BansPath with every ban held.
|
||||
func (rq *request) listBans() {
|
||||
rq.answerBans(rq.h.ledger.Snapshot())
|
||||
}
|
||||
|
||||
// banToAdd is the body of POST BansPath.
|
||||
type banToAdd struct {
|
||||
// Netblock is a netblock, or a client's address, which stands for the
|
||||
@@ -126,44 +67,44 @@ type banToAdd struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// addBan answers POST BansPath: it bans the netblock the body names, as
|
||||
// an admin, from now for the duration the body gives, with its reason,
|
||||
// handleAddBan answers POST BansPath: it bans the netblock the body names,
|
||||
// as an admin, from now for the duration the body gives, with its reason,
|
||||
// and answers with that ban.
|
||||
func (rq *request) addBan() {
|
||||
toAdd, err := rq.readBanToAdd()
|
||||
func (h *handler) handleAddBan() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
toAdd, err := readBanToAdd(r)
|
||||
|
||||
var (
|
||||
netblock netip.Prefix
|
||||
expires time.Time
|
||||
now = rq.h.now()
|
||||
)
|
||||
var (
|
||||
netblock netip.Prefix
|
||||
expires time.Time
|
||||
now = h.now()
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
netblock, err = rq.h.parseBanNetblock(toAdd.Netblock)
|
||||
if err == nil {
|
||||
netblock, err = h.parseBanNetblock(toAdd.Netblock)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
expires, err = expiry(toAdd.Duration, now)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ban := h.ledger.BanForAdmin(netblock, now, expires, toAdd.Reason)
|
||||
h.answerBans(w, []bans.Ban{ban})
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
expires, err = expiry(toAdd.Duration, now)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
http.Error(rq.out, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ban := rq.h.ledger.BanForAdmin(netblock, now, expires, toAdd.Reason)
|
||||
rq.answerBans([]bans.Ban{ban})
|
||||
}
|
||||
|
||||
// readBanToAdd reads the body of POST BansPath: a JSON object with
|
||||
// nothing but whitespace after it, in at most banBodyMaxBytes.
|
||||
func (rq *request) readBanToAdd() (banToAdd, error) {
|
||||
func readBanToAdd(r *http.Request) (banToAdd, error) {
|
||||
data, err := io.ReadAll(
|
||||
http.MaxBytesReader(nil, rq.in.Body, banBodyMaxBytes),
|
||||
http.MaxBytesReader(nil, r.Body, banBodyMaxBytes),
|
||||
)
|
||||
rq.requestBytes = int64(len(data))
|
||||
|
||||
if err != nil {
|
||||
return banToAdd{}, fmt.Errorf("%w: %w", errNotBanToAdd, err)
|
||||
}
|
||||
@@ -250,29 +191,31 @@ func expiry(duration string, now time.Time) (time.Time, error) {
|
||||
return now.Add(length), nil
|
||||
}
|
||||
|
||||
// liftBans answers DELETE BansPath/<client>: it lifts every ban active on
|
||||
// a netblock the client's address is in, and answers with those bans, or
|
||||
// with 404 when none is active.
|
||||
func (rq *request) liftBans() {
|
||||
client, err := pathAddress(rq.in.URL.Path, BansPath+"/")
|
||||
if err != nil {
|
||||
http.Error(rq.out, err.Error(), http.StatusBadRequest)
|
||||
// handleLiftBans answers DELETE BansPath/<client>: it lifts every ban
|
||||
// active on a netblock the client's address is in, and answers with those
|
||||
// bans, or with 404 when none is active.
|
||||
func (h *handler) handleLiftBans() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
client, err := pathAddress(r.URL.Path, BansPath+"/")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
lifted := h.ledger.Lift(client, h.now())
|
||||
if len(lifted) == 0 {
|
||||
http.Error(
|
||||
w,
|
||||
"no ban is active on "+client.String(),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
h.answerBans(w, lifted)
|
||||
}
|
||||
|
||||
lifted := rq.h.ledger.Lift(client, rq.h.now())
|
||||
if len(lifted) == 0 {
|
||||
http.Error(
|
||||
rq.out,
|
||||
"no ban is active on "+client.String(),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.answerBans(lifted)
|
||||
}
|
||||
|
||||
// clientAnswer is the answer to GET ClientsPath<ip>: the client the
|
||||
@@ -284,29 +227,31 @@ type clientAnswer struct {
|
||||
Bans []state.BanEntry `json:"bans"`
|
||||
}
|
||||
|
||||
// showClient answers GET ClientsPath<ip> with what waf knows of the client:
|
||||
// its counters, its history, which holds its country as last looked up and its
|
||||
// offences, and its bans with their notes.
|
||||
func (rq *request) showClient() {
|
||||
addr, err := pathAddress(rq.in.URL.Path, ClientsPath)
|
||||
if err != nil {
|
||||
http.Error(rq.out, err.Error(), http.StatusBadRequest)
|
||||
// handleShowClient answers GET ClientsPath<ip> with what waf knows of the
|
||||
// client: its counters, its history, which holds its country as last looked
|
||||
// up and its offences, and its bans with their notes.
|
||||
func (h *handler) handleShowClient() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
addr, err := pathAddress(r.URL.Path, ClientsPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
answer := clientAnswer{
|
||||
Bans: state.BanEntries(h.ledger.Covering(addr)),
|
||||
}
|
||||
|
||||
client, seen := h.limiter.Client(
|
||||
clientGroup(h.config.IPv6GroupPrefix, addr),
|
||||
)
|
||||
if seen {
|
||||
answer.Client = &client
|
||||
}
|
||||
|
||||
h.answerJSON(w, answer)
|
||||
}
|
||||
|
||||
answer := clientAnswer{
|
||||
Bans: state.BanEntries(rq.h.ledger.Covering(addr)),
|
||||
}
|
||||
|
||||
client, seen := rq.h.limiter.Client(
|
||||
clientGroup(rq.h.config.IPv6GroupPrefix, addr),
|
||||
)
|
||||
if seen {
|
||||
answer.Client = &client
|
||||
}
|
||||
|
||||
rq.answerJSON(answer)
|
||||
}
|
||||
|
||||
// pathAddress reads the client's address that follows prefix in path.
|
||||
@@ -322,27 +267,27 @@ func pathAddress(path, prefix string) (netip.Addr, error) {
|
||||
}
|
||||
|
||||
// answerBans answers with held under bans, as bans.json lists them.
|
||||
func (rq *request) answerBans(held []bans.Ban) {
|
||||
rq.answerJSON(struct {
|
||||
func (h *handler) answerBans(w http.ResponseWriter, held []bans.Ban) {
|
||||
h.answerJSON(w, struct {
|
||||
Bans []state.BanEntry `json:"bans"`
|
||||
}{state.BanEntries(held)})
|
||||
}
|
||||
|
||||
// answerJSON answers with value as indented JSON.
|
||||
func (rq *request) answerJSON(value any) {
|
||||
func (h *handler) answerJSON(w http.ResponseWriter, value any) {
|
||||
body, err := json.MarshalIndent(value, "", " ")
|
||||
if err != nil {
|
||||
rq.h.processLog.Error(
|
||||
h.processLog.Error(
|
||||
"encoding an answer failed",
|
||||
"error",
|
||||
err.Error(),
|
||||
)
|
||||
http.Error(rq.out, http.StatusText(http.StatusInternalServerError),
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError),
|
||||
http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.out.Header().Set("Content-Type", "application/json")
|
||||
_, _ = rq.out.Write(append(body, '\n'))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write(append(body, '\n'))
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -333,6 +334,39 @@ func TestBanToAddThatCannotBeReadIsRefused(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBanToAddIsLoggedWithTheBytesOfItsBody(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s, _, _ := startWithClock(
|
||||
t,
|
||||
"",
|
||||
map[string]string{adminToken: adminSecret},
|
||||
)
|
||||
|
||||
s.admin(http.MethodPost, waf.BansPath, banOtherClient, http.StatusOK)
|
||||
|
||||
line := s.out.endedLines(t, s.sent)[s.sent-1]
|
||||
if line.RequestBytes != int64(len(banOtherClient)) {
|
||||
t.Errorf(
|
||||
"the ban to add was logged with request_bytes %d, want %d",
|
||||
line.RequestBytes,
|
||||
len(banOtherClient),
|
||||
)
|
||||
}
|
||||
|
||||
// The health endpoint reads none of a body sent to it.
|
||||
s.ownEndpointRequest("", http.MethodGet, wafprotocol.HealthPath, "ok",
|
||||
http.StatusOK)
|
||||
|
||||
line = s.out.endedLines(t, s.sent)[s.sent-1]
|
||||
if line.RequestBytes != 0 {
|
||||
t.Errorf(
|
||||
"the health request was logged with request_bytes %d, want 0",
|
||||
line.RequestBytes,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientEndpointShowsTheClientAndItsBans(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
+12
-8
@@ -45,8 +45,11 @@ func (a activeBan) check(
|
||||
}
|
||||
|
||||
ban, banned, madePermanent := check(rq.client, now)
|
||||
|
||||
var expires string
|
||||
if banned {
|
||||
rq.line.BanExpires = banExpires(ban)
|
||||
expires = banExpires(ban)
|
||||
rq.line.BanExpires = expires
|
||||
}
|
||||
|
||||
if madePermanent {
|
||||
@@ -61,7 +64,7 @@ func (a activeBan) check(
|
||||
return &verdict{
|
||||
action: banAction(ban.Permanent()),
|
||||
figures: map[string]any{
|
||||
figureNetblock: ban.Netblock.String(), "ban_expires": rq.line.BanExpires,
|
||||
figureNetblock: ban.Netblock.String(), "ban_expires": expires,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -148,6 +151,7 @@ func (b banMaker) banForLimit(
|
||||
rq.line.LimitHit = hit.Window
|
||||
}
|
||||
|
||||
rq.brokeLimit = true
|
||||
rq.line.Offence = requestlog.OffenceLimit
|
||||
|
||||
b.metrics.LimitHit(hit)
|
||||
@@ -259,9 +263,9 @@ func (b banMaker) withClientNotes(
|
||||
status int,
|
||||
netblock netip.Prefix,
|
||||
) bans.Notes {
|
||||
notes.ASN = rq.line.ASN
|
||||
notes.ASName = rq.line.ASName
|
||||
notes.Country = rq.line.Country
|
||||
notes.ASN = rq.lookupAnswer.ASN
|
||||
notes.ASName = rq.lookupAnswer.ASName
|
||||
notes.Country = rq.lookupAnswer.Country
|
||||
notes.Reputation = rq.reputation
|
||||
notes.Request = rq.noted(now, status)
|
||||
notes.Requests = b.netblockRequests(netblock)
|
||||
@@ -328,11 +332,11 @@ func (b banMaker) netblockRequests(netblock netip.Prefix) int64 {
|
||||
func (rq *request) noted(now time.Time, status int) bans.Request {
|
||||
return bans.Request{
|
||||
Time: now,
|
||||
Method: rq.line.Method,
|
||||
Host: rq.line.Host,
|
||||
Method: rq.method,
|
||||
Host: rq.site,
|
||||
Path: rq.requestURI,
|
||||
Status: status,
|
||||
UserAgent: rq.line.UserAgent,
|
||||
UserAgent: rq.userAgent,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ type biasedThresholds struct {
|
||||
func (b biasedThresholds) limitPercentages(
|
||||
rq *request,
|
||||
) (percentage, percentage) {
|
||||
asn, country := rq.line.ASN, rq.line.Country
|
||||
asn, country := rq.lookupAnswer.ASN, rq.lookupAnswer.Country
|
||||
|
||||
unknown := percentage{percent: whole}
|
||||
if country == "" && !rq.skippedLookupWait {
|
||||
@@ -115,7 +115,7 @@ func (b biasedThresholds) limitPercentages(
|
||||
|
||||
otherTags := percentage{percent: whole}
|
||||
|
||||
for _, tag := range rq.line.Tags {
|
||||
for _, tag := range rq.tags {
|
||||
if tag != asnTag(asn) && tag != countryTag(country) {
|
||||
otherTags = lowest(
|
||||
otherTags,
|
||||
|
||||
@@ -43,6 +43,15 @@ func (b blocklists) check(
|
||||
}
|
||||
}
|
||||
|
||||
// refusesWhateverTheLookupAnswers reports whether blocklists refuses rq, as
|
||||
// refusedByBlocklists says. The lookup's answer plays no part in it.
|
||||
func (b blocklists) refusesWhateverTheLookupAnswers(
|
||||
rq *request,
|
||||
_ time.Time,
|
||||
) bool {
|
||||
return b.refusedByBlocklists(b.lists.ListedBy(rq.client))
|
||||
}
|
||||
|
||||
// refusedByBlocklists reports whether the heuristic blocklists refuses a
|
||||
// client, given listedBy, the blocklists that list it: it does when one lists
|
||||
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
||||
|
||||
@@ -33,7 +33,7 @@ func (b byteLimits) check(
|
||||
rq *request,
|
||||
now time.Time,
|
||||
) *verdict {
|
||||
if !rq.counted || rq.line.WouldAction != "" {
|
||||
if !rq.counted || rq.wouldAction != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -121,9 +121,9 @@ func (c coreRuleSet) alertCoreRuleSetMatch(
|
||||
Host: rq.site,
|
||||
Client: rq.client,
|
||||
Netblock: clientGroup(c.ipv6GroupPrefix, rq.client),
|
||||
ASN: rq.line.ASN,
|
||||
ASName: rq.line.ASName,
|
||||
Country: rq.line.Country,
|
||||
ASN: rq.lookupAnswer.ASN,
|
||||
ASName: rq.lookupAnswer.ASName,
|
||||
Country: rq.lookupAnswer.Country,
|
||||
Reason: "scored by the Core Rule Set at or over " +
|
||||
"SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD",
|
||||
Detail: detail,
|
||||
|
||||
@@ -27,7 +27,7 @@ func (c countries) check(
|
||||
_ time.Time,
|
||||
) *verdict {
|
||||
allowed := c.allowed
|
||||
country := rq.line.Country
|
||||
country := rq.lookupAnswer.Country
|
||||
|
||||
if len(allowed) == 0 || slices.Contains(allowed, country) {
|
||||
return nil
|
||||
|
||||
@@ -58,3 +58,14 @@ func TestHealthEndpointCountsTowardNoLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthEndpointIsFoundAtItsPathPercentEncoded(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// %68 is h. An endpoint is found by the decoded path.
|
||||
s, _, _ := startWithClock(t, "", nil)
|
||||
|
||||
got := s.ownEndpointRequest("", http.MethodGet, "/_sww/%68ealthz", "",
|
||||
http.StatusOK)
|
||||
wantBody(t, got, "ok\n")
|
||||
}
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
// and holds the settings and parts it reads. Add one line for it to one of the
|
||||
// lists that newHandler builds, which hands it those settings and parts. Its
|
||||
// settings go in config.WAF, and the comment at the top of its file names them.
|
||||
// A heuristic of afterLookup that can refuse a request whatever the lookup
|
||||
// answers also implements refusesWithoutLookup.
|
||||
//
|
||||
// Three places outside its file may change with it:
|
||||
// - waitForGeoJS in newHandler lists the settings that need the lookup's
|
||||
// answer;
|
||||
// - requestCanGetTag in internal/config/waf.go holds which setting can give a
|
||||
// request which tag;
|
||||
// - findBesideLookup does the work of each heuristic of besideLookup, by
|
||||
// name.
|
||||
|
||||
// heuristic is one of the checks that decide what waf does with a
|
||||
// request.
|
||||
@@ -20,6 +30,15 @@ type heuristic interface {
|
||||
check(ctx context.Context, rq *request, now time.Time) *verdict
|
||||
}
|
||||
|
||||
// refusesWithoutLookup is a heuristic of afterLookup that can tell, before the
|
||||
// lookup answers, that it will refuse a request whatever the answer.
|
||||
type refusesWithoutLookup interface {
|
||||
// refusesWhateverTheLookupAnswers reports whether the heuristic will refuse
|
||||
// rq at now, whatever the lookup answers about its client. It changes
|
||||
// nothing.
|
||||
refusesWhateverTheLookupAnswers(rq *request, now time.Time) bool
|
||||
}
|
||||
|
||||
// verdict is a heuristic refusing a request, or banning its client: the action
|
||||
// the log line names, one of requestlog's Action constants, and the figures the
|
||||
// heuristic acted on, by name, such as the count and the limit.
|
||||
|
||||
@@ -94,11 +94,11 @@ func (rq *request) noteLookup(result lookupResult) {
|
||||
rq.line.ASName = rq.lookupAnswer.ASName
|
||||
rq.line.Country = rq.lookupAnswer.Country
|
||||
|
||||
if tag := asnTag(rq.line.ASN); tag != "" {
|
||||
if tag := asnTag(rq.lookupAnswer.ASN); tag != "" {
|
||||
rq.addTag(tag)
|
||||
}
|
||||
|
||||
if tag := countryTag(rq.line.Country); tag != "" {
|
||||
if tag := countryTag(rq.lookupAnswer.Country); tag != "" {
|
||||
rq.addTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
+30
-31
@@ -177,30 +177,6 @@ func newHandler(params Params) *handler {
|
||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||
}
|
||||
|
||||
h.blocklists = blocklists{
|
||||
lists: params.Lists,
|
||||
action: cfg.BlocklistAction,
|
||||
hitNoter: hitNoter,
|
||||
}
|
||||
h.rateLimits = rateLimits{
|
||||
limiter: params.Limiter,
|
||||
exemptNets: cfg.RateLimitExemptNets,
|
||||
exemptPaths: cfg.RateLimitExemptPaths,
|
||||
banResponse: cfg.BanResponse,
|
||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||
biasedThresholds: biasedThresholds{
|
||||
lists: params.Lists,
|
||||
tagLimitPercent: cfg.TagLimitPercent,
|
||||
asnBytesLimitPercent: cfg.ASNBytesLimitPercent,
|
||||
countryBytesLimitPercent: cfg.CountryBytesLimitPercent,
|
||||
unknownCountryLimitPercent: cfg.UnknownCountryLimitPercent,
|
||||
blocklistAction: cfg.BlocklistAction,
|
||||
blocklistLimitPercent: cfg.BlocklistLimitPercent,
|
||||
dnsblAbuseIPDBAction: cfg.DNSBLAbuseIPDBAction,
|
||||
dnsblAbuseIPDBLimitPercent: cfg.DNSBLAbuseIPDBLimitPercent,
|
||||
},
|
||||
banMaker: sharedBanMaker,
|
||||
}
|
||||
h.trapPaths = trapPaths{paths: cfg.TrapPaths, banMaker: sharedBanMaker}
|
||||
h.ruleFiles = ruleFiles{
|
||||
rules: params.Rules,
|
||||
@@ -230,7 +206,11 @@ func newHandler(params Params) *handler {
|
||||
}
|
||||
h.afterLookup = []heuristic{
|
||||
countries{allowed: cfg.ExclusivelyAllowedCountries},
|
||||
h.blocklists,
|
||||
blocklists{
|
||||
lists: params.Lists,
|
||||
action: cfg.BlocklistAction,
|
||||
hitNoter: hitNoter,
|
||||
},
|
||||
dnsbl{
|
||||
dnsbl: params.DNSBL,
|
||||
action: cfg.DNSBLAbuseIPDBAction,
|
||||
@@ -246,7 +226,25 @@ func newHandler(params Params) *handler {
|
||||
hitNoter: hitNoter,
|
||||
},
|
||||
tagDeny{tags: cfg.TagDeny},
|
||||
h.rateLimits,
|
||||
rateLimits{
|
||||
limiter: params.Limiter,
|
||||
exemptNets: cfg.RateLimitExemptNets,
|
||||
exemptPaths: cfg.RateLimitExemptPaths,
|
||||
banResponse: cfg.BanResponse,
|
||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||
biasedThresholds: biasedThresholds{
|
||||
lists: params.Lists,
|
||||
tagLimitPercent: cfg.TagLimitPercent,
|
||||
asnBytesLimitPercent: cfg.ASNBytesLimitPercent,
|
||||
countryBytesLimitPercent: cfg.CountryBytesLimitPercent,
|
||||
unknownCountryLimitPercent: cfg.UnknownCountryLimitPercent,
|
||||
blocklistAction: cfg.BlocklistAction,
|
||||
blocklistLimitPercent: cfg.BlocklistLimitPercent,
|
||||
dnsblAbuseIPDBAction: cfg.DNSBLAbuseIPDBAction,
|
||||
dnsblAbuseIPDBLimitPercent: cfg.DNSBLAbuseIPDBLimitPercent,
|
||||
},
|
||||
banMaker: sharedBanMaker,
|
||||
},
|
||||
}
|
||||
h.besideLookup = []heuristic{h.trapPaths, h.ruleFiles, h.coreRuleSet}
|
||||
h.afterBesideLookup = []heuristic{tagDeny{tags: cfg.TagDeny}}
|
||||
@@ -268,6 +266,8 @@ func newHandler(params Params) *handler {
|
||||
},
|
||||
}
|
||||
|
||||
h.ownEndpoints = h.routes()
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
@@ -293,6 +293,9 @@ type handler struct {
|
||||
// awaitingReport holds the decision on each answered question by its id
|
||||
// until its report comes, and nil for a question still being handled.
|
||||
awaitingReport *lru.Cache[string, *decision]
|
||||
// ownEndpoints answers every request that is not a question, as routes
|
||||
// lists the endpoints.
|
||||
ownEndpoints http.Handler
|
||||
|
||||
// The lists of heuristics, each in the order its heuristics run. A client
|
||||
// in SWW_WAF_ALLOW_NETS skips them all.
|
||||
@@ -323,11 +326,7 @@ type handler struct {
|
||||
// afterAnswer, they only ban.
|
||||
onReport []heuristic
|
||||
|
||||
// The heuristics of the lists that other steps of a request use too:
|
||||
// refusedWhateverTheAnswer asks blocklists and rateLimits, and
|
||||
// findBesideLookup does the work of trapPaths, ruleFiles and coreRuleSet.
|
||||
blocklists blocklists
|
||||
rateLimits rateLimits
|
||||
// The heuristics of besideLookup, whose work findBesideLookup does.
|
||||
trapPaths trapPaths
|
||||
ruleFiles ruleFiles
|
||||
coreRuleSet coreRuleSet
|
||||
|
||||
+27
-20
@@ -548,16 +548,11 @@ func addCoreRuleSet(t *testing.T, params *waf.Params) {
|
||||
params.CoreRuleSet = coreRuleSet
|
||||
}
|
||||
|
||||
// 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.
|
||||
// addCounters adds to params the table of clients, the ledger and the
|
||||
// anomaly counters, as sww builds them from params' settings.
|
||||
func addCounters(params *waf.Params) {
|
||||
cfg := params.Config
|
||||
|
||||
params.Metrics = metrics.New(
|
||||
cfg.MetricsBusiestCountriesAndASNs,
|
||||
cfg.InstanceName,
|
||||
)
|
||||
params.Limiter = ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{
|
||||
PerMinute: cfg.RateLimitPerMinute,
|
||||
@@ -587,26 +582,18 @@ func addCounters(params *waf.Params) {
|
||||
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 and its
|
||||
// blacklist from abuseIPDBBlacklistURL, 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.
|
||||
// setting is set, the metrics newMetrics returns, and GeoJS at geojsURL, as
|
||||
// sww builds them from params' settings and the parts addCounters added.
|
||||
func addOutsideSources(
|
||||
params *waf.Params, geojsURL, abuseIPDBCheckURL string,
|
||||
) {
|
||||
cfg := params.Config
|
||||
m, limiter, ledger := params.Metrics, params.Limiter, params.Ledger
|
||||
limiter, ledger := params.Limiter, params.Ledger
|
||||
processLog, alertQueue := params.ProcessLog, params.Alerts
|
||||
|
||||
params.Lists = reputation.New(reputation.Params{
|
||||
@@ -655,6 +642,9 @@ func addOutsideSources(
|
||||
})
|
||||
}
|
||||
|
||||
m := newMetrics(params)
|
||||
params.Metrics = m
|
||||
|
||||
params.GeoJS = geojs.New(geojs.Params{
|
||||
URL: geojsURL,
|
||||
Answered: func(answer geojs.Answer) {
|
||||
@@ -667,12 +657,29 @@ func addOutsideSources(
|
||||
},
|
||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
||||
})
|
||||
}
|
||||
|
||||
m.AddReputation(params.Lists, params.DNSBL)
|
||||
// newMetrics returns the metrics, as sww builds them from params' settings,
|
||||
// read from the ledger, the table of clients, the rules, the lists, the
|
||||
// zones' verdicts, and AbuseIPDB's while its key is set.
|
||||
func newMetrics(params *waf.Params) *metrics.Metrics {
|
||||
cfg := params.Config
|
||||
metricsParams := metrics.Params{
|
||||
BusiestCount: cfg.MetricsBusiestCountriesAndASNs,
|
||||
InstanceName: cfg.InstanceName,
|
||||
Now: params.Now,
|
||||
Bans: params.Ledger,
|
||||
Clients: params.Limiter,
|
||||
Rules: params.Rules,
|
||||
Lists: params.Lists,
|
||||
DNSBL: params.DNSBL,
|
||||
}
|
||||
|
||||
if cfg.AbuseIPDBKey != "" {
|
||||
m.AddAbuseIPDB(params.AbuseIPDB)
|
||||
metricsParams.AbuseIPDB = params.AbuseIPDB
|
||||
}
|
||||
|
||||
return metrics.New(metricsParams)
|
||||
}
|
||||
|
||||
// newClient returns an HTTP client that sends requests as they are made,
|
||||
|
||||
@@ -67,11 +67,14 @@ func (r rateLimits) check(
|
||||
}
|
||||
}
|
||||
|
||||
// overAWholeRateLimit reports whether the heuristic rate_limits will refuse rq
|
||||
// at now, whatever its client's limit percentages: the rate limits count it,
|
||||
// and one more request from its client takes it over a whole rate limit. It
|
||||
// counts nothing.
|
||||
func (r rateLimits) overAWholeRateLimit(rq *request, now time.Time) bool {
|
||||
// refusesWhateverTheLookupAnswers reports whether the heuristic rate_limits
|
||||
// will refuse rq at now, whatever its client's limit percentages, which the
|
||||
// lookup's answer can lower: the rate limits count it, and one more request
|
||||
// from its client takes it over a whole rate limit. It counts nothing.
|
||||
func (r rateLimits) refusesWhateverTheLookupAnswers(
|
||||
rq *request,
|
||||
now time.Time,
|
||||
) bool {
|
||||
return r.countedByRateLimits(rq) &&
|
||||
r.limiter.OverARateLimit(
|
||||
clientGroup(r.ipv6GroupPrefix, rq.client),
|
||||
|
||||
@@ -54,7 +54,7 @@ func (r refusalLimit) check(
|
||||
// What the client was sent, or in observe mode would have been.
|
||||
status := rq.out.status
|
||||
|
||||
switch rq.line.WouldAction {
|
||||
switch rq.wouldAction {
|
||||
case requestlog.ActionBlockedByBlockRule,
|
||||
requestlog.ActionBlockedByCoreRuleSet:
|
||||
status = http.StatusForbidden
|
||||
|
||||
@@ -47,9 +47,9 @@ func (n reputationHitNoter) noteHit(
|
||||
Host: rq.site,
|
||||
Client: rq.client,
|
||||
Netblock: clientGroup(n.ipv6GroupPrefix, rq.client),
|
||||
ASN: rq.line.ASN,
|
||||
ASName: rq.line.ASName,
|
||||
Country: rq.line.Country,
|
||||
ASN: rq.lookupAnswer.ASN,
|
||||
ASName: rq.lookupAnswer.ASName,
|
||||
Country: rq.lookupAnswer.Country,
|
||||
Reason: reason,
|
||||
Detail: detail,
|
||||
})
|
||||
|
||||
+42
-20
@@ -24,42 +24,64 @@ type refusal struct {
|
||||
action string
|
||||
}
|
||||
|
||||
// decision is what the checks note of a request: its log line, and what
|
||||
// counting the request needs once it has ended. waf keeps a copy of it
|
||||
// until the request's report comes. It holds no credentials, since the log line
|
||||
// never does, and neither the request's objects nor their bodies.
|
||||
// decision is what the checks note of a request: its log line, what the
|
||||
// heuristics tell each other, and what counting the request needs once it has
|
||||
// ended. Nothing reads the log line back to decide. waf keeps a copy of it
|
||||
// until the request's report comes. It holds no credentials, since the log
|
||||
// line never does, and neither the request's objects nor their bodies.
|
||||
type decision struct {
|
||||
line requestlog.Line
|
||||
client netip.Addr
|
||||
// requestURI is the path with the query, as a ban's notes keep it.
|
||||
requestURI string
|
||||
// method and userAgent are the request's method and User-Agent, and
|
||||
// requestURI its path with the query, as a ban's notes keep them.
|
||||
// startRequest sets them.
|
||||
method, userAgent, requestURI string
|
||||
// tags are the request's tags, sorted, each once. addTag adds them, for
|
||||
// tagLists, routeWatchTags, noteLookup, dnsbl, abuseIPDB, ruleFiles and
|
||||
// coreRuleSet.
|
||||
tags []string
|
||||
// site is, for a question, the host the visitor asked for, which the
|
||||
// alerts name, since one waf serves many sites.
|
||||
// alerts and a ban's notes name, since one waf serves many sites.
|
||||
// answerQuestion sets it.
|
||||
site string
|
||||
// allowed is true for a question answered with DispositionAllow.
|
||||
allowed bool
|
||||
// lookedUp is true once the client's AS number and country have been
|
||||
// looked up, whether or not an answer was there, and lookupAnswer is
|
||||
// what the lookup gave then, the zero Answer while GeoJS had given none.
|
||||
// noteLookup sets them.
|
||||
lookedUp bool
|
||||
lookupAnswer geojs.Answer
|
||||
// skippedLookupWait is true for a request that did not wait for GeoJS and
|
||||
// found no answer kept. SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT does not apply to it,
|
||||
// so that not waiting never lowers its client's limits.
|
||||
// so that not waiting never lowers its client's limits. noteLookup sets
|
||||
// it.
|
||||
skippedLookupWait bool
|
||||
// counted is true for a request the rate limits counted, whose bytes
|
||||
// the byte limits count once it has ended. rateLimitPercent and
|
||||
// bytesLimitPercent are then its client's limit percentages for the rate
|
||||
// limits and for the byte limits.
|
||||
// limits and for the byte limits. rateLimits sets them.
|
||||
counted bool
|
||||
rateLimitPercent, bytesLimitPercent percentage
|
||||
// attack is true for a request that matched a ban rule or asked for a
|
||||
// trap path, blockedByBlockRule for one a block rule refused, and
|
||||
// blockedByCoreRuleSet for one the Core Rule Set refused, each an offence
|
||||
// its client's history counts.
|
||||
// its client's history counts. trapPaths and ruleFiles set attack,
|
||||
// ruleFiles blockedByBlockRule, and coreRuleSet blockedByCoreRuleSet.
|
||||
attack, blockedByBlockRule, blockedByCoreRuleSet bool
|
||||
// wouldAction is, in observe mode, the action enforce mode would have
|
||||
// refused the request with, one of requestlog's Action constants, and ""
|
||||
// otherwise. check sets it.
|
||||
wouldAction string
|
||||
// brokeLimit is true for a request that broke a limit, an offence its
|
||||
// client's history counts. banForLimit sets it.
|
||||
brokeLimit bool
|
||||
// blocklisted is true once blocklists finds a blocklist that lists the
|
||||
// client, dnsblListed once dnsbl finds a DNSBL zone's verdict that does,
|
||||
// and abuseIPDBHit once abuseIPDB finds AbuseIPDB's score of it a hit.
|
||||
blocklisted, dnsblListed, abuseIPDBHit bool
|
||||
// reputation is the reputation sources that list the client, for the
|
||||
// notes of a ban the request makes.
|
||||
// notes of a ban the request makes. noteHit adds them.
|
||||
reputation []bans.ReputationHit
|
||||
}
|
||||
|
||||
@@ -72,11 +94,7 @@ type request struct {
|
||||
in *http.Request
|
||||
out *responseWriter
|
||||
|
||||
// blocklisted is true once a blocklist is found to list the client,
|
||||
// dnsblListed once a DNSBL zone's verdict is, and abuseIPDBHit once
|
||||
// AbuseIPDB's score of it is a hit.
|
||||
blocklisted, dnsblListed, abuseIPDBHit bool
|
||||
start time.Time
|
||||
start time.Time
|
||||
// checked is when the checks were done.
|
||||
checked time.Time
|
||||
// refused is what the request was refused with, nil if it was not.
|
||||
@@ -106,6 +124,8 @@ func (h *handler) startRequest(
|
||||
rq := &request{
|
||||
decision: decision{
|
||||
client: client,
|
||||
method: r.Method,
|
||||
userAgent: r.UserAgent(),
|
||||
requestURI: r.URL.RequestURI(),
|
||||
line: requestlog.Line{
|
||||
Time: requestlog.FormatTime(start),
|
||||
@@ -151,11 +171,12 @@ func (h *handler) startRequest(
|
||||
}
|
||||
|
||||
// addTag adds tag to the request's tags, which stay sorted and hold each tag
|
||||
// once.
|
||||
// once, and to its log line.
|
||||
func (rq *request) addTag(tag string) {
|
||||
at, found := slices.BinarySearch(rq.line.Tags, tag)
|
||||
at, found := slices.BinarySearch(rq.tags, tag)
|
||||
if !found {
|
||||
rq.line.Tags = slices.Insert(rq.line.Tags, at, tag)
|
||||
rq.tags = slices.Insert(rq.tags, at, tag)
|
||||
rq.line.Tags = rq.tags
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,6 +217,7 @@ func (rq *request) check(ctx context.Context) *refusal {
|
||||
|
||||
switch {
|
||||
case rq.h.config.Observe:
|
||||
rq.wouldAction = action
|
||||
rq.line.WouldAction = action
|
||||
|
||||
return nil
|
||||
@@ -371,7 +393,7 @@ func (rq *request) addToHistory() {
|
||||
Status: rq.out.status,
|
||||
RequestBytes: rq.requestBytes,
|
||||
ResponseBytes: rq.out.bytes,
|
||||
BrokeLimit: rq.line.Offence == requestlog.OffenceLimit,
|
||||
BrokeLimit: rq.brokeLimit,
|
||||
Attack: rq.attack,
|
||||
BlockedByBlockRule: rq.blockedByBlockRule,
|
||||
BlockedByCoreRuleSet: rq.blockedByCoreRuleSet,
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package waf
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
||||
)
|
||||
|
||||
// routes returns the router of waf's own endpoints. A question never reaches
|
||||
// it, since ServeHTTP answers a question first. No check runs on a request
|
||||
// here: waf's address is on the private network, where the peers are rproxy
|
||||
// and the admin's tools, not visitors.
|
||||
func (h *handler) routes() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(matchDecodedPath)
|
||||
|
||||
// The report writes no log line of its own: it finishes the line of the
|
||||
// request it reports.
|
||||
r.Post(wafprotocol.ReportPath, h.handleReport())
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(h.logOwnEndpoint)
|
||||
// The answer to a path with no endpoint, and to a method the path has
|
||||
// no endpoint for, is set in this group, so that it is logged too.
|
||||
r.NotFound(answerNotFound)
|
||||
r.MethodNotAllowed(answerNotFound)
|
||||
|
||||
r.Get(wafprotocol.HealthPath, handleHealth())
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(requireToken(h.config.MetricsToken))
|
||||
r.Get(MetricsPath, h.metrics.ServeHTTP)
|
||||
})
|
||||
|
||||
// The handlers under BansPath/ and ClientsPath read the address from
|
||||
// the path themselves, so that one that is not an address gets 400
|
||||
// after the token check.
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(requireToken(h.config.AdminToken))
|
||||
r.Get(BansPath, h.handleListBans())
|
||||
r.Post(BansPath, h.handleAddBan())
|
||||
r.Delete(BansPath+"/*", h.handleLiftBans())
|
||||
r.Get(ClientsPath+"*", h.handleShowClient())
|
||||
})
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// matchDecodedPath has the router match the decoded path, which the handlers
|
||||
// read. chi matches the path as sent when it is encoded in an unusual way,
|
||||
// which would make /_sww/%68ealthz a path with no endpoint.
|
||||
func matchDecodedPath(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
chi.RouteContext(r.Context()).RoutePath = r.URL.Path
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// logOwnEndpoint writes one log line for each request, with the action
|
||||
// own_endpoint. The line's request_bytes is how much of the body the handler
|
||||
// read.
|
||||
func (h *handler) logOwnEndpoint(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
peer := peerAddress(r)
|
||||
|
||||
rq := h.startRequest(w, r, peer, peer, false)
|
||||
defer rq.finish()
|
||||
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
|
||||
counted := r.Clone(r.Context())
|
||||
counted.Body = &countedBody{
|
||||
ReadCloser: r.Body,
|
||||
read: &rq.requestBytes,
|
||||
}
|
||||
|
||||
next.ServeHTTP(rq.out, counted)
|
||||
})
|
||||
}
|
||||
|
||||
// countedBody is a request's body that adds the bytes read of it to read.
|
||||
type countedBody struct {
|
||||
io.ReadCloser
|
||||
|
||||
read *int64
|
||||
}
|
||||
|
||||
// Read reads part of the body.
|
||||
func (b *countedBody) Read(p []byte) (int, error) {
|
||||
n, err := b.ReadCloser.Read(p)
|
||||
*b.read += int64(n)
|
||||
|
||||
return n, err
|
||||
}
|
||||
|
||||
// requireToken returns middleware that lets a request through only when it
|
||||
// carries token, as Authorization: Bearer <token>. It answers 401 to a request
|
||||
// without it. While token is unset, it answers 404, as a path with no endpoint
|
||||
// does.
|
||||
func requireToken(token string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case token == "":
|
||||
answerNotFound(w, r)
|
||||
case !hasToken(r, token):
|
||||
w.Header().Set("WWW-Authenticate", "Bearer")
|
||||
http.Error(
|
||||
w,
|
||||
http.StatusText(http.StatusUnauthorized),
|
||||
http.StatusUnauthorized,
|
||||
)
|
||||
default:
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// hasToken reports whether r carries token, as Authorization: Bearer
|
||||
// <token>.
|
||||
func hasToken(r *http.Request, token string) bool {
|
||||
scheme, sent, _ := strings.Cut(r.Header.Get("Authorization"), " ")
|
||||
|
||||
return strings.EqualFold(scheme, "Bearer") &&
|
||||
subtle.ConstantTimeCompare([]byte(sent), []byte(token)) == 1
|
||||
}
|
||||
|
||||
// answerNotFound answers 404, for a path with no endpoint, and for a method
|
||||
// the path has no endpoint for.
|
||||
func answerNotFound(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
}
|
||||
@@ -23,7 +23,7 @@ func (t tagDeny) check(
|
||||
rq *request,
|
||||
_ time.Time,
|
||||
) *verdict {
|
||||
for _, tag := range rq.line.Tags {
|
||||
for _, tag := range rq.tags {
|
||||
if slices.Contains(t.tags, tag) {
|
||||
return &verdict{
|
||||
action: requestlog.ActionClientDenied,
|
||||
|
||||
+36
-53
@@ -58,8 +58,8 @@ func New(params Params) *Server {
|
||||
}
|
||||
|
||||
// ServeHTTP handles one request to waf: a request carrying
|
||||
// wafprotocol.ClientHeader is rproxy's question, and any other is for waf
|
||||
// itself.
|
||||
// wafprotocol.ClientHeader is rproxy's question, and any other is for one of
|
||||
// waf's own endpoints.
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if len(r.Header.Values(wafprotocol.ClientHeader)) > 0 {
|
||||
h.answerQuestion(w, r)
|
||||
@@ -67,23 +67,27 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
h.answerOwnEndpoint(w, r)
|
||||
h.ownEndpoints.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// refusedWhateverTheAnswer reports whether the request is one that does not wait
|
||||
// for GeoJS, since the checks will refuse it at now whatever GeoJS answers about
|
||||
// its client: the heuristic blocklists or rate_limits will refuse it, as a
|
||||
// function in that heuristic's file says. In observe mode, which refuses
|
||||
// nothing, none is. rproxy lets a request through when waf answers too late.
|
||||
// its client: a heuristic of afterLookup that implements refusesWithoutLookup
|
||||
// says it will refuse it. In observe mode, which refuses nothing, none is.
|
||||
// rproxy lets a request through when waf answers too late.
|
||||
func (rq *request) refusedWhateverTheAnswer(now time.Time) bool {
|
||||
if rq.h.config.Observe {
|
||||
return false
|
||||
}
|
||||
|
||||
listedBy := rq.h.blocklists.lists.ListedBy(rq.client)
|
||||
for _, each := range rq.h.afterLookup {
|
||||
knows, ok := each.(refusesWithoutLookup)
|
||||
if ok && knows.refusesWhateverTheLookupAnswers(rq, now) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return rq.h.blocklists.refusedByBlocklists(listedBy) ||
|
||||
rq.h.rateLimits.overAWholeRateLimit(rq, now)
|
||||
return false
|
||||
}
|
||||
|
||||
// answerQuestion runs the checks on the visitor's request a question
|
||||
@@ -202,8 +206,8 @@ func (rq *request) allow() {
|
||||
|
||||
answer := wafprotocol.Answer{Disposition: wafprotocol.DispositionAllow}
|
||||
if rq.h.config.AddLookupHeaders {
|
||||
answer.ASN = rq.line.ASN
|
||||
answer.Country = rq.line.Country
|
||||
answer.ASN = rq.lookupAnswer.ASN
|
||||
answer.Country = rq.lookupAnswer.Country
|
||||
}
|
||||
|
||||
wafprotocol.SetAnswerHeaders(rq.out.Header(), answer)
|
||||
@@ -240,50 +244,29 @@ func (rq *request) deny(r refusal) {
|
||||
http.Error(rq.out, http.StatusText(r.status), r.status)
|
||||
}
|
||||
|
||||
// answerOwnEndpoint answers a request to waf's own address. No check
|
||||
// runs on it: the address is on the private network, where the peers are
|
||||
// rproxy and the admin's tools, not visitors.
|
||||
func (h *handler) answerOwnEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost && r.URL.Path == wafprotocol.ReportPath {
|
||||
h.takeReport(w, r)
|
||||
// handleReport answers POST wafprotocol.ReportPath: it finishes the request
|
||||
// the report names, if it holds that request's decision, and ignores the
|
||||
// report otherwise.
|
||||
func (h *handler) handleReport() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
report, err := wafprotocol.ReadReport(w, r)
|
||||
if err != nil {
|
||||
h.processLog.Warn("a report is refused", "error", err.Error())
|
||||
http.Error(w, "the body is not a report", http.StatusBadRequest)
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
held, found := h.awaitingReport.Peek(report.RequestID)
|
||||
// Remove reports true to one report only, of several that name the
|
||||
// id. A question still being handled is held as nil.
|
||||
if found && held != nil &&
|
||||
h.awaitingReport.Remove(report.RequestID) {
|
||||
h.finishWith(r.Context(), held, report)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
peer := peerAddress(r)
|
||||
|
||||
rq := h.startRequest(w, r, peer, peer, false)
|
||||
defer rq.finish()
|
||||
|
||||
if r.Method == http.MethodGet && r.URL.Path == wafprotocol.HealthPath {
|
||||
rq.answerHealth()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.answerAdmin()
|
||||
}
|
||||
|
||||
// takeReport answers POST wafprotocol.ReportPath: it finishes the request the
|
||||
// report names, if it holds that request's decision, and ignores the report
|
||||
// otherwise.
|
||||
func (h *handler) takeReport(w http.ResponseWriter, r *http.Request) {
|
||||
report, err := wafprotocol.ReadReport(w, r)
|
||||
if err != nil {
|
||||
h.processLog.Warn("a report is refused", "error", err.Error())
|
||||
http.Error(w, "the body is not a report", http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
held, found := h.awaitingReport.Peek(report.RequestID)
|
||||
// Remove reports true to one report only, of several that name the id. A
|
||||
// question still being handled is held as nil.
|
||||
if found && held != nil && h.awaitingReport.Remove(report.RequestID) {
|
||||
h.finishWith(r.Context(), held, report)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// finishWith finishes a request with what its report says, and writes the
|
||||
|
||||
Reference in new issue
Block a user