console: every process checks sealed targets against its key (R-38)
The first pass of each process tries the key in use on every sealed target and marks each one it cannot open as needing re-entry, whether or not the key was just made: a process that made the key and stopped before marking no longer leaves the next one silent. The drain's seal hold persists the same mark, so seal_key fires and the page says the URL needs entering. MarkConnectorNeedsReentry replaces the bulk mark.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+169
−33
internal/console/alertengine_pass.go+53 −17internal/console/alertengine_pass_test.go+37 −0internal/console/alertnotify.go+7 −0internal/console/alertnotify_test.go+25 −1internal/console/alertstore.go+32 −9internal/console/alertstore_test.go+15 −6
diff --git a/internal/console/alertengine_pass.go b/internal/console/alertengine_pass.go| index 0368b7ea6..8831689d5 100644 |
| --- a/internal/console/alertengine_pass.go |
| +++ b/internal/console/alertengine_pass.go |
| @@ -26,8 +26,9 @@ import ( |
| // what is firing, and writes the beat the console box's agent watches |
| // (spec §8). |
| // |
| -// The first pass of a process opens the connector seal and, once per |
| -// database, migrates the watcher's delivery history (alert_seen) so nobody |
| +// The first pass of a process opens the connector seal, tries it on every |
| +// sealed target and marks the ones it cannot open (ruling R-38), and, once |
| +// per database, migrates the watcher's delivery history (alert_seen) so nobody |
| // is told twice, or not told a clear they are owed. Every pass seeds each |
| // operator not yet seeded an email connector, so an operator made later is |
| // reached within a minute (ruling R-20); each address is seeded once ever, |
| @@ -71,9 +72,10 @@ type alertEngineState struct { |
| started bool |
| passes int |
| // sealErr is why the seal key could not be used, for every pass after. |
| - // reentryPending is a new key's re-entry marking still to do. |
| - sealErr error |
| - reentryPending bool |
| + // sealChecked is set once every sealed target has been tried with the |
| + // key in use (ruling R-38). |
| + sealErr error |
| + sealChecked bool |
| // since is each box rule firing on the dashboard, since when. |
| since map[alertKey]time.Time |
| // unreachableAt is when operators were last checked for a usable |
| @@ -167,15 +169,12 @@ func (s *Server) alertEngineStep(ctx context.Context, now time.Time) (open []ale |
| log.Printf("console: alert engine: seed operator connectors: %v", err) |
| jobFailed(ctx, fmt.Errorf("seed operator connectors: %w", err)) |
| } |
| - if e.reentryPending { |
| - if n, err := s.store.MarkSealedConnectorsNeedReentry(); err != nil { |
| - log.Printf("console: alert engine: mark sealed connectors for re-entry: %v", err) |
| - jobFailed(ctx, fmt.Errorf("mark sealed connectors for re-entry: %w", err)) |
| + if !e.sealChecked { |
| + if err := s.checkSealedTargets(); err != nil { |
| + log.Printf("console: alert engine: check sealed connectors: %v", err) |
| + jobFailed(ctx, fmt.Errorf("check sealed connectors: %w", err)) |
| } else { |
| - e.reentryPending = false |
| - if n > 0 { |
| - log.Printf("console: alert engine: a new seal key was made; %d Slack and webhook connectors need re-entering", n) |
| - } |
| + e.sealChecked = true |
| } |
| } |
| settings, err := s.store.AlertRuleSettings(alertScopeFleet) |
| @@ -217,9 +216,9 @@ func (s *Server) alertEngineStep(ctx context.Context, now time.Time) (open []ale |
| // openAlertSeal opens the connector seal on the first pass (spec §5.1). A |
| // key that cannot be used leaves every sealed connector held (the drain's |
| // "no seal key"), fires seal_key and notes it in the job status on every |
| -// pass; a key made fresh, while sealed connectors exist, marks them as |
| -// needing re-entry. A seal already open (a test's) or no key path (tests) |
| -// is left as it is. |
| +// pass. A seal already open (a test's) or no key path (tests) is left as it |
| +// is. Whether the key was made just now is not trusted to say what it can |
| +// open: checkSealedTargets tries every target with it. |
| func (s *Server) openAlertSeal() { |
| e := &s.alertEngine |
| if s.alertSeal.Load() != nil || s.cfg.AlertSealKeyPath == "" { |
| @@ -231,8 +230,45 @@ func (s *Server) openAlertSeal() { |
| e.sealErr = err |
| return |
| } |
| + if created { |
| + log.Printf("console: alert engine: made a new seal key; Slack and webhook connectors sealed before it need re-entering") |
| + } |
| s.alertSeal.Store(seal) |
| - e.reentryPending = created |
| +} |
| + |
| +// checkSealedTargets tries the key in use on every sealed target not yet |
| +// marked, and marks each one it cannot open as needing re-entry (ruling |
| +// R-38), so seal_key fires and the page says so. It runs on each process's |
| +// first pass, whether or not the key was just made: a process that made |
| +// the key and stopped before marking leaves the next one a key that looks |
| +// old. With no key in use there is nothing to try; seal_key already fires |
| +// for that (sealErr). |
| +func (s *Server) checkSealedTargets() error { |
| + seal := s.alertSeal.Load() |
| + if seal == nil { |
| + return nil |
| + } |
| + targets, err := s.store.SealedAlertTargets() |
| + if err != nil { |
| + return err |
| + } |
| + n := 0 |
| + for id, sealed := range targets { |
| + if _, err := seal.Open(sealed); err == nil { |
| + continue |
| + } |
| + marked, err := s.store.MarkConnectorNeedsReentry(id) |
| + if err != nil { |
| + return err |
| + } |
| + if marked { |
| + n++ |
| + } |
| + } |
| + if n > 0 { |
| + log.Printf("console: alert engine: the seal key cannot open %d Slack and webhook connectors; they need re-entering", n) |
| + } |
| + return nil |
| } |
| // seedOperatorConnectors gives every operator not yet seeded their email |
diff --git a/internal/console/alertengine_pass_test.go b/internal/console/alertengine_pass_test.go| index 5e06a8096..406fa8bfa 100644 |
| --- a/internal/console/alertengine_pass_test.go |
| +++ b/internal/console/alertengine_pass_test.go |
| @@ -514,6 +514,43 @@ func TestEnginePassNewSealKeyMarksReentry(t *testing.T) { |
| } |
| } |
| +// Ruling R-38: a process made the new key and stopped before marking the |
| +// connectors sealed under the lost one. The next process finds a key |
| +// already there, so nothing says the key is new; its first pass still |
| +// tries every sealed target, marks the one it cannot open, and seal_key |
| +// fires. A connector sealed under the key in use is left alone. |
| +func TestEnginePassSealReconcilesAfterInterruptedKeyLoss(t *testing.T) { |
| + key := filepath.Join(t.TempDir(), "alert-connectors.key") |
| + current, created, err := openConnectorSeal(key) // the crashed process's work |
| + if err != nil || !created { |
| + t.Fatalf("make the key: created %v, %v", created, err) |
| + } |
| + ef := newEngineFixture(t, func(c *Config) { c.AlertSealKeyPath = key }) |
| + ef.srv.alertSeal.Store(nil) |
| + ef.operator(t, opA) |
| + lost := ef.addHook(t, alertConnectorSlack, "https://hooks.slack.com/services/T0/B0/secret", true) // the fixture's own key |
| + sealed, err := current.Seal([]byte("https://hooks.slack.com/services/T1/B1/other")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + kept, err := ef.st.AddAlertConnector(alertConnector{Scope: alertScopeFleet, Kind: alertConnectorSlack, Name: "kept", |
| + Confirmed: true}, sealed, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + ef.passesTo(t, 0, 5*time.Minute) |
| + if !ef.connector(t, lost).NeedsReentry { |
| + t.Fatal("a connector the key cannot open does not need re-entering") |
| + } |
| + if ef.connector(t, kept).NeedsReentry { |
| + t.Fatal("a connector sealed under the key in use was marked") |
| + } |
| + if got := ef.mailsTo(opA); len(got) != 1 || !strings.Contains(got[0], "the console cannot open the key that protects their addresses") { |
| + t.Fatalf("A got %v, want one seal_key fire", got) |
| + } |
| +} |
| + |
| // Spec §5.2, R2 #2, R-23: on a host-claim console the engine is off. It |
| // logs that and returns before any read or write, and the job drops off the |
| // list as the other off-on-host-claim workers do. |
diff --git a/internal/console/alertnotify.go b/internal/console/alertnotify.go| index 56c5711da..2811666c8 100644 |
| --- a/internal/console/alertnotify.go |
| +++ b/internal/console/alertnotify.go |
| @@ -637,6 +637,9 @@ func alertWithheld(c alertConnector, kind string) string { |
| // alertTarget is where c sends: its address, or its opened URL. hold says |
| // why there is none to use (no seal key yet, or one that cannot open it). |
| +// A target the key in use cannot open is marked as needing re-entry there |
| +// and then (ruling R-38): held in silence, it would read as On and fire |
| +// nothing. |
| func (s *Server) alertTarget(c alertConnector) (target, hold string, err error) { |
| sealed, plain, err := s.store.AlertConnectorTarget(c.ID) |
| if err != nil { |
| @@ -651,6 +654,10 @@ func (s *Server) alertTarget(c alertConnector) (target, hold string, err error) |
| } |
| p, err := seal.Open(sealed) |
| if err != nil { |
| + if _, err := s.store.MarkConnectorNeedsReentry(c.ID); err != nil { |
| + return "", "", err |
| + } |
| + log.Printf("console: alert connector %d: the seal key cannot open its URL; it needs re-entering", c.ID) |
| return "", "seal", nil |
| } |
| return string(p), "", nil |
diff --git a/internal/console/alertnotify_test.go b/internal/console/alertnotify_test.go| index aad5cd217..4623163a0 100644 |
| --- a/internal/console/alertnotify_test.go |
| +++ b/internal/console/alertnotify_test.go |
| @@ -486,7 +486,7 @@ func TestDrainSuspendsAfter24hTellsOnce(t *testing.T) { |
| func TestDrainSkipsNeedsReentry(t *testing.T) { |
| nf := newNotifyFixture(t) |
| id := nf.addHook(t, alertConnectorWebhook, "https://alerts.example.com/in", true) |
| - if _, err := nf.st.MarkSealedConnectorsNeedReentry(); err != nil { |
| + if _, err := nf.st.MarkConnectorNeedsReentry(id); err != nil { |
| t.Fatal(err) |
| } |
| nf.queue(t, id, 0, "fire", fireNote()) |
| @@ -511,6 +511,30 @@ func TestDrainSkipsNeedsReentry(t *testing.T) { |
| } |
| } |
| +// Ruling R-38: a target the key in use cannot open is held and marked as |
| +// needing re-entry, so seal_key fires and the page says so; it is not left |
| +// held in silence. |
| +func TestDrainSealHoldMarksReentry(t *testing.T) { |
| + nf := newNotifyFixture(t) |
| + id := nf.addHook(t, alertConnectorSlack, "https://hooks.slack.com/services/T/B/x", true) |
| + other, _, err := openConnectorSeal(filepath.Join(t.TempDir(), "other.key")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + nf.srv.alertSeal.Store(other) |
| + nf.queue(t, id, 0, "fire", fireNote()) |
| + nf.drain(t) |
| + if n := len(nf.hook.requests()); n != 0 { |
| + t.Fatalf("%d sends with a key that cannot open the target", n) |
| + } |
| + if !nf.connector(t, id).NeedsReentry { |
| + t.Fatal("the seal hold did not mark the connector as needing re-entry") |
| + } |
| + if got := nf.outbox(t, id); len(got) != 1 || got[0].sent || got[0].attempts != 0 { |
| + t.Fatalf("outbox = %+v, want the row kept unsent, no attempt counted", got) |
| + } |
| +} |
| + |
| func TestDrainSkipsRevokedOperatorQueued(t *testing.T) { |
| nf := newNotifyFixture(t) |
| const op = "ops-a@example.com" |
diff --git a/internal/console/alertstore.go b/internal/console/alertstore.go| index fc59be369..24173e22c 100644 |
| --- a/internal/console/alertstore.go |
| +++ b/internal/console/alertstore.go |
| @@ -917,17 +917,40 @@ func (s *Store) SetConnectorHealth(id int64, ok bool, now time.Time) (suspendedN |
| return suspendedNow, nil |
| } |
| -// MarkSealedConnectorsNeedReentry marks every connector with a sealed target |
| -// as needing re-entry, after the seal key was lost and a new one made: what |
| -// they hold can no longer be opened. Email connectors are left alone, since |
| -// their addresses are not sealed. n counts the newly marked. |
| -func (s *Store) MarkSealedConnectorsNeedReentry() (n int, err error) { |
| - res, err := s.db.Exec(`UPDATE alert_connectors SET needs_reentry = 1 WHERE target_sealed IS NOT NULL AND needs_reentry = 0`) |
| +// SealedAlertTargets is every sealed target not yet marked as needing |
| +// re-entry, by connector id, for the engine's first pass to try with the |
| +// key in use (ruling R-38). Email connectors have none: their addresses |
| +// are not sealed. |
| +func (s *Store) SealedAlertTargets() (map[int64][]byte, error) { |
| + rows, err := s.db.Query(`SELECT id, target_sealed FROM alert_connectors WHERE target_sealed IS NOT NULL AND needs_reentry = 0`) |
| if err != nil { |
| - return 0, err |
| + return nil, err |
| + } |
| + defer rows.Close() |
| + out := map[int64][]byte{} |
| + for rows.Next() { |
| + var id int64 |
| + var sealed []byte |
| + if err := rows.Scan(&id, &sealed); err != nil { |
| + return nil, err |
| + } |
| + out[id] = sealed |
| + } |
| + return out, rows.Err() |
| +} |
| + |
| +// MarkConnectorNeedsReentry marks connector id as needing re-entry: the |
| +// key in use cannot open its sealed target, so its URL must be entered |
| +// again (ruling R-38). The mark persists, so seal_key fires and the page |
| +// says so on every pass until it is re-entered. Only a connector with a |
| +// sealed target is marked; marked is true when it was not already. |
| +func (s *Store) MarkConnectorNeedsReentry(id int64) (marked bool, err error) { |
| + res, err := s.db.Exec(`UPDATE alert_connectors SET needs_reentry = 1 WHERE id = ? AND target_sealed IS NOT NULL AND needs_reentry = 0`, id) |
| + if err != nil { |
| + return false, err |
| } |
| - c, err := res.RowsAffected() |
| - return int(c), err |
| + n, err := res.RowsAffected() |
| + return n == 1, err |
| } |
| // ---- rules ------------------------------------------------------------------- |
diff --git a/internal/console/alertstore_test.go b/internal/console/alertstore_test.go| index 783d379ac..9027db927 100644 |
| --- a/internal/console/alertstore_test.go |
| +++ b/internal/console/alertstore_test.go |
| @@ -250,14 +250,20 @@ func TestFinishOutboxBackoff(t *testing.T) { |
| } |
| } |
| -func TestMarkSealedNeedReentryLeavesEmail(t *testing.T) { |
| +func TestMarkConnectorNeedsReentryLeavesEmail(t *testing.T) { |
| s := openTestStore(t) |
| email := alertTestConnector(t, s, alertConnectorEmail) |
| slack := alertTestConnector(t, s, alertConnectorSlack) |
| hook := alertTestConnector(t, s, alertConnectorWebhook) |
| - n, err := s.MarkSealedConnectorsNeedReentry() |
| - if err != nil || n != 2 { |
| - t.Fatalf("marked %d, %v; want 2", n, err) |
| + targets, err := s.SealedAlertTargets() |
| + if err != nil || len(targets) != 2 || targets[email] != nil || string(targets[slack]) != "sealed-target" { |
| + t.Fatalf("sealed targets = %v, %v; want Slack's and the webhook's", targets, err) |
| + } |
| + for _, id := range []int64{email, slack, hook} { |
| + marked, err := s.MarkConnectorNeedsReentry(id) |
| + if err != nil || marked != (id != email) { |
| + t.Fatalf("mark %d = %v, %v", id, marked, err) |
| + } |
| } |
| cs, _ := s.AlertConnectors("fleet") |
| got := map[int64]bool{} |
| @@ -267,8 +273,11 @@ func TestMarkSealedNeedReentryLeavesEmail(t *testing.T) { |
| if got[email] || !got[slack] || !got[hook] { |
| t.Fatalf("needs re-entry = %v (email %d, slack %d, webhook %d)", got, email, slack, hook) |
| } |
| - if n, _ := s.MarkSealedConnectorsNeedReentry(); n != 0 { |
| - t.Fatalf("second mark counted %d again", n) |
| + if marked, _ := s.MarkConnectorNeedsReentry(slack); marked { |
| + t.Fatal("a second mark counted again") |
| + } |
| + if targets, _ := s.SealedAlertTargets(); len(targets) != 0 { |
| + t.Fatalf("marked connectors are tried again: %v", targets) |
| } |
| sealed, plain, err := s.AlertConnectorTarget(email) |
| if err != nil || sealed != nil || plain != "ops@example.com" { |