* 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.
191 lines
6.5 KiB
Go
191 lines
6.5 KiB
Go
// Alerts: the operator alert RPCs. A caller acting for a principal is
|
|
// confined to that principal's org; a service acting for no one names the org
|
|
// on the request.
|
|
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"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/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/alert"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
var errAlertsUnavailable = status.Error(codes.Unavailable, "alert store unavailable")
|
|
|
|
// alertOrg is the org a call acts in.
|
|
func alertOrg(ctx context.Context, requested string) (org.OrgID, error) {
|
|
if id, ok := org.Scope(ctx); ok {
|
|
return id, nil
|
|
}
|
|
if requested == "" {
|
|
return "", status.Error(codes.InvalidArgument, "org_id is required")
|
|
}
|
|
return org.OrgID(requested), nil
|
|
}
|
|
|
|
func alertStatus(err error) error {
|
|
switch {
|
|
case errors.Is(err, alert.ErrInvalid):
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
case errors.Is(err, alert.ErrNotFound):
|
|
return status.Error(codes.NotFound, alert.ErrNotFound.Error())
|
|
default:
|
|
return mapError(err)
|
|
}
|
|
}
|
|
|
|
func alertProto(a alert.Alert) *pb.Alert {
|
|
return &pb.Alert{
|
|
Id: a.ID, OrgId: string(a.Org), Kind: a.Kind, Subject: a.Subject,
|
|
Severity: string(a.Severity), Title: a.Title, Body: a.Body, Link: a.Link,
|
|
Condition: a.Condition, State: string(a.State),
|
|
FirstRaised: timestamp(a.FirstRaised), LastSeen: timestamp(a.LastSeen), Count: a.Count,
|
|
ClosedBy: a.ClosedBy, ClosedAt: timestamp(a.ClosedAt),
|
|
Deliveries: mapValues(a.Deliveries, alertDeliveryProto),
|
|
}
|
|
}
|
|
|
|
func alertValue(a *pb.Alert) alert.Alert {
|
|
return alert.Alert{
|
|
ID: a.GetId(),
|
|
Org: org.OrgID(a.GetOrgId()), Kind: a.GetKind(), Subject: a.GetSubject(),
|
|
Severity: alert.Severity(a.GetSeverity()), Title: a.GetTitle(), Body: a.GetBody(), Link: a.GetLink(),
|
|
Condition: a.GetCondition(), State: alert.State(a.GetState()),
|
|
FirstRaised: timeValue(a.GetFirstRaised()), LastSeen: timeValue(a.GetLastSeen()), Count: a.GetCount(),
|
|
ClosedBy: a.GetClosedBy(), ClosedAt: timeValue(a.GetClosedAt()),
|
|
Deliveries: mapValues(a.GetDeliveries(), alertDeliveryValue),
|
|
}
|
|
}
|
|
|
|
func alertDeliveryProto(d alert.Delivery) *pb.AlertDelivery {
|
|
return &pb.AlertDelivery{
|
|
Id: d.ID, Event: d.Event, State: d.State, CreatedAt: timestamp(d.CreatedAt),
|
|
Attempts: mapValues(d.Attempts, func(a alert.Attempt) *pb.AlertAttempt {
|
|
return &pb.AlertAttempt{N: a.N, At: timestamp(a.At), StatusCode: int64(a.StatusCode), Error: a.Error}
|
|
}),
|
|
}
|
|
}
|
|
|
|
func alertDeliveryValue(d *pb.AlertDelivery) alert.Delivery {
|
|
return alert.Delivery{
|
|
ID: d.GetId(), Event: d.GetEvent(), State: d.GetState(), CreatedAt: timeValue(d.GetCreatedAt()),
|
|
Attempts: mapValues(d.GetAttempts(), func(a *pb.AlertAttempt) alert.Attempt {
|
|
return alert.Attempt{N: a.GetN(), At: timeValue(a.GetAt()), StatusCode: int(a.GetStatusCode()), Error: a.GetError()}
|
|
}),
|
|
}
|
|
}
|
|
|
|
func (s *server) RaiseAlert(ctx context.Context, r *pb.RaiseAlertRequest) (*pb.RaiseAlertResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
orgID, err := alertOrg(ctx, r.GetOrgId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stored, err := s.deps.Alerts.RaiseAlert(ctx, alert.Input{
|
|
Org: orgID, Kind: r.GetKind(), Subject: r.GetSubject(),
|
|
Severity: alert.Severity(r.GetSeverity()), Title: r.GetTitle(), Body: r.GetBody(), Link: r.GetLink(),
|
|
Condition: r.GetCondition(),
|
|
})
|
|
if err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.RaiseAlertResponse{Alert: alertProto(stored)}, nil
|
|
}
|
|
|
|
func (s *server) ResolveAlert(ctx context.Context, r *pb.ResolveAlertRequest) (*pb.ResolveAlertResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
orgID, err := alertOrg(ctx, r.GetOrgId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
key := alert.Key{Org: orgID, Kind: r.GetKind(), Subject: r.GetSubject()}
|
|
if err := s.deps.Alerts.ResolveAlert(ctx, key, access.ActorFromContext(ctx)); err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.ResolveAlertResponse{}, nil
|
|
}
|
|
|
|
func (s *server) DismissAlert(ctx context.Context, r *pb.DismissAlertRequest) (*pb.DismissAlertResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
orgID, err := alertOrg(ctx, r.GetOrgId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.deps.Alerts.DismissAlert(ctx, orgID, r.GetId(), access.ActorFromContext(ctx)); err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.DismissAlertResponse{}, nil
|
|
}
|
|
|
|
func (s *server) ListAlerts(ctx context.Context, r *pb.ListAlertsRequest) (*pb.ListAlertsResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
orgID, err := alertOrg(ctx, r.GetOrgId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
filter := alert.Filter{Limit: int(r.GetLimit())}
|
|
for _, st := range r.GetStates() {
|
|
filter.States = append(filter.States, alert.State(st))
|
|
}
|
|
alerts, err := s.deps.Alerts.ListAlerts(ctx, orgID, filter)
|
|
if err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.ListAlertsResponse{Alerts: mapValues(alerts, alertProto)}, nil
|
|
}
|
|
|
|
// requireUnscoped refuses a caller confined to an org: the delivery queue
|
|
// spans every org.
|
|
func requireUnscoped(ctx context.Context) error {
|
|
if _, ok := org.Scope(ctx); ok {
|
|
return status.Error(codes.PermissionDenied, "alert deliveries are not available to an org-scoped caller")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *server) ClaimAlertDeliveries(ctx context.Context, r *pb.ClaimAlertDeliveriesRequest) (*pb.ClaimAlertDeliveriesResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
if err := requireUnscoped(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
due, err := s.deps.Alerts.ClaimAlertDeliveries(ctx, int(r.GetLimit()))
|
|
if err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.ClaimAlertDeliveriesResponse{Due: mapValues(due, func(d alert.Due) *pb.DueAlertDelivery {
|
|
return &pb.DueAlertDelivery{Delivery: alertDeliveryProto(d.Delivery), Alert: alertProto(d.Alert), Attempt: d.Attempt}
|
|
})}, nil
|
|
}
|
|
|
|
func (s *server) RecordAlertAttempt(ctx context.Context, r *pb.RecordAlertAttemptRequest) (*pb.RecordAlertAttemptResponse, error) {
|
|
if s.deps.Alerts == nil {
|
|
return nil, errAlertsUnavailable
|
|
}
|
|
if err := requireUnscoped(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
err := s.deps.Alerts.RecordAlertAttempt(ctx, alert.Outcome{
|
|
DeliveryID: r.GetDeliveryId(), Skipped: r.GetSkipped(), StatusCode: int(r.GetStatusCode()), Error: r.GetError(),
|
|
})
|
|
if err != nil {
|
|
return nil, alertStatus(err)
|
|
}
|
|
return &pb.RecordAlertAttemptResponse{}, nil
|
|
}
|