Implement lab 7 transport security and worker containment
This commit is contained in:
+178
@@ -0,0 +1,178 @@
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
ROLES = (
|
||||
"lb",
|
||||
"api",
|
||||
"worker",
|
||||
"relay",
|
||||
"migrate",
|
||||
"reconciler",
|
||||
"postgres",
|
||||
"s3",
|
||||
"broker",
|
||||
"valkey",
|
||||
"otel-collector",
|
||||
"prometheus",
|
||||
"loki",
|
||||
"tempo",
|
||||
"grafana",
|
||||
"openbao",
|
||||
"operator",
|
||||
"outsider",
|
||||
)
|
||||
OPERATOR = Path(
|
||||
os.environ.get("LAB7_OPERATOR_DIR", str(Path.home() / ".local/share/mtusi2026lab7/operator"))
|
||||
)
|
||||
RUNTIME = Path("secrets/pki")
|
||||
|
||||
|
||||
def run(*args):
|
||||
subprocess.run(
|
||||
["openssl", *map(str, args)], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE
|
||||
)
|
||||
|
||||
|
||||
def bootstrap():
|
||||
OPERATOR.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
OPERATOR.chmod(0o700)
|
||||
RUNTIME.mkdir(parents=True, exist_ok=True)
|
||||
if not (OPERATOR / "ca.key").exists():
|
||||
run(
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:3072",
|
||||
"-nodes",
|
||||
"-keyout",
|
||||
OPERATOR / "ca.key",
|
||||
"-out",
|
||||
OPERATOR / "ca.crt",
|
||||
"-days",
|
||||
"3650",
|
||||
"-subj",
|
||||
"/CN=Lab7 operator CA",
|
||||
"-addext",
|
||||
"basicConstraints=critical,CA:TRUE",
|
||||
"-addext",
|
||||
"keyUsage=critical,keyCertSign,cRLSign",
|
||||
)
|
||||
(OPERATOR / "ca.key").chmod(0o600)
|
||||
(OPERATOR / "index").touch(exist_ok=True)
|
||||
if not (OPERATOR / "crlnumber").exists():
|
||||
(OPERATOR / "crlnumber").write_text("1000\n")
|
||||
(OPERATOR / "ca.cnf").write_text(
|
||||
f"[ca]\ndefault_ca = authority\n[authority]\ndatabase = {OPERATOR}/index\n"
|
||||
f"certificate = {OPERATOR}/ca.crt\nprivate_key = {OPERATOR}/ca.key\n"
|
||||
f"crlnumber = {OPERATOR}/crlnumber\ndefault_md = sha256\ndefault_crl_days = 30\n"
|
||||
)
|
||||
run("ca", "-gencrl", "-config", OPERATOR / "ca.cnf", "-out", OPERATOR / "ca.crl")
|
||||
for role in ROLES:
|
||||
issue(role, replace=False)
|
||||
foreign = RUNTIME / "foreign"
|
||||
foreign.mkdir(exist_ok=True)
|
||||
if not (foreign / "tls.crt").exists():
|
||||
run(
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-nodes",
|
||||
"-keyout",
|
||||
foreign / "tls.key",
|
||||
"-out",
|
||||
foreign / "tls.crt",
|
||||
"-days",
|
||||
"1",
|
||||
"-subj",
|
||||
"/CN=worker",
|
||||
)
|
||||
for file in foreign.iterdir():
|
||||
file.chmod(0o444)
|
||||
|
||||
|
||||
def issue(role, replace=True):
|
||||
target = RUNTIME / role
|
||||
target.mkdir(exist_ok=True)
|
||||
if replace or not (target / "tls.crt").exists():
|
||||
for file in target.iterdir():
|
||||
file.chmod(0o600)
|
||||
if (target / "tls.crt").exists():
|
||||
shutil.copyfile(target / "tls.crt", OPERATOR / f"{role}-previous.crt")
|
||||
run(
|
||||
"req",
|
||||
"-new",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-nodes",
|
||||
"-keyout",
|
||||
target / "tls.key",
|
||||
"-out",
|
||||
OPERATOR / f"{role}.csr",
|
||||
"-subj",
|
||||
f"/CN={role}",
|
||||
)
|
||||
ext = OPERATOR / f"{role}.ext"
|
||||
ext.write_text(
|
||||
f"subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid:always,issuer\n"
|
||||
f"basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature,keyEncipherment\nextendedKeyUsage=serverAuth,clientAuth\nsubjectAltName=DNS:{role},DNS:localhost,IP:127.0.0.1\n"
|
||||
)
|
||||
run(
|
||||
"x509",
|
||||
"-req",
|
||||
"-in",
|
||||
OPERATOR / f"{role}.csr",
|
||||
"-CA",
|
||||
OPERATOR / "ca.crt",
|
||||
"-CAkey",
|
||||
OPERATOR / "ca.key",
|
||||
"-set_serial",
|
||||
str(int.from_bytes(os.urandom(16), "big")),
|
||||
"-days",
|
||||
"1",
|
||||
"-extfile",
|
||||
ext,
|
||||
"-out",
|
||||
target / "tls.crt",
|
||||
)
|
||||
(target / "tls.pem").write_bytes(
|
||||
(target / "tls.crt").read_bytes() + (target / "tls.key").read_bytes()
|
||||
)
|
||||
for name in ["ca.crt", "ca.crl"]:
|
||||
if (target / name).exists():
|
||||
(target / name).chmod(0o600)
|
||||
shutil.copyfile(OPERATOR / name, target / name)
|
||||
for file in target.iterdir():
|
||||
file.chmod(0o444)
|
||||
target.chmod(0o755)
|
||||
|
||||
|
||||
def revoke(role):
|
||||
cert = OPERATOR / f"{role}-previous.crt"
|
||||
if not cert.exists():
|
||||
cert = RUNTIME / role / "tls.crt"
|
||||
run("ca", "-revoke", cert, "-config", OPERATOR / "ca.cnf")
|
||||
run("ca", "-gencrl", "-config", OPERATOR / "ca.cnf", "-out", OPERATOR / "ca.crl")
|
||||
for directory in RUNTIME.iterdir():
|
||||
path = directory / "ca.crl"
|
||||
if path.exists():
|
||||
path.chmod(0o600)
|
||||
shutil.copyfile(OPERATOR / "ca.crl", path)
|
||||
path.chmod(0o444)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("action", choices=["bootstrap", "issue", "revoke"])
|
||||
parser.add_argument("role", nargs="?", choices=ROLES)
|
||||
args = parser.parse_args()
|
||||
if args.action == "bootstrap":
|
||||
bootstrap()
|
||||
elif args.role:
|
||||
(issue if args.action == "issue" else revoke)(args.role)
|
||||
else:
|
||||
parser.error("role required")
|
||||
print("PKI operation completed; private material not displayed.")
|
||||
Reference in New Issue
Block a user