forked from mtusi2026/mtusi2026lab7
fix(pki): bind operator token cache to the active certificate
This commit is contained in:
+15
-1
@@ -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"],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -16,6 +16,17 @@ ENDPOINTS = {
|
||||
|
||||
|
||||
def fetch(service, path, *, params=None, json_result=True):
|
||||
if service == "otel":
|
||||
from scripts.bootstrap_security import OPERATOR, SECRET, write_private
|
||||
|
||||
# Operator credentials can change during CA rollover; never keep a
|
||||
# stale copied certificate for the collector health probe.
|
||||
for name in ["cert.pem", "key.pem"]:
|
||||
write_private(
|
||||
SECRET / "probe/operator" / name,
|
||||
(OPERATOR / "client" / name).read_bytes(),
|
||||
mode=0o644,
|
||||
)
|
||||
url = ENDPOINTS[service] + path
|
||||
if params:
|
||||
url += "?" + urlencode(params, doseq=True)
|
||||
|
||||
Reference in New Issue
Block a user