Karta. Webhooks
(Karta v1.0.0)
Webhooks: a server's events (Karta.Webhooks.Catalogue) sent to URLs
its owners choose, signed as Standard Webhooks
(Karta.Webhooks.Signature), or in a provider's format
(Karta.Webhooks.Format).
Each server has at most 20 webhooks. A webhook's URL is https:
(http: only where the operator allows it, allow_http), without a
fragment; a user name and password in it are kept apart, encrypted, and
sent as Basic authentication (Karta.Webhooks.Webhook). Requests go
through Karta.NetGuard,
so a URL whose host is private, loopback, carrier-grade NAT, unique
local, NAT64 or 6to4 gets no request unless the operator allows that
network.
The secret is shown once, when the webhook is made or its secret is
rotated (rotate_secret/2); it is stored encrypted. After a rotation the
previous secret keeps signing too (a second signature) for a while
(default a day, at most a week), so that receivers can switch without
losing a request; in Mailgun's and SendGrid's formats, which have room
for one signature, the new secret signs at once. In SendGrid's format the
secret is a private key, never shown: its owners get the public key
(Karta.Webhooks.Format.public_key/1). Webhooks in Postal's format have
no secret: the key of their organisation signs them
(Karta.Webhooks.PostalKey), made when its first Postal-format webhook
or inbound endpoint is added. Webhooks in Postmark's format have none
either: Postmark's requests are not signed.
Events (emit/2) are recorded in the transaction that records what
happened (a delivery attempt, an open, ...), and only for the server's
enabled webhooks that subscribe to their type: one
Karta.Webhooks.Event with its data as it will be sent
(Karta.Webhooks.Payload), and one Karta.Webhooks.Delivery per
webhook, with its job (Karta.Webhooks.Sender). Nothing is recorded when
no webhook wants the event, and nothing is sent before the transaction
commits. Messages with an empty envelope
sender (Karta's own delivery status notifications) have no events.
Deliveries are sent by Karta.Webhooks.Sender; those that failed
for good (dead letters), or any others, can be sent again: one
(replay/1) or all of a webhook's in a time range (replay/2). A
replay sends the same event again, with the same webhook-id, and
gives it every attempt anew. send_test/1 sends a webhook.test event
to one webhook. Events, with their deliveries and attempts, are removed
retention_days after they happened (Karta.Webhooks.Pruner).
Changes to a server's webhooks are announced on the server's topic
(:webhooks, Karta.Changes).
Configuration (config :karta, Karta.Webhooks): allow_http (default
false), retention_days (default 30).
Summary
Functions
Every request made for a delivery, oldest first (replays included).
A changeset for a form (Karta.Webhooks.Webhook.changeset/3).
Adds a webhook to the server: attrs name, url, format (default
:standard), events (a list; left out for every event of the format)
and enabled (default true). Returns its secret once (nil where the
format has none, or does not show it: Postal's, Postmark's, SendGrid's).
Removes a webhook, with its deliveries.
Records events of the server (build returns them,
Karta.Webhooks.Payload.event/0) for its enabled webhooks that
subscribe to their types (and whose format has them). build is called
only when the server has an enabled webhook. Call it inside the
transaction that records what happened.
A webhook of the server, or nil.
A delivery of the webhook with its event, or nil.
The server's webhooks, oldest first.
The webhook's deliveries, newest first, each with its event: the
entries of page_deliveries/2, so that there is one pagination
mechanism (Karta.Pager). Its before is a delivery id whose older
deliveries are asked for (the page after it), which is the pager's
after; limit is at most 200.
A page of the webhook's deliveries, newest first, each with its event
(Karta.Pager): options state (:pending, :succeeded or
:failed), after or before (a delivery id of this webhook; anything
else is the first page) and limit (default 50, at most 200).
Sends a delivery again: the same event, every attempt anew. Not while
it is pending ({:error, :pending}), nor while its webhook is switched
off ({:error, :disabled}).
Sends the webhook's deliveries of events that happened from from to
to (DateTimes, inclusive; options, both optional) again: those in
states (default [:failed], the dead letters; :succeeded may be
added). At most 10000 per call, oldest first; ask again for the
rest. Returns how many, or {:error, :disabled} while the webhook is
switched off.
Gives the webhook a new secret, returned once (nil in SendGrid's
format, whose new public key the webhook shows). In Mailgun's format the
server's other webhooks with the same signing key (those made by
Karta.Webhooks.MailgunSettings share one) get the new key too. The
previous one keeps signing for keep_previous_for seconds (option;
default a day, at most a week; 0 drops it at once), in Standard
Webhooks' format only. A webhook in a format without a secret has none
to rotate ({:error, :no_secret}).
The secrets that sign the webhook's requests at now: the current one first.
Sends a webhook.test event to this webhook alone, whatever events it
subscribes to; {:error, :disabled} while it is switched off.
The server's other webhooks that sign with this webhook's key, by name
and URL: in Mailgun's format, those made by
Karta.Webhooks.MailgunSettings share one, and a rotation
(rotate_secret/2) gives them the new key too. None in the other
formats. webhook must be as stored (its secret read).
The event types a webhook of Karta's own format may name: types/0 and,
while the messaging module is on, the messaging events (which a webhook
with no event list does not receive).
The event types webhooks can subscribe to.
Changes a webhook's name, URL, events or enabled. Enabling it again
clears why it was disabled and since when it was failing; disabling it
records when.
Functions
@spec attempts(Karta.Webhooks.Delivery.t()) :: [Karta.Webhooks.Attempt.t()]
Every request made for a delivery, oldest first (replays included).
@spec change(Karta.Webhooks.Webhook.t(), map()) :: Ecto.Changeset.t()
A changeset for a form (Karta.Webhooks.Webhook.changeset/3).
@spec create(Karta.Servers.Server.t(), map()) :: {:ok, Karta.Webhooks.Webhook.t(), String.t() | nil} | {:error, Ecto.Changeset.t()}
Adds a webhook to the server: attrs name, url, format (default
:standard), events (a list; left out for every event of the format)
and enabled (default true). Returns its secret once (nil where the
format has none, or does not show it: Postal's, Postmark's, SendGrid's).
@spec delete(Karta.Webhooks.Webhook.t()) :: {:ok, Karta.Webhooks.Webhook.t()}
Removes a webhook, with its deliveries.
@spec emit(Ecto.UUID.t(), (-> [Karta.Webhooks.Payload.event()])) :: :ok
Records events of the server (build returns them,
Karta.Webhooks.Payload.event/0) for its enabled webhooks that
subscribe to their types (and whose format has them). build is called
only when the server has an enabled webhook. Call it inside the
transaction that records what happened.
@spec get(Karta.Servers.Server.t(), term()) :: Karta.Webhooks.Webhook.t() | nil
A webhook of the server, or nil.
@spec get_delivery(Karta.Webhooks.Webhook.t(), term()) :: Karta.Webhooks.Delivery.t() | nil
A delivery of the webhook with its event, or nil.
@spec list(Karta.Servers.Server.t()) :: [Karta.Webhooks.Webhook.t()]
The server's webhooks, oldest first.
@spec list_deliveries(Karta.Webhooks.Webhook.t(), keyword()) :: [ Karta.Webhooks.Delivery.t() ]
The webhook's deliveries, newest first, each with its event: the
entries of page_deliveries/2, so that there is one pagination
mechanism (Karta.Pager). Its before is a delivery id whose older
deliveries are asked for (the page after it), which is the pager's
after; limit is at most 200.
@spec page_deliveries(Karta.Webhooks.Webhook.t(), keyword()) :: Karta.Pager.Page.t()
A page of the webhook's deliveries, newest first, each with its event
(Karta.Pager): options state (:pending, :succeeded or
:failed), after or before (a delivery id of this webhook; anything
else is the first page) and limit (default 50, at most 200).
@spec replay(Karta.Webhooks.Delivery.t()) :: {:ok, Karta.Webhooks.Delivery.t()} | {:error, :pending | :disabled}
Sends a delivery again: the same event, every attempt anew. Not while
it is pending ({:error, :pending}), nor while its webhook is switched
off ({:error, :disabled}).
@spec replay(Karta.Webhooks.Webhook.t(), keyword()) :: {:ok, non_neg_integer()} | {:error, :disabled}
Sends the webhook's deliveries of events that happened from from to
to (DateTimes, inclusive; options, both optional) again: those in
states (default [:failed], the dead letters; :succeeded may be
added). At most 10000 per call, oldest first; ask again for the
rest. Returns how many, or {:error, :disabled} while the webhook is
switched off.
@spec rotate_secret(Karta.Webhooks.Webhook.t(), keyword()) :: {:ok, Karta.Webhooks.Webhook.t(), String.t() | nil} | {:error, :no_secret}
Gives the webhook a new secret, returned once (nil in SendGrid's
format, whose new public key the webhook shows). In Mailgun's format the
server's other webhooks with the same signing key (those made by
Karta.Webhooks.MailgunSettings share one) get the new key too. The
previous one keeps signing for keep_previous_for seconds (option;
default a day, at most a week; 0 drops it at once), in Standard
Webhooks' format only. A webhook in a format without a secret has none
to rotate ({:error, :no_secret}).
@spec secrets(Karta.Webhooks.Webhook.t(), DateTime.t()) :: [String.t() | nil, ...]
The secrets that sign the webhook's requests at now: the current one first.
@spec send_test(Karta.Webhooks.Webhook.t()) :: {:ok, Karta.Webhooks.Delivery.t()} | {:error, :disabled}
Sends a webhook.test event to this webhook alone, whatever events it
subscribes to; {:error, :disabled} while it is switched off.
@spec sharing_key(Karta.Webhooks.Webhook.t()) :: [Karta.Webhooks.Webhook.t()]
The server's other webhooks that sign with this webhook's key, by name
and URL: in Mailgun's format, those made by
Karta.Webhooks.MailgunSettings share one, and a rotation
(rotate_secret/2) gives them the new key too. None in the other
formats. webhook must be as stored (its secret read).
@spec subscribable_types() :: [String.t()]
The event types a webhook of Karta's own format may name: types/0 and,
while the messaging module is on, the messaging events (which a webhook
with no event list does not receive).
@spec types() :: [String.t()]
The event types webhooks can subscribe to.
@spec update(Karta.Webhooks.Webhook.t(), map()) :: {:ok, Karta.Webhooks.Webhook.t()} | {:error, Ecto.Changeset.t()}
Changes a webhook's name, URL, events or enabled. Enabling it again
clears why it was disabled and since when it was failing; disabling it
records when.