Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): re-check HTTPS monitor certificates hourly (#2673)
Sven van Ginkelab5d40b84c3a
diff --git a/agent/network_monitor_cert.go b/agent/network_monitor_cert.goindex 08dffed27..5486b1991 100644--- a/agent/network_monitor_cert.go+++ b/agent/network_monitor_cert.go@@ -13,10 +13,9 @@ import ("github.com/henrygd/beszel/internal/entities/monitor")-const (- certCheckInterval = 24 * time.Hour- certCheckRetryInterval = time.Hour-)+// certCheckInterval is how often a certificate is re-read, so renewals show up+// within the hour.+const certCheckInterval = time.Hour// certChecker fetches the leaf certificate for an HTTPS target.type certChecker func(context.Context, string) (monitor.CertInfo, error)diff --git a/agent/network_monitor_cert_test.go b/agent/network_monitor_cert_test.goindex 805833164..4e24b3930 100644--- a/agent/network_monitor_cert_test.go+++ b/agent/network_monitor_cert_test.go@@ -57,7 +57,6 @@ func TestRefreshCertCadence(t *testing.T) {defer task.cancel()var calls intvar fail error- // Far enough out that the regular interval applies for the whole test.expires := time.Now().Add(365 * 24 * time.Hour).UnixMilli()check := func(context.Context, string) (monitor.CertInfo, error) {calls++@@ -79,51 +78,23 @@ func TestRefreshCertCadence(t *testing.T) {task.refreshCert(check)assert.Equal(t, 2, calls)- // Failures keep the last known certificate and retry sooner.+ // Failures keep the last known certificate and retry after the interval.fail = errors.New("connection refused")time.Sleep(certCheckInterval)task.refreshCert(check)assert.Equal(t, 3, calls)assert.Equal(t, expires+2, task.certInfo().Expires)- time.Sleep(certCheckRetryInterval)fail = nil+ time.Sleep(certCheckInterval - time.Second)+ task.refreshCert(check)+ assert.Equal(t, 3, calls)+ time.Sleep(time.Second)task.refreshCert(check)assert.Equal(t, 4, calls)assert.Equal(t, expires+4, task.certInfo().Expires)})}-func TestRefreshCertRetriesSoonerNearExpiry(t *testing.T) {- for _, tc := range []struct {- name string- expires time.Duration // relative to the check- interval time.Duration- }{- {"expired", -time.Hour, certCheckRetryInterval},- {"expires before next regular check", certCheckInterval - time.Minute, certCheckRetryInterval},- {"expires after next regular check", certCheckInterval + time.Minute, certCheckInterval},- } {- t.Run(tc.name, func(t *testing.T) {- synctest.Test(t, func(t *testing.T) {- task := newMonitorTask(monitor.Config{ID: "test", Target: "https://example.test", Protocol: "http"})- defer task.cancel()- var calls int- check := func(context.Context, string) (monitor.CertInfo, error) {- calls++- return monitor.CertInfo{Expires: time.Now().Add(tc.expires).UnixMilli()}, nil- }- task.refreshCert(check)- time.Sleep(tc.interval - time.Second)- task.refreshCert(check)- assert.Equal(t, 1, calls)- time.Sleep(time.Second)- task.refreshCert(check)- assert.Equal(t, 2, calls)- })- })- }-}-func TestCertCheckEnabled(t *testing.T) {tests := []struct {protocol, target stringdiff --git a/agent/network_monitor_task.go b/agent/network_monitor_task.goindex 3ff6ed8f7..e005e6b9d 100644--- a/agent/network_monitor_task.go+++ b/agent/network_monitor_task.go@@ -119,10 +119,8 @@ func (task *monitorTask) runProbe(probe monitorProbe) *monitor.Result {}// refreshCert checks the certificate of an HTTPS target when due. A failed-// check keeps the last known certificate and retries sooner, as does a-// certificate that expires before the next regular check, so renewals show up-// quickly. Concurrent callers skip rather than wait, and no lock is held during-// network I/O.+// check keeps the last known certificate. Concurrent callers skip rather than+// wait, and no lock is held during network I/O.func (task *monitorTask) refreshCert(check certChecker) {if check == nil || !certCheckEnabled(task.config) {return@@ -143,19 +141,13 @@ func (task *monitorTask) refreshCert(check certChecker) {if task.ctx.Err() != nil {return}+ task.nextCertCheck = time.Now().Add(certCheckInterval)if err != nil {- task.nextCertCheck = time.Now().Add(certCheckRetryInterval)slog.Warn("certificate check failed", "err", err, "target", task.config.Target)return}task.cert = &infotask.certUnsent = true- now := time.Now()- interval := certCheckInterval- if time.UnixMilli(info.Expires).Before(now.Add(certCheckInterval)) {- interval = certCheckRetryInterval- }- task.nextCertCheck = now.Add(interval)}// certInfo returns a copy of the latest certificate info, or nil if unknown.