Files
mtusi2026lab7-fork/scripts/local_https.py

21 lines
1.0 KiB
Python

"""Operator loopback client preserving TLS server-name and CA verification."""
import http.client,json,socket,ssl
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
PORTS={'prometheus':17909,'loki':17310,'tempo':17320}
class Connection(http.client.HTTPSConnection):
def connect(self):
raw=socket.create_connection(('127.0.0.1',self.port),timeout=8)
self.sock=self._context.wrap_socket(raw,server_hostname=self.host)
def get(host,path):
context=ssl.create_default_context(cafile=str(ROOT/'secrets/ca/bundle.pem'))
context.verify_flags|=ssl.VERIFY_CRL_CHECK_LEAF
context.load_cert_chain(str(ROOT/'secrets/tls/operator/server.crt'),str(ROOT/'secrets/tls/operator/server.key'))
connection=Connection(host,PORTS[host],context=context,timeout=8)
try:
connection.request('GET',path);response=connection.getresponse();data=response.read()
if response.status!=200:raise OSError('Backend not ready: '+host+' '+str(response.status))
return json.loads(data)
finally:connection.close()