From 82fd68a41b13c28d3aff5eb448d54cd161f99fb1 Mon Sep 17 00:00:00 2001 From: clawbot Date: Sat, 21 Feb 2026 00:54:59 -0800 Subject: [PATCH] fix: deduplicate TLS expiry warnings to prevent notification spam (closes #18) checkTLSExpiry fired every monitoring cycle with no deduplication, causing notification spam for expiring certificates. Added an in-memory map tracking the last notification time per domain/IP pair, suppressing re-notification within the TLS check interval. Added TestTLSExpiryWarningDedup to verify deduplication works. --- internal/watcher/watcher.go | 71 +++++++++++++++++++++----------- internal/watcher/watcher_test.go | 55 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 25 deletions(-) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 742834c..0f683ff 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -6,6 +6,7 @@ import ( "log/slog" "sort" "strings" + "sync" "time" "go.uber.org/fx" @@ -40,15 +41,17 @@ type Params struct { // Watcher orchestrates all monitoring checks on a schedule. type Watcher struct { - log *slog.Logger - config *config.Config - state *state.State - resolver DNSResolver - portCheck PortChecker - tlsCheck TLSChecker - notify Notifier - cancel context.CancelFunc - firstRun bool + log *slog.Logger + config *config.Config + state *state.State + resolver DNSResolver + portCheck PortChecker + tlsCheck TLSChecker + notify Notifier + cancel context.CancelFunc + firstRun bool + expiryNotifiedMu sync.Mutex + expiryNotified map[string]time.Time } // New creates a new Watcher instance wired into the fx lifecycle. @@ -57,14 +60,15 @@ func New( params Params, ) (*Watcher, error) { w := &Watcher{ - log: params.Logger.Get(), - config: params.Config, - state: params.State, - resolver: params.Resolver, - portCheck: params.PortCheck, - tlsCheck: params.TLSCheck, - notify: params.Notify, - firstRun: true, + log: params.Logger.Get(), + config: params.Config, + state: params.State, + resolver: params.Resolver, + portCheck: params.PortCheck, + tlsCheck: params.TLSCheck, + notify: params.Notify, + firstRun: true, + expiryNotified: make(map[string]time.Time), } lifecycle.Append(fx.Hook{ @@ -100,14 +104,15 @@ func NewForTest( n Notifier, ) *Watcher { return &Watcher{ - log: slog.Default(), - config: cfg, - state: st, - resolver: res, - portCheck: pc, - tlsCheck: tc, - notify: n, - firstRun: true, + log: slog.Default(), + config: cfg, + state: st, + resolver: res, + portCheck: pc, + tlsCheck: tc, + notify: n, + firstRun: true, + expiryNotified: make(map[string]time.Time), } } @@ -691,6 +696,22 @@ func (w *Watcher) checkTLSExpiry( return } + // Deduplicate expiry warnings: don't re-notify for the same + // hostname within the TLS check interval. + dedupKey := fmt.Sprintf("expiry:%s:%s", hostname, ip) + + w.expiryNotifiedMu.Lock() + + lastNotified, seen := w.expiryNotified[dedupKey] + if seen && time.Since(lastNotified) < w.config.TLSInterval { + w.expiryNotifiedMu.Unlock() + + return + } + + w.expiryNotified[dedupKey] = time.Now() + w.expiryNotifiedMu.Unlock() + msg := fmt.Sprintf( "Host: %s\nIP: %s\nCN: %s\n"+ "Expires: %s (%.0f days)", diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go index 57b56c7..cf15c23 100644 --- a/internal/watcher/watcher_test.go +++ b/internal/watcher/watcher_test.go @@ -506,6 +506,61 @@ func TestTLSExpiryWarning(t *testing.T) { } } +func TestTLSExpiryWarningDedup(t *testing.T) { + t.Parallel() + + cfg := defaultTestConfig(t) + cfg.Hostnames = []string{"www.example.com"} + cfg.TLSInterval = 24 * time.Hour + + w, deps := newTestWatcher(t, cfg) + + deps.resolver.allRecords["www.example.com"] = map[string]map[string][]string{ + "ns1.example.com.": {"A": {"1.2.3.4"}}, + } + deps.resolver.ipAddresses["www.example.com"] = []string{ + "1.2.3.4", + } + deps.portChecker.results["1.2.3.4:80"] = true + deps.portChecker.results["1.2.3.4:443"] = true + deps.tlsChecker.certs["1.2.3.4:www.example.com"] = &tlscheck.CertificateInfo{ + CommonName: "www.example.com", + Issuer: "DigiCert", + NotAfter: time.Now().Add(3 * 24 * time.Hour), + SubjectAlternativeNames: []string{ + "www.example.com", + }, + } + + ctx := t.Context() + + // First run = baseline, no notifications + w.RunOnce(ctx) + + // Second run should fire one expiry warning + w.RunOnce(ctx) + + // Third run should NOT fire another warning (dedup) + w.RunOnce(ctx) + + notifications := deps.notifier.getNotifications() + + expiryCount := 0 + + for _, n := range notifications { + if n.Title == "TLS Expiry Warning: www.example.com" { + expiryCount++ + } + } + + if expiryCount != 1 { + t.Errorf( + "expected exactly 1 expiry warning (dedup), got %d", + expiryCount, + ) + } +} + func TestGracefulShutdown(t *testing.T) { t.Parallel() -- 2.49.1