Files
mtusi2026lab7/scripts/rotate_worker.py

38 lines
1.1 KiB
Python

"""Rotate the worker's S3 password and reconfigure scoped OpenBao KV."""
import ssl
import subprocess
from secrets import token_hex
from bao_operator import ROOT, SECRETS, STATE, install
from pki import CA_CERT, publish
import json
def update_s3():
subprocess.run(["docker", "compose", "run", "--rm", "--no-deps", "s3-init"],
cwd=ROOT, check=True, timeout=75, capture_output=True)
def main():
path = SECRETS / "worker_s3_secret"
old = path.read_bytes()
new = (token_hex(24) + "\n").encode("ascii")
state = json.loads(STATE.read_text(encoding="utf-8"))
context = ssl.create_default_context(cafile=CA_CERT)
try:
publish(path, new)
update_s3()
install(context, state, ("worker",))
except Exception:
publish(path, old)
update_s3()
install(context, state, ("worker",))
raise SystemExit("Rotation failed. Previous credential restored.") from None
print("Worker password changed. The running worker reloads after its next OpenBao refresh.")
if __name__ == "__main__":
main()