Documentation
¶
Overview ¶
Package webhook manages workspace webhooks and the signing/delivery of their JSON payloads. Signing secrets are encrypted at rest.
Index ¶
- Constants
- Variables
- func BuildPayload(e *models.AppEvent) ([]byte, error)
- func Deliver(ctx context.Context, client *http.Client, url, secret string, ...) (int, error)
- func GenerateSecret() (string, error)
- func Sign(secret string, body []byte) string
- type Enqueuer
- type Input
- type Payload
- type Service
- func (s *Service) Create(workspaceID uint, in Input) (*models.Webhook, error)
- func (s *Service) Delete(workspaceID, id uint) error
- func (s *Service) Deliveries(workspaceID, id uint, limit int) ([]models.WebhookDelivery, error)
- func (s *Service) Get(workspaceID, id uint) (*models.Webhook, error)
- func (s *Service) List(workspaceID uint) ([]models.Webhook, error)
- func (s *Service) Redeliver(workspaceID, webhookID, deliveryID uint) error
- func (s *Service) Reencrypt(ctx context.Context, workspaceID uint) (int, error)
- func (s *Service) Test(ctx context.Context, workspaceID, id uint) error
- func (s *Service) Update(workspaceID, id uint, in Input) (*models.Webhook, error)
Constants ¶
const EventTest = "webhook.test"
EventTest is the synthetic event used by the test endpoint.
const SignatureHeader = "X-Miabi-Signature"
SignatureHeader carries the HMAC-SHA256 signature of the request body.
Variables ¶
var ( ErrNotFound = errors.New("webhook not found") ErrURLRequired = errors.New("webhook url is required") ErrURLInvalid = errors.New("webhook url must be a valid http(s) url") ErrURLBlocked = errors.New("webhook url targets a disallowed (internal) address") ErrInvalidEvent = errors.New("unknown or non-notifiable event") )
Functions ¶
func BuildPayload ¶
BuildPayload serializes an application event into a webhook payload.
func Deliver ¶
func Deliver(ctx context.Context, client *http.Client, url, secret string, headers map[string]string, body []byte) (int, error)
Deliver POSTs a signed JSON body to url and returns the HTTP status code. A transport error returns (0, err); a non-2xx/3xx status is reported via the returned code (the caller decides whether to retry). User-supplied headers are applied first; Content-Type, User-Agent, and the signature cannot be overridden.
func GenerateSecret ¶
GenerateSecret returns a 64-char hex string from 32 random bytes.
Types ¶
type Enqueuer ¶
Enqueuer schedules an asynchronous webhook delivery. Implemented by the worker producer; an interface here avoids a webhook->worker import cycle.
type Payload ¶
type Payload struct {
Event string `json:"event"`
WorkspaceID uint `json:"workspace_id"`
ApplicationID uint `json:"application_id,omitempty"`
ApplicationName string `json:"application_name,omitempty"`
ApplicationSlug string `json:"application_slug,omitempty"`
Severity string `json:"severity,omitempty"`
Message string `json:"message,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Timestamp string `json:"timestamp"`
}
Payload is the JSON body POSTed to a webhook endpoint.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages webhook configuration and synchronous test delivery.
func NewService ¶
func NewService(repo *repositories.WebhookRepository, deliveries *repositories.WebhookDeliveryRepository, enqueuer Enqueuer) *Service
func (*Service) Deliveries ¶
Deliveries returns a webhook's recent delivery log.
func (*Service) Redeliver ¶
Redeliver re-sends a past delivery's source event to its webhook (async). If the delivery was a test (no source event), a fresh test delivery is sent.
func (*Service) Reencrypt ¶
Reencrypt re-encrypts this owner's workspace secrets under the workspace's active DEK (key rotation). Idempotent; returns the number rewritten.