* feat(state): durable operator alerts owned by the State Store Alerts dedup on org+kind+subject, resolve themselves when a condition clears, and are raised, resolved, dismissed and listed through the state contract. Task transitions raise needs-human and failed alerts in the same transaction; the store checks service health and prunes closed alerts. Each raise and resolve queues a webhook delivery whose attempts are kept. * feat(notify): deliver operator alerts to the webhook; raise config and release alerts notify.webhook now receives raised and resolved alerts above a severity threshold with HMAC-signed payloads, bounded retry and every attempt recorded on the alert. archied raises model alias, provider and credential-binding alerts from the effective configuration, an update alert from the release channel, and a credential-rejected alert from the egress resolver. The dashboard API lists and dismisses the caller's org's alerts. * feat(ui): alert bell in the top bar and webhook alert settings The bell lists the org's open alerts by severity with dismiss and a link per alert, and refreshes from the event stream. The task settings page gains the webhook severity threshold and signing secret. ui/dist rebuilt.
95 lines
2.7 KiB
Go
95 lines
2.7 KiB
Go
// Package alertwebhook delivers operator alerts to the notify webhook.
|
|
package alertwebhook
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/alert"
|
|
"github.com/samcharles93/archie-core/internal/webhookguard"
|
|
)
|
|
|
|
// SignatureHeader carries the payload's HMAC-SHA256 when a secret is set.
|
|
const SignatureHeader = "X-Archie-Signature"
|
|
|
|
const timeout = 10 * time.Second
|
|
|
|
type payload struct {
|
|
Event string `json:"event"`
|
|
Attempt int64 `json:"attempt"`
|
|
Alert body `json:"alert"`
|
|
}
|
|
|
|
type body struct {
|
|
ID int64 `json:"id"`
|
|
Org string `json:"org"`
|
|
Kind string `json:"kind"`
|
|
Subject string `json:"subject"`
|
|
Severity string `json:"severity"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
Link string `json:"link"`
|
|
State string `json:"state"`
|
|
FirstRaised time.Time `json:"first_raised"`
|
|
LastSeen time.Time `json:"last_seen"`
|
|
Count int64 `json:"count"`
|
|
}
|
|
|
|
// Payload is the JSON body sent for a due delivery.
|
|
func Payload(due alert.Due) ([]byte, error) {
|
|
a := due.Alert
|
|
return json.Marshal(payload{
|
|
Event: due.Event, Attempt: due.Attempt,
|
|
Alert: body{
|
|
ID: a.ID, Org: string(a.Org), Kind: a.Kind, Subject: a.Subject, Severity: string(a.Severity),
|
|
Title: a.Title, Body: a.Body, Link: a.Link, State: string(a.State),
|
|
FirstRaised: a.FirstRaised, LastSeen: a.LastSeen, Count: a.Count,
|
|
},
|
|
})
|
|
}
|
|
|
|
// Send posts due to webhook, signed with secret when it is not empty. The
|
|
// outcome's error names what went wrong without the webhook URL, which may
|
|
// carry a token.
|
|
func Send(ctx context.Context, client *http.Client, webhook, secret string, due alert.Due) alert.Outcome {
|
|
out := alert.Outcome{DeliveryID: due.ID}
|
|
data, err := Payload(due)
|
|
if err != nil {
|
|
out.Error = "encode payload"
|
|
return out
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhook, bytes.NewReader(data))
|
|
if err != nil {
|
|
out.Error = "invalid webhook URL"
|
|
return out
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Archie-Event", due.Event)
|
|
req.Header.Set("X-Archie-Delivery", fmt.Sprint(due.ID))
|
|
if secret != "" {
|
|
req.Header.Set(SignatureHeader, webhookguard.SignHMAC(data, secret))
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
if urlErr, ok := errors.AsType[*url.Error](err); ok {
|
|
err = urlErr.Err
|
|
}
|
|
out.Error = err.Error()
|
|
return out
|
|
}
|
|
_ = resp.Body.Close()
|
|
out.StatusCode = resp.StatusCode
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
out.Error = fmt.Sprintf("webhook answered %d", resp.StatusCode)
|
|
}
|
|
return out
|
|
}
|