Files
OpenVPN-Monitoring-Simple/APP_PROFILER/test_helper_config.py
T
ayurishchevandClaude Sonnet 5.5 4abd2044f0 Scenario 2: install and update as system services (Alpine, Debian, Ubuntu)
- deploy/native: install.sh, update.sh (backup, new code prepared aside,
  stop services, separate DB migration phase, start, automatic rollback;
  OpenVPN keeps running), backup.sh, restore.sh, build-ui.sh (Node 20
  container), lib.sh OS adapter (OpenRC and systemd).
- Root helper reads /etc/ovpmon/helper.conf (service manager, unit, config
  path), validated strictly; process.py/server.py honour OVPMON_RUNTIME,
  OVPMON_OPENVPN_SERVICE, OVPMON_OPENVPN_CONFIG. Debian/Ubuntu use
  openvpn-server@server and /etc/openvpn/server/server.conf.
- Service definitions rewritten (systemd, OpenRC). Fixes found by real runs:
  no seccomp-based options in the profiler unit (they imply NoNewPrivileges
  and break setuid doas); OpenRC start_pre extends PATH instead of
  replacing it; helper syntax check works on a read-only tree.
- Verified in containers with real init systems (Debian 12, Ubuntu 24.04,
  Alpine 3.23): install, PKI/server config/OpenVPN start, update on
  pre-versioned databases, rollback, backup/restore.
- Test: test_helper_config.py. Docs: Scenario 2 guide, change summary,
  README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 10:45:24 +03:00

84 lines
3.6 KiB
Python

"""One test for the root helper's optional configuration (/etc/ovpmon/helper.conf).
The helper has no .py extension, so it is loaded by path. Nothing here touches a real service:
subprocess.run is replaced, the config files live in a temporary directory.
"""
import importlib.machinery
import importlib.util
import os
import subprocess
import types
import pytest
HELPER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "DOCS", "General",
"privilege-separation", "ovpmon-helper")
def load_helper():
loader = importlib.machinery.SourceFileLoader("ovpmon_helper_under_test", HELPER)
spec = importlib.util.spec_from_loader(loader.name, loader)
mod = importlib.util.module_from_spec(spec)
loader.exec_module(mod)
return mod
def write_conf(tmp_path, text, mode=0o644):
p = tmp_path / "helper.conf"
p.write_text(text)
p.chmod(mode)
return str(p)
def test_helper_config(tmp_path, monkeypatch):
if not os.path.isfile(HELPER):
pytest.skip("the helper source (DOCS/General/privilege-separation/ovpmon-helper) is not part of this tree")
h = load_helper()
# 1. Without helper.conf the old hard-coded values apply
assert h.load_config(str(tmp_path / "missing.conf")) == {}
assert h.STAGED == "/var/lib/ovpmon/staging/server.conf"
assert h.TARGET == "/etc/openvpn/server.conf"
assert h.PKI_DIR == "/opt/OpenVPN-Monitoring-Simple/APP_PROFILER/easy-rsa/pki"
assert h.CRL_SRC == h.PKI_DIR + "/crl.pem"
assert h.service_command("restart") == ["/sbin/rc-service", "openvpn", "restart"]
# 2. A valid file (here owned by the test user instead of root) is applied; PKI follows install_dir
own = os.getuid()
conf = write_conf(tmp_path, "[helper]\ninstall_dir = /srv/ovpmon\nservice_manager = systemd\n"
"config_path = /etc/openvpn/server/server.conf\n")
h.load_config(conf, owner_uid=own)
assert h.TARGET == "/etc/openvpn/server/server.conf"
assert h.PKI_DIR == "/srv/ovpmon/APP_PROFILER/easy-rsa/pki"
assert h.SERVICE_UNIT == "openvpn-server@server" # systemd default unit
# 3. Command lines: systemd -> systemctl <action> <unit>; openrc -> rc-service <unit> <action>
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
return subprocess.CompletedProcess(cmd, 0, stdout="ok", stderr="")
monkeypatch.setattr(h, "subprocess", types.SimpleNamespace(run=fake_run))
assert h.service("stop") == (0, "ok")
assert calls[-1][1:] == ["stop", "openvpn-server@server"] and calls[-1][0].endswith("systemctl")
h.load_config(write_conf(tmp_path, "[helper]\nservice_unit = openvpn\n"), owner_uid=own)
h.service("status")
assert calls[-1] == ["/sbin/rc-service", "openvpn", "status"]
# 4. Unsafe or malformed files are rejected; so is a file that is not owned by root
for bad in ("[helper]\ninstall_dir = /opt/../etc\n",
"[helper]\nservice_unit = x; reboot\n",
"[helper]\nservice_unit = -rf\n",
"[helper]\nservice_manager = runit\n",
"[helper]\nconfig_path = /tmp/server.conf\n",
"[helper]\nstaged_path = /etc/passwd\n",
"[helper]\nunknown = 1\n",
"install_dir = /opt/x\n"):
with pytest.raises(h.Reject):
h.load_config(write_conf(tmp_path, bad), owner_uid=own)
with pytest.raises(h.Reject):
h.load_config(write_conf(tmp_path, "[helper]\n", mode=0o666), owner_uid=own) # group/world writable
with pytest.raises(h.Reject):
h.load_config(write_conf(tmp_path, "[helper]\n"), owner_uid=own + 1) # wrong owner