Files
mtusi2026lab7/scripts/local_pki.py
T
2026-11-11 14:09:06 +03:00

190 lines
8.2 KiB
Python

#!/usr/bin/env python3
"""Generate local identities and only the runtime material each service needs."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import secrets
import subprocess
import tempfile
ROOT = Path("secrets")
CA = ROOT / "authority"
RUNTIME = ROOT / "runtime"
ROLES = ("api", "worker", "dispatcher", "reconciler")
SERVERS = ("api", "openbao", "db", "s3", "broker", "collector",
"prometheus", "loki", "tempo", "grafana")
def openssl(*args, capture=False):
result = subprocess.run(["openssl", *map(str, args)], check=True,
stdout=subprocess.PIPE if capture else subprocess.DEVNULL,
stderr=subprocess.PIPE)
return result.stdout.decode() if capture else None
def write(path, content, mode=0o444):
path.parent.mkdir(parents=True, exist_ok=True)
if path.is_file() and path.read_text() == content:
path.chmod(mode)
return
with tempfile.NamedTemporaryFile(mode="w", dir=path.parent, delete=False) as handle:
handle.write(content)
temporary = Path(handle.name)
temporary.chmod(mode)
temporary.replace(path)
def random_file(path):
if not path.exists():
write(path, secrets.token_hex(32))
def certificate(name, directory=None, issuer=CA):
directory = directory or RUNTIME / name
directory.mkdir(parents=True, exist_ok=True)
key, cert = directory / "service.key", directory / "service.crt"
if cert.exists():
return
dns = ["DNS:" + name]
if name in ("public", "grafana", "openbao"):
dns += ["DNS:localhost", "IP:127.0.0.1"]
with tempfile.TemporaryDirectory(dir=ROOT) as scratch:
folder = Path(scratch)
private, csr, extensions = (folder / x for x in ("key", "csr", "extensions"))
extensions.write_text(
"basicConstraints=critical,CA:FALSE\n"
"keyUsage=critical,digitalSignature,keyEncipherment\n"
"extendedKeyUsage=serverAuth,clientAuth\n"
"subjectAltName=" + ",".join(dns) + "\n"
)
openssl("req", "-new", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256",
"-nodes", "-subj", "/CN=" + name, "-keyout", private, "-out", csr)
openssl("x509", "-req", "-days", "7", "-in", csr, "-CA", issuer / "ca.crt",
"-CAkey", issuer / "ca.key", "-set_serial", "0x" + secrets.token_hex(16),
"-extfile", extensions, "-out", cert)
write(key, private.read_text())
write(directory / "ca.crt", (CA / "ca.crt").read_text())
write(directory / "service.pem", cert.read_text() + key.read_text())
def authority(directory, name):
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
if not (directory / "ca.crt").exists():
openssl("req", "-new", "-x509", "-days", "365", "-newkey", "ec", "-pkeyopt",
"ec_paramgen_curve:P-256", "-nodes", "-subj", "/CN=" + name,
"-addext", "basicConstraints=critical,CA:TRUE,pathlen:0",
"-addext", "keyUsage=critical,keyCertSign,cRLSign",
"-keyout", directory / "ca.key", "-out", directory / "ca.crt")
def pin(directory):
value = openssl("x509", "-in", directory / "service.crt", "-noout", "-serial", capture=True)
return format(int(value.strip().split("=")[1], 16), "x")
def activate_lb(directory):
write(RUNTIME / "lb" / "client.pem", (directory / "service.pem").read_text())
write(RUNTIME / "api" / "lb-serial", pin(directory))
def activate_collector_client(source):
runtime = RUNTIME / "collector" / "client"
for name in ("service.crt", "service.key"):
write(runtime / name, (source / name).read_text())
fingerprint = openssl("x509", "-in", source / "service.crt", "-noout", "-fingerprint",
"-sha256", capture=True).split("=", 1)[1].strip()
for target in ("loki", "tempo"):
write(RUNTIME / target / "collector-fingerprint", fingerprint.replace(":", "").lower() + "\n")
def bootstrap():
ROOT.mkdir(mode=0o700, exist_ok=True)
ROOT.chmod(0o700)
(ROOT / "rotation" / "probe").mkdir(parents=True, exist_ok=True)
authority(CA, "Lab7 local authority")
authority(ROOT / "foreign", "Lab7 untrusted test authority")
for name in set(ROLES) | set(SERVERS) | {"operator", "public", "intruder", "scraper"}:
certificate(name)
certificate("lb", ROOT / "foreign" / "client", ROOT / "foreign")
for name in ("public.crt", "private.key"):
source = "service.crt" if name == "public.crt" else "service.key"
write(RUNTIME / "s3" / name, (RUNTIME / "s3" / source).read_text())
write(RUNTIME / "lb" / "ca.crt", (CA / "ca.crt").read_text())
write(RUNTIME / "lb" / "server.pem", (RUNTIME / "public" / "service.pem").read_text())
if not (RUNTIME / "api" / "lb-serial").exists():
initial_lb = ROOT / "rotation" / "initial-lb"
certificate("lb", initial_lb)
activate_lb(initial_lb)
if not (RUNTIME / "collector" / "client" / "service.crt").exists():
initial = ROOT / "rotation" / "initial-collector"
certificate("collector-export", initial)
activate_collector_client(initial)
for name in ("db_owner", *ROLES):
random_file(ROOT / "bootstrap" / "db" / name)
for name in ("root", "api", "worker", "reconciler"):
random_file(ROOT / "bootstrap" / "s3" / name)
random_file(ROOT / "bootstrap" / "grafana" / "admin")
for name in ("producer", "worker"):
random_file(ROOT / "bootstrap" / "broker" / name)
acl = ["user default off"]
commands = {
"producer": "+ping +xadd +xgroup|create",
"worker": "+ping +xgroup|create +xautoclaim +xreadgroup +xack",
}
for name, permissions in commands.items():
raw = (ROOT / "bootstrap" / "broker" / name).read_bytes()
hashed = hashlib.sha256(raw).hexdigest()
acl.append(f"user {name} on #{hashed} ~attachments.jobs {permissions}")
write(RUNTIME / "broker" / "users.acl", "\n".join(acl) + "\n")
for role in ROLES:
write(Path("secrets") / "policies" / (role + ".hcl"),
(Path("infra/openbao") / (role + ".hcl")).read_text())
grafana = RUNTIME / "grafana"
data = []
for service, port in (("prometheus", 9090), ("loki", 3100), ("tempo", 3200)):
item = dict(name=service.title(), uid=service, type=service, access="proxy",
url=f"https://{service}:{port}", isDefault=service == "prometheus",
jsonData={"tlsAuth": True, "tlsAuthWithCACert": True},
secureJsonData={"tlsCACert": (grafana / "ca.crt").read_text(),
"tlsClientCert": (grafana / "service.crt").read_text(),
"tlsClientKey": (grafana / "service.key").read_text()})
if service == "loki":
item["jsonData"]["derivedFields"] = [{
"name": "TraceID", "matcherRegex": '"trace_id":"([0-9a-f]{32})"',
"datasourceUid": "tempo", "url": "$${__value.raw}",
}]
if service == "tempo":
item["jsonData"]["tracesToLogsV2"] = {"datasourceUid": "loki", "filterByTraceID": True}
data.append(item)
write(grafana / "sources.yaml", json.dumps({"apiVersion": 1, "datasources": data}, indent=2))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=("bootstrap", "rotate-lb", "rotate-collector"))
args = parser.parse_args()
os.umask(0o022)
if args.action == "bootstrap":
bootstrap()
elif args.action == "rotate-lb":
previous = ROOT / "rotation" / "probe" / "previous.pem"
write(previous, (RUNTIME / "lb" / "client.pem").read_text(), 0o444)
directory = ROOT / "rotation" / secrets.token_hex(8)
certificate("lb", directory)
activate_lb(directory)
else:
old = RUNTIME / "collector" / "client"
previous = ROOT / "rotation" / "probe" / "collector-previous.pem"
write(previous, (old / "service.crt").read_text() + (old / "service.key").read_text())
directory = ROOT / "rotation" / secrets.token_hex(8)
certificate("collector-export", directory)
activate_collector_client(directory)
print("Local PKI updated:", args.action)
if __name__ == "__main__":
main()