ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(monitoring): give the TLS certificate read its own clock
Juan Deniscceb444d2704
diff --git a/backend/app/api/monitors.py b/backend/app/api/monitors.pyindex be919d3e..80a67dfc 100644--- a/backend/app/api/monitors.py+++ b/backend/app/api/monitors.py@@ -29,7 +29,14 @@ def list_monitors():q=request.args.get('q') or None,page_id=page_id,)- return jsonify({'monitors': [m.to_dict() for m in monitors]})+ # One extra query for every row's sparkline, not one per row.+ sparks = MonitorService.recent_response_times([m.id for m in monitors])+ payload = []+ for monitor in monitors:+ item = monitor.to_dict()+ item['spark'] = sparks.get(monitor.id, [])+ payload.append(item)+ return jsonify({'monitors': payload})@monitors_bp.route('/stats', methods=['GET'])diff --git a/backend/app/models/status_page.py b/backend/app/models/status_page.pyindex a627b6c5..1f06cbce 100644--- a/backend/app/models/status_page.py+++ b/backend/app/models/status_page.py@@ -88,9 +88,12 @@ class StatusComponent(db.Model):# an outage on a status page.is_paused = db.Column(db.Boolean, default=False, nullable=False)- # TLS certificate observed on the last https probe.+ # TLS certificate observed on the last https probe. `cert_checked_at` is not+ # the same as `last_check_at`: reading the certificate opens a second+ # connection, so it is throttled independently of the probe itself.cert_issuer = db.Column(db.String(256))cert_expires_at = db.Column(db.DateTime)+ cert_checked_at = db.Column(db.DateTime)# Optional binding to a managed WordPress site. When set, the component's# status is driven from the site's health checks (#26) rather than a network@@ -150,6 +153,7 @@ class StatusComponent(db.Model):'is_paused': bool(self.is_paused),'cert_issuer': self.cert_issuer,'cert_expires_at': self.cert_expires_at.isoformat() if self.cert_expires_at else None,+ 'cert_checked_at': self.cert_checked_at.isoformat() if self.cert_checked_at else None,'wordpress_site_id': self.wordpress_site_id,'status': self.status,'last_check_at': self.last_check_at.isoformat() if self.last_check_at else None,diff --git a/backend/app/services/monitor_service.py b/backend/app/services/monitor_service.pyindex 0f719165..384b8a4a 100644--- a/backend/app/services/monitor_service.py+++ b/backend/app/services/monitor_service.py@@ -253,6 +253,8 @@ class MonitorService:monitor.last_check_at = datetime.utcnow()if result.get('response_time') is not None:monitor.last_response_time = result.get('response_time')+ if result.get('cert_checked_at'):+ monitor.cert_checked_at = result['cert_checked_at']if result.get('cert_issuer'):monitor.cert_issuer = result['cert_issuer']if result.get('cert_expires_at'):@@ -406,14 +408,22 @@ class MonitorService:@staticmethoddef _maybe_attach_certificate(monitor, result):"""Read the peer certificate for https monitors, at most every- CERT_REFRESH_SECONDS — it costs a second connection and barely moves."""+ CERT_REFRESH_SECONDS — it costs a second connection and barely moves.++ Throttled on ``cert_checked_at``, not ``last_check_at``: an active+ monitor is checked every 30s, so gating on the probe clock would read the+ certificate once and then never refresh it.+ """target = monitor.check_target or ''if not target.startswith('https://'):return- if monitor.cert_expires_at and monitor.last_check_at:- age = datetime.utcnow() - monitor.last_check_at- if age.total_seconds() < CERT_REFRESH_SECONDS:+ if monitor.cert_checked_at:+ age = (datetime.utcnow() - monitor.cert_checked_at).total_seconds()+ if age < CERT_REFRESH_SECONDS:return+ # Stamped even when the read fails, so an unreachable TLS endpoint is+ # retried on the same cadence instead of on every single probe.+ result['cert_checked_at'] = datetime.utcnow()try:cert = MonitorService._probe_certificate(target, monitor.check_timeout or 10, bool(monitor.verify_tls))@@ -473,6 +483,30 @@ class MonitorService:query = query.limit(limit)return query.all()+ @staticmethod+ def recent_response_times(monitor_ids, per_monitor=24, hours=6):+ """Last N response times per monitor, oldest first — the list sparkline.++ One query for the whole page rather than one per row, bounded by a time+ window so a busy monitor's history can't crowd out a quiet one's (which+ a single global LIMIT would do).+ """+ if not monitor_ids:+ return {}+ since = datetime.utcnow() - timedelta(hours=hours)+ rows = HealthCheck.query.filter(+ HealthCheck.component_id.in_(list(monitor_ids)),+ HealthCheck.response_time.isnot(None),+ HealthCheck.checked_at >= since,+ ).order_by(HealthCheck.checked_at.desc()).all()++ out = {}+ for row in rows:+ bucket = out.setdefault(row.component_id, [])+ if len(bucket) < per_monitor:+ bucket.append(row.response_time)+ return {mid: list(reversed(values)) for mid, values in out.items()}+@staticmethoddef recompute_uptime(monitor):"""Recompute uptime_24h/7d/30d/90d from HealthCheck rows (fraction ofdiff --git a/backend/migrations/versions/081_monitors_first_class.py b/backend/migrations/versions/081_monitors_first_class.pyindex faea4671..22d2aaa1 100644--- a/backend/migrations/versions/081_monitors_first_class.py+++ b/backend/migrations/versions/081_monitors_first_class.py@@ -55,6 +55,10 @@ NEW_COLUMNS = [('consecutive_failures', sa.Integer(), {'nullable': False, 'server_default': '0'}),('cert_issuer', sa.String(length=256), {'nullable': True}),('cert_expires_at', sa.DateTime(), {'nullable': True}),+ # When the certificate was last read, which is NOT when the monitor was last+ # checked: the TLS read is throttled to once every few hours because it costs+ # a second connection, so it needs a clock of its own.+ ('cert_checked_at', sa.DateTime(), {'nullable': True}),]diff --git a/backend/tests/test_monitor_service.py b/backend/tests/test_monitor_service.pyindex d73d35b0..3b54b607 100644--- a/backend/tests/test_monitor_service.py+++ b/backend/tests/test_monitor_service.py@@ -194,6 +194,61 @@ def test_ping_uses_platform_appropriate_flags(app):assert run.call_args[0][0][:3] == ['ping', '-c', '1']+# ---------------------------------------------------------------------------+# TLS certificate throttling+# ---------------------------------------------------------------------------++def test_certificate_is_read_when_never_read_before(app):+ monitor = _monitor(check_target='https://example.test/')+ expires = datetime.utcnow() + timedelta(days=40)+ result = {}+ with patch.object(MonitorService, '_probe_certificate',+ return_value={'cert_issuer': "Let's Encrypt", 'cert_expires_at': expires}) as probe:+ MonitorService._maybe_attach_certificate(monitor, result)+ assert probe.called+ assert result['cert_issuer'] == "Let's Encrypt"+ assert result['cert_checked_at'] is not None+++def test_certificate_read_is_throttled_by_its_own_clock(app):+ """Throttling must key on cert_checked_at, not last_check_at: an active+ monitor is probed every 30s, so gating on the probe clock would read the+ certificate once and then never refresh it."""+ monitor = _monitor(check_target='https://example.test/')+ monitor.cert_checked_at = datetime.utcnow() - timedelta(minutes=5)+ # A recent probe must NOT unblock a cert re-read.+ monitor.last_check_at = datetime.utcnow()+ db.session.commit()++ with patch.object(MonitorService, '_probe_certificate') as probe:+ MonitorService._maybe_attach_certificate(monitor, {})+ assert not probe.called++ monitor.cert_checked_at = datetime.utcnow() - timedelta(hours=7)+ db.session.commit()+ with patch.object(MonitorService, '_probe_certificate', return_value={}) as probe:+ MonitorService._maybe_attach_certificate(monitor, {})+ assert probe.called+++def test_certificate_failure_still_stamps_the_clock(app):+ """Otherwise an unreachable TLS endpoint is retried on every single probe."""+ monitor = _monitor(check_target='https://example.test/')+ result = {}+ with patch.object(MonitorService, '_probe_certificate', side_effect=OSError('refused')):+ MonitorService._maybe_attach_certificate(monitor, result)+ assert result['cert_checked_at'] is not None+++def test_certificate_skipped_for_non_https(app):+ monitor = _monitor(check_target='http://example.test/')+ result = {}+ with patch.object(MonitorService, '_probe_certificate') as probe:+ MonitorService._maybe_attach_certificate(monitor, result)+ assert not probe.called+ assert result == {}++# ---------------------------------------------------------------------------# Recording: status, retries, uptime, incidents# ---------------------------------------------------------------------------