alerts

package
v1.0.134 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package alerts is the producer-facing seam for security alerting. It owns the alert Payload DTO and a publish-once dispatch indirection, so alert producers (the scan engines, policy, …) can fire alerts without depending on the webhook-delivery implementation, which stays in package main and is installed once at startup via SetSink. ADR-0002/0003 (sibling to the obs sink seam).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fire

func Fire(event string, p Payload)

Fire dispatches an alert through the installed sink. It is a no-op when no sink is installed (e.g. unit tests that never wire alerting), so producers never need a nil check or a dependency on the delivery layer.

func SetSink

func SetSink(fn Sink)

SetSink installs the dispatch implementation. Intended to be called once at startup (publish-once); a later call atomically replaces the sink.

func StartRetryLoop added in v1.0.16

func StartRetryLoop(ctx context.Context, current func() *Store)

StartRetryLoop runs a background loop that retries failed deliveries. current resolves the store on every pass, so package main can pass a closure over its process-wide singleton (tolerating test reassignment — the pre-extraction loop read the global on each tick the same way).

func ValidateURL added in v1.0.16

func ValidateURL(raw string) error

ValidateURL checks that a webhook URL is well-formed (http/https with a non-empty host). Unlike main's validateExternalURL, it does not perform DNS resolution at config time — the SSRF check is deferred to delivery time via the ssrf-guarded dialer so that operator-configured internal endpoints remain usable.

Types

type Delivery added in v1.0.16

type Delivery struct {
	Timestamp   string `json:"timestamp"`
	WebhookID   string `json:"webhookId"`
	WebhookName string `json:"webhookName"`
	Event       string `json:"event"`
	StatusCode  int    `json:"statusCode,omitempty"` // 0 if delivery failed before HTTP
	Success     bool   `json:"success"`
	Error       string `json:"error,omitempty"`
	Attempt     int    `json:"attempt"` // 1-based
}

Delivery records a single webhook delivery attempt.

type Payload

type Payload struct {
	Event     string `json:"event"`
	Timestamp string `json:"timestamp"`
	Actor     string `json:"actor"` // client IP or username
	Host      string `json:"host"`
	Detail    string `json:"detail"` // virus name / rule name / pattern
	Source    string `json:"source"` // "clamav","yara","threatfeed","policy","auth"
}

Payload is the JSON body delivered to webhooks for a security event.

type Sink

type Sink func(event string, p Payload)

Sink dispatches an alert. The real implementation (webhook fan-out, retry, HMAC signing, SSRF-guarded delivery) lives in package main.

type Store added in v1.0.16

type Store struct {
	// contains filtered or unexported fields
}

Store holds the configured webhooks and the delivery-attempt history. The zero value is usable (main's process-wide singleton and several tests construct it as &Store{} / &AlertStore{}); Init sets the persistence path.

func (*Store) Add added in v1.0.16

func (as *Store) Add(h Webhook) Webhook

Add stores a new webhook and returns it (secret redacted).

func (*Store) Delete added in v1.0.16

func (as *Store) Delete(id string) bool

Delete removes the webhook with the given ID.

func (*Store) Deliver added in v1.0.16

func (as *Store) Deliver(h Webhook, payload Payload) bool

Deliver performs one delivery attempt (attempt=1) and records the result. Exposed for main's webhook-test admin endpoint; Dispatch and the retry loop use it internally.

func (*Store) DeliveryHistory added in v1.0.16

func (as *Store) DeliveryHistory() []Delivery

DeliveryHistory returns the most recent delivery records (newest first).

func (*Store) Dispatch added in v1.0.16

func (as *Store) Dispatch(event string, payload Payload)

Dispatch fans payload out to all enabled webhooks matching event. Always non-blocking: delivery happens in background goroutines. Q17: Duplicate events with the same event+detail are suppressed within dedupTTL. package main's fireAlert wrapper (installed as the alerts.Fire sink) calls this on the process-wide store.

func (*Store) GetByID added in v1.0.16

func (as *Store) GetByID(id string) (Webhook, bool)

GetByID returns the webhook with the given ID (secret included — callers use it for delivery/signing, never for display).

func (*Store) Init added in v1.0.16

func (as *Store) Init(path string)

Init sets the persistence path and loads any persisted webhooks.

func (*Store) List added in v1.0.16

func (as *Store) List() []Webhook

List returns the webhooks with secrets redacted.

func (*Store) RecordDelivery added in v1.0.16

func (as *Store) RecordDelivery(d Delivery)

RecordDelivery appends a delivery record to the in-memory history ring.

func (*Store) ResetDedupForTest added in v1.0.16

func (as *Store) ResetDedupForTest()

ResetDedupForTest clears the Q17 dedup window so a test can fire the same event+detail twice and observe both deliveries (needed under -count>1 / -shuffle=on where a previous run already fired the pair).

func (*Store) Update added in v1.0.16

func (as *Store) Update(id string, upd Webhook) bool

Update replaces the webhook with the given ID. An empty Secret in upd preserves the existing secret.

type Webhook added in v1.0.16

type Webhook struct {
	ID      string   `json:"id"`
	Name    string   `json:"name"`
	URL     string   `json:"url"`
	Events  []string `json:"events"` // e.g. ["threat_detected","policy_block"]
	Enabled bool     `json:"enabled"`
	Secret  string   `json:"secret,omitempty"` // HMAC-SHA256 signing secret (never returned in list)
}

Webhook describes a single webhook endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL