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(email): a dovecot probe that cannot run reports unknown, not stopped
Juan Denis7e551da45c5d
diff --git a/backend/app/plugins/serverkit-email/dovecot_service.py b/backend/app/plugins/serverkit-email/dovecot_service.pyindex a081c3d7..b108a123 100644--- a/backend/app/plugins/serverkit-email/dovecot_service.py+++ b/backend/app/plugins/serverkit-email/dovecot_service.py@@ -122,8 +122,20 @@ ssl_prefer_server_ciphers = yesinstalled = Falseif installed:- running = ServiceControl.is_active('dovecot')- enabled = ServiceControl.is_enabled('dovecot')+ try:+ running = ServiceControl.is_active('dovecot')+ enabled = ServiceControl.is_enabled('dovecot')+ except (subprocess.SubprocessError, FileNotFoundError):+ # No systemctl (container, non-systemd init), or systemd not+ # answering inside PROBE_TIMEOUT: "could not check", not "not+ # running" — and never a 500. Wider than FileNotFoundError on+ # purpose: is_enabled already swallows the missing binary, but+ # the probe timeout in both wrappers still raises+ # TimeoutExpired straight through get_status and out of+ # /api/v1/email/status. The sibling services in this extension+ # catch the same pair.+ running = None+ enabled = None# Its own try: failing to read the version says nothing about# whether dovecot is installed. Collapsing the two is how# FirewallService._check_ufw reported a working firewall as absent.diff --git a/backend/app/plugins/serverkit-email/email_service.py b/backend/app/plugins/serverkit-email/email_service.pyindex de2333c1..a05b40b7 100644--- a/backend/app/plugins/serverkit-email/email_service.py+++ b/backend/app/plugins/serverkit-email/email_service.py@@ -36,7 +36,11 @@ class EmailService:spam = SpamAssassinService.get_status()all_installed = postfix['installed'] and dovecot['installed']- all_running = postfix.get('running', False) and dovecot.get('running', False)+ # bool(): a component whose probe could not run reports running=None+ # (see DovecotService.get_status), and `True and None` is None — the+ # top-level flag is documented as a boolean, so keep it one. The+ # honest per-component tri-state is still in the nested dicts.+ all_running = bool(postfix.get('running', False) and dovecot.get('running', False))return {'installed': all_installed,diff --git a/backend/tests/test_email_dovecot_probe.py b/backend/tests/test_email_dovecot_probe.pynew file mode 100644index 00000000..d2725404--- /dev/null+++ b/backend/tests/test_email_dovecot_probe.py@@ -0,0 +1,114 @@+"""DovecotService.get_status() must never turn a failed probe into a 500.++``ServiceControl.is_active`` deliberately raises ``FileNotFoundError`` when+systemctl is absent (see the docstring in ``app/utils/system.py``: a host+without systemd is "could not check", not "not running"). ``get_status`` called+it unguarded, so ``GET /api/v1/email/status`` crashed on every non-systemd host+-- and the page's catch renders that as "Mail server not installed" with an+Install button, on a box already running Dovecot.++Both wrappers also carry ``timeout=PROBE_TIMEOUT``, so a systemd that stops+answering raises ``subprocess.TimeoutExpired`` straight through. That is the+same failure with a different exception type, which is why the guard catches+``subprocess.SubprocessError`` too rather than just the missing binary.++The unknown case reports ``None`` -- neither True nor False -- because+fabricating ``running=False`` here is exactly the probe dishonesty the plan-75+round removed from the firewall and SSL services.+"""+import importlib.util+import subprocess+from pathlib import Path++import pytest++_DOVECOT = (Path(__file__).resolve().parent.parent+ / 'app' / 'plugins' / 'serverkit-email' / 'dovecot_service.py')+++def _load():+ """Import the extension's dovecot_service by path.++ The plugin directory is hyphenated, so it is not importable as a package+ name; the loader the panel uses does the same thing at runtime.+ """+ if not _DOVECOT.exists(): # pragma: no cover - live copy is gitignored+ pytest.skip('serverkit-email live copy not materialised')+ spec = importlib.util.spec_from_file_location('_sk_dovecot_probe', _DOVECOT)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++class _Raiser:+ """Stand-in ServiceControl whose probes raise *exc*."""++ def __init__(self, exc):+ self._exc = exc++ def is_active(self, service):+ raise self._exc++ def is_enabled(self, service):+ raise self._exc+++def dovecot(monkeypatch):+ module = _load()+ # Installed, so get_status reaches the probes at all.+ monkeypatch.setattr(module, 'is_command_available', lambda cmd: True)+ return module++[email protected]('exc', [+ FileNotFoundError('systemctl'),+ subprocess.TimeoutExpired(cmd=['systemctl', 'is-active', 'dovecot'], timeout=30),+])+def test_unavailable_probe_reports_unknown_not_stopped(dovecot, monkeypatch, exc):+ monkeypatch.setattr(dovecot, 'ServiceControl', _Raiser(exc))++ status = dovecot.DovecotService.get_status()++ assert status['installed'] is True+ # None, not False: the probe could not run, so "stopped" would be a lie.+ assert status['running'] is None+ assert status['enabled'] is None+++def test_enabled_probe_failure_alone_is_also_survivable(dovecot, monkeypatch):+ """is_enabled swallows FileNotFoundError itself but not TimeoutExpired.++ Guarding only the is_active line -- the literal review comment -- would+ leave this second escape route open on the very next statement.+ """+ class _EnabledTimesOut:+ def is_active(self, service):+ return True++ def is_enabled(self, service):+ raise subprocess.TimeoutExpired(cmd=['systemctl'], timeout=30)++ monkeypatch.setattr(dovecot, 'ServiceControl', _EnabledTimesOut())++ status = dovecot.DovecotService.get_status()++ assert status['running'] is None+ assert status['enabled'] is None+++def test_working_probe_still_reports_booleans(dovecot, monkeypatch):+ """The guard must not blur the healthy path into the unknown one."""+ class _Healthy:+ def is_active(self, service):+ return True++ def is_enabled(self, service):+ return False++ monkeypatch.setattr(dovecot, 'ServiceControl', _Healthy())++ status = dovecot.DovecotService.get_status()++ assert status['running'] is True+ assert status['enabled'] is Falsediff --git a/builtin-extensions/serverkit-email/backend/dovecot_service.py b/builtin-extensions/serverkit-email/backend/dovecot_service.pyindex a081c3d7..b108a123 100644--- a/builtin-extensions/serverkit-email/backend/dovecot_service.py+++ b/builtin-extensions/serverkit-email/backend/dovecot_service.py@@ -122,8 +122,20 @@ ssl_prefer_server_ciphers = yesinstalled = Falseif installed:- running = ServiceControl.is_active('dovecot')- enabled = ServiceControl.is_enabled('dovecot')+ try:+ running = ServiceControl.is_active('dovecot')+ enabled = ServiceControl.is_enabled('dovecot')+ except (subprocess.SubprocessError, FileNotFoundError):+ # No systemctl (container, non-systemd init), or systemd not+ # answering inside PROBE_TIMEOUT: "could not check", not "not+ # running" — and never a 500. Wider than FileNotFoundError on+ # purpose: is_enabled already swallows the missing binary, but+ # the probe timeout in both wrappers still raises+ # TimeoutExpired straight through get_status and out of+ # /api/v1/email/status. The sibling services in this extension+ # catch the same pair.+ running = None+ enabled = None# Its own try: failing to read the version says nothing about# whether dovecot is installed. Collapsing the two is how# FirewallService._check_ufw reported a working firewall as absent.diff --git a/builtin-extensions/serverkit-email/backend/email_service.py b/builtin-extensions/serverkit-email/backend/email_service.pyindex de2333c1..a05b40b7 100644--- a/builtin-extensions/serverkit-email/backend/email_service.py+++ b/builtin-extensions/serverkit-email/backend/email_service.py@@ -36,7 +36,11 @@ class EmailService:spam = SpamAssassinService.get_status()all_installed = postfix['installed'] and dovecot['installed']- all_running = postfix.get('running', False) and dovecot.get('running', False)+ # bool(): a component whose probe could not run reports running=None+ # (see DovecotService.get_status), and `True and None` is None — the+ # top-level flag is documented as a boolean, so keep it one. The+ # honest per-component tri-state is still in the nested dicts.+ all_running = bool(postfix.get('running', False) and dovecot.get('running', False))return {'installed': all_installed,