waf.Server held its parts as public fields only for the tests to read. The tests now keep the Params the server was built from in a test-only wafAndParts, which embeds both, so every assertion reads as before. newWAFWith takes a change to the Params instead of a build function, so the Params kept are the ones the server was built from. Model: opus-5-5
This commit is contained in:
1 parent
56a57125f1
commit
45252b7486
16 files changed
+81
-98
No files matched your search
@@ -54,7 +54,7 @@ func TestClientsOnTheAbuseIPDBBlacklistAreBannedForTheBanDays(t *testing.T) {
|
||||
})
|
||||
|
||||
// Two clients in one netblock make one ban, with the higher score.
|
||||
server.BanAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||
{Addr: netip.MustParseAddr(client), Score: 95},
|
||||
{Addr: netip.MustParseAddr(otherClient), Score: 100},
|
||||
{Addr: netip.MustParseAddr("2001:db8:5::1"), Score: 90},
|
||||
@@ -88,7 +88,7 @@ func TestAbuseIPDBBlacklistMakesNoBanInObserveMode(t *testing.T) {
|
||||
map[string]string{mode: "observe"},
|
||||
)
|
||||
|
||||
server.BanAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||
{Addr: netip.MustParseAddr(client), Score: 100},
|
||||
})
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -250,7 +249,7 @@ func TestObserveModeWorksOutABanOnlyWhenItsAlertWouldBeSent(t *testing.T) {
|
||||
// never sent them, and returns the queue they wait in as well.
|
||||
func startWithAlerts(
|
||||
t *testing.T, env map[string]string,
|
||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
||||
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
return startAppWithAlerts(
|
||||
@@ -263,7 +262,7 @@ func startWithAlerts(
|
||||
// startAppWithAlerts is startWithAlerts in front of the app handler.
|
||||
func startAppWithAlerts(
|
||||
t *testing.T, handler http.HandlerFunc, env map[string]string,
|
||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
||||
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
app := startApp(t, handler)
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
// The anomaly thresholds: the prefix of a scope followed by the end of a
|
||||
@@ -332,7 +331,7 @@ func TestRequestCountsForTheASNumberGeoJSGivesBeforeItEnds(t *testing.T) {
|
||||
// kept.
|
||||
geojsURL, _, release := startHeldGeoJS(t)
|
||||
|
||||
var server atomic.Pointer[waf.Server]
|
||||
var server atomic.Pointer[wafAndParts]
|
||||
|
||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {
|
||||
release()
|
||||
|
||||
@@ -411,7 +411,7 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) {
|
||||
env map[string]string
|
||||
path string
|
||||
// prepare gets the server ready, at now, before the question.
|
||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
||||
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||
}{
|
||||
{"SWW_WAF_DENY_NETS", map[string]string{denyNets: client}, "/", leaveAsItIs},
|
||||
{"a ban", nil, "/", banClient},
|
||||
@@ -426,21 +426,21 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) {
|
||||
"a DNS blocklist", map[string]string{
|
||||
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionDeny,
|
||||
},
|
||||
"/", func(_ *testing.T, server *waf.Server, _ time.Time) {
|
||||
"/", func(_ *testing.T, server *wafAndParts, _ time.Time) {
|
||||
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
||||
},
|
||||
},
|
||||
{
|
||||
"AbuseIPDB",
|
||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
||||
"/", func(_ *testing.T, server *waf.Server, _ time.Time) {
|
||||
"/", func(_ *testing.T, server *wafAndParts, _ time.Time) {
|
||||
// Over SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default.
|
||||
loadScores(server, map[string]int64{client: 80})
|
||||
},
|
||||
},
|
||||
{
|
||||
"SWW_WAF_TAG_DENY", map[string]string{tagLists: "vpn=" + vpnList, tagDeny: "vpn"},
|
||||
"/", func(t *testing.T, server *waf.Server, _ time.Time) {
|
||||
"/", func(t *testing.T, server *wafAndParts, _ time.Time) {
|
||||
t.Helper()
|
||||
loadLists(t, server, map[string][]string{vpnList: {client}})
|
||||
},
|
||||
@@ -647,7 +647,7 @@ func TestObserveModeAsksGeoJSNothingForARequestOverTheHeaderLimits(
|
||||
// for env. No request to it may use the network.
|
||||
func newWAFInBubble(
|
||||
t *testing.T, env map[string]string,
|
||||
) (*waf.Server, *output) {
|
||||
) (*wafAndParts, *output) {
|
||||
t.Helper()
|
||||
|
||||
settings := map[string]string{tagDeny: "country=KP"}
|
||||
@@ -661,7 +661,7 @@ func newWAFInBubble(
|
||||
// askDirectly hands a question about a request from client straight to server,
|
||||
// as change leaves it, and returns the answer's status and how long it took.
|
||||
func askDirectly(
|
||||
t *testing.T, server *waf.Server, change func(*http.Request),
|
||||
t *testing.T, server *wafAndParts, change func(*http.Request),
|
||||
) (int, time.Duration) {
|
||||
t.Helper()
|
||||
|
||||
@@ -685,7 +685,7 @@ func askDirectly(
|
||||
}
|
||||
|
||||
// banClient bans client at now.
|
||||
func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
func banClient(_ *testing.T, server *wafAndParts, now time.Time) {
|
||||
server.Ledger.Ban(
|
||||
netip.MustParsePrefix(client+"/32"),
|
||||
now,
|
||||
@@ -694,7 +694,7 @@ func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
}
|
||||
|
||||
// countClient counts one request from client at now for the rate limits.
|
||||
func countClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
func countClient(_ *testing.T, server *wafAndParts, now time.Time) {
|
||||
server.Limiter.Count(
|
||||
netip.MustParsePrefix(client+"/32"),
|
||||
now,
|
||||
@@ -703,7 +703,7 @@ func countClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
}
|
||||
|
||||
// listClient puts client on the blocklist at dropURL.
|
||||
func listClient(t *testing.T, server *waf.Server, now time.Time) {
|
||||
func listClient(t *testing.T, server *wafAndParts, now time.Time) {
|
||||
t.Helper()
|
||||
|
||||
err := server.Lists.Load([]reputation.List{{
|
||||
@@ -715,7 +715,7 @@ func listClient(t *testing.T, server *waf.Server, now time.Time) {
|
||||
}
|
||||
|
||||
// leaveAsItIs prepares nothing.
|
||||
func leaveAsItIs(*testing.T, *waf.Server, time.Time) {}
|
||||
func leaveAsItIs(*testing.T, *wafAndParts, time.Time) {}
|
||||
|
||||
func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -726,7 +726,7 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
||||
name string
|
||||
env map[string]string
|
||||
// prepare gets the server ready, at now, before the question.
|
||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
||||
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||
// cookie is the request's Cookie header.
|
||||
cookie string
|
||||
status int
|
||||
@@ -811,7 +811,7 @@ func TestAQuestionWaitsForASlowGeoJSOnlyAsLongAsTheDefaultSays(
|
||||
name string
|
||||
env map[string]string
|
||||
// prepare gets the server ready, at now, before the question.
|
||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
||||
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||
status int
|
||||
action string
|
||||
}{
|
||||
@@ -877,7 +877,7 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs(
|
||||
held, _, _ := startHeldGeoJS(t)
|
||||
resolver, releaseResolver := startHeldResolver(t)
|
||||
|
||||
server, out, _ := newWAFWith(t, waf.New,
|
||||
server, out, _ := newWAFWith(t, func(*waf.Params) {},
|
||||
silentGeoJSURL, silentAbuseIPDBURL, time.Now, map[string]string{
|
||||
tagDeny: "country=KP", coreRuleSetMode: block,
|
||||
dnsblZones: dnsblZone, dnsblResolver: resolver,
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -466,7 +465,7 @@ func (c *clock) advance(d time.Duration) {
|
||||
// window.
|
||||
func startWithClock(
|
||||
t *testing.T, geojsURL string, env map[string]string,
|
||||
) (*sender, *clock, *waf.Server) {
|
||||
) (*sender, *clock, *wafAndParts) {
|
||||
t.Helper()
|
||||
|
||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {})
|
||||
|
||||
@@ -62,7 +62,7 @@ func coreRuleSetMatch() coreruleset.Result {
|
||||
// at its default but for env. No request to it may use the network.
|
||||
func newWAFHoldingTheCoreRuleSet(
|
||||
t *testing.T, crs *heldCoreRuleSet, env map[string]string,
|
||||
) (*waf.Server, *output, *alerts.Queue) {
|
||||
) (*wafAndParts, *output, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
settings := map[string]string{
|
||||
@@ -71,11 +71,9 @@ func newWAFHoldingTheCoreRuleSet(
|
||||
}
|
||||
maps.Copy(settings, env)
|
||||
|
||||
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
|
||||
server, out, queue := newWAFWith(t, func(params *waf.Params) {
|
||||
params.CoreRuleSet = crs
|
||||
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
|
||||
|
||||
return waf.New(params)
|
||||
}, silentGeoJSURL, abuseIPDBURL, time.Now, settings)
|
||||
|
||||
return server, out, queue
|
||||
@@ -105,7 +103,7 @@ func wantAnswer(
|
||||
// left no trace: line has none of its fields, and server has counted no match
|
||||
// of it and raised no alert for one.
|
||||
func wantNoCoreRuleSetTrace(
|
||||
t *testing.T, server *waf.Server, queue *alerts.Queue, line logLine,
|
||||
t *testing.T, server *wafAndParts, queue *alerts.Queue, line logLine,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -529,7 +529,7 @@ func TestBanForALoweredLimitGivesThePercentageInItsNotesAndItsAlert(
|
||||
// clock.
|
||||
func startWithLookups(
|
||||
t *testing.T, env map[string]string,
|
||||
) (*sender, *waf.Server, *alerts.Queue) {
|
||||
) (*sender, *wafAndParts, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
s, _, server, queue := startWithLookupsAndClock(t, env)
|
||||
@@ -544,7 +544,7 @@ func startWithLookups(
|
||||
// country, and no other address.
|
||||
func startWithLookupsAndClock(
|
||||
t *testing.T, env map[string]string,
|
||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
||||
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
path := filepath.Join(t.TempDir(), "ipinfo_lite.mmdb")
|
||||
@@ -584,7 +584,7 @@ func (s *sender) uploadFrom(from string) logLine {
|
||||
// straight to server's handler, without the network, and returns once it is
|
||||
// answered.
|
||||
func serveFromDE(
|
||||
t *testing.T, server *waf.Server, method string, body io.Reader,
|
||||
t *testing.T, server *wafAndParts, method string, body io.Reader,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
package waf
|
||||
|
||||
import "sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
|
||||
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
||||
const BesideLookupTimeLimit = besideLookupTimeLimit
|
||||
|
||||
// BanAbuseIPDBBlacklist does on s what a fetch of AbuseIPDB's blacklist with
|
||||
// the clients on listed does.
|
||||
func (s *Server) BanAbuseIPDBBlacklist(listed []reputation.BlacklistEntry) {
|
||||
h := s.Handler.(*handler) //nolint:forcetypeassert // New makes it one
|
||||
BanAbuseIPDBBlacklist(h.config, h.ledger, h.processLog, h.now(), listed)
|
||||
}
|
||||
@@ -150,7 +150,7 @@ func TestPathUnderTheOwnPrefixIsNeitherForwardedNorRefused(t *testing.T) {
|
||||
|
||||
// historyOf returns the history of the client at addr.
|
||||
func historyOf(
|
||||
t *testing.T, server *waf.Server, addr string,
|
||||
t *testing.T, server *wafAndParts, addr string,
|
||||
) ratelimit.History {
|
||||
t.Helper()
|
||||
|
||||
|
||||
+1
-13
@@ -115,21 +115,9 @@ type CoreRuleSetInspector interface {
|
||||
) (coreruleset.Result, []byte, error)
|
||||
}
|
||||
|
||||
// Server is the server waf runs, with the parts of its Params. The tests
|
||||
// read them.
|
||||
// Server is the server waf runs.
|
||||
type Server struct {
|
||||
*http.Server
|
||||
|
||||
Ledger *bans.Ledger
|
||||
Limiter *ratelimit.Limiter
|
||||
GeoJS *geojs.GeoJS
|
||||
Anomalies *anomaly.Counters
|
||||
LookupFile *lookup.File
|
||||
Lists *reputation.Lists
|
||||
DNSBL *reputation.DNSBL
|
||||
AbuseIPDB *reputation.AbuseIPDB
|
||||
RouteWatch *routewatch.Client
|
||||
Metrics *metrics.Metrics
|
||||
}
|
||||
|
||||
// newHandler returns the handler with what every request shares, the parts
|
||||
|
||||
+36
-12
@@ -316,7 +316,7 @@ func startProxyWithGeoJS(
|
||||
func startProxyWithClock(
|
||||
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
||||
env map[string]string,
|
||||
) (string, *output, *waf.Server) {
|
||||
) (string, *output, *wafAndParts) {
|
||||
t.Helper()
|
||||
|
||||
addr, out, server, _ := startProxyWithAlerts(
|
||||
@@ -335,7 +335,7 @@ func startProxyWithClock(
|
||||
func startProxyWithAlerts(
|
||||
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
||||
env map[string]string,
|
||||
) (string, *output, *waf.Server, *alerts.Queue) {
|
||||
) (string, *output, *wafAndParts, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
server, out, alertQueue := newWAF(t, geojsURL, now, env)
|
||||
@@ -346,7 +346,7 @@ func startProxyWithAlerts(
|
||||
|
||||
// serve serves server on a port of its own until the test ends, and sets its
|
||||
// Addr to where it listens.
|
||||
func serve(t *testing.T, server *waf.Server) {
|
||||
func serve(t *testing.T, server *wafAndParts) {
|
||||
t.Helper()
|
||||
|
||||
listener, err := (&net.ListenConfig{}).Listen(
|
||||
@@ -422,8 +422,29 @@ func startRProxy(
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
// newWAF makes waf's server, without starting it, and
|
||||
// returns it, what it writes, and the queue of the alerts it raises, as the
|
||||
// wafAndParts is waf's server with the Params it was built from, so that a
|
||||
// test can read the parts the server uses.
|
||||
type wafAndParts struct {
|
||||
*waf.Server
|
||||
waf.Params
|
||||
}
|
||||
|
||||
// banAbuseIPDBBlacklist does on w what a fetch of AbuseIPDB's blacklist with
|
||||
// the clients on listed does.
|
||||
func (w *wafAndParts) banAbuseIPDBBlacklist(
|
||||
listed []reputation.BlacklistEntry,
|
||||
) {
|
||||
waf.BanAbuseIPDBBlacklist(
|
||||
w.Config,
|
||||
w.Ledger,
|
||||
w.ProcessLog,
|
||||
w.Now(),
|
||||
listed,
|
||||
)
|
||||
}
|
||||
|
||||
// newWAF makes waf's server, without starting it, and returns it with its
|
||||
// parts, what it writes, and the queue of the alerts it raises, as the
|
||||
// settings in env make it. No alert is sent from the queue: they wait in it,
|
||||
// for the test to look at.
|
||||
// With no geojsURL, there is no stand-in for GeoJS to look clients up at, and
|
||||
@@ -438,19 +459,21 @@ func newWAF(
|
||||
geojsURL string,
|
||||
now func() time.Time,
|
||||
env map[string]string,
|
||||
) (*waf.Server, *output, *alerts.Queue) {
|
||||
) (*wafAndParts, *output, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
return newWAFWith(t, waf.New, geojsURL, abuseIPDBURL, now, env)
|
||||
return newWAFWith(
|
||||
t, func(*waf.Params) {}, geojsURL, abuseIPDBURL, now, env,
|
||||
)
|
||||
}
|
||||
|
||||
// newWAFWith is newWAF for the server build makes, with clients checked with
|
||||
// AbuseIPDB at abuseIPDBCheckURL.
|
||||
// newWAFWith is newWAF with change applied to the Params before the server is
|
||||
// built from them, and clients checked with AbuseIPDB at abuseIPDBCheckURL.
|
||||
func newWAFWith(
|
||||
t *testing.T, build func(waf.Params) *waf.Server,
|
||||
t *testing.T, change func(*waf.Params),
|
||||
geojsURL, abuseIPDBCheckURL string, now func() time.Time,
|
||||
env map[string]string,
|
||||
) (*waf.Server, *output, *alerts.Queue) {
|
||||
) (*wafAndParts, *output, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
settings := map[string]string{
|
||||
@@ -518,8 +541,9 @@ func newWAFWith(
|
||||
addCoreRuleSet(t, ¶ms)
|
||||
addCounters(¶ms)
|
||||
addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL)
|
||||
change(¶ms)
|
||||
|
||||
server := build(params)
|
||||
server := &wafAndParts{Server: waf.New(params), Params: params}
|
||||
|
||||
return server, out, alertQueue
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
const refusalsLimitPerMinute = "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE"
|
||||
@@ -52,7 +51,7 @@ func (r refused) send(s *sender, from string) logLine {
|
||||
// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE at limit, and the settings in env.
|
||||
func startForRefusalLimit(
|
||||
t *testing.T, limit string, env map[string]string,
|
||||
) (*sender, *clock, *waf.Server) {
|
||||
) (*sender, *clock, *wafAndParts) {
|
||||
t.Helper()
|
||||
|
||||
settings := map[string]string{
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
// The reputation settings.
|
||||
@@ -139,13 +138,13 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
||||
for _, tc := range []struct {
|
||||
heuristic string
|
||||
env map[string]string
|
||||
load func(*testing.T, *waf.Server)
|
||||
load func(*testing.T, *wafAndParts)
|
||||
figures string
|
||||
}{
|
||||
{
|
||||
"blocklists",
|
||||
map[string]string{blocklistURLs: dropURL, blocklistAction: actionDeny},
|
||||
func(t *testing.T, server *waf.Server) {
|
||||
func(t *testing.T, server *wafAndParts) {
|
||||
t.Helper()
|
||||
loadLists(t, server, map[string][]string{dropURL: {client}})
|
||||
},
|
||||
@@ -157,7 +156,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
||||
dnsblZones: dnsblZone, dnsblResolver: noResolver,
|
||||
dnsblAbuseIPDBAction: actionDeny,
|
||||
},
|
||||
func(_ *testing.T, server *waf.Server) {
|
||||
func(_ *testing.T, server *wafAndParts) {
|
||||
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
||||
},
|
||||
`{"zones": ["` + dnsblZone + `"]}`,
|
||||
@@ -165,7 +164,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
||||
{
|
||||
"abuseipdb",
|
||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
||||
func(_ *testing.T, server *waf.Server) {
|
||||
func(_ *testing.T, server *wafAndParts) {
|
||||
loadScores(server, map[string]int64{client: 80})
|
||||
},
|
||||
`{"score": 80, "min_score": 75}`,
|
||||
@@ -560,7 +559,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
env map[string]string
|
||||
load func(*waf.Server)
|
||||
load func(*wafAndParts)
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
@@ -568,7 +567,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
||||
map[string]string{
|
||||
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionLog,
|
||||
},
|
||||
func(server *waf.Server) {
|
||||
func(server *wafAndParts) {
|
||||
loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}})
|
||||
},
|
||||
[]string{"asn=64496", "country=DE", "dnsbl=" + dnsblZone},
|
||||
@@ -576,7 +575,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
||||
{
|
||||
abuseipdb,
|
||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog},
|
||||
func(server *waf.Server) {
|
||||
func(server *wafAndParts) {
|
||||
loadScores(server, map[string]int64{fromDE: 80})
|
||||
},
|
||||
[]string{abuseipdb, "asn=64496", "country=DE"},
|
||||
@@ -1256,7 +1255,7 @@ func verdictsFetched() time.Time {
|
||||
// verdictsFetched, as dnsbl.json would at start: one that lists the
|
||||
// client from each zone listedBy gives for it, and one that does not from
|
||||
// each other zone.
|
||||
func loadVerdicts(server *waf.Server, listedBy map[string][]string) {
|
||||
func loadVerdicts(server *wafAndParts, listedBy map[string][]string) {
|
||||
verdicts := make(
|
||||
[]reputation.Verdict,
|
||||
0,
|
||||
@@ -1281,7 +1280,7 @@ func loadVerdicts(server *waf.Server, listedBy map[string][]string) {
|
||||
// loadScores puts into server's AbuseIPDB the score scores gives each
|
||||
// client, an IPv4 address, fetched at verdictsFetched, as
|
||||
// abuseipdb-scores.json would at start.
|
||||
func loadScores(server *waf.Server, scores map[string]int64) {
|
||||
func loadScores(server *wafAndParts, scores map[string]int64) {
|
||||
kept := make([]reputation.Score, 0, len(scores))
|
||||
for client, score := range scores {
|
||||
kept = append(kept, reputation.Score{
|
||||
@@ -1295,7 +1294,7 @@ func loadScores(server *waf.Server, scores map[string]int64) {
|
||||
|
||||
// wantAbuseIPDBChecks checks how many clients server has checked with
|
||||
// AbuseIPDB.
|
||||
func wantAbuseIPDBChecks(t *testing.T, server *waf.Server, want int) {
|
||||
func wantAbuseIPDBChecks(t *testing.T, server *wafAndParts, want int) {
|
||||
t.Helper()
|
||||
|
||||
if got := server.AbuseIPDB.Checked(); got != want {
|
||||
@@ -1307,7 +1306,7 @@ func wantAbuseIPDBChecks(t *testing.T, server *waf.Server, want int) {
|
||||
// its lines, fetched at listsFetched, as lists.json would at start.
|
||||
func loadLists(
|
||||
t *testing.T,
|
||||
server *waf.Server,
|
||||
server *wafAndParts,
|
||||
copies map[string][]string,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
|
||||
// routeWatchURL is SWW_WAF_ROUTEWATCH_URL, and tagsMissing the log line's field
|
||||
@@ -58,7 +57,7 @@ func startRouteWatch(t *testing.T, routes map[string]standInRoute) string {
|
||||
// waitForRouteWatch waits until routewatch's answer about from, in the client
|
||||
// group of the given length, is kept, so that the next request gets its tags.
|
||||
func waitForRouteWatch(
|
||||
t *testing.T, server *waf.Server, from string, bits int,
|
||||
t *testing.T, server *wafAndParts, from string, bits int,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
+1
-13
@@ -104,12 +104,10 @@ type Report struct {
|
||||
// is far above rproxy's, and a question refused for its size would have rproxy
|
||||
// let the request through.
|
||||
func New(params Params) *Server {
|
||||
h := newHandler(params)
|
||||
|
||||
return &Server{
|
||||
Server: &http.Server{
|
||||
Addr: params.Config.ListenAddr,
|
||||
Handler: h,
|
||||
Handler: newHandler(params),
|
||||
ErrorLog: slog.NewLogLogger(
|
||||
params.ProcessLog.Handler(),
|
||||
slog.LevelWarn,
|
||||
@@ -118,16 +116,6 @@ func New(params Params) *Server {
|
||||
WriteTimeout: wafTimeout,
|
||||
IdleTimeout: wafIdleTimeout,
|
||||
},
|
||||
Ledger: h.ledger,
|
||||
Limiter: h.limiter,
|
||||
GeoJS: h.geojs,
|
||||
Anomalies: h.anomalies,
|
||||
LookupFile: h.lookupFile,
|
||||
Lists: params.Lists,
|
||||
DNSBL: params.DNSBL,
|
||||
AbuseIPDB: params.AbuseIPDB,
|
||||
RouteWatch: params.RouteWatch,
|
||||
Metrics: h.metrics,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ type WAF struct {
|
||||
t *testing.T
|
||||
addr string
|
||||
out *output
|
||||
server *waf.Server
|
||||
server *wafAndParts
|
||||
clock *clock
|
||||
queue *alerts.Queue
|
||||
}
|
||||
@@ -69,14 +69,14 @@ func startWAF(
|
||||
) *WAF {
|
||||
t.Helper()
|
||||
|
||||
return startWAFWith(t, geojsURL, env, func(*waf.Server) {})
|
||||
return startWAFWith(t, geojsURL, env, func(*wafAndParts) {})
|
||||
}
|
||||
|
||||
// startWAFWith is startWAF with change applied to the server before
|
||||
// it starts.
|
||||
func startWAFWith(
|
||||
t *testing.T, geojsURL string, env map[string]string,
|
||||
change func(*waf.Server),
|
||||
change func(*wafAndParts),
|
||||
) *WAF {
|
||||
t.Helper()
|
||||
|
||||
@@ -663,7 +663,7 @@ func TestQuestionWhoseBodyStopsArrivingGetsNoAnswer(t *testing.T) {
|
||||
t,
|
||||
"",
|
||||
map[string]string{metricsToken: token},
|
||||
func(server *waf.Server) { server.ReadTimeout = readTimeout },
|
||||
func(server *wafAndParts) { server.ReadTimeout = readTimeout },
|
||||
)
|
||||
|
||||
conn := dial(t, s.addr)
|
||||
|
||||
Reference in new issue
Block a user