Something went wrong. Try again.
Webhook-to-SSE gateway with hierarchical topic routing and signature verification
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105package main
import ( "os" "path/filepath" "strings" "testing")
func loadYAML(t *testing.T, content string) (*Configuration, error) { t.Helper() path := filepath.Join(t.TempDir(), "wicket.yaml") if err := os.WriteFile(path, []byte(content), 0644); err != nil { t.Fatal(err) } return LoadConfiguration(path)}
// os.ExpandEnv turns an unset variable into "", and an empty HMAC key or// bearer token is one that anyone can sign or send.func TestLoadConfiguration_refusesAVerificationWithoutItsSecret(t *testing.T) { cases := []struct { name string path string wantErr string }{ {"hmac-sha256 without a secret", "verify: hmac-sha256\n signature_header: X-Sig", `path "hooks": verify hmac-sha256 needs a secret`}, {"hmac-sha1 without a secret", "verify: hmac-sha1\n signature_header: X-Sig", `path "hooks": verify hmac-sha1 needs a secret`}, {"bearer without a secret", "verify: bearer", `path "hooks": verify bearer needs a secret`}, {"bearer with an unset variable", `verify: bearer` + "\n secret: \"${WICKET_UNSET_VAR_xyz}\"", `path "hooks": verify bearer needs a secret`}, {"rfc9421 without keys_url", "verify: rfc9421", `path "hooks": verify rfc9421 needs a keys_url`}, {"a method wicket does not have", "verify: hmac-sha512\n secret: s", `path "hooks": unknown verification method: hmac-sha512`}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { _, err := loadYAML(t, "paths:\n hooks:\n "+c.path+"\n") if err == nil || !strings.Contains(err.Error(), c.wantErr) { t.Errorf("LoadConfiguration() = %v, want %q", err, c.wantErr) } }) }}
func TestLoadConfiguration_acceptsEachVerificationWithItsSecret(t *testing.T) { cases := []struct { name string path string }{ {"hmac-sha256", "verify: hmac-sha256\n secret: s\n signature_header: X-Sig"}, {"hmac-sha1", "verify: hmac-sha1\n secret: s\n signature_header: X-Sig"}, {"bearer", "verify: bearer\n secret: s"}, {"rfc9421", "verify: rfc9421\n keys_url: https://keys.example.com/jwks.json"}, {"no verification", "subscribe_secret: s"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if _, err := loadYAML(t, "paths:\n hooks:\n "+c.path+"\n"); err != nil { t.Errorf("LoadConfiguration() = %v, want no error", err) } }) }}
// A subscribe_secret that names an unset variable expands to "", and a// path whose only protection is "" would admit every subscriber.func TestLoadConfiguration_refusesAnEmptySubscribeSecret(t *testing.T) { cases := []struct { name string path string }{ {"an unset variable", `subscribe_secret: "${WICKET_UNSET_VAR_xyz}"`}, {"an empty string", `subscribe_secret: ""`}, {"an empty string beside service accounts", "subscribe_secret: \"\"\n subscribe_service_accounts: [ns/sa]"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { _, err := loadYAML(t, "paths:\n hooks:\n "+c.path+"\n") want := `path "hooks": subscribe_secret is empty` if err == nil || !strings.Contains(err.Error(), want) { t.Errorf("LoadConfiguration() = %v, want %q", err, want) } }) }}
func TestConfigurationFile_aReloadThatDoesNotValidateKeepsTheConfigurationThatWorked(t *testing.T) { file, current, _ := newConfigurationFile(t, "paths:\n alertmanager:\n verify: bearer\n secret: the-token\n") file.Reload() os.WriteFile(file.path, []byte("paths:\n alertmanager:\n verify: bearer\n"), 0644)
if err := file.Reload(); err == nil { t.Fatal("a reload of a bearer path without a secret succeeded") }
if pc := current.Load().LookupVerification("alertmanager"); pc == nil || pc.Secret != "the-token" { t.Errorf("a failed reload replaced the configuration: %+v", pc) }}
func TestLoadConfiguration_theExampleLoads(t *testing.T) { if _, err := LoadConfiguration("wicket.yaml.example"); err != nil { t.Errorf("LoadConfiguration(wicket.yaml.example) = %v", err) }}