AshHooks.Endpoint (AshHooks v3.0.0)

Copy Markdown View Source

Configures an Ash resource as an outbound webhook destination, including its enable/disable state and references to signing secrets.

use Ash.Resource,
  data_layer: AshSqlite.DataLayer,
  extensions: [AshHooks.Endpoint]

# Map a declared :active boolean instead of injecting :status.
endpoint do
  status_attribute :active
  enabled_values [true]
  disabled_value false
end

The extension injects url, status (:enabled | :disabled), and four secret references (secret_ref required, previous_secret_ref / legacy_secret_ref / legacy_previous_secret_ref for rotation and the legacy envelope). All four are AshHooks.Endpoint.SecretRef. The type rejects secret-shaped literals (whsec_ / whsk_ / whpk_ prefixed material) at cast, on every write path including consumer-defined actions: secrets live behind consumer callbacks; store references to those secrets in the endpoint rows.

With status_attribute set, the extension uses that attribute instead of injecting status. The dispatcher skips endpoints whose mapped value is outside enabled_values, and the 410 auto-disable flips the mapped attribute to disabled_value. The mapped attribute must be declared by the consumer (fail-closed at compile).

The dispatcher skips non-enabled endpoints entirely (no delivery row). URL casting checks the scheme, metadata hostnames, and literal IP addresses on every write path. Send-time validation resolves hostnames and rejects nonpublic addresses; the HTTP adapter pins the validated connection address.

Govern read access to target URLs and secret references with your own resource policies. The package does not inject read policies.

Policy obligation: protect the injected :disable action wherever you expose it to application callers. The 410 rule uses it as a system write (authorize?: false). A permissive resource policy can otherwise allow callers to disable endpoints. See AshHooks.OutboundDelivery for the delivery ledger's action policy requirements.

Summary

Functions

The resolved auto-disable policy for an endpoint resource, or nil when the resource declares no auto_disable section: a map with :failure_window_seconds, :failure_share, :min_attempts, :warning_share, and :warning_min_attempts (defaults applied).

Whether an endpoint row is deliverable under its configured status source: the injected status attribute (default) or the consumer-owned status_attribute mapping (enabled_values / disabled_value). The dispatcher's skip decision and the send path's dead-letter decision both route through here, so exactly one switch governs delivery.

Retires one endpoint's previous signing key at once: the old reference leaves the row and its retirement time stands as the record. A row with no previous key is returned unchanged.

The scheduled retirement sweep: expires every endpoint whose previous key's overlap window has ended (previous_secret_retired_at <= now). Schedule it alongside your retention sweeps; the window an endpoint never outlives is the one roll_secret/2 set. Returns {:ok, retired_count}.

Rolls the endpoint's signing secret reference with an overlap window: the incumbent reference moves to previous_secret_ref — the Standard Webhooks zero-downtime seam: BOTH keys sign every delivery until the window ends — the new reference takes its place, and the old key's retirement is scheduled.

The endpoint lifecycle statuses — :disabled is the durable circuit breaker the 410 rule and operators flip.

Functions

auto_disable_policy(resource)

@spec auto_disable_policy(module()) :: map() | nil

The resolved auto-disable policy for an endpoint resource, or nil when the resource declares no auto_disable section: a map with :failure_window_seconds, :failure_share, :min_attempts, :warning_share, and :warning_min_attempts (defaults applied).

enabled?(endpoint)

@spec enabled?(map()) :: boolean()

Whether an endpoint row is deliverable under its configured status source: the injected status attribute (default) or the consumer-owned status_attribute mapping (enabled_values / disabled_value). The dispatcher's skip decision and the send path's dead-letter decision both route through here, so exactly one switch governs delivery.

endpoint(body)

(macro)

expire_previous_secret(endpoint, opts \\ [])

@spec expire_previous_secret(struct(), keyword()) ::
  {:ok, struct()} | {:error, term()}

Retires one endpoint's previous signing key at once: the old reference leaves the row and its retirement time stands as the record. A row with no previous key is returned unchanged.

:tenant applies to the gated write (required on multitenant resources — the tenancy pre-flight runs before it).

expire_previous_secrets(resource, opts \\ [])

@spec expire_previous_secrets(module(), keyword()) ::
  {:ok, non_neg_integer()} | {:error, term()}

The scheduled retirement sweep: expires every endpoint whose previous key's overlap window has ended (previous_secret_retired_at <= now). Schedule it alongside your retention sweeps; the window an endpoint never outlives is the one roll_secret/2 set. Returns {:ok, retired_count}.

Options: :now (the application clock) and :tenant (required on multitenant resources).

roll_secret(endpoint, opts \\ [])

@spec roll_secret(struct(), keyword()) :: {:ok, struct()} | {:error, term()}

Rolls the endpoint's signing secret reference with an overlap window: the incumbent reference moves to previous_secret_ref — the Standard Webhooks zero-downtime seam: BOTH keys sign every delivery until the window ends — the new reference takes its place, and the old key's retirement is scheduled.

{:ok, rolled} =
  AshHooks.Endpoint.roll_secret(endpoint,
    # your store already holds the new secret under this reference
    new_secret_ref: "acme-2026-11",
    overlap_seconds: 24 * 3_600
  )

Options: :new_secret_ref (required — a fresh store reference; rolling onto the incumbent is {:error, :same_reference}), :overlap_seconds (default 86,400), :now (the application clock), :tenant. Emits [:ash_hooks, :endpoint, :roll] with the endpoint id. The row carries references and lifecycle timestamps only — never secret material. The gated write fences concurrent rolls ({:error, :rolled_underneath} when the incumbent moved first); a resource without key_lifecycle :on fails with a named error.

statuses()

@spec statuses() :: [atom()]

The endpoint lifecycle statuses — :disabled is the durable circuit breaker the 410 rule and operators flip.