feat: complete lab 7 for grade 4
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Rotate the allowlisted LB client leaf and roll the CRL across both API proxies."""
|
||||
import fcntl
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from pki import ROOT, STATE, issue, openssl, crl
|
||||
|
||||
CONFIG = "/usr/local/etc/haproxy/haproxy.cfg"
|
||||
|
||||
|
||||
def compose(*args, capture=False):
|
||||
return subprocess.run([str(ROOT / "scripts/compose.sh"), *args], check=True, timeout=45,
|
||||
capture_output=capture).stdout
|
||||
|
||||
|
||||
def reload(service):
|
||||
host_file = STATE / ("lb/client.pem" if service == "lb" else "trust/ca.crl")
|
||||
mounted_file = "/run/identity/client.pem" if service == "lb" else "/run/trust/ca.crl"
|
||||
expected = hashlib.sha256(host_file.read_bytes()).hexdigest().encode()
|
||||
for _ in range(25):
|
||||
try:
|
||||
actual = compose("exec", "-T", service, "sha256sum", mounted_file, capture=True).split()[0]
|
||||
if expected == actual:
|
||||
compose("exec", "-T", service, "haproxy", "-c", "-f", CONFIG, capture=True)
|
||||
break
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
else:
|
||||
raise RuntimeError("HAProxy cannot read or validate the newly mounted TLS file")
|
||||
before = compose("exec", "-T", service, "cat", "/proc/1/task/1/children", capture=True).split()
|
||||
compose("exec", "-T", service, "sh", "-ec",
|
||||
'tr "\\000" " " </proc/1/cmdline | grep -Eq "(^|/)haproxy .* -W |^haproxy -W "; kill -USR2 1')
|
||||
for _ in range(25):
|
||||
after = compose("exec", "-T", service, "cat", "/proc/1/task/1/children", capture=True).split()
|
||||
if set(after).difference(before):
|
||||
return
|
||||
time.sleep(0.2)
|
||||
raise RuntimeError("HAProxy did not start a replacement worker")
|
||||
|
||||
|
||||
def probe(mode):
|
||||
for attempt in range(5):
|
||||
try:
|
||||
compose("run", "--rm", "-T", "probe", mode)
|
||||
return
|
||||
except subprocess.CalledProcessError:
|
||||
if attempt == 4:
|
||||
raise
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
def install(directory, name, data):
|
||||
temporary = directory / (name + ".next")
|
||||
with temporary.open("xb") as file:
|
||||
os.fchmod(file.fileno(), 0o444)
|
||||
file.write(data)
|
||||
file.flush()
|
||||
os.fsync(file.fileno())
|
||||
temporary.replace(directory / name)
|
||||
|
||||
|
||||
def rotate():
|
||||
previous = STATE / "rotation"
|
||||
lb = STATE / "lb"
|
||||
target = previous / f"candidate-{time.time_ns()}"
|
||||
issue(str(target), "client", "lb-to-api", "clientAuth", ("DNS:lb-to-api",))
|
||||
openssl("verify", "-purpose", "sslclient", "-verify_hostname", "lb-to-api",
|
||||
"-CAfile", STATE / "trust/ca.crt", target / "client.crt")
|
||||
# Prove the intended identity is in SAN as well as the CN enforced by HAProxy.
|
||||
subprocess.run(["openssl", "x509", "-in", str(target / "client.crt"), "-noout",
|
||||
"-ext", "subjectAltName"], check=True, capture_output=True).stdout.index(b"DNS:lb-to-api")
|
||||
old = (lb / "client.pem").read_bytes()
|
||||
install(previous, "old.pem", old)
|
||||
install(lb, "client.pem", (target / "client.pem").read_bytes())
|
||||
try:
|
||||
reload("lb")
|
||||
probe("leaf-current")
|
||||
except (OSError, RuntimeError, subprocess.SubprocessError):
|
||||
install(lb, "client.pem", old)
|
||||
reload("lb")
|
||||
raise
|
||||
# From here the replacement stays installed, including on partial CRL rollout.
|
||||
openssl("ca", "-config", "ca.cnf", "-revoke", previous / "old.pem")
|
||||
crl()
|
||||
for service in ("api-a-tls", "api-b-tls"):
|
||||
reload(service)
|
||||
probe("leaf-current")
|
||||
probe("leaf-revoked")
|
||||
print("PASS: new LB leaf accepted; old leaf denied on both API TLS listeners")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
lock = STATE / "operator/rotate-leaf.lock"
|
||||
with os.fdopen(os.open(lock, os.O_CREAT | os.O_WRONLY | os.O_NOFOLLOW, 0o600), "w") as file:
|
||||
fcntl.flock(file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
rotate()
|
||||
except (OSError, RuntimeError, ValueError, subprocess.SubprocessError):
|
||||
raise SystemExit("Leaf rotation stopped; inspect active certificate and CRL before rerunning.") from None
|
||||
Reference in New Issue
Block a user