console: a stale heartbeat leaves certificates unjudged (R-40)
A heartbeat pass judges cert_expiring only when no listed box is stale: aliasReadingsFrom leaves a stale box out, so a host only it reports would be taken for gone, closed, and fired again when the box came back. With a stale box listed, cert_expiring is treated as an unread heartbeat.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+34
−3
internal/console/alertengine_pass_test.go+28 −0internal/console/alertsignals.go+6 −3
diff --git a/internal/console/alertengine_pass_test.go b/internal/console/alertengine_pass_test.go| index 406fa8bfa..9cd5bdcd4 100644 |
| --- a/internal/console/alertengine_pass_test.go |
| +++ b/internal/console/alertengine_pass_test.go |
| @@ -929,6 +929,34 @@ func TestEnginePassUnreadHeartbeatKeepsCertExpiring(t *testing.T) { |
| ef.openAndQuiet(t, "cert_expiring") |
| } |
| +// Ruling R-40: a box whose heartbeat has gone stale is left out of the |
| +// certificate readings, which is not the same as its host no longer being |
| +// served. Its expiring certificate stays open, unjudged, until the box |
| +// reports again or drops out of the listing. |
| +func TestEnginePassStaleHeartbeatKeepsCertExpiring(t *testing.T) { |
| + ef := newEngineFixture(t, nil) |
| + ef.operator(t, opA) |
| + expiring := &status.BoxMetrics{Aliases: map[string]status.AliasReading{ |
| + "shop.example.com": {Route: "shop.example.com", CertExpiresAt: alertT0.Add(3 * 24 * time.Hour).Unix()}}} |
| + ef.heartbeat(t, "box-1", alertT0, expiring) |
| + ef.heartbeat(t, "box-2", alertT0, nil) |
| + ef.passesTo(t, 0, 5*time.Minute) |
| + if n := ef.count(t, `SELECT COUNT(*) FROM alert_incidents WHERE closed_at IS NULL AND rule = 'cert_expiring'`); n != 1 { |
| + t.Fatalf("%d open cert_expiring incidents, want 1", n) |
| + } |
| + // box-1 stops reporting; box-2 goes on. Past 15 minutes box-1 is stale. |
| + for m := 6; m <= 30; m++ { |
| + ef.heartbeat(t, "box-2", alertT0.Add(time.Duration(m)*time.Minute), nil) |
| + ef.passAt(t, time.Duration(m)*time.Minute) |
| + } |
| + if n := ef.count(t, `SELECT COUNT(*) FROM alert_incidents WHERE closed_at IS NULL AND rule = 'cert_expiring'`); n != 1 { |
| + t.Fatalf("%d open cert_expiring incidents with box-1 stale, want 1", n) |
| + } |
| + if n := ef.count(t, `SELECT COUNT(*) FROM alert_outbox WHERE kind = 'clear'`); n != 0 { |
| + t.Fatalf("%d clears queued while box-1 is stale, want none", n) |
| + } |
| +} |
| + |
| // An alert object that does not parse is stepped over: the box beside it |
| // still fires, and the junk box's state is left as it was. |
| func TestEnginePassSurvivesJunk(t *testing.T) { |
diff --git a/internal/console/alertsignals.go b/internal/console/alertsignals.go| index 30daf48df..829a751f3 100644 |
| --- a/internal/console/alertsignals.go |
| +++ b/internal/console/alertsignals.go |
| @@ -372,8 +372,9 @@ func alertReadFailed(ctx context.Context, err error) { |
| // (Review Focus 3: every box with an object is a member, whether or not it |
| // ever fired anything) and alert_delivery_broken, and cert_expiring for |
| // every host the fresh boxes serve. Certificates are judged only when every |
| -// heartbeat was read, so a host on a box whose read failed is not taken |
| -// for gone. |
| +// heartbeat was read and none is stale (ruling R-40): aliasReadingsFrom |
| +// leaves a stale box out, so a host only it reports would otherwise be |
| +// taken for gone, closed, and fired again when the box came back. |
| func heartbeatBoxSignals(boxes []status.Box, unread map[string]bool, settings map[string]alertRuleSetting, now time.Time, sig *alertSignals) { |
| silentAfter := time.Duration(alertThreshold(settings, "box_silent")) * time.Minute |
| for _, b := range boxes { |
| @@ -389,7 +390,9 @@ func heartbeatBoxSignals(boxes []status.Box, unread map[string]bool, settings ma |
| } |
| sig.cover("box_silent", unread) |
| sig.cover("alert_delivery_broken", unread) |
| - if len(unread) > 0 { |
| + if len(unread) > 0 || slices.ContainsFunc(boxes, func(b status.Box) bool { |
| + return status.BoxStale(b, now, status.SnapshotPeriod) |
| + }) { |
| return |
| } |
| within := time.Duration(alertThreshold(settings, "cert_expiring")*24) * time.Hour |