Files
NODEDC_MISSION_CORE/tests/test_vps_installer_permissions.py
T

61 lines
2.7 KiB
Python

"""The service account must traverse installer code under a restrictive umask."""
import importlib.util
import json
import os
import sys
from pathlib import Path
from types import SimpleNamespace
def test_clean_install_under_private_umask_keeps_code_accessible(tmp_path, monkeypatch):
source = Path(__file__).resolve().parents[1] / "apps/vps-host"
monkeypatch.syspath_prepend(str(source))
spec = importlib.util.spec_from_file_location("vps_install_permissions", source / "install.py")
installer = importlib.util.module_from_spec(spec)
spec.loader.exec_module(installer)
root, state, unit = tmp_path / "product", tmp_path / "state", tmp_path / "service"
payload = tmp_path / "payload"
payload.mkdir()
(payload / "entry.py").write_text("# synthetic service payload\n")
for name, value in (("ROOT", root), ("STATE", state), ("UNIT", unit), ("SOURCE", payload)):
monkeypatch.setattr(installer, name, value)
# A clean install's credential generation is not relevant to access modes.
monkeypatch.setattr(installer.pwd, "getpwnam", lambda _: SimpleNamespace(pw_uid=1, pw_gid=1))
monkeypatch.setattr(installer.os, "chown", lambda *args: None)
monkeypatch.setattr(installer.ssl, "PEM_cert_to_DER_cert", lambda _: b"test cert")
monkeypatch.setattr(installer.firewall, "apply", lambda *args: False)
monkeypatch.setattr(installer, "healthy", lambda: None)
def run(*args, **kwargs):
if args[0] == "openssl":
(state / "tls.key").write_text("private test key")
(state / "tls.crt").write_text("test certificate")
if args[0] == "runuser":
for directory in (root, root / "releases", kwargs["cwd"]):
assert directory.stat().st_mode & 0o777 == 0o755
assert state.stat().st_mode & 0o777 == 0o700
assert (state / "tls.key").stat().st_mode & 0o777 == 0o600
(state / "connect.key").write_text("test invitation")
monkeypatch.setattr(installer, "run", run)
monkeypatch.setattr(installer.os, "geteuid", lambda: 0)
proposal = {
"previous": None,
"plan_sha256": "plan",
"artifact": "release",
"configuration": {"public_host": "test.example", "api_port": 19443},
"firewall": {"mode": "external"},
}
previous = os.umask(0o077)
try:
installer.apply(
SimpleNamespace(expect="plan", host="test.example", api_port=19443, tracker_port=9922),
proposal,
)
finally:
os.umask(previous)
sys.modules.pop("firewall", None)
assert json.loads((root / "installed.json").read_text())["status"] == "healthy"
assert (root / "installed.json").stat().st_mode & 0o777 == 0o600