forked from mtusi2026/mtusi2026lab7
wip: apply pki-ingress-mtls groups 3-5, start group 6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -331,6 +331,7 @@ import secrets
|
||||
import shutil
|
||||
import signal
|
||||
import socket
|
||||
import ssl
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -352,6 +353,12 @@ from typing import NamedTuple
|
||||
UTC = timezone.utc # noqa: UP017
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
DEFAULT_PORT = 18080
|
||||
# с лабы 7 API — только HTTPS (change pki-ingress-mtls): hostname — из SAN
|
||||
# сертификата ingress, CA — стенда (`make pki_stand`)
|
||||
API_HOST = "localhost"
|
||||
STAND_CA_FILE = ROOT / ".pki" / "stand" / "ca.crt"
|
||||
CA_FILE = str(STAND_CA_FILE)
|
||||
_TLS_CONTEXT: ssl.SSLContext | None = None
|
||||
USERS = "/api/v1/users"
|
||||
CHATS = "/api/v1/chats"
|
||||
SESSIONS = "/api/v1/auth/sessions"
|
||||
@@ -393,8 +400,40 @@ def group_alive(group: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def set_ca_file(path: str) -> None:
|
||||
"""CA проверки сервера для всех клиентов драйвера (`--ca-file`)."""
|
||||
global CA_FILE, _TLS_CONTEXT # noqa: PLW0603
|
||||
CA_FILE = path
|
||||
_TLS_CONTEXT = None
|
||||
|
||||
|
||||
def tls_context() -> ssl.SSLContext:
|
||||
"""Один построитель контекста для `urllib` и `http.client`: цепочка по
|
||||
CA стенда (`--ca-file`) и hostname проверяются всегда (change
|
||||
pki-ingress-mtls, design, решение 11)."""
|
||||
global _TLS_CONTEXT # noqa: PLW0603
|
||||
if _TLS_CONTEXT is None:
|
||||
_TLS_CONTEXT = ssl.create_default_context(cafile=CA_FILE or None)
|
||||
return _TLS_CONTEXT
|
||||
|
||||
|
||||
def https_connection(
|
||||
port: int,
|
||||
*,
|
||||
timeout: float = 5,
|
||||
) -> http.client.HTTPSConnection:
|
||||
"""Соединение с сервером на порту хоста по HTTPS с проверкой."""
|
||||
return http.client.HTTPSConnection(
|
||||
API_HOST,
|
||||
port,
|
||||
timeout=timeout,
|
||||
context=tls_context(),
|
||||
)
|
||||
|
||||
|
||||
def base_url(port: int) -> str:
|
||||
return f"http://127.0.0.1:{port}"
|
||||
# с лабы 7 — HTTPS с hostname из SAN сертификата (`localhost`)
|
||||
return f"https://{API_HOST}:{port}"
|
||||
|
||||
|
||||
def basic(username: str, password: str) -> str:
|
||||
@@ -433,7 +472,11 @@ def call(
|
||||
headers=headers,
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
with urllib.request.urlopen(
|
||||
request,
|
||||
timeout=timeout,
|
||||
context=tls_context(),
|
||||
) as response:
|
||||
status = response.status
|
||||
hdrs = dict(response.headers)
|
||||
payload = response.read()
|
||||
@@ -14973,6 +15016,13 @@ def backend_outage(
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument("--port", type=int, default=DEFAULT_PORT)
|
||||
# CA проверки сертификата сервера (change pki-ingress-mtls, design,
|
||||
# решение 11): по умолчанию CA_FILE окружения или CA стенда; пустая
|
||||
# строка — системное хранилище. Выключить проверку нельзя
|
||||
parser.add_argument(
|
||||
"--ca-file",
|
||||
default=os.environ.get("CA_FILE") or str(STAND_CA_FILE),
|
||||
)
|
||||
sub = parser.add_subparsers(dest="cmd", required=True)
|
||||
for name in ("start", "stop", "status", "flow", "smoke", "student", "all"):
|
||||
sub.add_parser(name)
|
||||
@@ -15109,6 +15159,7 @@ def main() -> int:
|
||||
)
|
||||
args = parser.parse_args()
|
||||
port = args.port
|
||||
set_ca_file(args.ca_file)
|
||||
|
||||
if args.cmd == "all":
|
||||
if cmd_start(port) != 0:
|
||||
|
||||
Reference in New Issue
Block a user