49 lines
2.8 KiB
Python
49 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Fresh private configuration/PKI test, independent of running containers."""
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import ssl
|
|
import subprocess
|
|
import tempfile
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
spec = importlib.util.spec_from_file_location("rmm_dev", ROOT / "deploy/dev.py")
|
|
dev = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(dev)
|
|
|
|
os.umask(0o077)
|
|
with tempfile.TemporaryDirectory(prefix="rmm-bootstrap-", dir=ROOT / "devstate") as folder:
|
|
dev.STATE = Path(folder) / "state"
|
|
# A broken host configuration must not influence the generated PKI.
|
|
hostile = Path(folder) / "host.cnf"
|
|
hostile.write_text("this is not an OpenSSL configuration\n")
|
|
os.environ["OPENSSL_CONF"] = str(hostile)
|
|
dev.init()
|
|
for name in (".env", "viewer-a.token", "viewer-b.token", "pki/ca.key", "pki/server.key"):
|
|
assert (dev.STATE / name).stat().st_mode & 0o777 == 0o600
|
|
assert dev.STATE.stat().st_mode & 0o777 == 0o700
|
|
assert (dev.STATE / "init.sql").stat().st_mode & 0o777 == 0o444
|
|
# Both OpenSSL (including 1.1.1 hosts) and the system TLS parser must accept CA.
|
|
env = dict(os.environ, OPENSSL_CONF=str(dev.OPENSSL_CONFIG))
|
|
subprocess.run(["openssl", "verify", "-purpose", "sslserver", "-CAfile", str(dev.STATE / "pki/ca.pem"), "-verify_hostname", "rmm-dev.internal", str(dev.STATE / "pki/server.pem")], env=env, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
for name in ("ca", "server"):
|
|
text = subprocess.check_output(["openssl", "x509", "-in", str(dev.STATE / f"pki/{name}.pem"), "-noout", "-text"], env=env, text=True)
|
|
assert text.count("X509v3 Basic Constraints:") == 1
|
|
assert text.count("X509v3 Key Usage:") == 1
|
|
# Exercise the isolated client role as well; runtime enrollment is tested separately.
|
|
for args in (
|
|
["req", "-new", "-config", str(dev.OPENSSL_CONFIG), "-key", str(dev.STATE / "pki/server.key"), "-out", str(Path(folder) / "client.csr"), "-subj", "/CN=bootstrap-client"],
|
|
["x509", "-req", "-in", str(Path(folder) / "client.csr"), "-CA", str(dev.STATE / "pki/ca.pem"), "-CAkey", str(dev.STATE / "pki/ca.key"), "-set_serial", "42", "-out", str(Path(folder) / "client.pem"), "-days", "1", "-extfile", str(dev.OPENSSL_CONFIG), "-extensions", "client"],
|
|
["verify", "-purpose", "sslclient", "-CAfile", str(dev.STATE / "pki/ca.pem"), str(Path(folder) / "client.pem")],
|
|
):
|
|
subprocess.run(["openssl", *args], env=env, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
ssl.create_default_context(cafile=str(dev.STATE / "pki/ca.pem"))
|
|
try:
|
|
dev.init()
|
|
except SystemExit:
|
|
pass
|
|
else:
|
|
raise AssertionError("must not overwrite private development state")
|
|
print("PASS fresh configuration/CA/SAN verification, permissions and overwrite refusal")
|