130 lines
4.3 KiB
Go
130 lines
4.3 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"filippo.io/age"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/seal"
|
|
"github.com/samcharles93/archie-core/modules/connectors"
|
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// hookID is the id of the source the intake tests deliver to.
|
|
var hookID = uuid.MustParse("5b0b9b1e-6f0e-4c1a-9a53-0d7c1f3a2b10")
|
|
|
|
// fakeSources serves one source by id and by webhook path.
|
|
type fakeSources struct{ src store.Source }
|
|
|
|
func (f fakeSources) Source(_ context.Context, _ string, id uuid.UUID) (store.Source, error) {
|
|
if id != f.src.ID {
|
|
return store.Source{}, store.ErrNotFound
|
|
}
|
|
return f.src, nil
|
|
}
|
|
|
|
func (f fakeSources) SourceByPath(_ context.Context, _, path string) (store.Source, error) {
|
|
if path != "hook" {
|
|
return store.Source{}, store.ErrNotFound
|
|
}
|
|
return f.src, nil
|
|
}
|
|
|
|
func sign(secret, body string) string {
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
mac.Write([]byte(body))
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
// TestHMACSourceVerifiesBeforeParsing guards the invariant that a webhook's
|
|
// signature is checked on the raw body before the connector sees it: a wrong
|
|
// or missing signature answers 401 and Parse never runs.
|
|
func TestHMACSourceVerifiesBeforeParsing(t *testing.T) {
|
|
const secret = "not-a-real-secret" //nolint:gosec // fixture signing secret the verifier must accept
|
|
const body = `{"id":"1"}`
|
|
id, err := age.GenerateX25519Identity()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sealer, err := seal.New(id.String(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sealed, err := sealer.Seal(secretLabel("org", hookID), []byte(secret))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := store.Source{
|
|
ID: hookID, OrgID: "org", Label: "monitoring", Connector: "c", Verification: store.HMAC,
|
|
HMACHeader: "X-Signature", HMACPrefix: "sha256=", HMACSecretSealed: sealed,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
header string
|
|
wantCode int
|
|
wantParse bool
|
|
}{
|
|
{"correct signature", "", "sha256=" + sign(secret, body), http.StatusAccepted, true},
|
|
{"correct signature in upper case", "", "sha256=" + strings.ToUpper(sign(secret, body)), http.StatusAccepted, true},
|
|
{"existing webhook URL", "/hooks/hook", "sha256=" + sign(secret, body), http.StatusAccepted, true},
|
|
{"display name is not a URL", "/hooks/monitoring", "sha256=" + sign(secret, body), http.StatusUnauthorized, false},
|
|
{"invalid UUID", "/hooks/id/monitoring", "sha256=" + sign(secret, body), http.StatusUnauthorized, false},
|
|
{"unknown UUID", "/hooks/id/" + uuid.Nil.String(), "sha256=" + sign(secret, body), http.StatusUnauthorized, false},
|
|
{"missing signature", "", "", http.StatusUnauthorized, false},
|
|
{"wrong secret", "", "sha256=" + sign("other-secret", body), http.StatusUnauthorized, false},
|
|
{"signature of another body", "", "sha256=" + sign(secret, `{"id":"2"}`), http.StatusUnauthorized, false},
|
|
{"missing prefix", "", sign(secret, body), http.StatusUnauthorized, false},
|
|
{"not hex", "", "sha256=zz", http.StatusUnauthorized, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
parsed := false
|
|
m := &Module{
|
|
orgID: "org", reader: fakeSources{src}, sealer: sealer, delivery: permitAll{},
|
|
log: slog.New(slog.DiscardHandler),
|
|
parse: func(context.Context, string, []byte, map[string]string) ([]connectors.Event, error) {
|
|
parsed = true
|
|
return nil, nil
|
|
},
|
|
}
|
|
path := tt.path
|
|
if path == "" {
|
|
path = "/hooks/id/" + hookID.String()
|
|
}
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, path, strings.NewReader(body))
|
|
if tt.header != "" {
|
|
req.Header.Set("X-Signature", tt.header)
|
|
}
|
|
mux := http.NewServeMux()
|
|
m.Routes(mux)
|
|
res := httptest.NewRecorder()
|
|
mux.ServeHTTP(res, req)
|
|
if res.Code != tt.wantCode {
|
|
t.Fatalf("status %d; want %d", res.Code, tt.wantCode)
|
|
}
|
|
if parsed != tt.wantParse {
|
|
t.Fatalf("connector parsed = %v, want %v", parsed, tt.wantParse)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// permitAll accepts a delivery from any address.
|
|
type permitAll struct{}
|
|
|
|
func (permitAll) AuthorizeDelivery(context.Context, governance.OrgID, string, string) governance.Decision {
|
|
return governance.Allowed()
|
|
}
|