Close certificate auth before revoking worker tokens and rotating its secret

This commit is contained in:
2026-11-16 12:03:49 +03:00
parent d8455d9417
commit 040fa8d4da
6 changed files with 156 additions and 5 deletions
+33 -1
View File
@@ -78,9 +78,37 @@ def configure():
raise
def revoke_worker_tokens():
# Replacing the pinned certificate prevents NEW auth, but already issued
# tokens must also be revoked before publishing a new working secret.
accessors = call("auth/token/accessors", method="LIST").get("data", {}).get("keys", [])
count = 0
for accessor in accessors:
try:
record = call("auth/token/lookup-accessor", {"accessor": accessor})["data"]
except urllib.error.HTTPError as exc:
if exc.code == 400:
continue
raise
if "worker" in record.get("policies", []):
call("auth/token/revoke-accessor", {"accessor": accessor})
count += 1
return count
def main():
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["init", "unseal", "configure", "seal"])
parser.add_argument(
"action",
choices=[
"init",
"unseal",
"configure",
"seal",
"revoke-worker-tokens",
"disable-worker-auth",
],
)
args = parser.parse_args()
os.umask(0o077)
if args.action == "init":
@@ -106,6 +134,10 @@ def main():
unseal()
elif args.action == "configure":
configure()
elif args.action == "revoke-worker-tokens":
revoke_worker_tokens()
elif args.action == "disable-worker-auth":
call("auth/cert/certs/worker", method="DELETE")
else:
call("sys/seal", {}, method="PUT")
print("OpenBao operator action complete; credential values were not printed.")
+38 -1
View File
@@ -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