Files

36 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""lab7: общий TLS-контекст собственных тестов (только стандартная библиотека).
Импорт модуля устанавливает глобальный urllib-opener с проверкой сертификата и hostname:
CA_FILE CA стенда (по умолчанию secrets/pki/ca.crt рядом с репозиторием)
CLIENT_CERT_FILE PEM с клиентским cert(+key) — только для прямых вызовов реплики
CLIENT_KEY_FILE (in-network тесты обращаются к api-short-ttl, минуя LB, с identity lb)
Отключить проверку сертификата нельзя: такого параметра нет намеренно.
"""
import os
import ssl
import urllib.request
from pathlib import Path
DEFAULT_BASE_URL = "https://localhost:8443"
_ROOT = Path(__file__).resolve().parents[1]
def ca_file():
value = os.environ.get("CA_FILE", "").strip()
if value:
return value
local = _ROOT / "secrets" / "pki" / "ca.crt"
return str(local) if local.exists() else None
def context():
ctx = ssl.create_default_context(cafile=ca_file())
pem = os.environ.get("CLIENT_CERT_FILE", "").strip()
if pem:
ctx.load_cert_chain(pem, os.environ.get("CLIENT_KEY_FILE", "").strip() or None)
return ctx
CONTEXT = context()
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPSHandler(context=CONTEXT)))