* 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.
210 lines
6.5 KiB
Go
210 lines
6.5 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/alert"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// fakeAlerts holds alerts in memory and applies the same transition rules the
|
|
// State Store does, so the test drives the RPC handlers' org handling and the
|
|
// domain's dedup, resolve and dismiss rules without a database.
|
|
type fakeAlerts struct {
|
|
alert.Store
|
|
next int64
|
|
alerts map[alert.Key]*alert.Alert
|
|
}
|
|
|
|
func (f *fakeAlerts) RaiseAlert(_ context.Context, in alert.Input) (alert.Alert, error) {
|
|
if err := in.Validate(); err != nil {
|
|
return alert.Alert{}, err
|
|
}
|
|
prev := f.alerts[in.Key]
|
|
next, _ := alert.Apply(prev, in, time.Now())
|
|
if prev == nil {
|
|
f.next++
|
|
next.ID = f.next
|
|
}
|
|
f.alerts[in.Key] = &next
|
|
return next, nil
|
|
}
|
|
|
|
func (f *fakeAlerts) ResolveAlert(_ context.Context, key alert.Key, by string) error {
|
|
if prev := f.alerts[key]; prev != nil {
|
|
next, _ := alert.Resolve(*prev, by, time.Now())
|
|
f.alerts[key] = &next
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeAlerts) DismissAlert(_ context.Context, orgID org.OrgID, id int64, by string) error {
|
|
for key, a := range f.alerts {
|
|
if a.ID == id && key.Org == orgID {
|
|
next, _ := alert.Dismiss(*a, by, time.Now())
|
|
f.alerts[key] = &next
|
|
return nil
|
|
}
|
|
}
|
|
return alert.ErrNotFound
|
|
}
|
|
|
|
func (f *fakeAlerts) ListAlerts(_ context.Context, orgID org.OrgID, filter alert.Filter) ([]alert.Alert, error) {
|
|
states := filter.States
|
|
if len(states) == 0 {
|
|
states = []alert.State{alert.StateOpen}
|
|
}
|
|
var out []alert.Alert
|
|
for key, a := range f.alerts {
|
|
if key.Org == orgID && slices.Contains(states, a.State) {
|
|
out = append(out, *a)
|
|
}
|
|
}
|
|
slices.SortFunc(out, func(a, b alert.Alert) int { return strings.Compare(a.Subject, b.Subject) })
|
|
return out, nil
|
|
}
|
|
|
|
type alertStep struct {
|
|
as org.OrgID // the caller's scope; empty is a service acting for no one
|
|
op string // raise, event, resolve, dismiss or list
|
|
org string // the org the request names
|
|
subject string
|
|
states []string
|
|
want []string // list: "org subject state xcount"
|
|
code codes.Code
|
|
}
|
|
|
|
func TestAlerts(t *testing.T) {
|
|
const a, b = "org-a", "org-b"
|
|
cases := []struct {
|
|
name string
|
|
steps []alertStep
|
|
}{
|
|
{"raising again bumps one row", []alertStep{
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "raise", org: a, subject: "t2"},
|
|
{op: "list", org: a, want: []string{"org-a t1 open x3", "org-a t2 open x1"}},
|
|
}},
|
|
{"a condition alert resolves itself", []alertStep{
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "resolve", org: a, subject: "t1"},
|
|
{op: "resolve", org: a, subject: "t1"},
|
|
{op: "list", org: a},
|
|
{op: "list", org: a, states: []string{"resolved"}, want: []string{"org-a t1 resolved x1"}},
|
|
}},
|
|
{"an event alert is not resolved by a check", []alertStep{
|
|
{op: "event", org: a, subject: "t1"},
|
|
{op: "resolve", org: a, subject: "t1"},
|
|
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
|
|
}},
|
|
{"a dismissed condition stays snoozed until it clears and recurs", []alertStep{
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "dismiss", as: a, subject: "t1"},
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "list", org: a},
|
|
{op: "resolve", org: a, subject: "t1"},
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
|
|
}},
|
|
{"a dismissed event comes back when it happens again", []alertStep{
|
|
{op: "event", org: a, subject: "t1"},
|
|
{op: "dismiss", as: a, subject: "t1"},
|
|
{op: "list", org: a},
|
|
{op: "event", org: a, subject: "t1"},
|
|
{op: "list", org: a, want: []string{"org-a t1 open x1"}},
|
|
}},
|
|
{"orgs never see each other's alerts", []alertStep{
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "raise", org: b, subject: "t1"},
|
|
{op: "raise", org: b, subject: "t2"},
|
|
{op: "list", as: a, want: []string{"org-a t1 open x1"}},
|
|
{op: "list", as: b, org: a, want: []string{"org-b t1 open x1", "org-b t2 open x1"}},
|
|
}},
|
|
{"a scoped caller cannot reach another org", []alertStep{
|
|
{op: "raise", org: a, subject: "t1"},
|
|
{op: "dismiss", as: b, org: a, subject: "t1", code: codes.NotFound},
|
|
{op: "raise", as: b, org: a, subject: "t9"},
|
|
{op: "list", as: a, want: []string{"org-a t1 open x1"}},
|
|
{op: "list", as: b, want: []string{"org-b t9 open x1"}},
|
|
}},
|
|
{"a service must name the org", []alertStep{
|
|
{op: "raise", subject: "t1", code: codes.InvalidArgument},
|
|
}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := &server{deps: Deps{Alerts: &fakeAlerts{alerts: map[alert.Key]*alert.Alert{}}}}
|
|
for i, step := range tc.steps {
|
|
ctx := context.Background()
|
|
if step.as != "" {
|
|
ctx = org.WithScope(ctx, step.as)
|
|
}
|
|
got, err := runAlertStep(ctx, s, step)
|
|
if status.Code(err) != step.code {
|
|
t.Fatalf("step %d (%s): code %v, want %v (%v)", i, step.op, status.Code(err), step.code, err)
|
|
}
|
|
if step.op == "list" && !slices.Equal(got, step.want) {
|
|
t.Fatalf("step %d: listed %q, want %q", i, got, step.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func runAlertStep(ctx context.Context, s *server, step alertStep) ([]string, error) {
|
|
switch step.op {
|
|
case "raise", "event":
|
|
_, err := s.RaiseAlert(ctx, &pb.RaiseAlertRequest{
|
|
OrgId: step.org, Kind: "k", Subject: step.subject, Severity: "action", Title: "t",
|
|
Condition: step.op == "raise",
|
|
})
|
|
return nil, err
|
|
case "resolve":
|
|
_, err := s.ResolveAlert(ctx, &pb.ResolveAlertRequest{OrgId: step.org, Kind: "k", Subject: step.subject})
|
|
return nil, err
|
|
case "dismiss":
|
|
// The alert's id is looked up under the org the step names, or the
|
|
// caller's own.
|
|
owner := cmpOr(step.org, string(step.as))
|
|
listed, err := s.deps.Alerts.ListAlerts(ctx, org.OrgID(owner), alert.Filter{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
id := int64(-1)
|
|
for _, a := range listed {
|
|
if a.Subject == step.subject {
|
|
id = a.ID
|
|
}
|
|
}
|
|
_, err = s.DismissAlert(ctx, &pb.DismissAlertRequest{OrgId: step.org, Id: id})
|
|
return nil, err
|
|
default:
|
|
r, err := s.ListAlerts(ctx, &pb.ListAlertsRequest{OrgId: step.org, States: step.states})
|
|
var out []string
|
|
for _, a := range r.GetAlerts() {
|
|
out = append(out, fmt.Sprintf("%s %s %s x%d", a.GetOrgId(), a.GetSubject(), a.GetState(), a.GetCount()))
|
|
}
|
|
return out, err
|
|
}
|
|
}
|
|
|
|
func cmpOr(values ...string) string {
|
|
for _, v := range values {
|
|
if v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|