forked from mtusi2026/mtusi2026lab7
выполение на 5
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
PKI_DIR = ROOT / "secrets" / "pki"
|
||||
SERVICES = {
|
||||
"rabbitmq": ("DNS:rabbitmq", "serverAuth"),
|
||||
"s3": ("DNS:s3", "serverAuth"),
|
||||
"postgres": ("DNS:postgres", "serverAuth"),
|
||||
"otel-collector": ("DNS:otel-collector", "serverAuth"),
|
||||
"tempo": ("DNS:tempo", "serverAuth"),
|
||||
"loki": ("DNS:loki", "serverAuth"),
|
||||
"prometheus": ("DNS:prometheus", "serverAuth"),
|
||||
"collector-prometheus-client": ("URI:spiffe://mtusi2026lab7/collector", "clientAuth"),
|
||||
"otel-api-client": ("URI:spiffe://mtusi2026lab7/api", "clientAuth"),
|
||||
"otel-worker-client": ("URI:spiffe://mtusi2026lab7/worker", "clientAuth"),
|
||||
"otel-publisher-client": ("URI:spiffe://mtusi2026lab7/publisher", "clientAuth"),
|
||||
"rabbitmq-api-client": ("URI:spiffe://mtusi2026lab7/api", "clientAuth"),
|
||||
"rabbitmq-worker-client": ("URI:spiffe://mtusi2026lab7/worker", "clientAuth"),
|
||||
"rabbitmq-publisher-client": ("URI:spiffe://mtusi2026lab7/publisher", "clientAuth"),
|
||||
"s3-api-client": ("URI:spiffe://mtusi2026lab7/api", "clientAuth"),
|
||||
"s3-worker-client": ("URI:spiffe://mtusi2026lab7/worker", "clientAuth"),
|
||||
"s3-publisher-client": ("URI:spiffe://mtusi2026lab7/publisher", "clientAuth"),
|
||||
"s3-migrate-client": ("URI:spiffe://mtusi2026lab7/migrate", "clientAuth"),
|
||||
"s3-reconciler-client": ("URI:spiffe://mtusi2026lab7/reconciler", "clientAuth"),
|
||||
"s3-bootstrap-client": ("URI:spiffe://mtusi2026lab7/s3-bootstrap", "clientAuth"),
|
||||
"s3-healthcheck-client": ("URI:spiffe://mtusi2026lab7/s3-healthcheck", "clientAuth"),
|
||||
}
|
||||
|
||||
|
||||
def openssl(*args: str) -> None:
|
||||
subprocess.run(
|
||||
["openssl", *args],
|
||||
check=True,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Issue local short-lived mTLS service identities.")
|
||||
parser.add_argument("--days", type=int, default=30)
|
||||
args = parser.parse_args()
|
||||
if not 1 <= args.days <= 90:
|
||||
parser.error("service certificate lifetime must be between 1 and 90 days")
|
||||
if not (PKI_DIR / "ca.key").is_file() or not (PKI_DIR / "ca.crt").is_file():
|
||||
raise SystemExit("Create the local CA first with scripts/bootstrap-pki.py.")
|
||||
if not shutil.which("openssl"):
|
||||
raise SystemExit("OpenSSL is required to issue service certificates.")
|
||||
missing = {
|
||||
name: identity for name, identity in SERVICES.items()
|
||||
if not (PKI_DIR / f"{name}.crt").exists()
|
||||
}
|
||||
if not missing:
|
||||
raise SystemExit("All service certificates already exist; rotate them explicitly.")
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="lab7-service-pki-") as directory:
|
||||
work = Path(directory)
|
||||
for name, (san, usage) in missing.items():
|
||||
key = PKI_DIR / f"{name}.key"
|
||||
cert = PKI_DIR / f"{name}.crt"
|
||||
csr = work / f"{name}.csr"
|
||||
extension = work / f"{name}.ext"
|
||||
openssl(
|
||||
"req", "-new", "-newkey", "rsa:2048", "-nodes",
|
||||
"-keyout", str(key), "-out", str(csr), "-subj", f"/CN={name}",
|
||||
)
|
||||
extension.write_text(
|
||||
"basicConstraints=critical,CA:FALSE\n"
|
||||
"keyUsage=critical,digitalSignature,keyEncipherment\n"
|
||||
f"extendedKeyUsage={usage}\n"
|
||||
f"subjectAltName={san}\n",
|
||||
encoding="ascii",
|
||||
)
|
||||
openssl(
|
||||
"x509", "-req", "-in", str(csr), "-CA", str(PKI_DIR / "ca.crt"),
|
||||
"-CAkey", str(PKI_DIR / "ca.key"), "-CAcreateserial", "-out", str(cert),
|
||||
"-days", str(args.days), "-sha256", "-extfile", str(extension),
|
||||
)
|
||||
os.chmod(key, 0o600)
|
||||
os.chmod(cert, 0o644)
|
||||
print(f"Issued {len(missing)} service certificates with {args.days}-day validity.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user