"""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 ; openrc -> rc-service 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