49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Issue a short-lived LB client certificate for the expiry security probe."""
|
|
from __future__ import annotations
|
|
|
|
import getpass
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
|
|
from bao_bootstrap import Bao, CLIENT_CERTS, PKI
|
|
|
|
|
|
def main() -> None:
|
|
role = "lb-client"
|
|
common_name = CLIENT_CERTS[role]
|
|
token = getpass.getpass("OpenBao operator root token (not saved): ")
|
|
if not token:
|
|
raise SystemExit("An operator token is required")
|
|
try:
|
|
data = Bao(token).request("POST", f"pki_int/issue/{role}", {
|
|
"common_name": common_name,
|
|
"ttl": "2m",
|
|
}).get("data", {})
|
|
certificate = data.get("certificate")
|
|
private_key = data.get("private_key")
|
|
if not certificate or not private_key:
|
|
raise RuntimeError("OpenBao did not issue the short-lived probe certificate")
|
|
chain = data.get("ca_chain") or []
|
|
cert_bytes = (certificate.rstrip() + "\n" + "\n".join(item.rstrip() for item in chain) + "\n").encode("ascii")
|
|
key_bytes = (private_key.rstrip() + "\n").encode("ascii")
|
|
target = Path("secrets/revocation-probe")
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
with tempfile.TemporaryDirectory(prefix="expiry-probe-", dir=target) as temp_dir:
|
|
temp = Path(temp_dir)
|
|
cert_tmp = temp / "lb-client.expiry.crt"
|
|
key_tmp = temp / "lb-client.expiry.key"
|
|
cert_tmp.write_bytes(cert_bytes)
|
|
key_tmp.write_bytes(key_bytes)
|
|
os.replace(cert_tmp, target / "lb-client.expiry.crt")
|
|
os.replace(key_tmp, target / "lb-client.expiry.key")
|
|
except (OSError, RuntimeError) as exc:
|
|
raise SystemExit(f"Expiry probe issuance failed ({type(exc).__name__}): {exc}") from None
|
|
print("Issued a two-minute LB identity for the expiry probe; private key was not displayed.")
|
|
print("Run security-probe now, then again after the certificate expires (allow about 150 seconds).")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|