Documentation
¶
Overview ¶
Package webhooks is notifier registrations and their delivery log.
── THE COMPOSABILITY SEAM IS THE `type` COLUMN ──────────────────────────────────────────────────
A registration is `{ type, name, events[], config{} }`. `type` selects a delivery module (see package notify) and OWNS the shape of `config`. Adding Slack later is a new entry in the notifier registry and a new `config` validator; not a migration, not a schema change, not a new table. `config` is stored as opaque JSON here and validated in the module that understands it.
── THE SIGNING SECRET IS STORED REVERSIBLY, DELIBERATELY ────────────────────────────────────────
Sessions and tokens in package auth are SHA-256'd because they are INBOUND credentials. A signing secret is OUTBOUND: this process must compute `HMAC(secret, body)` on every delivery, so it needs the plaintext. The precedent is package registries, not auth: reversible at rest, protected by the 0700 directory and 0600 database file, and — the part enforced here — no function in this package returns it except SecretOf, and only notify calls that. List and Get project columns explicitly.
── WHY DELIVERIES ARE PERSISTED AND BOUNDED ─────────────────────────────────────────────────────
jobs keeps the last 50 in memory and evicts, so a retry that re-read a job minutes later could find it gone. Everything a delivery needs is captured at emit time and written here; Prune caps the rows per notifier.
Index ¶
- Constants
- func IsError(err error) bool
- type CreateArgs
- type Delivery
- type DeliveryRow
- type Error
- type NotifierRow
- type PublicConfig
- type Result
- type Webhooks
- func (w *Webhooks) Create(a CreateArgs) (NotifierRow, *string, error)
- func (w *Webhooks) Deliveries(notifierID int64, limit int64) ([]DeliveryRow, error)
- func (w *Webhooks) DeliveryWithPayload(id int64) (*Delivery, error)
- func (w *Webhooks) FinishDelivery(id int64, r Result) error
- func (w *Webhooks) Get(id int64) (*NotifierRow, error)
- func (w *Webhooks) List() ([]NotifierRow, error)
- func (w *Webhooks) NoteResult(notifierID int64, status string) error
- func (w *Webhooks) Prune(notifierID int64) error
- func (w *Webhooks) RawConfigOf(id int64) (*omap.Map, error)
- func (w *Webhooks) Remove(id int64) (bool, error)
- func (w *Webhooks) SecretOf(id int64) (*string, error)
- func (w *Webhooks) SetEnabled(id int64, enabled bool) (bool, error)
- func (w *Webhooks) SettleInterrupted() error
- func (w *Webhooks) StartDelivery(notifierID int64, eventID, event string, payload []byte) (int64, error)
- func (w *Webhooks) SubscribedTo(event string) ([]NotifierRow, error)
Constants ¶
const MaxDeliveriesPerNotifier = 200
MaxDeliveriesPerNotifier: enough to debug a failing endpoint, bounded on disk.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CreateArgs ¶
type CreateArgs struct {
Type string
Name string
Config *omap.Map
Events []string
// ValidateConfig is supplied by the notifier registry so this package never learns what a
// Slack config looks like.
ValidateConfig func(kind string, c *omap.Map) error
// Signs: whether this type signs. A type whose config already carries its credential gets no
// secret — minting one anyway would leave a live credential nothing can use or rotate.
Signs bool
}
CreateArgs is a registration.
type Delivery ¶
Delivery is one delivery with its stored payload — the read behind redelivery. Payload is nil for rows written before 0.25.0 and for drops that never had an envelope; the caller must refuse those by name rather than inventing an event to replay.
type DeliveryRow ¶
type DeliveryRow struct {
ID int64 `json:"id"`
NotifierID int64 `json:"notifierId"`
EventID string `json:"eventId"`
Event string `json:"event"`
Status string `json:"status"`
Attempts int64 `json:"attempts"`
ResponseCode *int64 `json:"responseCode"`
Error *string `json:"error"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
DeliveryRow is one delivery as listed.
type Error ¶
type Error struct{ Msg string }
Error is a refused registration. The API maps it to 400.
type NotifierRow ¶
type NotifierRow struct {
ID int64 `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Config *omap.Map `json:"config"`
// Events is event names, or the single entry "*" meaning everything including future events.
Events []string `json:"events"`
Enabled bool `json:"enabled"`
CreatedAt int64 `json:"createdAt"`
LastStatus *string `json:"lastStatus"`
LastAt *int64 `json:"lastAt"`
}
NotifierRow is what a caller may see. Note the absence of `secret` — that is the security property, in the type.
type PublicConfig ¶
PublicConfig masks credential-bearing config fields for display. Injected rather than imported, so this package still knows nothing about what a webhook is.
type Webhooks ¶
Webhooks is the notifiers and deliveries tables. Owner: the server.
func New ¶
func New(s *store.Store, publicConfig PublicConfig) *Webhooks
New wraps a store. publicConfig nil = identity.
func (*Webhooks) Create ¶
func (w *Webhooks) Create(a CreateArgs) (NotifierRow, *string, error)
Create registers a notifier. Returns the row AND the generated signing secret (nil for a non-signing type) — the only time the secret leaves this package toward a caller.
func (*Webhooks) Deliveries ¶
func (w *Webhooks) Deliveries(notifierID int64, limit int64) ([]DeliveryRow, error)
Deliveries lists the newest first. limit is clamped to 1..500.
func (*Webhooks) DeliveryWithPayload ¶
DeliveryWithPayload is nil when unknown.
func (*Webhooks) FinishDelivery ¶
FinishDelivery records the outcome. Only the first 300 chars of the error's first line are kept: a wall of HTML from an error page belongs in nobody's database.
func (*Webhooks) Get ¶
func (w *Webhooks) Get(id int64) (*NotifierRow, error)
Get is one masked row; nil when unknown.
func (*Webhooks) List ¶
func (w *Webhooks) List() ([]NotifierRow, error)
List is every row, with credential-bearing config fields masked.
SubscribedTo deliberately does NOT go through the mask — the dispatcher needs the real values to make the request. Conflating the two is how a masked value ends up POSTed to a masked URL.
func (*Webhooks) NoteResult ¶
NoteResult mirrors the last outcome onto the notifier, so a list view answers "is this thing working".
func (*Webhooks) Prune ¶
Prune caps the log per notifier. Called after each delivery.
`status != 'pending'` is not decoration: a delivery retrying against a slow endpoint lives for up to 21 seconds with its row already inserted; a burst of newer deliveries would push it out of the keep-set and DELETE it, and the later FinishDelivery would update zero rows.
func (*Webhooks) RawConfigOf ¶
RawConfigOf is the UNMASKED config, for delivery only — the test route and the dispatcher.
func (*Webhooks) SecretOf ¶
SecretOf is the signing secret, for package notify only. Named to be conspicuous in a diff: a second caller is the moment to ask whether it should exist. nil when unknown.
func (*Webhooks) SetEnabled ¶
SetEnabled flips the flag; false when unknown.
func (*Webhooks) SettleInterrupted ¶ added in v0.46.0
SettleInterrupted fails every delivery still `pending` — called once at boot, before the dispatcher starts, so nothing can be in flight. A restart or a recovered panic leaves such a row behind; Prune never removes a pending row, so without this it would read "pending" forever.
func (*Webhooks) StartDelivery ¶
func (w *Webhooks) StartDelivery(notifierID int64, eventID, event string, payload []byte) (int64, error)
StartDelivery opens a row. Everything needed is captured NOW — the source job may be evicted later. payload is the whole envelope as sent (nil = none), and is what makes REDELIVERY possible.
func (*Webhooks) SubscribedTo ¶
func (w *Webhooks) SubscribedTo(event string) ([]NotifierRow, error)
SubscribedTo is every enabled notifier subscribed to this event, unmasked. The dispatcher's only read path. Filtered here rather than with a LIKE on the JSON: the list is tiny and a LIKE would match 'job.failed' inside 'job.failed_x'.