Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1b90073c1 |
No files matched your search
+32
-93
@@ -75,7 +75,7 @@ type WAF struct {
|
|||||||
BytesLimitPerMinute int64
|
BytesLimitPerMinute int64
|
||||||
BytesLimitPerHour int64
|
BytesLimitPerHour int64
|
||||||
BytesLimitPerDay int64
|
BytesLimitPerDay int64
|
||||||
BytesCounted BytesCounted
|
BytesCounted string
|
||||||
// LookupSource is where each client's AS number and country are looked
|
// LookupSource is where each client's AS number and country are looked
|
||||||
// up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere.
|
// up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere.
|
||||||
// LookupDBPath is the lookup database, the IPinfo Lite file looked up in
|
// 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
|
// 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
|
// and country in headers (SWW_WAF_ADD_LOOKUP_HEADERS), by default unless
|
||||||
// LookupSource is off.
|
// LookupSource is off.
|
||||||
LookupSource LookupSource
|
LookupSource string
|
||||||
LookupDBPath string
|
LookupDBPath string
|
||||||
ASNAndCountryLookupTimeout time.Duration
|
ASNAndCountryLookupTimeout time.Duration
|
||||||
AddLookupHeaders bool
|
AddLookupHeaders bool
|
||||||
@@ -131,7 +131,7 @@ type WAF struct {
|
|||||||
// BlocklistLimitPercent is the percentage of every limit it gets.
|
// BlocklistLimitPercent is the percentage of every limit it gets.
|
||||||
BlocklistURLs []string
|
BlocklistURLs []string
|
||||||
BlocklistRefresh time.Duration
|
BlocklistRefresh time.Duration
|
||||||
BlocklistAction ReputationAction
|
BlocklistAction string
|
||||||
BlocklistLimitPercent int64
|
BlocklistLimitPercent int64
|
||||||
// DNSBLZones are the DNSBL zones clients are asked about
|
// DNSBLZones are the DNSBL zones clients are asked about
|
||||||
// (SWW_WAF_DNSBL_ZONES), through DNSBLResolver (SWW_WAF_DNSBL_RESOLVER), or
|
// (SWW_WAF_DNSBL_ZONES), through DNSBLResolver (SWW_WAF_DNSBL_RESOLVER), or
|
||||||
@@ -154,7 +154,7 @@ type WAF struct {
|
|||||||
AbuseIPDBKey string
|
AbuseIPDBKey string
|
||||||
AbuseIPDBMinScore int64
|
AbuseIPDBMinScore int64
|
||||||
AbuseIPDBDailyBudget int
|
AbuseIPDBDailyBudget int
|
||||||
DNSBLAbuseIPDBAction ReputationAction
|
DNSBLAbuseIPDBAction string
|
||||||
DNSBLAbuseIPDBLimitPercent int64
|
DNSBLAbuseIPDBLimitPercent int64
|
||||||
IPReputationCacheTTL time.Duration
|
IPReputationCacheTTL time.Duration
|
||||||
DNSBLAbuseIPDBTimeout time.Duration
|
DNSBLAbuseIPDBTimeout time.Duration
|
||||||
@@ -237,7 +237,7 @@ type WAF struct {
|
|||||||
// reads none.
|
// reads none.
|
||||||
// CoreRuleSetExclusionsFile is the path of the file of its exclusions
|
// CoreRuleSetExclusionsFile is the path of the file of its exclusions
|
||||||
// (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while there is none.
|
// (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while there is none.
|
||||||
CoreRuleSetMode CoreRuleSetMode
|
CoreRuleSetMode string
|
||||||
CoreRuleSetParanoiaLevel int
|
CoreRuleSetParanoiaLevel int
|
||||||
CoreRuleSetScoreThreshold int
|
CoreRuleSetScoreThreshold int
|
||||||
CoreRuleSetDisabledRules []int
|
CoreRuleSetDisabledRules []int
|
||||||
@@ -316,66 +316,20 @@ type WAF struct {
|
|||||||
settings []slog.Attr
|
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.
|
// The values of SWW_WAF_CORE_RULE_SET_MODE.
|
||||||
const (
|
const (
|
||||||
// CoreRuleSetModeOff runs no request through the Core Rule Set.
|
// CoreRuleSetModeOff runs no request through the Core Rule Set.
|
||||||
CoreRuleSetModeOff CoreRuleSetMode = "off"
|
CoreRuleSetModeOff = off
|
||||||
// CoreRuleSetModeDetect logs and alerts a match, and refuses nothing.
|
// CoreRuleSetModeDetect logs and alerts a match, and refuses nothing.
|
||||||
CoreRuleSetModeDetect CoreRuleSetMode = "detect"
|
CoreRuleSetModeDetect = "detect"
|
||||||
// CoreRuleSetModeBlock refuses a match with 403.
|
// CoreRuleSetModeBlock refuses a match with 403.
|
||||||
CoreRuleSetModeBlock CoreRuleSetMode = "block"
|
CoreRuleSetModeBlock = "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 (
|
const (
|
||||||
day = 24 * time.Hour
|
day = 24 * time.Hour
|
||||||
ipv4Bits = 32
|
ipv4Bits = 32
|
||||||
@@ -604,13 +558,10 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
|||||||
),
|
),
|
||||||
BytesLimitPerHour: env.size("SWW_WAF_BYTES_LIMIT_PER_HOUR", "50G"),
|
BytesLimitPerHour: env.size("SWW_WAF_BYTES_LIMIT_PER_HOUR", "50G"),
|
||||||
BytesLimitPerDay: env.size("SWW_WAF_BYTES_LIMIT_PER_DAY", "100G"),
|
BytesLimitPerDay: env.size("SWW_WAF_BYTES_LIMIT_PER_DAY", "100G"),
|
||||||
BytesCounted: env.bytesCounted(
|
BytesCounted: env.bytesCounted("SWW_WAF_BYTES_COUNTED", "both"),
|
||||||
"SWW_WAF_BYTES_COUNTED",
|
|
||||||
BytesCountedBoth,
|
|
||||||
),
|
|
||||||
LookupSource: env.lookupSource(
|
LookupSource: env.lookupSource(
|
||||||
"SWW_WAF_LOOKUP_SOURCE",
|
"SWW_WAF_LOOKUP_SOURCE",
|
||||||
LookupSourceGeoJS,
|
"geojs",
|
||||||
),
|
),
|
||||||
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
|
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
|
||||||
ASNAndCountryLookupTimeout: env.milliseconds(
|
ASNAndCountryLookupTimeout: env.milliseconds(
|
||||||
@@ -807,9 +758,9 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
|||||||
cfg.InstanceName, cfg.LogRemoteURL != nil)
|
cfg.InstanceName, cfg.LogRemoteURL != nil)
|
||||||
// The app gets the headers by default, unless no client is looked up.
|
// The app gets the headers by default, unless no client is looked up.
|
||||||
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
|
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
|
||||||
strconv.FormatBool(cfg.LookupSource != LookupSourceOff))
|
strconv.FormatBool(cfg.LookupSource != off))
|
||||||
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
|
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
|
||||||
"SWW_WAF_BLOCKLIST_ACTION", string(ReputationActionDeny))
|
"SWW_WAF_BLOCKLIST_ACTION", "deny")
|
||||||
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
|
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
|
||||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25")
|
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25")
|
||||||
|
|
||||||
@@ -907,12 +858,9 @@ func (e *environment) count(name, defaultValue string) int64 {
|
|||||||
|
|
||||||
// bytesCounted reads the setting that is which body bytes count toward the
|
// bytesCounted reads the setting that is which body bytes count toward the
|
||||||
// byte limits and the byte anomaly thresholds: response, request or both.
|
// byte limits and the byte anomaly thresholds: response, request or both.
|
||||||
func (e *environment) bytesCounted(
|
func (e *environment) bytesCounted(name, defaultValue string) string {
|
||||||
name string, defaultValue BytesCounted,
|
value := e.value(name, defaultValue)
|
||||||
) BytesCounted {
|
if value != "response" && value != "request" && value != "both" {
|
||||||
value := BytesCounted(e.value(name, string(defaultValue)))
|
|
||||||
if value != BytesCountedResponse && value != BytesCountedRequest &&
|
|
||||||
value != BytesCountedBoth {
|
|
||||||
e.check(name, fmt.Errorf("%q %w", value, errNotBytesCounted))
|
e.check(name, fmt.Errorf("%q %w", value, errNotBytesCounted))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -957,10 +905,8 @@ func (e *environment) trapPaths(name string) []string {
|
|||||||
|
|
||||||
// coreRuleSetMode reads the setting that is what the Core Rule Set does: off,
|
// coreRuleSetMode reads the setting that is what the Core Rule Set does: off,
|
||||||
// detect or block.
|
// detect or block.
|
||||||
func (e *environment) coreRuleSetMode(
|
func (e *environment) coreRuleSetMode(name, defaultValue string) string {
|
||||||
name string, defaultValue CoreRuleSetMode,
|
mode := e.value(name, defaultValue)
|
||||||
) CoreRuleSetMode {
|
|
||||||
mode := CoreRuleSetMode(e.value(name, string(defaultValue)))
|
|
||||||
if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect &&
|
if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect &&
|
||||||
mode != CoreRuleSetModeBlock {
|
mode != CoreRuleSetModeBlock {
|
||||||
e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode))
|
e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode))
|
||||||
@@ -1110,24 +1056,20 @@ func (e *environment) refresh(name, defaultValue string) time.Duration {
|
|||||||
// action reads a setting that is what is done with a client a blocklist
|
// 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
|
// or a DNSBL zone lists: deny, log, or limit:<percent>, which it returns
|
||||||
// as limit and the percentage.
|
// as limit and the percentage.
|
||||||
func (e *environment) action(
|
func (e *environment) action(name, defaultValue string) (string, int64) {
|
||||||
name, defaultValue string,
|
value := e.value(name, defaultValue)
|
||||||
) (ReputationAction, int64) {
|
if value == "deny" || value == "log" {
|
||||||
value := ReputationAction(e.value(name, defaultValue))
|
|
||||||
if value == ReputationActionDeny || value == ReputationActionLog {
|
|
||||||
return value, 0
|
return value, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
percentText, isLimit := strings.CutPrefix(
|
percentText, isLimit := strings.CutPrefix(value, "limit:")
|
||||||
string(value), string(ReputationActionLimit)+":",
|
|
||||||
)
|
|
||||||
|
|
||||||
percent, err := ParsePercent(percentText)
|
percent, err := ParsePercent(percentText)
|
||||||
if !isLimit || err != nil {
|
if !isLimit || err != nil {
|
||||||
e.check(name, fmt.Errorf("%q %w", value, errNotAction))
|
e.check(name, fmt.Errorf("%q %w", value, errNotAction))
|
||||||
}
|
}
|
||||||
|
|
||||||
return ReputationActionLimit, percent
|
return "limit", percent
|
||||||
}
|
}
|
||||||
|
|
||||||
// zones reads the setting that is the list of DNSBL zones. It is empty by
|
// zones reads the setting that is the list of DNSBL zones. It is empty by
|
||||||
@@ -1178,12 +1120,9 @@ func (e *environment) routeWatchURL(name string) string {
|
|||||||
|
|
||||||
// lookupSource reads the setting that is where clients are looked up:
|
// lookupSource reads the setting that is where clients are looked up:
|
||||||
// geojs, file, or off.
|
// geojs, file, or off.
|
||||||
func (e *environment) lookupSource(
|
func (e *environment) lookupSource(name, defaultValue string) string {
|
||||||
name string, defaultValue LookupSource,
|
source := e.value(name, defaultValue)
|
||||||
) LookupSource {
|
if source != "geojs" && source != fileSource && source != off {
|
||||||
source := LookupSource(e.value(name, string(defaultValue)))
|
|
||||||
if source != LookupSourceGeoJS && source != LookupSourceFile &&
|
|
||||||
source != LookupSourceOff {
|
|
||||||
e.check(name, fmt.Errorf("%q %w", source, errNotLookupSource))
|
e.check(name, fmt.Errorf("%q %w", source, errNotLookupSource))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1195,12 +1134,12 @@ func (e *environment) lookupSource(
|
|||||||
// one source at a time.
|
// one source at a time.
|
||||||
func (e *environment) checkLookupDBPath(cfg *WAF) {
|
func (e *environment) checkLookupDBPath(cfg *WAF) {
|
||||||
switch {
|
switch {
|
||||||
case cfg.LookupSource == LookupSourceFile && cfg.LookupDBPath == "":
|
case cfg.LookupSource == fileSource && cfg.LookupDBPath == "":
|
||||||
e.check("SWW_WAF_LOOKUP_SOURCE", fmt.Errorf(
|
e.check("SWW_WAF_LOOKUP_SOURCE", fmt.Errorf(
|
||||||
"is file while SWW_WAF_LOOKUP_DB_PATH is unset; %w",
|
"is file while SWW_WAF_LOOKUP_DB_PATH is unset; %w",
|
||||||
errNeedsDBPath,
|
errNeedsDBPath,
|
||||||
))
|
))
|
||||||
case cfg.LookupSource != LookupSourceFile && cfg.LookupDBPath != "":
|
case cfg.LookupSource != fileSource && cfg.LookupDBPath != "":
|
||||||
e.check("SWW_WAF_LOOKUP_DB_PATH", fmt.Errorf(
|
e.check("SWW_WAF_LOOKUP_DB_PATH", fmt.Errorf(
|
||||||
"is set while SWW_WAF_LOOKUP_SOURCE is %s; %w",
|
"is set while SWW_WAF_LOOKUP_SOURCE is %s; %w",
|
||||||
cfg.LookupSource,
|
cfg.LookupSource,
|
||||||
@@ -1225,7 +1164,7 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.LookupSource != LookupSourceOff {
|
if cfg.LookupSource != off {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -614,7 +614,7 @@ func TestCoreRuleSetSettings(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
environment{
|
environment{
|
||||||
coreRuleSetMode: string(config.CoreRuleSetModeDetect),
|
coreRuleSetMode: config.CoreRuleSetModeDetect,
|
||||||
coreRuleSetParanoiaLevel: "4",
|
coreRuleSetParanoiaLevel: "4",
|
||||||
coreRuleSetScoreThreshold: "10",
|
coreRuleSetScoreThreshold: "10",
|
||||||
coreRuleSetDisabledRules: "942100, 920350",
|
coreRuleSetDisabledRules: "942100, 920350",
|
||||||
@@ -2033,9 +2033,8 @@ func TestReputationSettingsAsSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
value string
|
value, action string
|
||||||
action config.ReputationAction
|
percent int64
|
||||||
percent int64
|
|
||||||
}{
|
}{
|
||||||
{actionDeny, actionDeny, 0},
|
{actionDeny, actionDeny, 0},
|
||||||
{actionLog, actionLog, 0},
|
{actionLog, actionLog, 0},
|
||||||
@@ -2168,7 +2167,7 @@ func TestDNSBLSettingsAsSet(t *testing.T) {
|
|||||||
// want are the zones, resolver, action and percent config.WAF gives.
|
// want are the zones, resolver, action and percent config.WAF gives.
|
||||||
want []string
|
want []string
|
||||||
wantResolver string
|
wantResolver string
|
||||||
wantAction config.ReputationAction
|
wantAction string
|
||||||
wantPercent int64
|
wantPercent int64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -3050,7 +3049,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
|
|||||||
logLevel: "info",
|
logLevel: "info",
|
||||||
ruleFilesDir: "/etc/sww/rules.d",
|
ruleFilesDir: "/etc/sww/rules.d",
|
||||||
ruleFilesEnabled: "true",
|
ruleFilesEnabled: "true",
|
||||||
coreRuleSetMode: string(config.CoreRuleSetModeBlock),
|
coreRuleSetMode: config.CoreRuleSetModeBlock,
|
||||||
coreRuleSetParanoiaLevel: "1",
|
coreRuleSetParanoiaLevel: "1",
|
||||||
coreRuleSetScoreThreshold: "5",
|
coreRuleSetScoreThreshold: "5",
|
||||||
coreRuleSetDisabledRules: defaultCoreRuleSetDisabledRules,
|
coreRuleSetDisabledRules: defaultCoreRuleSetDisabledRules,
|
||||||
|
|||||||
+391
-266
@@ -16,12 +16,33 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"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.
|
// Metrics are sww's metrics. They are safe for concurrent use.
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
// registry gives every metric registered with it the label instance.
|
// registry gives every metric registered with it the label instance.
|
||||||
@@ -37,12 +58,10 @@ type Metrics struct {
|
|||||||
rateLimitHits *prometheus.CounterVec
|
rateLimitHits *prometheus.CounterVec
|
||||||
offences *prometheus.CounterVec
|
offences *prometheus.CounterVec
|
||||||
coreRuleSetMatches *prometheus.CounterVec
|
coreRuleSetMatches *prometheus.CounterVec
|
||||||
// ruleMatches are made by AddRules, and reputationHits by
|
ruleMatches *prometheus.CounterVec
|
||||||
// AddReputation.
|
reputationHits *prometheus.CounterVec
|
||||||
ruleMatches *prometheus.CounterVec
|
countries *busiest
|
||||||
reputationHits *prometheus.CounterVec
|
asns *busiest
|
||||||
countries *busiest
|
|
||||||
asns *busiest
|
|
||||||
|
|
||||||
// GeoJSRequests are the requests to GeoJS, and GeoJSFailures those
|
// GeoJSRequests are the requests to GeoJS, and GeoJSFailures those
|
||||||
// that failed. RequestsWithoutGeoJSAnswer are the requests that needed
|
// that failed. RequestsWithoutGeoJSAnswer are the requests that needed
|
||||||
@@ -60,20 +79,18 @@ type Metrics struct {
|
|||||||
stateFileEditsSetAside *prometheus.CounterVec
|
stateFileEditsSetAside *prometheus.CounterVec
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns the metrics, with the Go runtime's and the process's own.
|
// New returns the metrics, with the Go runtime's and the process's own, and
|
||||||
// busiestCount is how many countries and how many AS numbers get series of
|
// those read from each part in params as the metrics are asked for.
|
||||||
// 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
|
//nolint:funlen // a few lines for each metric, a list that grows with them
|
||||||
func New(busiestCount int, instanceName string) *Metrics {
|
func New(params Params) *Metrics {
|
||||||
byStatus := []string{"status_class", "action"}
|
byStatus := []string{"status_class", "action"}
|
||||||
byFile := []string{"file"}
|
byFile := []string{"file"}
|
||||||
registry := prometheus.NewRegistry()
|
registry := prometheus.NewRegistry()
|
||||||
|
|
||||||
m := &Metrics{
|
m := &Metrics{
|
||||||
registry: prometheus.WrapRegistererWith(
|
registry: prometheus.WrapRegistererWith(
|
||||||
prometheus.Labels{"instance": instanceName}, registry),
|
prometheus.Labels{"instance": params.InstanceName}, registry),
|
||||||
handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
handler: promhttp.HandlerFor(registry, promhttp.HandlerOpts{}),
|
||||||
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
|
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: "sww_requests_in_flight",
|
Name: "sww_requests_in_flight",
|
||||||
@@ -114,8 +131,19 @@ func New(busiestCount int, instanceName string) *Metrics {
|
|||||||
"and the rule's id.",
|
"and the rule's id.",
|
||||||
[]string{"mode", "rule_id"},
|
[]string{"mode", "rule_id"},
|
||||||
),
|
),
|
||||||
countries: newCountries(busiestCount),
|
ruleMatches: counterVec(
|
||||||
asns: newASNs(busiestCount),
|
"sww_rule_matches_total",
|
||||||
|
"Requests that matched a rule of the rule files, by its id and action.",
|
||||||
|
[]string{"rule_id", "action"},
|
||||||
|
),
|
||||||
|
reputationHits: counterVec(
|
||||||
|
"sww_reputation_hits_total",
|
||||||
|
"Requests whose client a blocklist, a DNSBL zone or AbuseIPDB lists, "+
|
||||||
|
"by the list's URL, the zone, or abuseipdb.",
|
||||||
|
[]string{sourceLabel},
|
||||||
|
),
|
||||||
|
countries: newCountries(params.BusiestCount),
|
||||||
|
asns: newASNs(params.BusiestCount),
|
||||||
GeoJSRequests: prometheus.NewCounter(prometheus.CounterOpts{
|
GeoJSRequests: prometheus.NewCounter(prometheus.CounterOpts{
|
||||||
Name: "sww_geojs_requests_total",
|
Name: "sww_geojs_requests_total",
|
||||||
Help: "Requests to GeoJS.",
|
Help: "Requests to GeoJS.",
|
||||||
@@ -167,6 +195,7 @@ func New(busiestCount int, instanceName string) *Metrics {
|
|||||||
m.inFlight, m.requests, m.requestBytes, m.responseBytes,
|
m.inFlight, m.requests, m.requestBytes, m.responseBytes,
|
||||||
m.requestDuration, m.upstreamDuration,
|
m.requestDuration, m.upstreamDuration,
|
||||||
m.rateLimitHits, m.offences, m.coreRuleSetMatches,
|
m.rateLimitHits, m.offences, m.coreRuleSetMatches,
|
||||||
|
m.ruleMatches, m.reputationHits,
|
||||||
m.countries, m.asns,
|
m.countries, m.asns,
|
||||||
m.GeoJSRequests, m.GeoJSFailures, m.RequestsWithoutGeoJSAnswer,
|
m.GeoJSRequests, m.GeoJSFailures, m.RequestsWithoutGeoJSAnswer,
|
||||||
m.stateFileWrites, m.stateFileWriteFailures,
|
m.stateFileWrites, m.stateFileWriteFailures,
|
||||||
@@ -174,259 +203,45 @@ func New(busiestCount int, instanceName string) *Metrics {
|
|||||||
m.stateFileEditsTakenIn, m.stateFileEditsSetAside,
|
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
|
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.
|
// ServeHTTP answers with the metrics in the Prometheus text format.
|
||||||
func (m *Metrics) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (m *Metrics) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
m.handler.ServeHTTP(w, r)
|
m.handler.ServeHTTP(w, r)
|
||||||
@@ -496,6 +311,12 @@ func (m *Metrics) CoreRuleSetMatched(mode string, id int) {
|
|||||||
m.coreRuleSetMatches.WithLabelValues(mode, strconv.Itoa(id)).Inc()
|
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
|
// StateFileWritten counts a write of the state or cache file name, of size
|
||||||
// bytes, that ended with err.
|
// bytes, that ended with err.
|
||||||
func (m *Metrics) StateFileWritten(name string, size int, err error) {
|
func (m *Metrics) StateFileWritten(name string, size int, err error) {
|
||||||
@@ -527,6 +348,310 @@ func (m *Metrics) StateFileEditSetAside(name string) {
|
|||||||
m.stateFileEditsSetAside.WithLabelValues(name).Inc()
|
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
|
// count counts a request that has ended, as RequestEnded and RequestReported
|
||||||
// describe.
|
// describe.
|
||||||
func (m *Metrics) count(
|
func (m *Metrics) count(
|
||||||
|
|||||||
@@ -0,0 +1,160 @@
|
|||||||
|
package metrics_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
listURL = "https://lists.example/block.txt"
|
||||||
|
zone = "dnsbl.example"
|
||||||
|
destination = "webhook"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Each stand-in answers with a number of its own, so that each metric
|
||||||
|
// shows which part it was read from.
|
||||||
|
|
||||||
|
type ledgerStandIn struct{}
|
||||||
|
|
||||||
|
func (ledgerStandIn) Made(string) int { return 1 }
|
||||||
|
|
||||||
|
func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 }
|
||||||
|
|
||||||
|
type clientTableStandIn struct{}
|
||||||
|
|
||||||
|
func (clientTableStandIn) Len() int { return 3 }
|
||||||
|
|
||||||
|
type ruleFilesStandIn struct{}
|
||||||
|
|
||||||
|
func (ruleFilesStandIn) Len() int { return 4 }
|
||||||
|
|
||||||
|
type listsStandIn struct{}
|
||||||
|
|
||||||
|
func (listsStandIn) URLs() []string { return []string{listURL} }
|
||||||
|
|
||||||
|
func (listsStandIn) Failures(string) int { return 5 }
|
||||||
|
|
||||||
|
func (listsStandIn) Fetched(string) time.Time { return time.Time{} }
|
||||||
|
|
||||||
|
type dnsblStandIn struct{}
|
||||||
|
|
||||||
|
func (dnsblStandIn) Zones() []string { return []string{zone} }
|
||||||
|
|
||||||
|
func (dnsblStandIn) Queries(string) int { return 6 }
|
||||||
|
|
||||||
|
func (dnsblStandIn) Failures(string) int { return 0 }
|
||||||
|
|
||||||
|
type abuseIPDBStandIn struct{}
|
||||||
|
|
||||||
|
func (abuseIPDBStandIn) Checked() int { return 0 }
|
||||||
|
|
||||||
|
func (abuseIPDBStandIn) Failures() int { return 0 }
|
||||||
|
|
||||||
|
func (abuseIPDBStandIn) BudgetLeft() int { return 7 }
|
||||||
|
|
||||||
|
type alertQueueStandIn struct{}
|
||||||
|
|
||||||
|
func (alertQueueStandIn) DestinationsSet() []string {
|
||||||
|
return []string{destination}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (alertQueueStandIn) Counts(string) alerts.Counts {
|
||||||
|
return alerts.Counts{Sent: 8}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (alertQueueStandIn) Suppressed() int64 { return 0 }
|
||||||
|
|
||||||
|
type lookupDatabaseStandIn struct{}
|
||||||
|
|
||||||
|
func (lookupDatabaseStandIn) LastRead() time.Time { return time.Time{} }
|
||||||
|
|
||||||
|
func (lookupDatabaseStandIn) ReadFailures() int { return 9 }
|
||||||
|
|
||||||
|
type remoteLogStandIn struct{}
|
||||||
|
|
||||||
|
func (remoteLogStandIn) Sent() int64 { return 10 }
|
||||||
|
|
||||||
|
func (remoteLogStandIn) Dropped() int64 { return 0 }
|
||||||
|
|
||||||
|
func (remoteLogStandIn) Depth() int { return 0 }
|
||||||
|
|
||||||
|
func TestMetricsAreReadFromEachPart(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
m := metrics.New(metrics.Params{
|
||||||
|
BusiestCount: 1,
|
||||||
|
InstanceName: "app",
|
||||||
|
Now: func() time.Time { return time.Time{} },
|
||||||
|
Bans: ledgerStandIn{},
|
||||||
|
Clients: clientTableStandIn{},
|
||||||
|
Rules: ruleFilesStandIn{},
|
||||||
|
Lists: listsStandIn{},
|
||||||
|
DNSBL: dnsblStandIn{},
|
||||||
|
AbuseIPDB: abuseIPDBStandIn{},
|
||||||
|
Alerts: alertQueueStandIn{},
|
||||||
|
LookupFile: lookupDatabaseStandIn{},
|
||||||
|
RemoteLog: remoteLogStandIn{},
|
||||||
|
})
|
||||||
|
scraped := scrape(t, m)
|
||||||
|
|
||||||
|
for _, line := range []string{
|
||||||
|
`sww_bans_made_total{cause="limit",instance="app"} 1`,
|
||||||
|
`sww_active_bans{instance="app"} 2`,
|
||||||
|
`sww_tracked_clients{instance="app"} 3`,
|
||||||
|
`sww_rules_loaded{instance="app"} 4`,
|
||||||
|
`sww_reputation_failures_total{instance="app",source="` + listURL + `"} 5`,
|
||||||
|
`sww_reputation_queries_total{instance="app",source="` + zone + `"} 6`,
|
||||||
|
`sww_reputation_daily_budget_remaining{instance="app",source="abuseipdb"} 7`,
|
||||||
|
`sww_alerts_sent_total{destination="` + destination + `",instance="app"} 8`,
|
||||||
|
`sww_lookup_database_read_failures_total{instance="app"} 9`,
|
||||||
|
`sww_remote_log_lines_sent_total{instance="app"} 10`,
|
||||||
|
} {
|
||||||
|
if !strings.Contains(scraped, "\n"+line+"\n") {
|
||||||
|
t.Errorf("metrics\n%s\nwant\n%s", scraped, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPartThatIsOffAddsNoMetric(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
scraped := scrape(t, metrics.New(metrics.Params{
|
||||||
|
BusiestCount: 1,
|
||||||
|
InstanceName: "app",
|
||||||
|
}))
|
||||||
|
|
||||||
|
for _, name := range []string{
|
||||||
|
"sww_bans_made_total",
|
||||||
|
"sww_tracked_clients",
|
||||||
|
"sww_rules_loaded",
|
||||||
|
"sww_reputation_",
|
||||||
|
"sww_alert",
|
||||||
|
"sww_lookup_database_",
|
||||||
|
"sww_remote_log_",
|
||||||
|
} {
|
||||||
|
if strings.Contains(scraped, name) {
|
||||||
|
t.Errorf("metrics\n%s\nshow %s", scraped, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// scrape returns the metrics m serves.
|
||||||
|
func scrape(t *testing.T, m *metrics.Metrics) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
scraped := httptest.NewRecorder()
|
||||||
|
m.ServeHTTP(
|
||||||
|
scraped,
|
||||||
|
httptest.NewRequestWithContext(
|
||||||
|
t.Context(), http.MethodGet, "/", http.NoBody,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return scraped.Body.String()
|
||||||
|
}
|
||||||
@@ -408,12 +408,9 @@ func TestKeyIsSentInTheKeyHeaderAndNeverShown(t *testing.T) {
|
|||||||
scores: map[string]int64{suspect: 100},
|
scores: map[string]int64{suspect: 100},
|
||||||
}
|
}
|
||||||
checker := newAbuseIPDB(abuseIPDB, p)
|
checker := newAbuseIPDB(abuseIPDB, p)
|
||||||
m := metrics.New(1, "app")
|
m := metrics.New(metrics.Params{
|
||||||
m.AddReputation(
|
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||||
reputation.New(params()),
|
})
|
||||||
reputation.NewDNSBL(dnsblParams()),
|
|
||||||
)
|
|
||||||
m.AddAbuseIPDB(checker)
|
|
||||||
|
|
||||||
// One check that AbuseIPDB answers, and one it refuses with an answer
|
// One check that AbuseIPDB answers, and one it refuses with an answer
|
||||||
// that names the key.
|
// that names the key.
|
||||||
@@ -466,12 +463,9 @@ func TestMetricsCountTheChecksTheFailuresAndTheBudgetLeft(t *testing.T) {
|
|||||||
p := abuseIPDBParams()
|
p := abuseIPDBParams()
|
||||||
p.DailyBudget = 5
|
p.DailyBudget = 5
|
||||||
checker := newAbuseIPDB(abuseIPDB, p)
|
checker := newAbuseIPDB(abuseIPDB, p)
|
||||||
m := metrics.New(1, "app")
|
m := metrics.New(metrics.Params{
|
||||||
m.AddReputation(
|
BusiestCount: 1, InstanceName: instanceName, AbuseIPDB: checker,
|
||||||
reputation.New(params()),
|
})
|
||||||
reputation.NewDNSBL(dnsblParams()),
|
|
||||||
)
|
|
||||||
m.AddAbuseIPDB(checker)
|
|
||||||
|
|
||||||
// One check that AbuseIPDB answers, and one that fails.
|
// One check that AbuseIPDB answers, and one that fails.
|
||||||
hitFrom(t, checker, suspect, true)
|
hitFrom(t, checker, suspect, true)
|
||||||
@@ -774,6 +768,9 @@ func wantBudgetLeft(t *testing.T, checker *reputation.AbuseIPDB, want int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// instanceName is the label instance of the metrics the tests build.
|
||||||
|
const instanceName = "app"
|
||||||
|
|
||||||
// scrapeMetrics returns the metrics m serves.
|
// scrapeMetrics returns the metrics m serves.
|
||||||
func scrapeMetrics(t *testing.T, m *metrics.Metrics) string {
|
func scrapeMetrics(t *testing.T, m *metrics.Metrics) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|||||||
@@ -319,8 +319,9 @@ func TestMetricsCountEachZonesQueriesAndThoseThatFailed(t *testing.T) {
|
|||||||
"99.2.0.192." + otherZone + ".": {rcode: serverFailure},
|
"99.2.0.192." + otherZone + ".": {rcode: serverFailure},
|
||||||
}}
|
}}
|
||||||
dnsbl := newDNSBL(resolver, dnsblParams(zone, otherZone))
|
dnsbl := newDNSBL(resolver, dnsblParams(zone, otherZone))
|
||||||
m := metrics.New(1, "app")
|
m := metrics.New(metrics.Params{
|
||||||
m.AddReputation(reputation.New(params()), dnsbl)
|
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||||
|
})
|
||||||
|
|
||||||
wantZones(t, dnsbl, listed)
|
wantZones(t, dnsbl, listed)
|
||||||
synctest.Wait()
|
synctest.Wait()
|
||||||
@@ -365,8 +366,9 @@ func TestZoneKeyIsMaskedInTheVerdictsTheFailuresAndTheMetrics(t *testing.T) {
|
|||||||
"99.2.0.192." + keyed + ".": {addrs: []string{listing}},
|
"99.2.0.192." + keyed + ".": {addrs: []string{listing}},
|
||||||
"100.2.0.192." + keyed + ".": {rcode: serverFailure},
|
"100.2.0.192." + keyed + ".": {rcode: serverFailure},
|
||||||
}}, p)
|
}}, p)
|
||||||
m := metrics.New(1, "app")
|
m := metrics.New(metrics.Params{
|
||||||
m.AddReputation(reputation.New(params()), dnsbl)
|
BusiestCount: 1, InstanceName: instanceName, DNSBL: dnsbl,
|
||||||
|
})
|
||||||
|
|
||||||
// Both clients are asked about before either answer comes, so that
|
// Both clients are asked about before either answer comes, so that
|
||||||
// the failure does not keep the zone from the other query.
|
// the failure does not keep the zone from the other query.
|
||||||
|
|||||||
@@ -2426,7 +2426,7 @@ func midnight() time.Time {
|
|||||||
// counts, with thresholds fill does not reach.
|
// counts, with thresholds fill does not reach.
|
||||||
func newParams(dir string) state.Params {
|
func newParams(dir string) state.Params {
|
||||||
discard := slog.New(slog.DiscardHandler)
|
discard := slog.New(slog.DiscardHandler)
|
||||||
m := metrics.New(1, "app")
|
m := metrics.New(metrics.Params{BusiestCount: 1, InstanceName: "app"})
|
||||||
queue := alerts.New(alerts.Params{
|
queue := alerts.New(alerts.Params{
|
||||||
WebhookURL: &url.URL{Scheme: "https", Host: "alerts.example"},
|
WebhookURL: &url.URL{Scheme: "https", Host: "alerts.example"},
|
||||||
Events: alerts.Events(),
|
Events: alerts.Events(),
|
||||||
|
|||||||
@@ -660,9 +660,7 @@ func TestCoreRuleSetExclusionsFileThatCannotBeUsedStopsTheStartWhileTheCoreRuleS
|
|||||||
|
|
||||||
status := run(t.Context(), map[string]string{
|
status := run(t.Context(), map[string]string{
|
||||||
listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(),
|
listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(),
|
||||||
"SWW_WAF_CORE_RULE_SET_MODE": string(
|
"SWW_WAF_CORE_RULE_SET_MODE": config.CoreRuleSetModeOff,
|
||||||
config.CoreRuleSetModeOff,
|
|
||||||
),
|
|
||||||
"SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path,
|
"SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path,
|
||||||
}, out)
|
}, out)
|
||||||
if status != 1 {
|
if status != 1 {
|
||||||
|
|||||||
+19
-12
@@ -217,7 +217,7 @@ func newLookupFile(
|
|||||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||||
alertQueue *alerts.Queue,
|
alertQueue *alerts.Queue,
|
||||||
) (*lookup.File, error) {
|
) (*lookup.File, error) {
|
||||||
if cfg.LookupSource != config.LookupSourceFile {
|
if cfg.LookupSource != "file" {
|
||||||
return nil, nil //nolint:nilnil // no lookup database, and no error
|
return nil, nil //nolint:nilnil // no lookup database, and no error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -372,26 +372,33 @@ func newMetrics(
|
|||||||
alertQueue *alerts.Queue, lookupFile *lookup.File,
|
alertQueue *alerts.Queue, lookupFile *lookup.File,
|
||||||
remote *remotelog.Sender,
|
remote *remotelog.Sender,
|
||||||
) *metrics.Metrics {
|
) *metrics.Metrics {
|
||||||
m := metrics.New(cfg.MetricsBusiestCountriesAndASNs, cfg.InstanceName)
|
params := metrics.Params{
|
||||||
m.AddBansAndClients(ledger, limiter, utcNow)
|
BusiestCount: cfg.MetricsBusiestCountriesAndASNs,
|
||||||
m.AddRules(ruleFiles)
|
InstanceName: cfg.InstanceName,
|
||||||
m.AddReputation(lists, dnsbl)
|
Now: utcNow,
|
||||||
|
Bans: ledger,
|
||||||
if cfg.AbuseIPDBKey != "" {
|
Clients: limiter,
|
||||||
m.AddAbuseIPDB(abuseIPDB)
|
Rules: ruleFiles,
|
||||||
|
Lists: lists,
|
||||||
|
DNSBL: dnsbl,
|
||||||
|
Alerts: alertQueue,
|
||||||
}
|
}
|
||||||
|
|
||||||
m.AddAlerts(alertQueue)
|
// A nil pointer in an interface field is not a nil interface, so a part
|
||||||
|
// that is off is left out rather than set to nil.
|
||||||
|
if cfg.AbuseIPDBKey != "" {
|
||||||
|
params.AbuseIPDB = abuseIPDB
|
||||||
|
}
|
||||||
|
|
||||||
if lookupFile != nil {
|
if lookupFile != nil {
|
||||||
m.AddLookupFile(lookupFile.LastRead, lookupFile.ReadFailures)
|
params.LookupFile = lookupFile
|
||||||
}
|
}
|
||||||
|
|
||||||
if remote != nil {
|
if remote != nil {
|
||||||
m.AddRemoteLog(remote)
|
params.RemoteLog = remote
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return metrics.New(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each
|
// 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",
|
"scored by AbuseIPDB at or over SWW_WAF_ABUSEIPDB_MIN_SCORE",
|
||||||
)
|
)
|
||||||
|
|
||||||
if rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
if rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,14 +86,13 @@ func (rq *request) countedBytes() int64 {
|
|||||||
response, request := rq.out.bytes, rq.requestBytes
|
response, request := rq.out.bytes, rq.requestBytes
|
||||||
|
|
||||||
switch rq.h.config.BytesCounted {
|
switch rq.h.config.BytesCounted {
|
||||||
case config.BytesCountedResponse:
|
case "response":
|
||||||
return response
|
return response
|
||||||
case config.BytesCountedRequest:
|
case "request":
|
||||||
return request
|
return request
|
||||||
case config.BytesCountedBoth:
|
default: // both
|
||||||
|
return response + request
|
||||||
}
|
}
|
||||||
|
|
||||||
return response + request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// banForLimit bans the client's netblock at now for a broken limit, the
|
// banForLimit bans the client's netblock at now for a broken limit, the
|
||||||
|
|||||||
@@ -63,8 +63,7 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
blocklisted := percentage{percent: whole}
|
blocklisted := percentage{percent: whole}
|
||||||
if rq.blocklisted &&
|
if rq.blocklisted && cfg.BlocklistAction == "limit" {
|
||||||
cfg.BlocklistAction == config.ReputationActionLimit {
|
|
||||||
blocklisted = percentage{
|
blocklisted = percentage{
|
||||||
cfg.BlocklistLimitPercent,
|
cfg.BlocklistLimitPercent,
|
||||||
"SWW_WAF_BLOCKLIST_ACTION",
|
"SWW_WAF_BLOCKLIST_ACTION",
|
||||||
@@ -73,7 +72,7 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
|||||||
|
|
||||||
dnsblAbuseIPDBListed := percentage{percent: whole}
|
dnsblAbuseIPDBListed := percentage{percent: whole}
|
||||||
if (rq.dnsblListed || rq.abuseIPDBHit) &&
|
if (rq.dnsblListed || rq.abuseIPDBHit) &&
|
||||||
cfg.DNSBLAbuseIPDBAction == config.ReputationActionLimit {
|
cfg.DNSBLAbuseIPDBAction == "limit" {
|
||||||
dnsblAbuseIPDBListed = percentage{
|
dnsblAbuseIPDBListed = percentage{
|
||||||
cfg.DNSBLAbuseIPDBLimitPercent,
|
cfg.DNSBLAbuseIPDBLimitPercent,
|
||||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION",
|
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION",
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,6 +41,5 @@ func (blocklists) check(
|
|||||||
// client, given listedBy, the blocklists that list it: it does when one lists
|
// client, given listedBy, the blocklists that list it: it does when one lists
|
||||||
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
||||||
func (rq *request) refusedByBlocklists(listedBy []string) bool {
|
func (rq *request) refusedByBlocklists(listedBy []string) bool {
|
||||||
return len(listedBy) > 0 && rq.h.config.BlocklistAction ==
|
return len(listedBy) > 0 && rq.h.config.BlocklistAction == deny
|
||||||
config.ReputationActionDeny
|
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ func (coreRuleSet) check(
|
|||||||
rq.line.CoreRuleSetScore = &result.Score
|
rq.line.CoreRuleSetScore = &result.Score
|
||||||
|
|
||||||
for _, id := range result.RuleIDs {
|
for _, id := range result.RuleIDs {
|
||||||
rq.h.metrics.CoreRuleSetMatched(string(cfg.CoreRuleSetMode), id)
|
rq.h.metrics.CoreRuleSetMatched(cfg.CoreRuleSetMode, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
threshold := cfg.CoreRuleSetScoreThreshold
|
threshold := cfg.CoreRuleSetScoreThreshold
|
||||||
@@ -99,7 +99,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect:
|
case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect:
|
||||||
detail["mode"] = string(config.CoreRuleSetModeDetect)
|
detail["mode"] = config.CoreRuleSetModeDetect
|
||||||
case rq.h.config.Observe:
|
case rq.h.config.Observe:
|
||||||
detail["mode"] = "observe"
|
detail["mode"] = "observe"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,8 +32,7 @@ func (dnsbl) check(ctx context.Context, rq *request, _ time.Time) *verdict {
|
|||||||
rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone))
|
rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !rq.dnsblListed ||
|
if !rq.dnsblListed || rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||||
rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,8 +49,7 @@ func (rq *request) startLookUp(
|
|||||||
) <-chan lookupResult {
|
) <-chan lookupResult {
|
||||||
result := make(chan lookupResult, 1)
|
result := make(chan lookupResult, 1)
|
||||||
|
|
||||||
if rq.h.config.LookupSource == config.LookupSourceOff ||
|
if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {
|
||||||
!canBePlaced(rq.client) {
|
|
||||||
result <- lookupResult{}
|
result <- lookupResult{}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@@ -58,7 +57,7 @@ func (rq *request) startLookUp(
|
|||||||
|
|
||||||
client := clientGroup(rq.h.config, rq.client)
|
client := clientGroup(rq.h.config, rq.client)
|
||||||
|
|
||||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
if rq.h.config.LookupSource == "file" {
|
||||||
result <- lookupResult{
|
result <- lookupResult{
|
||||||
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
|
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-20
@@ -551,16 +551,11 @@ func addCoreRuleSet(t *testing.T, params *waf.Params) {
|
|||||||
params.CoreRuleSet = coreRuleSet
|
params.CoreRuleSet = coreRuleSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// addCounters adds to params the metrics, the table of clients, the ledger
|
// addCounters adds to params the table of clients, the ledger and the
|
||||||
// and the anomaly counters, as sww builds them from params' settings. The
|
// anomaly counters, as sww builds them from params' settings.
|
||||||
// metrics read the ledger, the table of clients and the rules.
|
|
||||||
func addCounters(params *waf.Params) {
|
func addCounters(params *waf.Params) {
|
||||||
cfg := params.Config
|
cfg := params.Config
|
||||||
|
|
||||||
params.Metrics = metrics.New(
|
|
||||||
cfg.MetricsBusiestCountriesAndASNs,
|
|
||||||
cfg.InstanceName,
|
|
||||||
)
|
|
||||||
params.Limiter = ratelimit.New(ratelimit.Limits{
|
params.Limiter = ratelimit.New(ratelimit.Limits{
|
||||||
PerMinute: cfg.RateLimitPerMinute,
|
PerMinute: cfg.RateLimitPerMinute,
|
||||||
PerHour: cfg.RateLimitPerHour,
|
PerHour: cfg.RateLimitPerHour,
|
||||||
@@ -587,26 +582,18 @@ func addCounters(params *waf.Params) {
|
|||||||
NamedNetblocks: cfg.AnomalyWatchedNets,
|
NamedNetblocks: cfg.AnomalyWatchedNets,
|
||||||
Alerts: params.Alerts,
|
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
|
// addOutsideSources adds to params the lists fetched from URLs, the DNSBL
|
||||||
// zones' verdicts, AbuseIPDB's scores from abuseIPDBCheckURL and its
|
// zones' verdicts, AbuseIPDB's scores from abuseIPDBCheckURL and its
|
||||||
// blacklist from abuseIPDBBlacklistURL, routewatch's answers while its
|
// blacklist from abuseIPDBBlacklistURL, routewatch's answers while its
|
||||||
// setting is set, and GeoJS at geojsURL, as sww builds
|
// setting is set, the metrics newMetrics returns, and GeoJS at geojsURL, as
|
||||||
// them from params' settings and the parts addCounters added. The metrics
|
// sww builds them from params' settings and the parts addCounters added.
|
||||||
// read the lists, the zones' verdicts, and AbuseIPDB's while its key is set.
|
|
||||||
func addOutsideSources(
|
func addOutsideSources(
|
||||||
params *waf.Params, geojsURL, abuseIPDBCheckURL string,
|
params *waf.Params, geojsURL, abuseIPDBCheckURL string,
|
||||||
) {
|
) {
|
||||||
cfg := params.Config
|
cfg := params.Config
|
||||||
m, limiter, ledger := params.Metrics, params.Limiter, params.Ledger
|
limiter, ledger := params.Limiter, params.Ledger
|
||||||
processLog, alertQueue := params.ProcessLog, params.Alerts
|
processLog, alertQueue := params.ProcessLog, params.Alerts
|
||||||
|
|
||||||
params.Lists = reputation.New(reputation.Params{
|
params.Lists = reputation.New(reputation.Params{
|
||||||
@@ -655,6 +642,9 @@ func addOutsideSources(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m := newMetrics(params)
|
||||||
|
params.Metrics = m
|
||||||
|
|
||||||
params.GeoJS = geojs.New(geojs.Params{
|
params.GeoJS = geojs.New(geojs.Params{
|
||||||
URL: geojsURL,
|
URL: geojsURL,
|
||||||
Answered: func(answer geojs.Answer) {
|
Answered: func(answer geojs.Answer) {
|
||||||
@@ -667,12 +657,29 @@ func addOutsideSources(
|
|||||||
},
|
},
|
||||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
m.AddReputation(params.Lists, params.DNSBL)
|
// newMetrics returns the metrics, as sww builds them from params' settings,
|
||||||
|
// read from the ledger, the table of clients, the rules, the lists, the
|
||||||
|
// zones' verdicts, and AbuseIPDB's while its key is set.
|
||||||
|
func newMetrics(params *waf.Params) *metrics.Metrics {
|
||||||
|
cfg := params.Config
|
||||||
|
metricsParams := metrics.Params{
|
||||||
|
BusiestCount: cfg.MetricsBusiestCountriesAndASNs,
|
||||||
|
InstanceName: cfg.InstanceName,
|
||||||
|
Now: params.Now,
|
||||||
|
Bans: params.Ledger,
|
||||||
|
Clients: params.Limiter,
|
||||||
|
Rules: params.Rules,
|
||||||
|
Lists: params.Lists,
|
||||||
|
DNSBL: params.DNSBL,
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.AbuseIPDBKey != "" {
|
if cfg.AbuseIPDBKey != "" {
|
||||||
m.AddAbuseIPDB(params.AbuseIPDB)
|
metricsParams.AbuseIPDB = params.AbuseIPDB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return metrics.New(metricsParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newClient returns an HTTP client that sends requests as they are made,
|
// newClient returns an HTTP client that sends requests as they are made,
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"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
|
// noteListed notes each of sources, the URLs of the blocklists or the
|
||||||
// DNSBL zones, their keys masked, that list the client, as noteHit does,
|
// DNSBL zones, their keys masked, that list the client, as noteHit does,
|
||||||
// with reason.
|
// with reason.
|
||||||
|
|||||||
@@ -421,7 +421,7 @@ func (rq *request) answerAtTheEnd() (geojs.Answer, bool) {
|
|||||||
return geojs.Answer{}, false
|
return geojs.Answer{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
if rq.h.config.LookupSource == "file" {
|
||||||
return rq.lookupAnswer, true
|
return rq.lookupAnswer, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user