forked from mtusi2026/mtusi2026lab7
202 lines
4.9 KiB
Python
202 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PKI_DIR = ROOT / "secrets" / "pki"
|
|
|
|
|
|
def openssl(*args: str) -> None:
|
|
subprocess.run(
|
|
["openssl", *args],
|
|
check=True,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
|
|
|
|
def create_leaf(
|
|
work: Path,
|
|
name: str,
|
|
subject: str,
|
|
sans: str,
|
|
extended_usage: str,
|
|
ca_key: Path,
|
|
ca_cert: Path,
|
|
) -> tuple[Path, Path]:
|
|
key = PKI_DIR / f"{name}.key"
|
|
cert = PKI_DIR / f"{name}.crt"
|
|
request = work / f"{name}.csr"
|
|
extensions = work / f"{name}.ext"
|
|
openssl(
|
|
"req",
|
|
"-new",
|
|
"-newkey",
|
|
"rsa:2048",
|
|
"-nodes",
|
|
"-keyout",
|
|
str(key),
|
|
"-out",
|
|
str(request),
|
|
"-subj",
|
|
subject,
|
|
)
|
|
extensions.write_text(
|
|
"basicConstraints=critical,CA:FALSE\n"
|
|
"keyUsage=critical,digitalSignature,keyEncipherment\n"
|
|
f"extendedKeyUsage={extended_usage}\n"
|
|
f"subjectAltName={sans}\n",
|
|
encoding="ascii",
|
|
)
|
|
openssl(
|
|
"x509",
|
|
"-req",
|
|
"-in",
|
|
str(request),
|
|
"-CA",
|
|
str(ca_cert),
|
|
"-CAkey",
|
|
str(ca_key),
|
|
"-CAcreateserial",
|
|
"-out",
|
|
str(cert),
|
|
"-days",
|
|
"397",
|
|
"-sha256",
|
|
"-extfile",
|
|
str(extensions),
|
|
)
|
|
os.chmod(key, 0o600)
|
|
os.chmod(cert, 0o644)
|
|
return cert, key
|
|
|
|
|
|
def bundle(cert: Path, key: Path, output: Path) -> None:
|
|
output.write_bytes(cert.read_bytes() + key.read_bytes())
|
|
os.chmod(output, 0o600)
|
|
|
|
|
|
def main() -> None:
|
|
if PKI_DIR.exists():
|
|
raise SystemExit("PKI files already exist; rotate them with an explicit rollover procedure.")
|
|
if not shutil.which("openssl"):
|
|
raise SystemExit("OpenSSL is required to generate local certificates.")
|
|
|
|
PKI_DIR.mkdir(mode=0o700, parents=True)
|
|
ca_key = PKI_DIR / "ca.key"
|
|
ca_cert = PKI_DIR / "ca.crt"
|
|
openssl(
|
|
"req",
|
|
"-x509",
|
|
"-newkey",
|
|
"rsa:3072",
|
|
"-nodes",
|
|
"-keyout",
|
|
str(ca_key),
|
|
"-out",
|
|
str(ca_cert),
|
|
"-days",
|
|
"3650",
|
|
"-sha256",
|
|
"-subj",
|
|
"/CN=mtusi2026lab7-local-ca",
|
|
"-addext",
|
|
"basicConstraints=critical,CA:TRUE",
|
|
"-addext",
|
|
"keyUsage=critical,keyCertSign,cRLSign",
|
|
)
|
|
os.chmod(ca_key, 0o600)
|
|
os.chmod(ca_cert, 0o644)
|
|
|
|
with tempfile.TemporaryDirectory(prefix="lab7-pki-") as directory:
|
|
work = Path(directory)
|
|
foreign_ca_key = PKI_DIR / "foreign-ca.key"
|
|
foreign_ca_cert = PKI_DIR / "foreign-ca.crt"
|
|
openssl(
|
|
"req",
|
|
"-x509",
|
|
"-newkey",
|
|
"rsa:2048",
|
|
"-nodes",
|
|
"-keyout",
|
|
str(foreign_ca_key),
|
|
"-out",
|
|
str(foreign_ca_cert),
|
|
"-days",
|
|
"365",
|
|
"-sha256",
|
|
"-subj",
|
|
"/CN=mtusi2026lab7-untrusted-ca",
|
|
"-addext",
|
|
"basicConstraints=critical,CA:TRUE",
|
|
"-addext",
|
|
"keyUsage=critical,keyCertSign,cRLSign",
|
|
)
|
|
os.chmod(foreign_ca_key, 0o600)
|
|
os.chmod(foreign_ca_cert, 0o644)
|
|
ingress_cert, ingress_key = create_leaf(
|
|
work,
|
|
"ingress",
|
|
"/CN=localhost",
|
|
"DNS:localhost,IP:127.0.0.1",
|
|
"serverAuth",
|
|
ca_key,
|
|
ca_cert,
|
|
)
|
|
create_leaf(
|
|
work,
|
|
"api",
|
|
"/CN=api",
|
|
"DNS:api,IP:127.0.0.1",
|
|
"serverAuth",
|
|
ca_key,
|
|
ca_cert,
|
|
)
|
|
client_cert, client_key = create_leaf(
|
|
work,
|
|
"lb-client",
|
|
"/CN=lab7-load-balancer",
|
|
"URI:spiffe://mtusi2026lab7/load-balancer",
|
|
"clientAuth",
|
|
ca_key,
|
|
ca_cert,
|
|
)
|
|
create_leaf(
|
|
work,
|
|
"wrong-client",
|
|
"/CN=lab7-untrusted-service",
|
|
"URI:spiffe://mtusi2026lab7/other-service",
|
|
"clientAuth",
|
|
ca_key,
|
|
ca_cert,
|
|
)
|
|
create_leaf(
|
|
work,
|
|
"foreign-client",
|
|
"/CN=lab7-foreign-service",
|
|
"URI:spiffe://mtusi2026lab7/load-balancer",
|
|
"clientAuth",
|
|
foreign_ca_key,
|
|
foreign_ca_cert,
|
|
)
|
|
create_leaf(
|
|
work,
|
|
"openbao",
|
|
"/CN=openbao",
|
|
"DNS:openbao,DNS:localhost,IP:127.0.0.1",
|
|
"serverAuth",
|
|
ca_key,
|
|
ca_cert,
|
|
)
|
|
bundle(ingress_cert, ingress_key, PKI_DIR / "ingress.pem")
|
|
bundle(client_cert, client_key, PKI_DIR / "lb-client.pem")
|
|
print("Local CA and leaf certificates created under secrets/pki with private permissions.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|