fix(pki): revoke existing service tokens with the retired leaf

This commit is contained in:
2026-11-20 20:16:17 +03:00
parent 0061722f93
commit 82f02a020f
2 changed files with 85 additions and 1 deletions
+84
View File
@@ -2,9 +2,12 @@
import argparse
import json
import os
import ssl
import time
from uuid import uuid4
import httpx
from cryptography import x509
from cryptography.hazmat.primitives import hashes
@@ -56,6 +59,7 @@ def fixture_check(role, *, reject=False):
META = """import json,pathlib
import httpx
from cryptography import x509
result={}
for directory in pathlib.Path('/identities').iterdir():
@@ -93,6 +97,19 @@ def reload_lb():
)
def old_lb_client():
context = ssl.create_default_context(cafile=TRUST / "ca.pem")
context.minimum_version = ssl.TLSVersion.TLSv1_3
directory = SECRET / "probe/revoked-lb"
context.load_cert_chain(directory / "cert.pem", directory / "key.pem")
return httpx.Client(
base_url="https://localhost:" + os.getenv("BAO_PORT", "8207") + "/v1/",
verify=context,
trust_env=False,
timeout=8,
)
def preserve_lb():
data = json.loads(
compose(
@@ -108,6 +125,21 @@ def preserve_lb():
for name, value in data.items():
write_private(directory / name, value, mode=0o644)
leaf = x509.load_pem_x509_certificate(data["cert.pem"].encode())
# Keep a real short-lived token issued to this exact old certificate.
# Only an ignored operator fixture stores it; never return its value.
with old_lb_client() as client:
response = client.post("auth/cert/login", json={})
response.raise_for_status()
token = response.json()["auth"]["client_token"]
check = client.get(
"auth/token/lookup-self", headers={"X-Vault-Token": token}
)
assert check.status_code == 200
write_private(
directory / "secrets.json",
json.dumps({"token": token}) + "\n",
mode=0o644,
)
return {
"serial": format(leaf.serial_number, "x"),
"expires_at": leaf.not_valid_after_utc.timestamp(),
@@ -130,17 +162,69 @@ def revoke(operator, leaf):
operator.authorized(
"PUT", "auth/cert/crls/service-issuer", {"crl": response.text}
)
# Revoking a certificate alone does not necessarily invalidate a token
# already issued by cert-auth. Revoke active tokens for the LB role too.
accessors = operator.authorized("LIST", "auth/token/accessors")["data"][
"keys"
]
revoked_tokens = 0
for accessor in accessors:
try:
details = operator.authorized(
"POST", "auth/token/lookup-accessor", {"accessor": accessor}
)["data"]
except RuntimeError as error:
# Short-lived unrelated tokens can expire after LIST.
if str(error).endswith("status 400"):
continue
raise
if "messenger-lb" in details.get("policies", []):
try:
operator.authorized(
"POST",
"auth/token/revoke-accessor",
{"accessor": accessor},
)
except RuntimeError as error:
if str(error).endswith("status 400"):
continue
raise
revoked_tokens += 1
assert revoked_tokens >= 1
revoked = json.loads((TRUST / "revoked.json").read_text())
revoked[leaf["serial"]] = True
write_private(
TRUST / "revoked.json", json.dumps(revoked) + "\n", mode=0o644
)
fixture_check("revoked-lb", reject=True)
old_token = json.loads(
(SECRET / "probe/revoked-lb/secrets.json").read_text()
)["token"]
with old_lb_client() as client:
auth = client.post("auth/cert/login", json={})
issue = client.post(
"pki/issue/lb",
headers={"X-Vault-Token": old_token},
json={
"common_name": "lb",
"uri_sans": "spiffe://messenger/lb",
"ttl": "120s",
},
)
assert auth.status_code in {401, 403}, (
"Revoked certificate could authenticate again"
)
assert issue.status_code in {401, 403}, (
"Old service token could issue a replacement certificate"
)
return {
"revoked_serial": leaf["serial"],
"still_unexpired_at_check": leaf["expires_at"] > time.time(),
"new_tls_connection_denied": True,
"bao_crl_updated": True,
"revoked_lb_service_tokens": revoked_tokens,
"old_certificate_new_auth_denied": True,
"old_token_pki_issue_denied": True,
}