forked from mtusi2026/mtusi2026lab7
191 lines
4.7 KiB
Python
191 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Operator PKI. Only service-specific files are mounted into containers."""
|
|
|
|
import argparse
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1] / "secrets"
|
|
CA = ROOT / "operator" / "ca"
|
|
TLS = ROOT / "tls"
|
|
SERVERS = {
|
|
"lb": "DNS:localhost,DNS:lb,IP:127.0.0.1",
|
|
"api": "DNS:api",
|
|
"postgres": "DNS:postgres",
|
|
"valkey": "DNS:valkey",
|
|
"broker": "DNS:broker",
|
|
"s3": "DNS:s3",
|
|
"openbao": "DNS:openbao,DNS:localhost,IP:127.0.0.1",
|
|
"otel-collector": "DNS:otel-collector",
|
|
"prometheus": "DNS:prometheus",
|
|
"loki": "DNS:loki",
|
|
"tempo": "DNS:tempo",
|
|
"grafana": "DNS:grafana,DNS:localhost,IP:127.0.0.1",
|
|
}
|
|
CLIENTS = (
|
|
"lb-client",
|
|
"api-client",
|
|
"worker-client",
|
|
"relay-client",
|
|
"reconciler-client",
|
|
"collector-client",
|
|
"prometheus-client",
|
|
"grafana-client",
|
|
"probe-client",
|
|
)
|
|
|
|
|
|
def openssl(*args):
|
|
subprocess.run(
|
|
["openssl", *map(str, args)],
|
|
cwd=CA,
|
|
check=True,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
|
|
|
|
def init_ca():
|
|
CA.mkdir(parents=True, exist_ok=True)
|
|
TLS.mkdir(parents=True, exist_ok=True)
|
|
if (CA / "ca.key").exists():
|
|
return
|
|
(CA / "issued").mkdir(exist_ok=True)
|
|
for name, value in {
|
|
"index.txt": "",
|
|
"index.txt.attr": "unique_subject = no\n",
|
|
"serial": "1000\n",
|
|
"crlnumber": "1000\n",
|
|
}.items():
|
|
(CA / name).write_text(value)
|
|
openssl(
|
|
"req",
|
|
"-new",
|
|
"-newkey",
|
|
"ec",
|
|
"-pkeyopt",
|
|
"ec_paramgen_curve:P-256",
|
|
"-nodes",
|
|
"-x509",
|
|
"-days",
|
|
90,
|
|
"-subj",
|
|
"/CN=MTUSI lab7 local root",
|
|
"-addext",
|
|
"basicConstraints=critical,CA:TRUE,pathlen:0",
|
|
"-addext",
|
|
"keyUsage=critical,keyCertSign,cRLSign",
|
|
"-keyout",
|
|
"ca.key",
|
|
"-out",
|
|
"ca.crt",
|
|
)
|
|
(CA / "ca.cnf").write_text("""[ca]
|
|
default_ca = signer
|
|
[signer]
|
|
database = index.txt
|
|
new_certs_dir = issued
|
|
certificate = ca.crt
|
|
private_key = ca.key
|
|
serial = serial
|
|
crlnumber = crlnumber
|
|
default_md = sha256
|
|
default_days = 7
|
|
default_crl_days = 30
|
|
policy = policy
|
|
unique_subject = no
|
|
[policy]
|
|
commonName = supplied
|
|
""")
|
|
refresh_crl()
|
|
|
|
|
|
def refresh_crl():
|
|
openssl("ca", "-config", "ca.cnf", "-gencrl", "-out", "current.crl")
|
|
trust = TLS / "trust"
|
|
trust.mkdir(exist_ok=True)
|
|
trust.chmod(0o755)
|
|
for source, name in (("ca.crt", "ca.crt"), ("current.crl", "revoked.crl")):
|
|
data = (CA / source).read_bytes()
|
|
(trust / name).write_bytes(data)
|
|
(trust / name).chmod(0o644)
|
|
|
|
|
|
def issue(name, *, rotate=False):
|
|
target = TLS / name
|
|
target.mkdir(exist_ok=True)
|
|
target.chmod(0o755)
|
|
key, cert = target / "service.key", target / "service.crt"
|
|
if cert.exists() and not rotate:
|
|
return
|
|
if rotate and cert.exists():
|
|
openssl("ca", "-config", "ca.cnf", "-revoke", cert)
|
|
purpose = "serverAuth" if name in SERVERS else "clientAuth"
|
|
san = SERVERS.get(name, "URI:urn:mtusi:lab7:" + name)
|
|
(CA / "leaf.cnf").write_text(
|
|
"[leaf]\nbasicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature\n"
|
|
f"extendedKeyUsage={purpose}\nsubjectAltName={san}\n"
|
|
"subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid,issuer\n"
|
|
)
|
|
openssl(
|
|
"req",
|
|
"-new",
|
|
"-newkey",
|
|
"ec",
|
|
"-pkeyopt",
|
|
"ec_paramgen_curve:P-256",
|
|
"-nodes",
|
|
"-subj",
|
|
"/CN=" + name,
|
|
"-keyout",
|
|
"leaf.key",
|
|
"-out",
|
|
"leaf.csr",
|
|
)
|
|
openssl(
|
|
"ca",
|
|
"-config",
|
|
"ca.cnf",
|
|
"-batch",
|
|
"-notext",
|
|
"-extfile",
|
|
"leaf.cnf",
|
|
"-extensions",
|
|
"leaf",
|
|
"-in",
|
|
"leaf.csr",
|
|
"-out",
|
|
"leaf.crt",
|
|
)
|
|
# A directory mount sees atomic leaf replacement. Host parent remains 0700.
|
|
for source, dest in (("leaf.key", key), ("leaf.crt", cert)):
|
|
temp = dest.with_suffix(".new")
|
|
shutil.move(CA / source, temp)
|
|
temp.chmod(0o644)
|
|
temp.replace(dest)
|
|
(target / "service.pem").write_bytes(cert.read_bytes() + key.read_bytes())
|
|
(target / "service.pem").chmod(0o644)
|
|
if rotate:
|
|
refresh_crl()
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--rotate", choices=[*SERVERS, *CLIENTS])
|
|
args = parser.parse_args()
|
|
os.umask(0o077)
|
|
ROOT.mkdir(exist_ok=True, mode=0o700)
|
|
init_ca()
|
|
if args.rotate:
|
|
issue(args.rotate, rotate=True)
|
|
else:
|
|
for name in (*SERVERS, *CLIENTS):
|
|
issue(name)
|
|
print("Local PKI ready.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|