Read every setting through viper (closes #211)
check / check (push) Successful in 3m47s

servewaf's and rproxy's settings are read through viper, bound to the same environment variable names as before, in place of the os.LookupEnv function Main passed down. NewViper turns AutomaticEnv and AllowEmptyEnv on, so a setting that is set but empty reads as set, as it did. A setting may also be given as a file, the _FILE form, which rproxy's settings now have too; rproxy still writes nothing to disk. The settings tests pass set into a viper instead of a lookup function.

Model: deepseek-v4-pro
This commit is contained in:
sneak committed 2026-11-07 05:29:14 +00:00
1 parent 2e2f6f7175
commit 3fa1f4b732
19 files changed
+274 -185

No files matched your search

+15 -10
View File
@@ -15,6 +15,8 @@ import (
"testing"
"time"
"github.com/spf13/viper"
"sneak.berlin/go/smallwebwaf/internal/alerts"
"sneak.berlin/go/smallwebwaf/internal/config"
"sneak.berlin/go/smallwebwaf/internal/lookup"
@@ -347,6 +349,17 @@ func serve(t *testing.T, server *servewaf.Server) {
// at serveWAFAddr, with the settings in env on top of the defaults, and returns
// where it listens. What it writes is dropped. SWRP_TIMEOUT_MS is a minute unless
// env sets it, so that on a busy host SWRP_DEFAULT_DISPOSITION does not decide.
// viperFrom returns a viper Set with each of settings, as the viper
// config.NewViper returns one bound to a process's environment.
func viperFrom(settings map[string]string) *viper.Viper {
v := viper.New()
for name, value := range settings {
v.Set(name, value)
}
return v
}
func startRProxy(
t *testing.T, serveWAFAddr, appURL string, env map[string]string,
) string {
@@ -359,11 +372,7 @@ func startRProxy(
}
maps.Copy(settings, env)
cfg, err := config.RProxyFromEnvironment(func(name string) (string, bool) {
value, ok := settings[name]
return value, ok
})
cfg, err := config.RProxyFromEnvironment(viperFrom(settings))
if err != nil {
t.Fatalf("settings %v: %v", settings, err)
}
@@ -423,11 +432,7 @@ func newServeWAFWith(
maps.Copy(settings, env)
cfg, err := config.FromEnvironment(func(name string) (string, bool) {
value, ok := settings[name]
return value, ok
})
cfg, err := config.FromEnvironment(viperFrom(settings))
if err != nil {
t.Fatalf("settings %v: %v", settings, err)
}