| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- // Package webhook — notificador SALIENTE de alertas (Fase 8.7b).
- //
- // Implementa alerts.Notifier con entrega a una URL externa: firma
- // HMAC-SHA256 del body, reintentos con backoff exponencial + jitter
- // (4xx no se reintenta salvo 429; 5xx sí), DLQ en tabla webhook_events si
- // se agotan los reintentos.
- //
- // Notify NUNCA bloquea al llamador: encola el evento y un worker único lo
- // drena. Si la cola está llena se descarta (la alerta ya está en /api/alerts).
- package webhook
- import (
- "bytes"
- "context"
- "crypto/hmac"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io"
- "log/slog"
- "math/rand"
- "net/http"
- "sync"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/alerts"
- "github.com/gnacho/netpulse/server-go/internal/config"
- "github.com/gnacho/netpulse/server-go/internal/db"
- )
- const (
- queueCap = 64
- maxBodyCap = 64 << 10 // 64 KB para acotar el cuerpo aceptado
- )
- // payload es el contrato del webhook (firmado): event_id para idempotencia
- // del receptor y datos de la alerta.
- type payload struct {
- EventID string `json:"event_id"`
- Timestamp string `json:"timestamp"` // RFC3339
- Type string `json:"type"`
- Severity string `json:"severity"`
- Title string `json:"title"`
- Description string `json:"description"`
- Category string `json:"category"`
- RouterID string `json:"routerId,omitempty"`
- App string `json:"app"`
- }
- // Notifier implementa alerts.Notifier con entrega asíncrona y DLQ.
- type Notifier struct {
- cfg config.Webhook
- db *db.DB
- client *http.Client
- ch chan alerts.AlertEvent
- done chan struct{}
- once sync.Once
- wg sync.WaitGroup
- }
- // NewNotifier crea el Notifier y arranca su worker.
- func NewNotifier(cfg config.Webhook, d *db.DB) *Notifier {
- n := &Notifier{
- cfg: cfg,
- db: d,
- client: &http.Client{Timeout: cfg.Timeout},
- ch: make(chan alerts.AlertEvent, queueCap),
- done: make(chan struct{}),
- }
- n.wg.Add(1)
- go n.worker()
- return n
- }
- // Notify encola el evento y vuelve inmediatamente.
- func (n *Notifier) Notify(ev alerts.AlertEvent) {
- select {
- case <-n.done:
- return
- case n.ch <- ev:
- default:
- slog.Warn("webhook: cola llena, evento descartado", "alertId", ev.ID, "title", ev.Title)
- }
- }
- // Close para el worker y espera al envío en curso.
- func (n *Notifier) Close() {
- n.once.Do(func() {
- close(n.done)
- n.wg.Wait()
- })
- }
- func (n *Notifier) worker() {
- defer n.wg.Done()
- for {
- select {
- case <-n.done:
- return
- case ev := <-n.ch:
- n.sendWithRetry(ev)
- }
- }
- }
- // payloadJSON construye el body firmable de un evento.
- func payloadJSON(ev alerts.AlertEvent) ([]byte, error) {
- return json.Marshal(payload{
- EventID: ev.ID,
- Timestamp: time.Unix(ev.Ts, 0).UTC().Format(time.RFC3339),
- Type: "alert." + ev.Category,
- Severity: ev.Severity,
- Title: ev.Title,
- Description: ev.Description,
- Category: ev.Category,
- RouterID: ev.RouterID,
- App: "netpulse",
- })
- }
- // sign calcula la firma HMAC-SHA256 del body crudo.
- func sign(body []byte, secret string) string {
- mac := hmac.New(sha256.New, []byte(secret))
- mac.Write(body)
- return "sha256=" + hex.EncodeToString(mac.Sum(nil))
- }
- // sendWithRetry envía con backoff exponencial + jitter. 4xx (salvo 429) no se
- // reintenta; 429/5xx sí. Al agotar reintentos guarda en DLQ.
- func (n *Notifier) sendWithRetry(ev alerts.AlertEvent) {
- body, err := payloadJSON(ev)
- if err != nil {
- slog.Error("webhook: payload no serializable", "alertId", ev.ID, "error", err)
- return
- }
- var lastErr error
- for attempt := 0; attempt <= n.cfg.Retries; attempt++ {
- ctx, cancel := context.WithTimeout(context.Background(), n.cfg.Timeout)
- err = n.sendOnce(ctx, ev.ID, body)
- cancel()
- if err == nil {
- return
- }
- lastErr = err
- var retryable bool
- if httpErr, ok := err.(*httpStatusError); ok {
- // 4xx (salvo 429): error del emisor, no reintentar jamás.
- if httpErr.status >= 400 && httpErr.status < 500 && httpErr.status != http.StatusTooManyRequests {
- slog.Warn("webhook: 4xx permanente, no se reintenta", "alertId", ev.ID, "status", httpErr.status)
- n.saveDLQ(ev, body, lastErr.Error())
- return
- }
- retryable = true // 429/5xx → reintentar
- } else {
- // Error de transporte (DNS, timeout, conexión rehusada): la
- // entrega puede tener éxito en un reintento → backoff (issue #203).
- retryable = true
- }
- if attempt < n.cfg.Retries && retryable {
- jitter := time.Duration(rand.Int63n(int64(n.cfg.RetryDelay)))
- backoff := n.cfg.RetryDelay * time.Duration(1<<attempt)
- time.Sleep(backoff + jitter)
- }
- }
- n.saveDLQ(ev, body, lastErr.Error())
- }
- // httpStatusError marca la respuesta HTTP para la decisión de reintento.
- type httpStatusError struct {
- status int
- body string
- }
- func (e *httpStatusError) Error() string {
- return fmt.Sprintf("webhook status %d: %s", e.status, e.body)
- }
- func (n *Notifier) sendOnce(ctx context.Context, eventID string, body []byte) error {
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, n.cfg.URL, bytes.NewReader(body))
- if err != nil {
- return err
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("User-Agent", "NetPulse-Webhook/2.0")
- req.Header.Set("X-Webhook-Signature", sign(body, n.cfg.Secret))
- resp, err := n.client.Do(req)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- respBody, _ := io.ReadAll(io.LimitReader(resp.Body, maxBodyCap))
- if resp.StatusCode >= 200 && resp.StatusCode < 300 {
- return nil
- }
- return &httpStatusError{status: resp.StatusCode, body: string(respBody)}
- }
- // saveDLQ persiste el evento no entregado para diagnóstico posterior.
- func (n *Notifier) saveDLQ(ev alerts.AlertEvent, body []byte, reason string) {
- if n.db == nil {
- return
- }
- if _, err := n.db.Exec(
- "INSERT OR REPLACE INTO webhook_events (event_id, payload, sent_at, error) VALUES (?, ?, ?, ?)",
- ev.ID, string(body), time.Now().Unix(), reason,
- ); err != nil {
- slog.Error("webhook: no se pudo guardar en DLQ", "alertId", ev.ID, "error", err)
- return
- }
- slog.Warn("webhook: evento a DLQ", "alertId", ev.ID, "error", reason)
- }
|