feat(alerts): send each organisation's alerts to its own webhook
This commit is contained in:
20 files changed
+419
-183
No files matched your search
@@ -15,10 +15,15 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/alertwebhook"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
// notifyCredential is the credential binding service an org's alert webhook
|
||||
// is signed with.
|
||||
const notifyCredential = "notify"
|
||||
|
||||
const (
|
||||
// deliveryInterval is how often due webhook deliveries are claimed.
|
||||
deliveryInterval = 15 * time.Second
|
||||
@@ -296,31 +301,35 @@ func (b *boot) recheckAlerts(ctx context.Context) {
|
||||
// as skipped; one that fails is retried by the store on its schedule.
|
||||
func (b *boot) deliverAlerts(store alert.Deliverer) func(context.Context) {
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
public := alertwebhook.PublicClient()
|
||||
// The system org's webhook may sit on the instance's own network; another
|
||||
// org's reaches the public internet only.
|
||||
clientFor := func(owner org.OrgID) *http.Client {
|
||||
if owner == "" || owner == org.DefaultOrgID {
|
||||
return client
|
||||
}
|
||||
return public
|
||||
}
|
||||
return func(ctx context.Context) {
|
||||
due, err := store.ClaimAlertDeliveries(ctx, 20)
|
||||
if err != nil {
|
||||
b.log.WarnContext(ctx, "alert deliveries not claimed", "err", err)
|
||||
return
|
||||
}
|
||||
notify := b.cfgHolder.Get().Notify
|
||||
notifies := map[org.OrgID]config.Notify{}
|
||||
for _, d := range due {
|
||||
notify, ok := notifies[d.Alert.Org]
|
||||
outcome := alert.Outcome{DeliveryID: d.ID}
|
||||
threshold := alert.Severity(notify.MinSeverity)
|
||||
if !threshold.Valid() {
|
||||
threshold = alert.SeverityWarning
|
||||
}
|
||||
if notify.Webhook == "" || d.Alert.Severity.Rank() < threshold.Rank() {
|
||||
outcome.Skipped = true
|
||||
} else {
|
||||
secret := ""
|
||||
if notify.Secret != (config.SecretRef{}) {
|
||||
secret, err = b.secrets.Resolve(notify.Secret)
|
||||
}
|
||||
if !ok {
|
||||
notify, err = b.alertNotify(ctx, d.Alert.Org)
|
||||
if err != nil {
|
||||
outcome.Error = "notify secret could not be resolved"
|
||||
} else {
|
||||
outcome = alertwebhook.Send(ctx, client, notify.Webhook, secret, d)
|
||||
b.log.WarnContext(ctx, "alert notification settings unavailable", "org", d.Alert.Org, "err", err)
|
||||
outcome.Error = "notification settings unavailable"
|
||||
}
|
||||
notifies[d.Alert.Org] = notify
|
||||
}
|
||||
if outcome.Error == "" {
|
||||
outcome = b.sendAlert(ctx, clientFor(d.Alert.Org), notify, d)
|
||||
}
|
||||
if err := store.RecordAlertAttempt(ctx, outcome); err != nil {
|
||||
b.log.WarnContext(ctx, "alert delivery not recorded", "delivery", d.ID, "err", err)
|
||||
@@ -329,6 +338,54 @@ func (b *boot) deliverAlerts(store alert.Deliverer) func(context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// sendAlert posts one delivery to notify's webhook, or closes it as skipped
|
||||
// when there is none or the alert is below its severity threshold.
|
||||
func (b *boot) sendAlert(ctx context.Context, client *http.Client, notify config.Notify, d alert.Due) alert.Outcome {
|
||||
threshold := alert.Severity(notify.MinSeverity)
|
||||
if !threshold.Valid() {
|
||||
threshold = alert.SeverityWarning
|
||||
}
|
||||
if notify.Webhook == "" || d.Alert.Severity.Rank() < threshold.Rank() {
|
||||
return alert.Outcome{DeliveryID: d.ID, Skipped: true}
|
||||
}
|
||||
secret := ""
|
||||
if notify.Secret != (config.SecretRef{}) {
|
||||
var err error
|
||||
if secret, err = b.secrets.Resolve(notify.Secret); err != nil {
|
||||
return alert.Outcome{DeliveryID: d.ID, Error: "notify secret could not be resolved"}
|
||||
}
|
||||
}
|
||||
return alertwebhook.Send(ctx, client, notify.Webhook, secret, d)
|
||||
}
|
||||
|
||||
// alertNotify is the webhook an org's alerts go to: its own task policy,
|
||||
// signed with the org's credential binding named notify. The system org's is
|
||||
// the daemon's.
|
||||
func (b *boot) alertNotify(ctx context.Context, owner org.OrgID) (config.Notify, error) {
|
||||
cfg := b.cfgHolder.Get()
|
||||
if owner == "" || owner == org.DefaultOrgID {
|
||||
return cfg.Notify, nil
|
||||
}
|
||||
var notify config.Notify
|
||||
for _, o := range b.organisations(ctx) {
|
||||
if o.owner != owner {
|
||||
continue
|
||||
}
|
||||
_, _, err := b.controlPlane.Query(o.act(ctx), controlplanestore.TaskPolicyKind, func(value []byte) (err error) {
|
||||
notify, err = controlplanestore.OrgNotify(value)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return config.Notify{}, err
|
||||
}
|
||||
if binding, ok := cfg.Containers.BoundCredentials(string(owner), []string{notifyCredential}, []string{notifyCredential})[notifyCredential]; ok {
|
||||
notify.Secret = binding.Secret
|
||||
}
|
||||
return notify, nil
|
||||
}
|
||||
return config.Notify{}, nil
|
||||
}
|
||||
|
||||
// credentialWatch raises an alert when a bound credential cannot be resolved
|
||||
// and clears it once it can. The egress path calls observe per request, so an
|
||||
// unchanged state is reported at most every credentialReportEvery.
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package alertwebhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PublicClient reaches public addresses only and follows no redirect. It
|
||||
// carries a webhook an org chose: the daemon sends from inside the instance's
|
||||
// network, so the org must not aim it at the daemon's own host or LAN.
|
||||
func PublicClient() *http.Client {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: timeout,
|
||||
Control: func(_, address string, _ syscall.RawConn) error {
|
||||
host, _, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ip := net.ParseIP(host); ip == nil || !public(ip) {
|
||||
return fmt.Errorf("webhook address %s is not public", host)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Transport: &http.Transport{DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}},
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
}
|
||||
}
|
||||
|
||||
func public(ip net.IP) bool {
|
||||
switch {
|
||||
case ip.IsLoopback(), ip.IsPrivate(), ip.IsUnspecified(),
|
||||
ip.IsLinkLocalUnicast(), ip.IsLinkLocalMulticast(),
|
||||
ip.IsInterfaceLocalMulticast(), ip.IsMulticast():
|
||||
return false
|
||||
}
|
||||
v4 := ip.To4()
|
||||
return v4 == nil || v4[0] != 100 || v4[1] < 64 || v4[1] > 127
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package alertwebhook
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublicRefusesInstanceNetworks(t *testing.T) {
|
||||
tests := []struct {
|
||||
ip string
|
||||
want bool
|
||||
}{
|
||||
{"8.8.8.8", true},
|
||||
{"2606:4700:4700::1111", true},
|
||||
{"127.0.0.1", false},
|
||||
{"::1", false},
|
||||
{"10.0.0.5", false},
|
||||
{"192.168.1.10", false},
|
||||
{"169.254.169.254", false},
|
||||
{"100.64.0.1", false},
|
||||
{"0.0.0.0", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.ip, func(t *testing.T) {
|
||||
if got := public(net.ParseIP(tt.ip)); got != tt.want {
|
||||
t.Fatalf("public(%s) = %v, want %v", tt.ip, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -202,6 +202,7 @@ var orgSeededKinds = map[string]bool{
|
||||
// instance's, so nothing is stored for it and the instance's values are not
|
||||
// copied in.
|
||||
var orgEmptyDocuments = map[string]string{
|
||||
TaskPolicyKind: `{"notify_webhook":"","notify_min_severity":"","notify_secret":{}}`,
|
||||
SchedulingPolicyKind: `{"poll_interval":"","max_retries":0,"dispatch":{"trigger":"","ack_reaction":""}}`,
|
||||
}
|
||||
|
||||
|
||||
@@ -183,11 +183,11 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any)
|
||||
// AgentProfile, WorkflowExecutionSettings, ExtensionSettings, UpdateSettings,
|
||||
// AuditRetention, ScheduleSettings (the zone every cron schedule reads in).
|
||||
// Pending an org owner of the channels they configure:
|
||||
// ChannelSettings, TaskPolicy (outbound notification webhook).
|
||||
// ChannelSettings.
|
||||
var instanceOnlyKinds = map[string]bool{
|
||||
ChannelSettingsKind: true,
|
||||
TaskPolicyKind: true, ToolSettingsKind: true,
|
||||
PluginSettingsKind: true, ContainerRuntimePoliciesKind: true, AgentProfileKind: true,
|
||||
ToolSettingsKind: true,
|
||||
PluginSettingsKind: true, ContainerRuntimePoliciesKind: true, AgentProfileKind: true,
|
||||
ExtensionSettingsKind: true, UpdateSettingsKind: true, WorkflowExecutionSettingsKind: true,
|
||||
AuditRetentionKind: true, ScheduleSettingsKind: true,
|
||||
}
|
||||
@@ -209,6 +209,8 @@ func (s *Server) checkOrgWrite(ctx context.Context, orgID, kind string, value []
|
||||
if orgID != storecontract.DefaultOrgID {
|
||||
return s.checkOrgProviders(ctx, orgID, value)
|
||||
}
|
||||
case TaskPolicyKind:
|
||||
return checkTaskPolicyWrite(orgID, value)
|
||||
case SchedulingPolicyKind:
|
||||
return checkSchedulingWrite(orgID, value)
|
||||
case ModelAliasesKind:
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package controlplanestore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/alert"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
const TaskPolicyKind = "task-policy"
|
||||
@@ -44,3 +46,31 @@ func taskPolicyOf(cfg config.Config) taskPolicy {
|
||||
NotifyMinSeverity: cfg.Notify.MinSeverity, NotifySecret: cfg.Notify.Secret,
|
||||
}
|
||||
}
|
||||
|
||||
// OrgNotify is the alert webhook a non-system org's task policy names. Its
|
||||
// signing secret is never read from the document (checkTaskPolicyWrite).
|
||||
func OrgNotify(stored []byte) (config.Notify, error) {
|
||||
var doc taskPolicy
|
||||
if err := json.Unmarshal(stored, &doc); err != nil {
|
||||
return config.Notify{}, fmt.Errorf("decode %s: %w", TaskPolicyKind, err)
|
||||
}
|
||||
return config.Notify{Webhook: doc.NotifyWebhook, MinSeverity: doc.NotifyMinSeverity}, nil
|
||||
}
|
||||
|
||||
// checkTaskPolicyWrite keeps an org from naming a secret: the reference
|
||||
// resolves against the instance's engines, so it would let the org sign with
|
||||
// any secret the instance holds. The org's signing key is its credential
|
||||
// binding named notify.
|
||||
func checkTaskPolicyWrite(orgID string, value []byte) error {
|
||||
if orgID == storecontract.DefaultOrgID {
|
||||
return nil
|
||||
}
|
||||
var doc taskPolicy
|
||||
if err := json.Unmarshal(value, &doc); err != nil {
|
||||
return err
|
||||
}
|
||||
if doc.NotifySecret != (config.SecretRef{}) {
|
||||
return fmt.Errorf("%w: notify_secret is the instance's; an org signs with its credential binding named notify", ErrValidation)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package controlplanestore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
func TestTaskPolicyNamesNoInstanceSecretForAnotherOrg(t *testing.T) {
|
||||
const signed = `{"notify_webhook":"https://example.com/hook","notify_secret":{"engine":"env","key":"INSTANCE_KEY"}}`
|
||||
const unsigned = `{"notify_webhook":"https://example.com/hook"}`
|
||||
tests := []struct {
|
||||
name, org, value string
|
||||
refused bool
|
||||
}{
|
||||
{"system org signs with a secret", storecontract.DefaultOrgID, signed, false},
|
||||
{"another org names an instance secret", "acme", signed, true},
|
||||
{"another org sets only its webhook", "acme", unsigned, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, TaskPolicyKind, []byte(tt.value))
|
||||
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
|
||||
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user