installer (not tagged): a new box trusts Proxmox by a copy of its own root CA; the pinned certificate is the fallback (R-1380)
This commit is contained in:
1 parent
327a7668e6
commit
fe8247665d
3 files changed
+182
-7
No files matched your search
@@ -22,6 +22,7 @@ import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
SCRIPT = os.path.join(HERE, "felhom-host-install.sh")
|
||||
@@ -726,6 +727,115 @@ def test_http_code_prints_the_status_on_success():
|
||||
assert _http_code_with("200").endswith("[200]")
|
||||
|
||||
|
||||
|
||||
# ── R-1380 (D80): a new box trusts Proxmox by a copy of its own root CA; the pinned certificate is the fallback ──────
|
||||
# NOT stubs: a real CA, a real leaf for 127.0.0.1 and a real `openssl s_server`, because the proof IS a TLS handshake.
|
||||
# Skipped (and it says so) where openssl is missing — the CI runner has none; the pre-push run on DooPlex has.
|
||||
class _PVE:
|
||||
def __init__(self, sb):
|
||||
import socket
|
||||
self.sb, self.proc = sb, None
|
||||
d = sb.path("pki")
|
||||
os.mkdir(d)
|
||||
def sh(*a):
|
||||
subprocess.run(a, check=True, capture_output=True, cwd=d)
|
||||
for ca in ("ca", "other"):
|
||||
sh("openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", ca + ".key", "-out", ca + ".pem", "-days", "30",
|
||||
"-subj", "/CN=" + ca, "-addext", "basicConstraints=critical,CA:TRUE")
|
||||
sh("openssl", "req", "-newkey", "rsa:2048", "-nodes", "-keyout", "leaf.key", "-out", "leaf.csr", "-subj", "/CN=node")
|
||||
open(os.path.join(d, "ext"), "w").write("subjectAltName=IP:127.0.0.1\nbasicConstraints=CA:FALSE\n")
|
||||
sh("openssl", "x509", "-req", "-in", "leaf.csr", "-CA", "ca.pem", "-CAkey", "ca.key", "-CAcreateserial", "-out", "leaf.pem",
|
||||
"-days", "30", "-extfile", "ext")
|
||||
s = socket.socket(); s.bind(("127.0.0.1", 0)); self.port = s.getsockname()[1]; s.close()
|
||||
self.proc = subprocess.Popen(["openssl", "s_server", "-accept", "127.0.0.1:%d" % self.port, "-cert", "leaf.pem", "-key", "leaf.key",
|
||||
"-www", "-quiet"], cwd=d, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
for _ in range(50):
|
||||
try:
|
||||
socket.create_connection(("127.0.0.1", self.port), timeout=0.2).close(); break
|
||||
except OSError:
|
||||
time.sleep(0.1)
|
||||
self.dir = d
|
||||
self.leaf_fp = subprocess.run(["openssl", "x509", "-in", "leaf.pem", "-noout", "-fingerprint", "-sha256"], capture_output=True,
|
||||
text=True, cwd=d).stdout.strip().split("=", 1)[1]
|
||||
|
||||
def close(self):
|
||||
if self.proc:
|
||||
self.proc.kill(); self.proc.wait()
|
||||
|
||||
|
||||
def _trust_run(source, dry=False):
|
||||
"""Run resolve_pve_trust against a real TLS server. Returns (ca_file, fp, copy bytes or None, source bytes, output, leaf fp, dst)."""
|
||||
if not shutil.which("openssl"):
|
||||
return None
|
||||
sb = Sandbox()
|
||||
pve = _PVE(sb)
|
||||
try:
|
||||
dst = sb.path("etc-felhom-agent", "pve-root-ca.pem")
|
||||
src = os.path.join(pve.dir, source)
|
||||
rc, out = sb.run(prelude(dry) + func("resolve_pve_trust") +
|
||||
'PVE_CA_SOURCE="%s" PVE_CA_COPY="%s" PVE_API_ADDR=127.0.0.1:%d\nresolve_pve_trust\n'
|
||||
'echo "RESULT ca=[$PVE_CA_FILE] fp=[$PVE_TLS_FP]"\n' % (src, dst, pve.port))
|
||||
assert rc == 0, out
|
||||
m = re.search(r"RESULT ca=\[(.*?)\] fp=\[(.*?)\]", out)
|
||||
assert m, out
|
||||
copy = open(dst, "rb").read() if os.path.exists(dst) else None
|
||||
return m.group(1), m.group(2), copy, open(src, "rb").read() if os.path.exists(src) else None, out, pve.leaf_fp, dst
|
||||
finally:
|
||||
pve.close()
|
||||
sb.close()
|
||||
|
||||
|
||||
def test_pve_trust_a_new_box_gets_the_ca_copy_and_no_fingerprint():
|
||||
r = _trust_run("ca.pem")
|
||||
if r is None:
|
||||
print(" (skipped: no openssl here)"); return
|
||||
ca, fp, copy, src, out, _, dst = r
|
||||
assert ca == dst and fp == "", "the box's own CA verified the handshake, so: ca_file set, NO fingerprint. Got ca=%r fp=%r\n%s" % (ca, fp, out)
|
||||
assert copy == src, "the copy is not the source, byte for byte"
|
||||
|
||||
|
||||
def test_pve_trust_a_copy_that_does_not_verify_falls_back_to_the_pin():
|
||||
r = _trust_run("other.pem") # a CA certificate, but not the one that signed the served certificate
|
||||
if r is None:
|
||||
print(" (skipped: no openssl here)"); return
|
||||
ca, fp, _, _, out, leaf_fp, _ = r
|
||||
assert ca == "" and fp == leaf_fp, "never neither: no proven copy → the served certificate is pinned. Got ca=%r fp=%r\n%s" % (ca, fp, out)
|
||||
assert "NOT used" in out and "did not verify" in out, out
|
||||
|
||||
|
||||
def test_pve_trust_a_source_that_is_not_a_ca_is_never_copied():
|
||||
for source, why in (("leaf.pem", "not a CA certificate"), ("leaf.key", "not exactly one certificate"), ("missing.pem", "not readable")):
|
||||
r = _trust_run(source)
|
||||
if r is None:
|
||||
print(" (skipped: no openssl here)"); return
|
||||
ca, fp, copy, _, out, leaf_fp, _ = r
|
||||
assert ca == "" and copy is None, "%s: copied although it is %s" % (source, why)
|
||||
assert fp == leaf_fp, "%s: the fallback pin is missing: %r" % (source, fp)
|
||||
assert why in out, "%s: the reason is not named:\n%s" % (source, out)
|
||||
|
||||
|
||||
def test_pve_trust_dry_run_writes_nothing():
|
||||
r = _trust_run("ca.pem", dry=True)
|
||||
if r is None:
|
||||
print(" (skipped: no openssl here)"); return
|
||||
_, _, copy, _, out, _, _ = r
|
||||
assert copy is None and "[DRY-RUN]" in out, out
|
||||
|
||||
|
||||
def test_pve_trust_is_wired_into_the_agent_config():
|
||||
body = func("step_agent_config")
|
||||
assert re.search(r"^\s+resolve_pve_trust\s*$", body, re.M), "step_agent_config no longer calls resolve_pve_trust"
|
||||
assert 'fp="$PVE_TLS_FP"' in body and 'PVE_CA_FILE="$PVE_CA_FILE"' in body, "the result does not reach the config writer"
|
||||
assert "\"ca_file\": os.environ.get('PVE_CA_FILE', '')" in body and "\"fingerprint\": os.environ['FP']" in body, \
|
||||
"the config writer does not write both keys"
|
||||
# the agent's root step and the installer name the same two files
|
||||
wrapper = open(os.path.join(os.path.dirname(SCRIPT), "..", "..", "felhom-agent", "configs", "felhom-os-apply"), encoding="utf-8").read() \
|
||||
if os.path.exists(os.path.join(os.path.dirname(SCRIPT), "..", "..", "felhom-agent", "configs", "felhom-os-apply")) else None
|
||||
if wrapper is not None:
|
||||
for name, path in (("PVE_CA_SOURCE", "/etc/pve/pve-root-ca.pem"), ("PVE_CA_COPY", "/etc/felhom-agent/pve-root-ca.pem")):
|
||||
assert '%s = "%s"' % (name, path) in wrapper and path in func("resolve_pve_trust"), "the installer and the agent's root step disagree on %s" % name
|
||||
|
||||
|
||||
def main():
|
||||
tests = [(n, f) for n, f in sorted(globals().items()) if n.startswith("test_") and callable(f)]
|
||||
fails = 0
|
||||
|
||||
Reference in new issue
Block a user