fix(19.3-06): warn at boot when turnstile routes have no secret (WR-08)
- BuildRouter logs one warning with the route count and the production flag when routes name turnstile:open or turnstile:closed and the verifier has no secret; the secret is never logged
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -659,3 +662,59 @@ func TestCORSProfileVaryKeptWithHandlerVary(t *testing.T) {
|
||||
t.Errorf("Vary = %q, want Accept-Language too", rec.Header().Values("Vary"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurnstileEmptySecretWarnsAtBoot(t *testing.T) {
|
||||
// boot builds a router on an app whose published logger writes JSON
|
||||
// lines to a buffer and returns the warnings naming turnstile.secret.
|
||||
boot := func(t *testing.T, env string, verifier *bouncer.Turnstile, plugins ...party.Plugin) []map[string]any {
|
||||
t.Helper()
|
||||
app := transportApp(t, env, map[string]string{"http.yaml": transportBodyLimits})
|
||||
var buf bytes.Buffer
|
||||
if err := app.Publish(slog.New(slog.NewJSONHandler(&buf, nil))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if verifier != nil {
|
||||
if err := app.Publish[*bouncer.Turnstile](verifier); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := BuildRouter(app, plugins); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out []map[string]any
|
||||
for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
var rec map[string]any
|
||||
if err := json.Unmarshal([]byte(line), &rec); err != nil {
|
||||
t.Fatalf("log line %q: %v", line, err)
|
||||
}
|
||||
if msg, _ := rec["msg"].(string); strings.HasPrefix(msg, "surf: turnstile.secret is empty") {
|
||||
out = append(out, rec)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
got := boot(t, "production", nil, turnstileRoutes("turnstile:closed", "turnstile:open"))
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("warnings = %v, want one", got)
|
||||
}
|
||||
if got[0]["level"] != "WARN" || got[0]["routes"] != float64(2) || got[0]["production"] != true {
|
||||
t.Fatalf("warning = %v, want WARN with routes 2 and production true", got[0])
|
||||
}
|
||||
if msg := got[0]["msg"].(string); !strings.Contains(msg, "turnstile:open passes every request") || !strings.Contains(msg, "503 in production") {
|
||||
t.Errorf("message = %q", msg)
|
||||
}
|
||||
|
||||
// A published verifier with a secret: no warning.
|
||||
if got := boot(t, "production", bouncer.NewTurnstile("test-secret", true), turnstileRoutes("turnstile:closed", "turnstile:open")); len(got) != 0 {
|
||||
t.Fatalf("warnings with a secret = %v", got)
|
||||
}
|
||||
// No turnstile routes: no warning.
|
||||
plain := routesPluginFunc{id: "acme.plain", routes: func(r pact.Router) { r.Get("/x", okHandler) }}
|
||||
if got := boot(t, "development", nil, plain); len(got) != 0 {
|
||||
t.Fatalf("warnings without turnstile routes = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user