Event types, field mappings and the stored-delivery inspector move into modules/events: copied from internal/domain/eventtype and internal/domain/mapping, which the old services still import. Each stored event records how its delivery was verified; a source that checks nothing refuses deliveries until approved; triggers gain approve, pause, resume, event-type and mapping binding, dispatch history and delete. The captures, mappings and bindings sections are reported as served.
148 lines
4.2 KiB
Go
148 lines
4.2 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"fmt"
|
|
"net/http"
|
|
"net/textproto"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/events/internal/eventtype"
|
|
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
|
)
|
|
|
|
// arrival is what a webhook request adds to the events it carried: how it was
|
|
// verified, who sent it and the headers it came with. A listening connector's
|
|
// events have none of it.
|
|
type arrival struct {
|
|
verification string
|
|
addr string
|
|
headers http.Header
|
|
}
|
|
|
|
// typeReader lists a source's event types.
|
|
type typeReader interface {
|
|
EventTypesOf(ctx context.Context, orgID, source string) ([]store.EventType, error)
|
|
}
|
|
|
|
// credentialWords mark a header as carrying a credential or a signature, which
|
|
// are never stored.
|
|
var credentialWords = []string{"authorization", "cookie", "token", "secret", "password", "key", "signature"}
|
|
|
|
// storable returns the headers worth keeping with a capture: everything except
|
|
// credentials and the source's own signature header.
|
|
func storable(h http.Header, src store.Source) http.Header {
|
|
out := http.Header{}
|
|
own := textproto.CanonicalMIMEHeaderKey(src.HMACHeader)
|
|
for name, values := range h {
|
|
lower := strings.ToLower(name)
|
|
if name == own || containsAny(lower, credentialWords) {
|
|
continue
|
|
}
|
|
out[name] = values
|
|
}
|
|
return out
|
|
}
|
|
|
|
func containsAny(s string, words []string) bool {
|
|
for _, w := range words {
|
|
if strings.Contains(s, w) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// decorate fills in what a webhook request adds to an event row. The raw
|
|
// headers and body are kept only when the source captures them. The event
|
|
// type is identified from the headers and body before either is dropped.
|
|
func (m *Module) decorate(ctx context.Context, row *store.Event, src store.Source, a arrival, index int) error {
|
|
row.Verification, row.RemoteAddr = a.verification, a.addr
|
|
if a.headers == nil {
|
|
return nil
|
|
}
|
|
if id := strings.TrimSpace(a.headers.Get(src.DeliveryHeader)); src.DeliveryHeader != "" && id != "" {
|
|
// A retry repeats the delivery id, so its events are the ones already stored.
|
|
row.SourceEventID = "delivery:" + id + ":" + strconv.Itoa(index)
|
|
}
|
|
if err := m.identify(ctx, row, src, a.headers); err != nil {
|
|
return err
|
|
}
|
|
if !src.Capture {
|
|
row.Body = nil
|
|
return nil
|
|
}
|
|
raw, err := json.Marshal(storable(a.headers, src))
|
|
if err != nil {
|
|
return fmt.Errorf("encode headers: %w", err)
|
|
}
|
|
row.Headers = raw
|
|
return nil
|
|
}
|
|
|
|
// identify names the event type that the delivery matches. It names none when
|
|
// no type or several types match.
|
|
func (m *Module) identify(ctx context.Context, row *store.Event, src store.Source, h http.Header) error {
|
|
if m.types == nil {
|
|
return nil
|
|
}
|
|
rows, err := m.types.EventTypesOf(ctx, row.OrgID, src.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
types, err := typesOf(rows)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sample := eventtype.Sample{Headers: lowered(h), Body: row.Body}
|
|
if found, ok := eventtype.Identify(types, src.Name, sample); ok {
|
|
id, err := uuid.Parse(found.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("event type id %q: %w", found.ID, err)
|
|
}
|
|
row.EventTypeID = uuid.NullUUID{UUID: id, Valid: true}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// lowered returns each header's first value under its lower-case name.
|
|
func lowered(h http.Header) map[string]string {
|
|
out := make(map[string]string, len(h))
|
|
for name, values := range h {
|
|
if len(values) > 0 {
|
|
out[strings.ToLower(name)] = values[0]
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// typesOf converts stored event types to the form rules are evaluated in.
|
|
func typesOf(rows []store.EventType) ([]eventtype.EventType, error) {
|
|
out := make([]eventtype.EventType, 0, len(rows))
|
|
for _, r := range rows {
|
|
t, err := typeOf(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, t)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func typeOf(r store.EventType) (eventtype.EventType, error) {
|
|
t := eventtype.EventType{ID: r.ID.String(), Source: r.Source, Name: r.Name}
|
|
if err := json.Unmarshal(r.Rule, &t.Rule); err != nil {
|
|
return t, fmt.Errorf("event type %q rule: %w", r.Name, err)
|
|
}
|
|
if len(r.Schema) > 0 {
|
|
if err := json.Unmarshal(r.Schema, &t.Schema); err != nil {
|
|
return t, fmt.Errorf("event type %q schema: %w", r.Name, err)
|
|
}
|
|
}
|
|
return t, nil
|
|
}
|