agent (unreleased): the CA copy keeps its previous root only for the changeover, and is compared exactly (the commit's security review)
gates / gates (push) Successful in 47s
gates / gates (push) Successful in 47s
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
ec7e699fd7
commit
54ac2c2ac9
3 files changed
+145
-29
No files matched your search
@@ -2648,6 +2648,9 @@ class TFake(Fake):
|
||||
if copy is not None:
|
||||
self.tree[osapply.PVE_CA_COPY] = copy
|
||||
self.openssl_stdin, self.puts = [], []
|
||||
self.serves_new = False # does pveproxy serve a certificate that verifies with the NEW root alone?
|
||||
self.dead_ca = set() # certificate texts openssl says are not a live CA (ended)
|
||||
self.handshakes = []
|
||||
self.plan = {"release_id": "pve-ca", "layer": "host", "mode": "pve-ca-copy"}
|
||||
|
||||
def read_bytes(self, p):
|
||||
@@ -2664,6 +2667,12 @@ class TFake(Fake):
|
||||
self.tree[path] = data
|
||||
|
||||
def host(self, argv, timeout=600, stdin=None):
|
||||
if argv[0] == "openssl" and argv[1] == "s_client":
|
||||
self.handshakes.append(argv)
|
||||
return (0, "Verify return code: 0 (ok)\n", "") if self.serves_new else (1, "", "Verify return code: 19 (self-signed certificate in certificate chain)\n")
|
||||
if argv[0] == "openssl" and "-checkend" in argv:
|
||||
return (1, "X509v3 Basic Constraints: critical\n CA:TRUE\nCertificate will expire\n", "") if stdin.strip() in self.dead_ca else \
|
||||
(0, "X509v3 Basic Constraints: critical\n CA:TRUE\nCertificate will not expire\n", "")
|
||||
if argv[0] == "openssl":
|
||||
self.calls.append(("host", argv))
|
||||
self.openssl_stdin.append(stdin)
|
||||
@@ -2704,26 +2713,62 @@ class PVECACopy(unittest.TestCase):
|
||||
self.assertEqual((rc, rep["pve_ca_copy"]["outcome"], rep["pve_ca_copy"]["replaced"]), (0, "written", True), rep)
|
||||
# the new certificate FIRST, and the copy's previous one kept behind it: Proxmox serves its old certificate until
|
||||
# pveproxy is reloaded, and an agent that restarts in that window must still have a root that verifies it
|
||||
self.assertTrue(f.tree[osapply.PVE_CA_COPY].startswith(CA_PEM_2))
|
||||
self.assertEqual(osapply.PEM_CERT_RE.findall(f.tree[osapply.PVE_CA_COPY]),
|
||||
osapply.PEM_CERT_RE.findall(CA_PEM_2) + osapply.PEM_CERT_RE.findall(CA_PEM))
|
||||
bundle = f.tree[osapply.PVE_CA_COPY]
|
||||
self.assertEqual(bundle, CA_PEM_2 + osapply.PEM_CERT_RE.findall(CA_PEM)[0] + b"\n")
|
||||
self.assertTrue(rep["pve_ca_copy"]["kept_previous"])
|
||||
self.assertEqual(f.openssl_stdin, [CA_PEM_2.decode()], "only the SOURCE is checked by openssl")
|
||||
# the same source again: nothing is written (the copy already starts with it)
|
||||
f2 = TFake(source=CA_PEM_2, copy=f.tree[osapply.PVE_CA_COPY])
|
||||
rc, rep = run(f2)
|
||||
self.assertEqual((rc, rep["pve_ca_copy"]["outcome"], f2.puts), (0, "same", []), rep)
|
||||
# a third change: never more than two certificates — the new one and the one that was first
|
||||
f3 = TFake(source=CA_PEM, copy=f.tree[osapply.PVE_CA_COPY])
|
||||
rc, rep = run(f3)
|
||||
self.assertEqual(f.openssl_stdin, [CA_PEM_2.decode()], "the SOURCE is checked in full; the kept one by its own check")
|
||||
|
||||
def test_the_previous_root_stays_only_for_the_changeover(self):
|
||||
old = osapply.PEM_CERT_RE.findall(CA_PEM)[0]
|
||||
bundle = CA_PEM_2 + old + b"\n"
|
||||
# Proxmox still serves the OLD certificate: nothing is written, the old root stays
|
||||
f = TFake(source=CA_PEM_2, copy=bundle)
|
||||
rc, rep = run(f)
|
||||
self.assertEqual((rc, rep["pve_ca_copy"]["outcome"], f.puts), (0, "same", []), rep)
|
||||
self.assertTrue(rep["pve_ca_copy"]["kept_previous"])
|
||||
self.assertEqual(len(f.handshakes), 1)
|
||||
self.assertIn(osapply.PVE_CA_SOURCE, f.handshakes[0], "the handshake is verified with the NEW root alone (the source file)")
|
||||
self.assertIn("-no-CApath", f.handshakes[0])
|
||||
self.assertIn("-verify_return_error", f.handshakes[0])
|
||||
# pveproxy was reloaded and serves a certificate of the NEW root: the old root is dropped
|
||||
f = TFake(source=CA_PEM_2, copy=bundle)
|
||||
f.serves_new = True
|
||||
rc, rep = run(f)
|
||||
self.assertEqual(rc, 0, rep)
|
||||
self.assertEqual(osapply.PEM_CERT_RE.findall(f3.tree[osapply.PVE_CA_COPY]),
|
||||
osapply.PEM_CERT_RE.findall(CA_PEM) + osapply.PEM_CERT_RE.findall(CA_PEM_2))
|
||||
# a first copy keeps nothing behind it
|
||||
self.assertEqual(f.tree[osapply.PVE_CA_COPY], CA_PEM_2, "after the changeover the copy is the new root alone")
|
||||
self.assertEqual((rep["pve_ca_copy"]["outcome"], rep["pve_ca_copy"]["dropped_previous"], rep["pve_ca_copy"]["kept_previous"]), ("written", True, False))
|
||||
# the old root has ENDED while Proxmox still serves the old certificate: it verifies nothing, so it is dropped
|
||||
f = TFake(source=CA_PEM_2, copy=bundle)
|
||||
f.dead_ca = {old.decode()}
|
||||
rc, rep = run(f)
|
||||
self.assertEqual(f.tree[osapply.PVE_CA_COPY], CA_PEM_2)
|
||||
# an ended previous root is not kept at the change itself either
|
||||
f = TFake(source=CA_PEM_2, copy=CA_PEM)
|
||||
f.dead_ca = {old.decode()}
|
||||
rc, rep = run(f)
|
||||
self.assertEqual(f.tree[osapply.PVE_CA_COPY], CA_PEM_2)
|
||||
self.assertFalse(rep["pve_ca_copy"]["kept_previous"])
|
||||
|
||||
def test_a_copy_with_anything_unchecked_in_it_is_rewritten(self):
|
||||
old = osapply.PEM_CERT_RE.findall(CA_PEM)[0]
|
||||
for name, copy in (("text after the source", CA_PEM_2 + b"anything at all\n"),
|
||||
("a third certificate", CA_PEM_2 + old + b"\n" + old + b"\n"),
|
||||
("a key in the file", CA_PEM_2 + b"-----BEGIN PRIVATE KEY-----\nAAAA\n-----END PRIVATE KEY-----\n"),
|
||||
("the two in the wrong order", CA_PEM + osapply.PEM_CERT_RE.findall(CA_PEM_2)[0] + b"\n")):
|
||||
f = TFake(source=CA_PEM_2, copy=copy)
|
||||
f.serves_new = True
|
||||
rc, rep = run(f)
|
||||
self.assertEqual(rc, 0, (name, rep))
|
||||
got = f.tree[osapply.PVE_CA_COPY]
|
||||
self.assertTrue(got.startswith(CA_PEM_2), name)
|
||||
self.assertNotIn(b"anything", got, name)
|
||||
self.assertNotIn(b"PRIVATE KEY", got, name)
|
||||
self.assertLessEqual(len(osapply.PEM_CERT_RE.findall(got)), 2, name)
|
||||
# a first copy keeps nothing behind it, and makes no handshake
|
||||
f4 = TFake()
|
||||
rc, rep = run(f4)
|
||||
self.assertFalse(rep["pve_ca_copy"]["kept_previous"])
|
||||
self.assertEqual(f4.tree[osapply.PVE_CA_COPY], CA_PEM)
|
||||
self.assertEqual((f4.tree[osapply.PVE_CA_COPY], f4.handshakes), (CA_PEM, []))
|
||||
|
||||
def test_a_source_that_is_not_a_ca_certificate_is_refused(self):
|
||||
self.refused(TFake(ca=False), "T3") # a leaf certificate
|
||||
@@ -2766,6 +2811,42 @@ class PVECACopy(unittest.TestCase):
|
||||
self.assertEqual(got, want, f"{name}: {rep}")
|
||||
if want == "written":
|
||||
self.assertIn(rep["pve_ca_copy"]["days_left"], (29, 30))
|
||||
# the two checks the changeover uses, with the REAL openssl and a REAL TLS server
|
||||
ended = True
|
||||
try: # an ENDED CA needs openssl 3.4+ (-not_after); older: that one check is not made
|
||||
sh("openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", "old.key", "-out", "old.pem",
|
||||
"-not_before", "20190101000000Z", "-not_after", "20200101000000Z",
|
||||
"-subj", "/CN=ended CA", "-addext", "basicConstraints=critical,CA:TRUE")
|
||||
except subprocess.CalledProcessError:
|
||||
ended = False
|
||||
sh("openssl", "req", "-newkey", "rsa:2048", "-nodes", "-keyout", "srv.key", "-out", "srv.csr", "-subj", "/CN=pve")
|
||||
sh("openssl", "x509", "-req", "-in", "srv.csr", "-CA", "ca.pem", "-CAkey", "ca.key", "-CAcreateserial", "-out", "srv.pem", "-days", "5")
|
||||
app = osapply.Apply.__new__(osapply.Apply)
|
||||
app.r = real
|
||||
block = lambda n: osapply.PEM_CERT_RE.findall(open(os.path.join(d, n), "rb").read())[0]
|
||||
self.assertTrue(app.is_live_ca(block("ca.pem")), "a CA that has not ended")
|
||||
self.assertFalse(app.is_live_ca(block("leaf.pem")), "a leaf is not a CA")
|
||||
if ended:
|
||||
self.assertFalse(app.is_live_ca(block("old.pem")), "an ended CA is not a live one")
|
||||
import socket
|
||||
s0 = socket.socket(); s0.bind(("127.0.0.1", 0)); port = s0.getsockname()[1]; s0.close()
|
||||
srv = subprocess.Popen(["openssl", "s_server", "-accept", f"127.0.0.1:{port}", "-cert", "srv.pem", "-key", "srv.key", "-quiet"],
|
||||
cwd=d, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
try:
|
||||
saved, osapply.PVE_LOCAL_API = osapply.PVE_LOCAL_API, f"127.0.0.1:{port}"
|
||||
ok = False
|
||||
for _ in range(40): # the server needs a moment to listen
|
||||
if app.pve_serves_with(os.path.join(d, "ca.pem")):
|
||||
ok = True
|
||||
break
|
||||
__import__("time").sleep(0.1)
|
||||
self.assertTrue(ok, "the server's certificate is signed by ca.pem: the handshake must verify with it alone")
|
||||
self.assertFalse(app.pve_serves_with(os.path.join(d, "leaf.pem")), "another certificate as the CA file must NOT verify")
|
||||
osapply.PVE_LOCAL_API = "127.0.0.1:1"
|
||||
self.assertFalse(app.pve_serves_with(os.path.join(d, "ca.pem")), "no answer is a no")
|
||||
finally:
|
||||
osapply.PVE_LOCAL_API = saved
|
||||
srv.kill(); srv.wait()
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
|
||||
|
||||
Reference in new issue
Block a user