Close certificate auth before revoking worker tokens and rotating its secret
This commit is contained in:
@@ -6,6 +6,9 @@ import json
|
||||
import secrets
|
||||
import ssl
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -22,8 +25,22 @@ def main():
|
||||
for name in ("cert.pem", "key.pem"):
|
||||
(snapshot / name).write_bytes((old / name).read_bytes())
|
||||
(snapshot / name).chmod(0o600)
|
||||
context = ssl.create_default_context(cafile=str(ROOT / "secrets/ca.pem"))
|
||||
context.load_cert_chain(snapshot / "cert.pem", snapshot / "key.pem")
|
||||
auth_request = urllib.request.Request(
|
||||
"https://localhost:18207/v1/auth/cert/login",
|
||||
data=b'{"name":"worker"}',
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
with urllib.request.urlopen(auth_request, context=context, timeout=5) as response:
|
||||
old_auth = json.load(response)["auth"]
|
||||
old_token = old_auth["client_token"]
|
||||
old_token_expiry = time.time() + old_auth["lease_duration"]
|
||||
seed_before = json.loads((ROOT / "secrets/bao_seed.json").read_text())
|
||||
(snapshot / "material.json").write_text(json.dumps(seed_before["worker"]))
|
||||
material = seed_before["worker"]
|
||||
material["REVOKED_BAO_TOKEN"] = old_token
|
||||
material["REVOKED_BAO_TOKEN_EXPIRES_AT"] = old_token_expiry
|
||||
(snapshot / "material.json").write_text(json.dumps(material))
|
||||
(snapshot / "material.json").chmod(0o644)
|
||||
# The containing operator-private directory stays 0700 on the host. This
|
||||
# disposable old-material mount is used only by the explicit negative probe.
|
||||
@@ -37,6 +54,26 @@ def main():
|
||||
subprocess.run(
|
||||
["docker", "compose", "stop", "worker"], cwd=ROOT, check=True, capture_output=True
|
||||
)
|
||||
# Close OLD certificate authentication before enumerating/revoking tokens;
|
||||
# otherwise an attacker could mint another token during the rotation gap.
|
||||
subprocess.run(
|
||||
["python3", "scripts/bao_operator.py", "disable-worker-auth"],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(auth_request, context=context, timeout=5):
|
||||
raise RuntimeError("Old worker certificate authentication remained enabled")
|
||||
except urllib.error.HTTPError as exc:
|
||||
if exc.code not in {400, 403}:
|
||||
raise
|
||||
subprocess.run(
|
||||
["python3", "scripts/bao_operator.py", "revoke-worker-tokens"],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
bootstrap.certificate("worker", replace=True)
|
||||
password = secrets.token_urlsafe(32)
|
||||
# psql receives SQL through stdin, never a process argument/output containing
|
||||
|
||||
Reference in New Issue
Block a user