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>
This commit is contained in:
1 parent
1c49d0b9c2
commit
4abd2044f0
26 files changed
+3326
-255
No files matched your search
@@ -16,7 +16,7 @@ def configure_server(db: Session = Depends(get_db)):
|
||||
pki.require_server_ready(db)
|
||||
# Generate to a temporary location or standard location
|
||||
# As per plan, we behave like srvconf
|
||||
output_path = "/etc/openvpn/server.conf"
|
||||
output_path = process.openvpn_config_path()
|
||||
|
||||
# Unprivileged service: render to the staging dir, the root helper validates and installs it
|
||||
if not process.is_container() and os.geteuid() != 0:
|
||||
|
||||
@@ -9,7 +9,14 @@ logger = logging.getLogger(__name__)
|
||||
def is_container():
|
||||
"""
|
||||
Checks if the application is running inside a Docker container.
|
||||
OVPMON_RUNTIME=host|container overrides the detection (a service that runs on a host inside a
|
||||
test container with /.dockerenv sets host).
|
||||
"""
|
||||
forced = os.environ.get("OVPMON_RUNTIME", "").strip().lower()
|
||||
if forced == "host":
|
||||
return False
|
||||
if forced == "container":
|
||||
return True
|
||||
if os.path.exists('/.dockerenv'):
|
||||
return True
|
||||
try:
|
||||
@@ -23,6 +30,18 @@ def is_container():
|
||||
HELPER_PATH = "/usr/local/sbin/ovpmon-helper"
|
||||
|
||||
|
||||
def openvpn_config_path():
|
||||
"""OpenVPN config path for root/container mode (OVPMON_OPENVPN_CONFIG, default /etc/openvpn/server.conf).
|
||||
Unprivileged mode only stages the file; the helper's own configuration decides where it is installed."""
|
||||
return os.environ.get("OVPMON_OPENVPN_CONFIG", "").strip() or "/etc/openvpn/server.conf"
|
||||
|
||||
|
||||
def openvpn_service_name():
|
||||
"""Service name for the native root path (OVPMON_OPENVPN_SERVICE, default 'openvpn';
|
||||
systemd hosts use 'openvpn-server@server')."""
|
||||
return os.environ.get("OVPMON_OPENVPN_SERVICE", "").strip() or "openvpn"
|
||||
|
||||
|
||||
def _run_helper(args):
|
||||
"""Call the root-side helper through doas (used when this process is unprivileged).
|
||||
Returns (returncode, parsed_json_or_None, raw_output)."""
|
||||
@@ -68,7 +87,7 @@ def control_service(action: str):
|
||||
if action not in ["start", "stop", "restart"]:
|
||||
raise ValueError("Invalid action")
|
||||
|
||||
CONFIG_PATH = "/etc/openvpn/server.conf"
|
||||
CONFIG_PATH = openvpn_config_path()
|
||||
PID_FILE = "/run/openvpn.pid"
|
||||
|
||||
# In Container: Use direct execution to avoid OpenRC/cgroups issues
|
||||
@@ -149,9 +168,9 @@ def control_service(action: str):
|
||||
|
||||
cmd = []
|
||||
if os_type == "alpine":
|
||||
cmd = ["rc-service", "openvpn", action]
|
||||
cmd = ["rc-service", openvpn_service_name(), action]
|
||||
else:
|
||||
cmd = ["systemctl", action, "openvpn"]
|
||||
cmd = ["systemctl", action, openvpn_service_name()]
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""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
|
||||
Reference in new issue
Block a user