fix(pki): bind operator token cache to the active certificate

This commit is contained in:
2026-11-20 20:35:01 +03:00
parent bd44b6003a
commit f3fd46b064
4 changed files with 89 additions and 2 deletions
+15 -1
View File
@@ -8,6 +8,7 @@ import time
import httpx
from cryptography import x509
from cryptography.hazmat.primitives import hashes
from scripts.bootstrap_security import (
OPERATOR,
@@ -37,6 +38,13 @@ class Operator:
timeout=10,
)
self.token = None
self.cert_fingerprint = (
x509.load_pem_x509_certificate(
(OPERATOR / "client/cert.pem").read_bytes()
)
.fingerprint(hashes.SHA256())
.hex()
)
def close(self):
self.http.close()
@@ -96,7 +104,11 @@ class Operator:
cached = OPERATOR / "operator-token.json"
if cached.exists():
data = json.loads(cached.read_text())
if data["expires_at"] > time.time() + 30:
if (
data["expires_at"] > time.time() + 30
and data.get("certificate_fingerprint")
== self.cert_fingerprint
):
self.token = data["token"]
return self.token
result = self.call("POST", "auth/cert/login", {})["auth"]
@@ -106,6 +118,7 @@ class Operator:
json.dumps(
{
"token": self.token,
"certificate_fingerprint": self.cert_fingerprint,
"expires_at": time.time() + result["lease_duration"],
}
)
@@ -289,6 +302,7 @@ class Operator:
json.dumps(
{
"token": created["client_token"],
"certificate_fingerprint": self.cert_fingerprint,
"expires_at": time.time() + created["lease_duration"],
}
)