Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9bc631315d | ||
|
|
1002433c35 | ||
|
|
805c64a256 |
No files matched your search
@@ -906,9 +906,11 @@ 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/state`, `internal/requestlog`, `internal/remotelog` and
|
||||
`internal/metrics`: the state and cache files, the log lines, the syslog
|
||||
sender and the metrics.
|
||||
- `internal/state`: the state and cache files.
|
||||
- `internal/requestlog`: the request log.
|
||||
- `internal/processlog`: the process's own messages.
|
||||
- `internal/remotelog`: the syslog sender.
|
||||
- `internal/metrics`: the metrics.
|
||||
|
||||
## Rationale
|
||||
|
||||
|
||||
@@ -104,8 +104,10 @@ never as a time.
|
||||
its own.
|
||||
- `internal/state`: reads the state and cache files at start, takes in edits
|
||||
while running, and writes the files when due and at the stop.
|
||||
- `internal/requestlog` and `internal/remotelog`: the lines on stdout, and
|
||||
sending them to the syslog server.
|
||||
- `internal/requestlog`: the request log, as JSON lines on stdout.
|
||||
- `internal/processlog`: the process lines on stdout, which hold the
|
||||
process's own messages.
|
||||
- `internal/remotelog`: sends the lines on stdout to the syslog server.
|
||||
- `internal/metrics`: the metrics, in the Prometheus text format.
|
||||
- `Dockerfile`: the lint and test phases, then the image.
|
||||
`deploy/example-app` is an app built on the image, with `rproxy` in its
|
||||
|
||||
+93
-32
@@ -75,7 +75,7 @@ type WAF struct {
|
||||
BytesLimitPerMinute int64
|
||||
BytesLimitPerHour int64
|
||||
BytesLimitPerDay int64
|
||||
BytesCounted string
|
||||
BytesCounted BytesCounted
|
||||
// LookupSource is where each client's AS number and country are looked
|
||||
// up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere.
|
||||
// LookupDBPath is the lookup database, the IPinfo Lite file looked up in
|
||||
@@ -86,7 +86,7 @@ type WAF struct {
|
||||
// AddLookupHeaders is true when the app is passed the client's AS number
|
||||
// and country in headers (SWW_WAF_ADD_LOOKUP_HEADERS), by default unless
|
||||
// LookupSource is off.
|
||||
LookupSource string
|
||||
LookupSource LookupSource
|
||||
LookupDBPath string
|
||||
ASNAndCountryLookupTimeout time.Duration
|
||||
AddLookupHeaders bool
|
||||
@@ -131,7 +131,7 @@ type WAF struct {
|
||||
// BlocklistLimitPercent is the percentage of every limit it gets.
|
||||
BlocklistURLs []string
|
||||
BlocklistRefresh time.Duration
|
||||
BlocklistAction string
|
||||
BlocklistAction ReputationAction
|
||||
BlocklistLimitPercent int64
|
||||
// DNSBLZones are the DNSBL zones clients are asked about
|
||||
// (SWW_WAF_DNSBL_ZONES), through DNSBLResolver (SWW_WAF_DNSBL_RESOLVER), or
|
||||
@@ -154,7 +154,7 @@ type WAF struct {
|
||||
AbuseIPDBKey string
|
||||
AbuseIPDBMinScore int64
|
||||
AbuseIPDBDailyBudget int
|
||||
DNSBLAbuseIPDBAction string
|
||||
DNSBLAbuseIPDBAction ReputationAction
|
||||
DNSBLAbuseIPDBLimitPercent int64
|
||||
IPReputationCacheTTL time.Duration
|
||||
DNSBLAbuseIPDBTimeout time.Duration
|
||||
@@ -237,7 +237,7 @@ type WAF struct {
|
||||
// reads none.
|
||||
// CoreRuleSetExclusionsFile is the path of the file of its exclusions
|
||||
// (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while there is none.
|
||||
CoreRuleSetMode string
|
||||
CoreRuleSetMode CoreRuleSetMode
|
||||
CoreRuleSetParanoiaLevel int
|
||||
CoreRuleSetScoreThreshold int
|
||||
CoreRuleSetDisabledRules []int
|
||||
@@ -316,20 +316,66 @@ type WAF struct {
|
||||
settings []slog.Attr
|
||||
}
|
||||
|
||||
// BytesCounted is which body bytes count toward the byte limits and the
|
||||
// byte anomaly thresholds, a value of SWW_WAF_BYTES_COUNTED.
|
||||
type BytesCounted string
|
||||
|
||||
// The values of SWW_WAF_BYTES_COUNTED.
|
||||
const (
|
||||
// BytesCountedResponse counts the response's body bytes.
|
||||
BytesCountedResponse BytesCounted = "response"
|
||||
// BytesCountedRequest counts the request's body bytes.
|
||||
BytesCountedRequest BytesCounted = "request"
|
||||
// BytesCountedBoth counts the body bytes of both.
|
||||
BytesCountedBoth BytesCounted = "both"
|
||||
)
|
||||
|
||||
// LookupSource is where each client's AS number and country are looked up,
|
||||
// a value of SWW_WAF_LOOKUP_SOURCE.
|
||||
type LookupSource string
|
||||
|
||||
// The values of SWW_WAF_LOOKUP_SOURCE.
|
||||
const (
|
||||
// LookupSourceGeoJS asks GeoJS.
|
||||
LookupSourceGeoJS LookupSource = "geojs"
|
||||
// LookupSourceFile looks clients up in the lookup database, the file
|
||||
// SWW_WAF_LOOKUP_DB_PATH names.
|
||||
LookupSourceFile LookupSource = "file"
|
||||
// LookupSourceOff looks no client up.
|
||||
LookupSourceOff LookupSource = "off"
|
||||
)
|
||||
|
||||
// ReputationAction is what is done with a client a reputation source
|
||||
// lists, a value of SWW_WAF_BLOCKLIST_ACTION and of
|
||||
// SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
||||
type ReputationAction string
|
||||
|
||||
// The values of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
||||
const (
|
||||
// ReputationActionDeny refuses the client's requests.
|
||||
ReputationActionDeny ReputationAction = "deny"
|
||||
// ReputationActionLimit gives the client a percentage of every limit,
|
||||
// written limit:<percent> in the setting.
|
||||
ReputationActionLimit ReputationAction = "limit"
|
||||
// ReputationActionLog only notes the hit in the log line and raises an
|
||||
// alert.
|
||||
ReputationActionLog ReputationAction = "log"
|
||||
)
|
||||
|
||||
// CoreRuleSetMode is what the Core Rule Set does, a value of
|
||||
// SWW_WAF_CORE_RULE_SET_MODE.
|
||||
type CoreRuleSetMode string
|
||||
|
||||
// The values of SWW_WAF_CORE_RULE_SET_MODE.
|
||||
const (
|
||||
// CoreRuleSetModeOff runs no request through the Core Rule Set.
|
||||
CoreRuleSetModeOff = off
|
||||
CoreRuleSetModeOff CoreRuleSetMode = "off"
|
||||
// CoreRuleSetModeDetect logs and alerts a match, and refuses nothing.
|
||||
CoreRuleSetModeDetect = "detect"
|
||||
CoreRuleSetModeDetect CoreRuleSetMode = "detect"
|
||||
// CoreRuleSetModeBlock refuses a match with 403.
|
||||
CoreRuleSetModeBlock = "block"
|
||||
CoreRuleSetModeBlock CoreRuleSetMode = "block"
|
||||
)
|
||||
|
||||
// fileSource is the SWW_WAF_LOOKUP_SOURCE that looks clients up in the
|
||||
// lookup database, the file SWW_WAF_LOOKUP_DB_PATH names.
|
||||
const fileSource = "file"
|
||||
|
||||
const (
|
||||
day = 24 * time.Hour
|
||||
ipv4Bits = 32
|
||||
@@ -558,10 +604,13 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
),
|
||||
BytesLimitPerHour: env.size("SWW_WAF_BYTES_LIMIT_PER_HOUR", "50G"),
|
||||
BytesLimitPerDay: env.size("SWW_WAF_BYTES_LIMIT_PER_DAY", "100G"),
|
||||
BytesCounted: env.bytesCounted("SWW_WAF_BYTES_COUNTED", "both"),
|
||||
BytesCounted: env.bytesCounted(
|
||||
"SWW_WAF_BYTES_COUNTED",
|
||||
BytesCountedBoth,
|
||||
),
|
||||
LookupSource: env.lookupSource(
|
||||
"SWW_WAF_LOOKUP_SOURCE",
|
||||
"geojs",
|
||||
LookupSourceGeoJS,
|
||||
),
|
||||
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
|
||||
ASNAndCountryLookupTimeout: env.milliseconds(
|
||||
@@ -758,9 +807,9 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
cfg.InstanceName, cfg.LogRemoteURL != nil)
|
||||
// The app gets the headers by default, unless no client is looked up.
|
||||
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
|
||||
strconv.FormatBool(cfg.LookupSource != off))
|
||||
strconv.FormatBool(cfg.LookupSource != LookupSourceOff))
|
||||
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
|
||||
"SWW_WAF_BLOCKLIST_ACTION", "deny")
|
||||
"SWW_WAF_BLOCKLIST_ACTION", string(ReputationActionDeny))
|
||||
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
|
||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25")
|
||||
|
||||
@@ -858,9 +907,12 @@ func (e *environment) count(name, defaultValue string) int64 {
|
||||
|
||||
// bytesCounted reads the setting that is which body bytes count toward the
|
||||
// byte limits and the byte anomaly thresholds: response, request or both.
|
||||
func (e *environment) bytesCounted(name, defaultValue string) string {
|
||||
value := e.value(name, defaultValue)
|
||||
if value != "response" && value != "request" && value != "both" {
|
||||
func (e *environment) bytesCounted(
|
||||
name string, defaultValue BytesCounted,
|
||||
) BytesCounted {
|
||||
value := BytesCounted(e.value(name, string(defaultValue)))
|
||||
if value != BytesCountedResponse && value != BytesCountedRequest &&
|
||||
value != BytesCountedBoth {
|
||||
e.check(name, fmt.Errorf("%q %w", value, errNotBytesCounted))
|
||||
}
|
||||
|
||||
@@ -905,8 +957,10 @@ func (e *environment) trapPaths(name string) []string {
|
||||
|
||||
// coreRuleSetMode reads the setting that is what the Core Rule Set does: off,
|
||||
// detect or block.
|
||||
func (e *environment) coreRuleSetMode(name, defaultValue string) string {
|
||||
mode := e.value(name, defaultValue)
|
||||
func (e *environment) coreRuleSetMode(
|
||||
name string, defaultValue CoreRuleSetMode,
|
||||
) CoreRuleSetMode {
|
||||
mode := CoreRuleSetMode(e.value(name, string(defaultValue)))
|
||||
if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect &&
|
||||
mode != CoreRuleSetModeBlock {
|
||||
e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode))
|
||||
@@ -1056,20 +1110,24 @@ func (e *environment) refresh(name, defaultValue string) time.Duration {
|
||||
// action reads a setting that is what is done with a client a blocklist
|
||||
// or a DNSBL zone lists: deny, log, or limit:<percent>, which it returns
|
||||
// as limit and the percentage.
|
||||
func (e *environment) action(name, defaultValue string) (string, int64) {
|
||||
value := e.value(name, defaultValue)
|
||||
if value == "deny" || value == "log" {
|
||||
func (e *environment) action(
|
||||
name, defaultValue string,
|
||||
) (ReputationAction, int64) {
|
||||
value := ReputationAction(e.value(name, defaultValue))
|
||||
if value == ReputationActionDeny || value == ReputationActionLog {
|
||||
return value, 0
|
||||
}
|
||||
|
||||
percentText, isLimit := strings.CutPrefix(value, "limit:")
|
||||
percentText, isLimit := strings.CutPrefix(
|
||||
string(value), string(ReputationActionLimit)+":",
|
||||
)
|
||||
|
||||
percent, err := ParsePercent(percentText)
|
||||
if !isLimit || err != nil {
|
||||
e.check(name, fmt.Errorf("%q %w", value, errNotAction))
|
||||
}
|
||||
|
||||
return "limit", percent
|
||||
return ReputationActionLimit, percent
|
||||
}
|
||||
|
||||
// zones reads the setting that is the list of DNSBL zones. It is empty by
|
||||
@@ -1120,9 +1178,12 @@ func (e *environment) routeWatchURL(name string) string {
|
||||
|
||||
// lookupSource reads the setting that is where clients are looked up:
|
||||
// geojs, file, or off.
|
||||
func (e *environment) lookupSource(name, defaultValue string) string {
|
||||
source := e.value(name, defaultValue)
|
||||
if source != "geojs" && source != fileSource && source != off {
|
||||
func (e *environment) lookupSource(
|
||||
name string, defaultValue LookupSource,
|
||||
) LookupSource {
|
||||
source := LookupSource(e.value(name, string(defaultValue)))
|
||||
if source != LookupSourceGeoJS && source != LookupSourceFile &&
|
||||
source != LookupSourceOff {
|
||||
e.check(name, fmt.Errorf("%q %w", source, errNotLookupSource))
|
||||
}
|
||||
|
||||
@@ -1134,12 +1195,12 @@ func (e *environment) lookupSource(name, defaultValue string) string {
|
||||
// one source at a time.
|
||||
func (e *environment) checkLookupDBPath(cfg *WAF) {
|
||||
switch {
|
||||
case cfg.LookupSource == fileSource && cfg.LookupDBPath == "":
|
||||
case cfg.LookupSource == LookupSourceFile && cfg.LookupDBPath == "":
|
||||
e.check("SWW_WAF_LOOKUP_SOURCE", fmt.Errorf(
|
||||
"is file while SWW_WAF_LOOKUP_DB_PATH is unset; %w",
|
||||
errNeedsDBPath,
|
||||
))
|
||||
case cfg.LookupSource != fileSource && cfg.LookupDBPath != "":
|
||||
case cfg.LookupSource != LookupSourceFile && cfg.LookupDBPath != "":
|
||||
e.check("SWW_WAF_LOOKUP_DB_PATH", fmt.Errorf(
|
||||
"is set while SWW_WAF_LOOKUP_SOURCE is %s; %w",
|
||||
cfg.LookupSource,
|
||||
@@ -1164,7 +1225,7 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.LookupSource != off {
|
||||
if cfg.LookupSource != LookupSourceOff {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -614,7 +614,7 @@ func TestCoreRuleSetSettings(t *testing.T) {
|
||||
},
|
||||
{
|
||||
environment{
|
||||
coreRuleSetMode: config.CoreRuleSetModeDetect,
|
||||
coreRuleSetMode: string(config.CoreRuleSetModeDetect),
|
||||
coreRuleSetParanoiaLevel: "4",
|
||||
coreRuleSetScoreThreshold: "10",
|
||||
coreRuleSetDisabledRules: "942100, 920350",
|
||||
@@ -2033,8 +2033,9 @@ func TestReputationSettingsAsSet(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
value, action string
|
||||
percent int64
|
||||
value string
|
||||
action config.ReputationAction
|
||||
percent int64
|
||||
}{
|
||||
{actionDeny, actionDeny, 0},
|
||||
{actionLog, actionLog, 0},
|
||||
@@ -2167,7 +2168,7 @@ func TestDNSBLSettingsAsSet(t *testing.T) {
|
||||
// want are the zones, resolver, action and percent config.WAF gives.
|
||||
want []string
|
||||
wantResolver string
|
||||
wantAction string
|
||||
wantAction config.ReputationAction
|
||||
wantPercent int64
|
||||
}{
|
||||
{
|
||||
@@ -3049,7 +3050,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
|
||||
logLevel: "info",
|
||||
ruleFilesDir: "/etc/sww/rules.d",
|
||||
ruleFilesEnabled: "true",
|
||||
coreRuleSetMode: config.CoreRuleSetModeBlock,
|
||||
coreRuleSetMode: string(config.CoreRuleSetModeBlock),
|
||||
coreRuleSetParanoiaLevel: "1",
|
||||
coreRuleSetScoreThreshold: "5",
|
||||
coreRuleSetDisabledRules: defaultCoreRuleSetDisabledRules,
|
||||
|
||||
+14
-24
@@ -84,12 +84,7 @@ type Params struct {
|
||||
// one request to GeoJS is under way at a time, and it asks about every
|
||||
// client waiting, up to maxPerRequest. It is safe for concurrent use.
|
||||
type GeoJS struct {
|
||||
url string
|
||||
answered func(Answer)
|
||||
now func() time.Time
|
||||
asked func()
|
||||
failed func(err error, askingAgainIn time.Duration)
|
||||
wentWithoutAnswer func()
|
||||
params Params
|
||||
// httpClient follows no redirect, so that visitors' addresses go to
|
||||
// GeoJS alone: a redirect is a failure.
|
||||
httpClient *http.Client
|
||||
@@ -140,12 +135,7 @@ func New(params Params) *GeoJS {
|
||||
}
|
||||
|
||||
return &GeoJS{
|
||||
url: params.URL,
|
||||
answered: params.Answered,
|
||||
now: params.Now,
|
||||
asked: params.Asked,
|
||||
failed: params.Failed,
|
||||
wentWithoutAnswer: params.WentWithoutAnswer,
|
||||
params: params,
|
||||
httpClient: &http.Client{
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
@@ -188,7 +178,7 @@ func (g *GeoJS) LookUp(
|
||||
|
||||
answer, found := g.kept(client)
|
||||
if !found {
|
||||
g.wentWithoutAnswer()
|
||||
g.params.WentWithoutAnswer()
|
||||
}
|
||||
|
||||
w, waiting := g.waiting[client]
|
||||
@@ -251,7 +241,7 @@ func (g *GeoJS) Load(answers []Answer) {
|
||||
|
||||
g.answers.Purge()
|
||||
|
||||
now := g.now()
|
||||
now := g.params.Now()
|
||||
|
||||
for _, answer := range answers {
|
||||
if now.Sub(answer.Answered) < keepFor {
|
||||
@@ -288,7 +278,7 @@ func (g *GeoJS) answerOrWait(
|
||||
}
|
||||
|
||||
if w == nil {
|
||||
g.wentWithoutAnswer()
|
||||
g.params.WentWithoutAnswer()
|
||||
|
||||
return Answer{}, nil // too many clients wait already
|
||||
}
|
||||
@@ -299,7 +289,7 @@ func (g *GeoJS) answerOrWait(
|
||||
}
|
||||
|
||||
if w.late {
|
||||
g.wentWithoutAnswer()
|
||||
g.params.WentWithoutAnswer()
|
||||
|
||||
return Answer{}, nil
|
||||
}
|
||||
@@ -310,7 +300,7 @@ func (g *GeoJS) answerOrWait(
|
||||
// kept returns client's answer, if GeoJS gave it less than keepFor ago,
|
||||
// and notes that it was used.
|
||||
func (g *GeoJS) kept(client netip.Prefix) (Answer, bool) {
|
||||
now := g.now()
|
||||
now := g.params.Now()
|
||||
|
||||
kept, found := g.answers.Get(client)
|
||||
if !found || now.Sub(kept.Answered) >= keepFor {
|
||||
@@ -327,7 +317,7 @@ func (g *GeoJS) kept(client netip.Prefix) (Answer, bool) {
|
||||
// GeoJS are for every client waiting, so they go on when the caller whose
|
||||
// ctx is given stops waiting.
|
||||
func (g *GeoJS) ask(ctx context.Context) {
|
||||
if g.asking || g.now().Before(g.retryAt) {
|
||||
if g.asking || g.params.Now().Before(g.retryAt) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -349,9 +339,9 @@ func (g *GeoJS) askAboutWaiting(ctx context.Context) {
|
||||
given, err := g.request(ctx, clients)
|
||||
kept, answered := g.keep(clients, given, err)
|
||||
|
||||
if g.answered != nil {
|
||||
if g.params.Answered != nil {
|
||||
for _, answer := range kept {
|
||||
g.answered(answer)
|
||||
g.params.Answered(answer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +390,7 @@ func (g *GeoJS) keep(
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
now := g.now()
|
||||
now := g.params.Now()
|
||||
kept := make([]Answer, 0, len(clients))
|
||||
leftOut := 0
|
||||
|
||||
@@ -444,7 +434,7 @@ func (g *GeoJS) keep(
|
||||
w.late = true
|
||||
}
|
||||
|
||||
g.failed(err, g.retryDelay)
|
||||
g.params.Failed(err, g.retryDelay)
|
||||
|
||||
return kept, false
|
||||
}
|
||||
@@ -474,7 +464,7 @@ func (g *GeoJS) request(
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
g.url,
|
||||
g.params.URL,
|
||||
http.NoBody,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -483,7 +473,7 @@ func (g *GeoJS) request(
|
||||
|
||||
req.URL.RawQuery = "ip=" + strings.Join(addrs, ",")
|
||||
|
||||
g.asked()
|
||||
g.params.Asked()
|
||||
|
||||
res, err := g.httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
||||
+266
-391
@@ -16,33 +16,12 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"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.
|
||||
@@ -58,10 +37,12 @@ type Metrics struct {
|
||||
rateLimitHits *prometheus.CounterVec
|
||||
offences *prometheus.CounterVec
|
||||
coreRuleSetMatches *prometheus.CounterVec
|
||||
ruleMatches *prometheus.CounterVec
|
||||
reputationHits *prometheus.CounterVec
|
||||
countries *busiest
|
||||
asns *busiest
|
||||
// ruleMatches are made by AddRules, and reputationHits by
|
||||
// AddReputation.
|
||||
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
|
||||
@@ -79,18 +60,20 @@ type Metrics struct {
|
||||
stateFileEditsSetAside *prometheus.CounterVec
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
//
|
||||
//nolint:funlen // a few lines for each metric, a list that grows with them
|
||||
func New(params Params) *Metrics {
|
||||
func New(busiestCount int, instanceName string) *Metrics {
|
||||
byStatus := []string{"status_class", "action"}
|
||||
byFile := []string{"file"}
|
||||
registry := prometheus.NewRegistry()
|
||||
|
||||
m := &Metrics{
|
||||
registry: prometheus.WrapRegistererWith(
|
||||
prometheus.Labels{"instance": params.InstanceName}, registry),
|
||||
prometheus.Labels{"instance": instanceName}, registry),
|
||||
handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
||||
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "sww_requests_in_flight",
|
||||
@@ -131,19 +114,8 @@ func New(params Params) *Metrics {
|
||||
"and the rule's id.",
|
||||
[]string{"mode", "rule_id"},
|
||||
),
|
||||
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),
|
||||
countries: newCountries(busiestCount),
|
||||
asns: newASNs(busiestCount),
|
||||
GeoJSRequests: prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "sww_geojs_requests_total",
|
||||
Help: "Requests to GeoJS.",
|
||||
@@ -195,7 +167,6 @@ func New(params Params) *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,
|
||||
@@ -203,45 +174,259 @@ func New(params Params) *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
|
||||
// config.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: config.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)
|
||||
@@ -311,12 +496,6 @@ 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) {
|
||||
@@ -348,310 +527,6 @@ 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 config.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: config.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(
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Package processlog builds the logger for the process's own messages,
|
||||
// which it writes as JSON lines marked "type":"process". The request log
|
||||
// is package requestlog's.
|
||||
package processlog
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
)
|
||||
|
||||
// Params are what New needs.
|
||||
type Params struct {
|
||||
// Output is where the lines are written.
|
||||
Output io.Writer
|
||||
// InstanceName, SWW_WAF_INSTANCE_NAME, is each line's instance.
|
||||
InstanceName string
|
||||
// Level, SWW_WAF_LOG_LEVEL, is the least severe level written.
|
||||
Level slog.Level
|
||||
}
|
||||
|
||||
// New returns the logger for the process's own messages: JSON lines on
|
||||
// params.Output, marked "type":"process", with the time in the same form
|
||||
// as a request line's. It writes only the messages at params.Level or more
|
||||
// severe; the request lines requestlog.Write writes are never held back.
|
||||
func New(params Params) *slog.Logger {
|
||||
handler := slog.NewJSONHandler(params.Output, &slog.HandlerOptions{
|
||||
Level: params.Level,
|
||||
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
|
||||
if attr.Key == slog.TimeKey && len(groups) == 0 {
|
||||
return slog.String(
|
||||
slog.TimeKey,
|
||||
requestlog.FormatTime(attr.Value.Time()),
|
||||
)
|
||||
}
|
||||
|
||||
return attr
|
||||
},
|
||||
})
|
||||
|
||||
return slog.New(handler).
|
||||
With("type", "process", "instance", params.InstanceName)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package processlog_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
)
|
||||
|
||||
// instanceName is the instance the tests' loggers are built with.
|
||||
const instanceName = "host1/blog"
|
||||
|
||||
func TestProcessLinesAreMarkedProcessAndGiveTheInstance(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
processlog.New(processlog.Params{
|
||||
Output: &out,
|
||||
InstanceName: instanceName,
|
||||
Level: slog.LevelInfo,
|
||||
}).
|
||||
Info("starting",
|
||||
"version", "v1")
|
||||
|
||||
var fields map[string]any
|
||||
|
||||
err := json.Unmarshal(out.Bytes(), &fields)
|
||||
if err != nil {
|
||||
t.Fatalf("decode %q: %v", out.String(), err)
|
||||
}
|
||||
|
||||
if fields["type"] != "process" ||
|
||||
fields["instance"] != "host1/blog" ||
|
||||
fields["msg"] != "starting" ||
|
||||
fields["level"] != "INFO" ||
|
||||
fields["version"] != "v1" {
|
||||
t.Errorf("process line %v", fields)
|
||||
}
|
||||
|
||||
timeText, _ := fields["time"].(string)
|
||||
|
||||
logged, err := time.Parse(time.RFC3339, timeText)
|
||||
if err != nil || !strings.HasSuffix(timeText, "Z") ||
|
||||
len(timeText) != len("2006-01-02T15:04:05.000Z") ||
|
||||
time.Since(logged) > time.Minute {
|
||||
t.Errorf(
|
||||
"process line time %q, want now in UTC with milliseconds",
|
||||
timeText,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessLoggerWritesTheMessagesAtItsLevelOrMoreSevere(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
levels := []slog.Level{
|
||||
slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError,
|
||||
}
|
||||
|
||||
for i, level := range levels {
|
||||
t.Run(level.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
processLog := processlog.New(processlog.Params{
|
||||
Output: &out,
|
||||
InstanceName: instanceName,
|
||||
Level: level,
|
||||
})
|
||||
for _, at := range levels {
|
||||
processLog.Log(t.Context(), at, "message")
|
||||
}
|
||||
|
||||
var got, want []string
|
||||
|
||||
for line := range strings.Lines(out.String()) {
|
||||
var fields struct {
|
||||
Level string `json:"level"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal([]byte(line), &fields)
|
||||
if err != nil {
|
||||
t.Fatalf("decode %q: %v", line, err)
|
||||
}
|
||||
|
||||
got = append(got, fields.Level)
|
||||
}
|
||||
|
||||
for _, written := range levels[i:] {
|
||||
want = append(want, written.String())
|
||||
}
|
||||
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("lines at %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func TestHistoryKeepsEveryRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestHistoryKeepsEveryRequest(t *testing.T) {
|
||||
func TestLookupReachesTheHistoryOfAClientInTheTable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
other := netip.MustParsePrefix("198.51.100.7/32")
|
||||
start := midnight()
|
||||
@@ -100,7 +100,10 @@ func TestLookupReachesTheHistoryOfAClientInTheTable(t *testing.T) {
|
||||
func TestResetKeepsTheHistory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{PerMinute: limit}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -123,7 +126,7 @@ func TestResetKeepsTheHistory(t *testing.T) {
|
||||
func TestRequestsAddsUpTheClientsInsideTheNetblock(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
|
||||
for client, requests := range map[string]int{
|
||||
"198.51.100.9/32": 2,
|
||||
|
||||
@@ -156,16 +156,28 @@ type Request struct {
|
||||
BlockedByCoreRuleSet bool
|
||||
}
|
||||
|
||||
// New returns a Limiter for limits, with no client counted yet, whose
|
||||
// table holds at most maxClients clients (SWW_WAF_MAX_TRACKED_CLIENTS). Past
|
||||
// it, the least recently seen client is dropped, with its history, and
|
||||
// starts afresh if it comes back.
|
||||
func New(limits Limits, maxClients int) *Limiter {
|
||||
clients, err := simplelru.NewLRU[netip.Prefix, *Client](maxClients, nil)
|
||||
// Params are what New needs.
|
||||
type Params struct {
|
||||
// Limits are what each client's requests and bytes are counted against.
|
||||
Limits Limits
|
||||
// MaxClients is the most clients the table holds,
|
||||
// SWW_WAF_MAX_TRACKED_CLIENTS. Past it, the least recently seen client
|
||||
// is dropped, with its history, and starts afresh if it comes back.
|
||||
MaxClients int
|
||||
}
|
||||
|
||||
// New returns a Limiter with no client counted yet.
|
||||
func New(params Params) *Limiter {
|
||||
clients, err := simplelru.NewLRU[netip.Prefix, *Client](
|
||||
params.MaxClients,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err) // NewLRU fails only for a size below one
|
||||
}
|
||||
|
||||
limits := params.Limits
|
||||
|
||||
return &Limiter{
|
||||
windows: [3]window{
|
||||
{
|
||||
|
||||
@@ -33,10 +33,10 @@ func TestOverARateLimitSaysWhatTheNextRequestWouldDoAndCountsNothing(
|
||||
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
now := midnight()
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: limit, PerDay: limit + 1},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit, PerDay: limit + 1},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
|
||||
if limiter.OverARateLimit(client, now) {
|
||||
t.Error("a client never seen is over a rate limit")
|
||||
@@ -72,7 +72,7 @@ func TestOverARateLimitSaysWhatTheNextRequestWouldDoAndCountsNothing(
|
||||
wantCount(t, limiter, client, now, minute)
|
||||
|
||||
// A limit that is off is never gone over.
|
||||
off := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
off := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
off.Count(client, now, whole)
|
||||
|
||||
if off.OverARateLimit(client, now) {
|
||||
@@ -95,7 +95,10 @@ func TestEachWindowRefusesAtItsLimitAndLetsTheClientBack(t *testing.T) {
|
||||
t.Run(tc.window, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(tc.limits, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: tc.limits,
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
quarter := tc.length / 4
|
||||
@@ -126,10 +129,10 @@ func TestEachWindowRefusesAtItsLimitAndLetsTheClientBack(t *testing.T) {
|
||||
func TestHitGivesTheLimitAndTheRequestsCounted(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: limit, PerHour: limit},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit, PerHour: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -159,10 +162,13 @@ func TestHitGivesTheLimitAndTheRequestsCounted(t *testing.T) {
|
||||
func TestClientGetsItsPercentageOfEachLimitRoundedDown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: 5, BytesPerDay: math.MaxInt64},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{
|
||||
PerMinute: 5,
|
||||
BytesPerDay: math.MaxInt64,
|
||||
},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -203,10 +209,10 @@ func TestZeroPercentIsAZeroAllowanceAndALimitOffStaysOff(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Only the hour has limits: the minute's and the day's are off.
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerHour: limit, BytesPerHour: 1000},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerHour: limit, BytesPerHour: 1000},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -253,7 +259,10 @@ func TestEachByteLimitIsBrokenByTheBytesCounted(t *testing.T) {
|
||||
t.Run(tc.window, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(tc.limits, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: tc.limits,
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
// 600 bytes are within the limit, 600 more over it.
|
||||
@@ -287,10 +296,10 @@ func TestEachByteLimitIsBrokenByTheBytesCounted(t *testing.T) {
|
||||
func TestALimitIsBrokenOnlyByWhatIsAddedToIt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: 2, BytesPerMinute: 1000},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: 2, BytesPerMinute: 1000},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
other := netip.MustParsePrefix("203.0.113.10/32")
|
||||
start := midnight()
|
||||
@@ -314,7 +323,7 @@ func TestALimitIsBrokenOnlyByWhatIsAddedToIt(t *testing.T) {
|
||||
func TestCountGivesTheBytesInEachWindow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -340,7 +349,10 @@ func TestCountGivesTheBytesInEachWindow(t *testing.T) {
|
||||
func TestResetSetsTheBytesBackToZero(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{BytesPerDay: 1000}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{BytesPerDay: 1000},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -356,7 +368,7 @@ func TestRefusalsOverTheLimitInAMinuteBreakTheRefusalLimit(
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -411,7 +423,7 @@ func TestRefusalsOverTheLimitInAMinuteBreakTheRefusalLimit(
|
||||
func TestCountGivesTheRequestsInEachWindow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -438,10 +450,10 @@ func TestCountGivesTheRequestsInEachWindow(t *testing.T) {
|
||||
func TestResetSetsTheCountsBackToZero(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: limit, PerDay: limit},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit, PerDay: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -463,7 +475,10 @@ func TestResetSetsTheCountsBackToZero(t *testing.T) {
|
||||
func TestClientBackAfterAWholeBucketIsWithinTheLimitAtOnce(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{PerHour: limit}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerHour: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -482,10 +497,10 @@ func TestClientBackAfterAWholeBucketIsWithinTheLimitAtOnce(t *testing.T) {
|
||||
func TestRefusedRequestsCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(
|
||||
ratelimit.Limits{PerMinute: limit, PerHour: 2 * limit},
|
||||
tableSize,
|
||||
)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit, PerHour: 2 * limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
refused := netip.MustParsePrefix("203.0.113.9/32")
|
||||
within := netip.MustParsePrefix("203.0.113.10/32")
|
||||
start := midnight()
|
||||
@@ -518,7 +533,10 @@ func TestRefusedRequestsCount(t *testing.T) {
|
||||
func TestRequestCountedLateGoesInTheBucketUnderWay(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{PerMinute: limit}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -534,7 +552,10 @@ func TestRequestCountedLateGoesInTheBucketUnderWay(t *testing.T) {
|
||||
func TestClockSetBackStartsTheBucketsAfresh(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{PerHour: limit}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerHour: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
@@ -562,7 +583,10 @@ func TestKeepsAtMostMaxClientsDroppingTheLeastRecentlySeen(t *testing.T) {
|
||||
|
||||
const maxClients = 3
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{PerMinute: 1}, maxClients)
|
||||
limiter := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerMinute: 1},
|
||||
MaxClients: maxClients,
|
||||
})
|
||||
now := midnight()
|
||||
|
||||
clients := make([]netip.Prefix, maxClients+1)
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestSnapshotListsTheClientsByAddress(t *testing.T) {
|
||||
"2001:db8::/64",
|
||||
}
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
for _, i := range []int{2, 3, 0, 1} {
|
||||
limiter.Count(netip.MustParsePrefix(want[i]), midnight(), whole)
|
||||
}
|
||||
@@ -52,7 +52,10 @@ func TestLoadedCountsCarryOn(t *testing.T) {
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
before := ratelimit.New(ratelimit.Limits{PerHour: limit}, tableSize)
|
||||
before := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerHour: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
for range limit {
|
||||
wantCount(t, before, client, start, "")
|
||||
}
|
||||
@@ -60,7 +63,10 @@ func TestLoadedCountsCarryOn(t *testing.T) {
|
||||
// Loaded into a new limiter, as across a restart, the client has no
|
||||
// fresh allowance.
|
||||
later := start.Add(time.Minute)
|
||||
after := ratelimit.New(ratelimit.Limits{PerHour: limit}, tableSize)
|
||||
after := ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{PerHour: limit},
|
||||
MaxClients: tableSize,
|
||||
})
|
||||
after.Load(before.Snapshot(), later)
|
||||
wantCount(t, after, client, later, hour)
|
||||
}
|
||||
@@ -71,7 +77,7 @@ func TestLoadEmptiesBucketsWhoseTimeHasPassed(t *testing.T) {
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
start := midnight()
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
limiter.Count(client, start, whole)
|
||||
limiter.CountBytes(client, start, 5, whole)
|
||||
limiter.CountRefusal(client, start, limit)
|
||||
@@ -80,7 +86,7 @@ func TestLoadEmptiesBucketsWhoseTimeHasPassed(t *testing.T) {
|
||||
loaded := func(now time.Time) ratelimit.Client {
|
||||
t.Helper()
|
||||
|
||||
after := ratelimit.New(ratelimit.Limits{}, tableSize)
|
||||
after := ratelimit.New(ratelimit.Params{MaxClients: tableSize})
|
||||
after.Load(limiter.Snapshot(), now)
|
||||
|
||||
return after.Snapshot()[0]
|
||||
@@ -142,7 +148,7 @@ func TestLoadDropsTheLeastRecentlySeenFirst(t *testing.T) {
|
||||
addr = addr.Next()
|
||||
}
|
||||
|
||||
limiter := ratelimit.New(ratelimit.Limits{}, maxClients)
|
||||
limiter := ratelimit.New(ratelimit.Params{MaxClients: maxClients})
|
||||
limiter.Load(clients, midnight())
|
||||
|
||||
got := limiter.Snapshot()
|
||||
|
||||
@@ -408,9 +408,12 @@ func TestKeyIsSentInTheKeyHeaderAndNeverShown(t *testing.T) {
|
||||
scores: map[string]int64{suspect: 100},
|
||||
}
|
||||
checker := newAbuseIPDB(abuseIPDB, p)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||
})
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(
|
||||
reputation.New(params()),
|
||||
reputation.NewDNSBL(dnsblParams()),
|
||||
)
|
||||
m.AddAbuseIPDB(checker)
|
||||
|
||||
// One check that AbuseIPDB answers, and one it refuses with an answer
|
||||
// that names the key.
|
||||
@@ -463,9 +466,12 @@ func TestMetricsCountTheChecksTheFailuresAndTheBudgetLeft(t *testing.T) {
|
||||
p := abuseIPDBParams()
|
||||
p.DailyBudget = 5
|
||||
checker := newAbuseIPDB(abuseIPDB, p)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||
})
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(
|
||||
reputation.New(params()),
|
||||
reputation.NewDNSBL(dnsblParams()),
|
||||
)
|
||||
m.AddAbuseIPDB(checker)
|
||||
|
||||
// One check that AbuseIPDB answers, and one that fails.
|
||||
hitFrom(t, checker, suspect, true)
|
||||
@@ -768,9 +774,6 @@ 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()
|
||||
|
||||
@@ -319,9 +319,8 @@ func TestMetricsCountEachZonesQueriesAndThoseThatFailed(t *testing.T) {
|
||||
"99.2.0.192." + otherZone + ".": {rcode: serverFailure},
|
||||
}}
|
||||
dnsbl := newDNSBL(resolver, dnsblParams(zone, otherZone))
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||
})
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(reputation.New(params()), dnsbl)
|
||||
|
||||
wantZones(t, dnsbl, listed)
|
||||
synctest.Wait()
|
||||
@@ -366,9 +365,8 @@ func TestZoneKeyIsMaskedInTheVerdictsTheFailuresAndTheMetrics(t *testing.T) {
|
||||
"99.2.0.192." + keyed + ".": {addrs: []string{listing}},
|
||||
"100.2.0.192." + keyed + ".": {rcode: serverFailure},
|
||||
}}, p)
|
||||
m := metrics.New(metrics.Params{
|
||||
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||
})
|
||||
m := metrics.New(1, "app")
|
||||
m.AddReputation(reputation.New(params()), dnsbl)
|
||||
|
||||
// Both clients are asked about before either answer comes, so that
|
||||
// the failure does not keep the zone from the other query.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Package requestlog writes the lines sww prints on stdout: one
|
||||
// Package requestlog writes the request log sww prints on stdout: one
|
||||
// JSON object per request, two from waf and a short one from rproxy,
|
||||
// marked "type":"request", and the process's own messages as JSON lines marked
|
||||
// "type":"process".
|
||||
// marked "type":"request". Package processlog writes the process's own
|
||||
// messages.
|
||||
package requestlog
|
||||
|
||||
import (
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
@@ -300,31 +299,3 @@ func Milliseconds(d time.Duration) float64 {
|
||||
time.Millisecond/time.Microsecond,
|
||||
)
|
||||
}
|
||||
|
||||
// NewProcessLogger returns the logger for the process's own messages:
|
||||
// JSON lines on w, marked "type":"process", with the time in the same form
|
||||
// as a request line's, and instanceName, SWW_WAF_INSTANCE_NAME, as instance.
|
||||
// It writes only the messages at level, SWW_WAF_LOG_LEVEL, or more severe;
|
||||
// the request lines Write writes are never held back.
|
||||
func NewProcessLogger(
|
||||
w io.Writer,
|
||||
instanceName string,
|
||||
level slog.Level,
|
||||
) *slog.Logger {
|
||||
handler := slog.NewJSONHandler(w, &slog.HandlerOptions{
|
||||
Level: level,
|
||||
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
|
||||
if attr.Key == slog.TimeKey && len(groups) == 0 {
|
||||
return slog.String(
|
||||
slog.TimeKey,
|
||||
FormatTime(attr.Value.Time()),
|
||||
)
|
||||
}
|
||||
|
||||
return attr
|
||||
},
|
||||
})
|
||||
|
||||
return slog.New(handler).
|
||||
With("type", "process", "instance", instanceName)
|
||||
}
|
||||
@@ -3,8 +3,6 @@ package requestlog_test
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -134,88 +132,3 @@ func TestWriteRProxyLineWritesOneJSONLineWithOnlyItsFields(t *testing.T) {
|
||||
t.Errorf("wrote %s, want %s", out.String(), want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessLinesAreMarkedProcessAndGiveTheInstance(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
requestlog.NewProcessLogger(&out, "host1/blog", slog.LevelInfo).
|
||||
Info("starting",
|
||||
"version", "v1")
|
||||
|
||||
var fields map[string]any
|
||||
|
||||
err := json.Unmarshal(out.Bytes(), &fields)
|
||||
if err != nil {
|
||||
t.Fatalf("decode %q: %v", out.String(), err)
|
||||
}
|
||||
|
||||
if fields["type"] != "process" ||
|
||||
fields["instance"] != "host1/blog" ||
|
||||
fields["msg"] != "starting" ||
|
||||
fields["level"] != "INFO" ||
|
||||
fields["version"] != "v1" {
|
||||
t.Errorf("process line %v", fields)
|
||||
}
|
||||
|
||||
timeText, _ := fields["time"].(string)
|
||||
|
||||
logged, err := time.Parse(time.RFC3339, timeText)
|
||||
if err != nil || !strings.HasSuffix(timeText, "Z") ||
|
||||
len(timeText) != len("2006-01-02T15:04:05.000Z") ||
|
||||
time.Since(logged) > time.Minute {
|
||||
t.Errorf(
|
||||
"process line time %q, want now in UTC with milliseconds",
|
||||
timeText,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessLoggerWritesTheMessagesAtItsLevelOrMoreSevere(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
levels := []slog.Level{
|
||||
slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError,
|
||||
}
|
||||
|
||||
for i, level := range levels {
|
||||
t.Run(level.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
|
||||
processLog := requestlog.NewProcessLogger(
|
||||
&out,
|
||||
"host1/blog",
|
||||
level,
|
||||
)
|
||||
for _, at := range levels {
|
||||
processLog.Log(t.Context(), at, "message")
|
||||
}
|
||||
|
||||
var got, want []string
|
||||
|
||||
for line := range strings.Lines(out.String()) {
|
||||
var fields struct {
|
||||
Level string `json:"level"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal([]byte(line), &fields)
|
||||
if err != nil {
|
||||
t.Fatalf("decode %q: %v", line, err)
|
||||
}
|
||||
|
||||
got = append(got, fields.Level)
|
||||
}
|
||||
|
||||
for _, written := range levels[i:] {
|
||||
want = append(want, written.String())
|
||||
}
|
||||
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("lines at %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
@@ -51,11 +51,11 @@ func TestServerHasTheFixedLimits(t *testing.T) {
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: io.Discard,
|
||||
ProcessLog: requestlog.NewProcessLogger(
|
||||
io.Discard,
|
||||
"test",
|
||||
slog.LevelInfo,
|
||||
),
|
||||
ProcessLog: processlog.New(processlog.Params{
|
||||
Output: io.Discard,
|
||||
InstanceName: "test",
|
||||
Level: slog.LevelInfo,
|
||||
}),
|
||||
})
|
||||
|
||||
if server.Addr != ":8080" || server.MaxHeaderBytes != 28<<10 ||
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
@@ -370,11 +370,11 @@ func startServerLogging(
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: requestLog,
|
||||
ProcessLog: requestlog.NewProcessLogger(
|
||||
processLog,
|
||||
"test",
|
||||
slog.LevelInfo,
|
||||
),
|
||||
ProcessLog: processlog.New(processlog.Params{
|
||||
Output: processLog,
|
||||
InstanceName: "test",
|
||||
Level: slog.LevelInfo,
|
||||
}),
|
||||
})
|
||||
|
||||
listener, err := (&net.ListenConfig{}).Listen(
|
||||
|
||||
@@ -2426,7 +2426,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(metrics.Params{BusiestCount: 1, InstanceName: "app"})
|
||||
m := metrics.New(1, "app")
|
||||
queue := alerts.New(alerts.Params{
|
||||
WebhookURL: &url.URL{Scheme: "https", Host: "alerts.example"},
|
||||
Events: alerts.Events(),
|
||||
@@ -2448,7 +2448,7 @@ func newParams(dir string) state.Params {
|
||||
AttackBanDuration: 7 * 24 * time.Hour,
|
||||
MaxBans: 5000,
|
||||
}),
|
||||
Limiter: ratelimit.New(ratelimit.Limits{}, 20000),
|
||||
Limiter: ratelimit.New(ratelimit.Params{MaxClients: 20000}),
|
||||
GeoJS: geojs.New(geojs.Params{Now: midnight}),
|
||||
Lists: reputation.New(reputation.Params{
|
||||
BlocklistURLs: []string{
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"go.uber.org/fx"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
||||
)
|
||||
|
||||
@@ -23,11 +23,11 @@ import (
|
||||
// setting for either.
|
||||
func RunRProxy(ctx context.Context, params Params) int {
|
||||
hostname, _ := os.Hostname() // "" when the host has no name to give
|
||||
processLog := requestlog.NewProcessLogger(
|
||||
params.Stdout,
|
||||
hostname,
|
||||
slog.LevelInfo,
|
||||
)
|
||||
processLog := processlog.New(processlog.Params{
|
||||
Output: params.Stdout,
|
||||
InstanceName: hostname,
|
||||
Level: slog.LevelInfo,
|
||||
})
|
||||
|
||||
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
||||
if err != nil {
|
||||
|
||||
@@ -660,7 +660,9 @@ func TestCoreRuleSetExclusionsFileThatCannotBeUsedStopsTheStartWhileTheCoreRuleS
|
||||
|
||||
status := run(t.Context(), map[string]string{
|
||||
listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(),
|
||||
"SWW_WAF_CORE_RULE_SET_MODE": config.CoreRuleSetModeOff,
|
||||
"SWW_WAF_CORE_RULE_SET_MODE": string(
|
||||
config.CoreRuleSetModeOff,
|
||||
),
|
||||
"SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path,
|
||||
}, out)
|
||||
if status != 1 {
|
||||
|
||||
+34
-35
@@ -19,10 +19,10 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/routewatch"
|
||||
"sneak.berlin/go/smallwebwaf/internal/rules"
|
||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||
@@ -43,8 +43,11 @@ const remoteLogStopTimeout = 2 * time.Second
|
||||
func RunWAF(ctx context.Context, params Params) int {
|
||||
// Until the settings are read, the one message is an invalid setting's
|
||||
// error, which every SWW_WAF_LOG_LEVEL lets through.
|
||||
processLog := requestlog.NewProcessLogger(params.Stdout,
|
||||
config.InstanceName(params.Viper), slog.LevelError)
|
||||
processLog := processlog.New(processlog.Params{
|
||||
Output: params.Stdout,
|
||||
InstanceName: config.InstanceName(params.Viper),
|
||||
Level: slog.LevelError,
|
||||
})
|
||||
|
||||
cfg, err := config.WAFFromEnvironment(params.Viper)
|
||||
if err != nil {
|
||||
@@ -63,11 +66,11 @@ func RunWAF(ctx context.Context, params Params) int {
|
||||
stdout = io.MultiWriter(params.Stdout, remote)
|
||||
}
|
||||
|
||||
processLog = requestlog.NewProcessLogger(
|
||||
stdout,
|
||||
cfg.InstanceName,
|
||||
cfg.LogLevel,
|
||||
)
|
||||
processLog = processlog.New(processlog.Params{
|
||||
Output: stdout,
|
||||
InstanceName: cfg.InstanceName,
|
||||
Level: cfg.LogLevel,
|
||||
})
|
||||
|
||||
if remote != nil {
|
||||
stopSending := startSending(ctx, remote, processLog)
|
||||
@@ -217,7 +220,7 @@ func newLookupFile(
|
||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||
alertQueue *alerts.Queue,
|
||||
) (*lookup.File, error) {
|
||||
if cfg.LookupSource != "file" {
|
||||
if cfg.LookupSource != config.LookupSourceFile {
|
||||
return nil, nil //nolint:nilnil // no lookup database, and no error
|
||||
}
|
||||
|
||||
@@ -238,14 +241,17 @@ func newLookupFile(
|
||||
|
||||
// newLimiter returns the table of clients, with the rate and byte limits.
|
||||
func newLimiter(cfg *config.WAF) *ratelimit.Limiter {
|
||||
return ratelimit.New(ratelimit.Limits{
|
||||
PerMinute: cfg.RateLimitPerMinute,
|
||||
PerHour: cfg.RateLimitPerHour,
|
||||
PerDay: cfg.RateLimitPerDay,
|
||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
||||
BytesPerHour: cfg.BytesLimitPerHour,
|
||||
BytesPerDay: cfg.BytesLimitPerDay,
|
||||
}, cfg.MaxTrackedClients)
|
||||
return ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{
|
||||
PerMinute: cfg.RateLimitPerMinute,
|
||||
PerHour: cfg.RateLimitPerHour,
|
||||
PerDay: cfg.RateLimitPerDay,
|
||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
||||
BytesPerHour: cfg.BytesLimitPerHour,
|
||||
BytesPerDay: cfg.BytesLimitPerDay,
|
||||
},
|
||||
MaxClients: cfg.MaxTrackedClients,
|
||||
})
|
||||
}
|
||||
|
||||
// newLedger returns the ledger of the bans, with the settings for them.
|
||||
@@ -372,33 +378,26 @@ func newMetrics(
|
||||
alertQueue *alerts.Queue, lookupFile *lookup.File,
|
||||
remote *remotelog.Sender,
|
||||
) *metrics.Metrics {
|
||||
params := metrics.Params{
|
||||
BusiestCount: cfg.MetricsBusiestCountriesAndASNs,
|
||||
InstanceName: cfg.InstanceName,
|
||||
Now: utcNow,
|
||||
Bans: ledger,
|
||||
Clients: limiter,
|
||||
Rules: ruleFiles,
|
||||
Lists: lists,
|
||||
DNSBL: dnsbl,
|
||||
Alerts: alertQueue,
|
||||
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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
m.AddAlerts(alertQueue)
|
||||
|
||||
if lookupFile != nil {
|
||||
params.LookupFile = lookupFile
|
||||
m.AddLookupFile(lookupFile.LastRead, lookupFile.ReadFailures)
|
||||
}
|
||||
|
||||
if remote != nil {
|
||||
params.RemoteLog = remote
|
||||
m.AddRemoteLog(remote)
|
||||
}
|
||||
|
||||
return metrics.New(params)
|
||||
return m
|
||||
}
|
||||
|
||||
// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each
|
||||
|
||||
@@ -54,7 +54,7 @@ func (abuseIPDB) check(
|
||||
"scored by AbuseIPDB at or over SWW_WAF_ABUSEIPDB_MIN_SCORE",
|
||||
)
|
||||
|
||||
if rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||
if rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -86,13 +86,14 @@ func (rq *request) countedBytes() int64 {
|
||||
response, request := rq.out.bytes, rq.requestBytes
|
||||
|
||||
switch rq.h.config.BytesCounted {
|
||||
case "response":
|
||||
case config.BytesCountedResponse:
|
||||
return response
|
||||
case "request":
|
||||
case config.BytesCountedRequest:
|
||||
return request
|
||||
default: // both
|
||||
return response + request
|
||||
case config.BytesCountedBoth:
|
||||
}
|
||||
|
||||
return response + request
|
||||
}
|
||||
|
||||
// banForLimit bans the client's netblock at now for a broken limit, the
|
||||
|
||||
@@ -63,7 +63,8 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
||||
}
|
||||
|
||||
blocklisted := percentage{percent: whole}
|
||||
if rq.blocklisted && cfg.BlocklistAction == "limit" {
|
||||
if rq.blocklisted &&
|
||||
cfg.BlocklistAction == config.ReputationActionLimit {
|
||||
blocklisted = percentage{
|
||||
cfg.BlocklistLimitPercent,
|
||||
"SWW_WAF_BLOCKLIST_ACTION",
|
||||
@@ -72,7 +73,7 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
||||
|
||||
dnsblAbuseIPDBListed := percentage{percent: whole}
|
||||
if (rq.dnsblListed || rq.abuseIPDBHit) &&
|
||||
cfg.DNSBLAbuseIPDBAction == "limit" {
|
||||
cfg.DNSBLAbuseIPDBAction == config.ReputationActionLimit {
|
||||
dnsblAbuseIPDBListed = percentage{
|
||||
cfg.DNSBLAbuseIPDBLimitPercent,
|
||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION",
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
)
|
||||
|
||||
@@ -41,5 +42,6 @@ func (blocklists) check(
|
||||
// client, given listedBy, the blocklists that list it: it does when one lists
|
||||
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
||||
func (rq *request) refusedByBlocklists(listedBy []string) bool {
|
||||
return len(listedBy) > 0 && rq.h.config.BlocklistAction == deny
|
||||
return len(listedBy) > 0 && rq.h.config.BlocklistAction ==
|
||||
config.ReputationActionDeny
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (coreRuleSet) check(
|
||||
rq.line.CoreRuleSetScore = &result.Score
|
||||
|
||||
for _, id := range result.RuleIDs {
|
||||
rq.h.metrics.CoreRuleSetMatched(cfg.CoreRuleSetMode, id)
|
||||
rq.h.metrics.CoreRuleSetMatched(string(cfg.CoreRuleSetMode), id)
|
||||
}
|
||||
|
||||
threshold := cfg.CoreRuleSetScoreThreshold
|
||||
@@ -99,7 +99,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) {
|
||||
|
||||
switch {
|
||||
case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect:
|
||||
detail["mode"] = config.CoreRuleSetModeDetect
|
||||
detail["mode"] = string(config.CoreRuleSetModeDetect)
|
||||
case rq.h.config.Observe:
|
||||
detail["mode"] = "observe"
|
||||
}
|
||||
|
||||
@@ -32,7 +32,8 @@ func (dnsbl) check(ctx context.Context, rq *request, _ time.Time) *verdict {
|
||||
rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone))
|
||||
}
|
||||
|
||||
if !rq.dnsblListed || rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||
if !rq.dnsblListed ||
|
||||
rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,8 @@ func (rq *request) startLookUp(
|
||||
) <-chan lookupResult {
|
||||
result := make(chan lookupResult, 1)
|
||||
|
||||
if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {
|
||||
if rq.h.config.LookupSource == config.LookupSourceOff ||
|
||||
!canBePlaced(rq.client) {
|
||||
result <- lookupResult{}
|
||||
|
||||
return result
|
||||
@@ -57,7 +58,7 @@ func (rq *request) startLookUp(
|
||||
|
||||
client := clientGroup(rq.h.config, rq.client)
|
||||
|
||||
if rq.h.config.LookupSource == "file" {
|
||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
||||
result <- lookupResult{
|
||||
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
|
||||
}
|
||||
|
||||
+42
-45
@@ -24,6 +24,7 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
||||
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
@@ -398,11 +399,11 @@ func startRProxy(
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: io.Discard,
|
||||
ProcessLog: requestlog.NewProcessLogger(
|
||||
io.Discard,
|
||||
"rproxy",
|
||||
slog.LevelInfo,
|
||||
),
|
||||
ProcessLog: processlog.New(processlog.Params{
|
||||
Output: io.Discard,
|
||||
InstanceName: "rproxy",
|
||||
Level: slog.LevelInfo,
|
||||
}),
|
||||
})
|
||||
|
||||
listener, err := (&net.ListenConfig{}).Listen(
|
||||
@@ -471,11 +472,11 @@ func newWAFWith(
|
||||
}
|
||||
|
||||
out := &output{}
|
||||
processLog := requestlog.NewProcessLogger(
|
||||
out,
|
||||
cfg.InstanceName,
|
||||
cfg.LogLevel,
|
||||
)
|
||||
processLog := processlog.New(processlog.Params{
|
||||
Output: out,
|
||||
InstanceName: cfg.InstanceName,
|
||||
Level: cfg.LogLevel,
|
||||
})
|
||||
|
||||
ruleFiles, err := rules.Load(rules.Params{
|
||||
Dir: cfg.RuleFilesDir, Enabled: cfg.RuleFilesEnabled, ProcessLog: processLog,
|
||||
@@ -551,19 +552,27 @@ func addCoreRuleSet(t *testing.T, params *waf.Params) {
|
||||
params.CoreRuleSet = coreRuleSet
|
||||
}
|
||||
|
||||
// addCounters adds to params the table of clients, the ledger and the
|
||||
// anomaly counters, as sww builds them from params' settings.
|
||||
// addCounters adds to params the metrics, the table of clients, the ledger
|
||||
// and the anomaly counters, as sww builds them from params' settings. The
|
||||
// metrics read the ledger, the table of clients and the rules.
|
||||
func addCounters(params *waf.Params) {
|
||||
cfg := params.Config
|
||||
|
||||
params.Limiter = ratelimit.New(ratelimit.Limits{
|
||||
PerMinute: cfg.RateLimitPerMinute,
|
||||
PerHour: cfg.RateLimitPerHour,
|
||||
PerDay: cfg.RateLimitPerDay,
|
||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
||||
BytesPerHour: cfg.BytesLimitPerHour,
|
||||
BytesPerDay: cfg.BytesLimitPerDay,
|
||||
}, cfg.MaxTrackedClients)
|
||||
params.Metrics = metrics.New(
|
||||
cfg.MetricsBusiestCountriesAndASNs,
|
||||
cfg.InstanceName,
|
||||
)
|
||||
params.Limiter = ratelimit.New(ratelimit.Params{
|
||||
Limits: ratelimit.Limits{
|
||||
PerMinute: cfg.RateLimitPerMinute,
|
||||
PerHour: cfg.RateLimitPerHour,
|
||||
PerDay: cfg.RateLimitPerDay,
|
||||
BytesPerMinute: cfg.BytesLimitPerMinute,
|
||||
BytesPerHour: cfg.BytesLimitPerHour,
|
||||
BytesPerDay: cfg.BytesLimitPerDay,
|
||||
},
|
||||
MaxClients: cfg.MaxTrackedClients,
|
||||
})
|
||||
params.Ledger = bans.New(bans.Params{
|
||||
LimitBanDuration: cfg.LimitBanDuration,
|
||||
LimitBanRepeatWindow: cfg.LimitBanRepeatWindow,
|
||||
@@ -582,18 +591,26 @@ 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, the metrics newMetrics returns, and GeoJS at geojsURL, as
|
||||
// sww builds them from params' settings and the parts addCounters added.
|
||||
// setting is set, and GeoJS at geojsURL, as sww builds
|
||||
// them from params' settings and the parts addCounters added. The metrics
|
||||
// read the lists, the zones' verdicts, and AbuseIPDB's while its key is set.
|
||||
func addOutsideSources(
|
||||
params *waf.Params, geojsURL, abuseIPDBCheckURL string,
|
||||
) {
|
||||
cfg := params.Config
|
||||
limiter, ledger := params.Limiter, params.Ledger
|
||||
m, limiter, ledger := params.Metrics, params.Limiter, params.Ledger
|
||||
processLog, alertQueue := params.ProcessLog, params.Alerts
|
||||
|
||||
params.Lists = reputation.New(reputation.Params{
|
||||
@@ -642,9 +659,6 @@ func addOutsideSources(
|
||||
})
|
||||
}
|
||||
|
||||
m := newMetrics(params)
|
||||
params.Metrics = m
|
||||
|
||||
params.GeoJS = geojs.New(geojs.Params{
|
||||
URL: geojsURL,
|
||||
Answered: func(answer geojs.Answer) {
|
||||
@@ -657,29 +671,12 @@ func addOutsideSources(
|
||||
},
|
||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
||||
})
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
m.AddReputation(params.Lists, params.DNSBL)
|
||||
|
||||
if cfg.AbuseIPDBKey != "" {
|
||||
metricsParams.AbuseIPDB = params.AbuseIPDB
|
||||
m.AddAbuseIPDB(params.AbuseIPDB)
|
||||
}
|
||||
|
||||
return metrics.New(metricsParams)
|
||||
}
|
||||
|
||||
// newClient returns an HTTP client that sends requests as they are made,
|
||||
|
||||
@@ -7,10 +7,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
)
|
||||
|
||||
// deny is the SWW_WAF_BLOCKLIST_ACTION and the SWW_WAF_DNSBL_ABUSEIPDB_ACTION that
|
||||
// refuses the requests of a client a source lists.
|
||||
const deny = "deny"
|
||||
|
||||
// noteListed notes each of sources, the URLs of the blocklists or the
|
||||
// DNSBL zones, their keys masked, that list the client, as noteHit does,
|
||||
// with reason.
|
||||
|
||||
@@ -421,7 +421,7 @@ func (rq *request) answerAtTheEnd() (geojs.Answer, bool) {
|
||||
return geojs.Answer{}, false
|
||||
}
|
||||
|
||||
if rq.h.config.LookupSource == "file" {
|
||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
||||
return rq.lookupAnswer, true
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user