42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import json
|
|
from pathlib import Path
|
|
import secrets
|
|
from shutil import copyfile
|
|
import subprocess
|
|
import sys
|
|
|
|
from pki import ROOT, TLS, issue, revoke
|
|
|
|
|
|
def run(*args):
|
|
subprocess.run(args, cwd=ROOT, check=True, timeout=90, stdout=subprocess.DEVNULL)
|
|
|
|
|
|
def main():
|
|
mode = sys.argv[1]
|
|
if mode == "secret":
|
|
path = ROOT / "secrets/db_api_password"
|
|
path.write_text(secrets.token_hex(24))
|
|
run(sys.executable, "scripts/security_setup.py")
|
|
run(sys.executable, "scripts/apply_roles.py")
|
|
run(sys.executable, "scripts/bao_operator.py")
|
|
run("docker", "compose", "restart", "api-a", "api-b")
|
|
run("docker", "compose", "restart", "api-a-tls", "api-b-tls")
|
|
print("Rotated API database credential without rebuilding images")
|
|
elif mode == "certificate":
|
|
saved = TLS / "revoked-api-a"
|
|
saved.mkdir(exist_ok=True)
|
|
for name in ("cert.pem", "key.pem", "ca.pem"):
|
|
copyfile(TLS / "api-a" / name, saved / name)
|
|
revoke("api-a")
|
|
issue("api-a")
|
|
run(sys.executable, "scripts/bao_operator.py")
|
|
run("docker", "compose", "restart", "api-a", "api-a-tls")
|
|
print("Rotated API leaf certificate and revoked previous identity")
|
|
else:
|
|
raise SystemExit("Usage: python scripts/rotate.py secret|certificate")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|