forked from mtusi2026/mtusi2026lab7
OpenBao 2.7.3 (raft, TLS 1.3 with client certificates) holds the PostgreSQL role passwords, the Valkey password and the S3 keys; each process logs in with its own mTLS identity, reads only its paths and renews its token in the background. Operator init, unseal, configure and seed go through infra/openbao/bao.py and make bao_*; checks are make bao_check and driver.py openbao-outage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
543 lines
20 KiB
Python
543 lines
20 KiB
Python
"""Приложение в процессе на «тесном» API: соединение на шаг и общий срок
|
||
JSON-запроса (спек `unit-of-work`). S3 и кэш — фейки: рабочий стенд, кроме его
|
||
PostgreSQL как сервера, не затрагивается."""
|
||
|
||
import asyncio
|
||
import logging
|
||
import time
|
||
from collections.abc import AsyncIterator
|
||
from pathlib import Path
|
||
from uuid import uuid4
|
||
|
||
import httpx
|
||
import pytest
|
||
from sqlalchemy import event
|
||
|
||
from builders import attachment_dto
|
||
from conftest import (
|
||
BLOCK_SECONDS,
|
||
DatabaseWarnings,
|
||
TcpProxy,
|
||
admin,
|
||
truncate_all,
|
||
)
|
||
from fakes import FakeCache, FakeDatabase, FakeObjectStorage
|
||
from src.api import dependencies
|
||
from src.core.repos import chat_cache
|
||
from src.core.services import ChatService
|
||
from src.core.services import session as session_module
|
||
from src.infra.db import create_engine, create_session_factory
|
||
from src.infra.db.session import engine, session_factory
|
||
from src.infra.db.uow import SqlAlchemyUnitOfWork
|
||
from src.infra.login_limit import create_login_limiter
|
||
from src.infra.password import KdfBudget, PasswordCheck, password_kdf
|
||
from src.infra.secrets import process_secrets
|
||
from src.main import app
|
||
from src.schemas.dataclasses import ChatFullDTO
|
||
from src.schemas.enums import AttachmentStatus
|
||
from src.utils.constants import DB_CLOSE_TIMEOUT_SECONDS
|
||
from src.utils.utc import utcnow
|
||
|
||
pytestmark = pytest.mark.anyio
|
||
|
||
ROOT = Path(__file__).resolve().parents[2]
|
||
SAMPLE = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
||
# запас на планировщик: срок проверяется сверху, не точно
|
||
MARGIN = 0.6
|
||
PASSWORD = "password-1234"
|
||
|
||
|
||
class _Recording:
|
||
"""Обёртка ASGI: сколько соединений занято в начале ответа."""
|
||
|
||
def __init__(self) -> None:
|
||
self.at_response_start: list[int] = []
|
||
|
||
async def __call__(self, scope, receive, send) -> None:
|
||
async def recording_send(message) -> None:
|
||
if message["type"] == "http.response.start":
|
||
self.at_response_start.append(engine.pool.checkedout())
|
||
await send(message)
|
||
|
||
await app(scope, receive, recording_send)
|
||
|
||
|
||
@pytest.fixture
|
||
def fake_clients(monkeypatch: pytest.MonkeyPatch) -> tuple[FakeCache, object]:
|
||
journal = FakeDatabase()
|
||
cache = FakeCache(journal)
|
||
storage = FakeObjectStorage(journal)
|
||
monkeypatch.setattr(dependencies, "cache", cache)
|
||
monkeypatch.setattr(dependencies, "object_storage", storage)
|
||
# предел входа и бюджет хэшей — свои на тест (change auth-hardening):
|
||
# настоящий клиент Valkey и asyncio.Semaphore привязываются к циклу
|
||
# событий первого теста, а у каждого теста свой цикл
|
||
monkeypatch.setattr(
|
||
dependencies,
|
||
"login_limiter",
|
||
create_login_limiter(cache, dependencies.settings.auth),
|
||
)
|
||
auth = dependencies.settings.auth
|
||
monkeypatch.setattr(
|
||
password_kdf,
|
||
"budget",
|
||
KdfBudget(
|
||
concurrency=auth.kdf_concurrency,
|
||
max_waiting=auth.kdf_max_waiting,
|
||
wait_timeout_seconds=auth.kdf_wait_timeout_seconds,
|
||
),
|
||
)
|
||
return cache, storage
|
||
|
||
|
||
@pytest.fixture
|
||
async def recording(fake_clients) -> AsyncIterator[_Recording]:
|
||
await truncate_all()
|
||
await engine.dispose()
|
||
yield _Recording()
|
||
await engine.dispose()
|
||
|
||
|
||
@pytest.fixture
|
||
async def client(recording: _Recording) -> AsyncIterator[httpx.AsyncClient]:
|
||
transport = httpx.ASGITransport(
|
||
app=recording,
|
||
raise_app_exceptions=False,
|
||
)
|
||
async with httpx.AsyncClient(
|
||
transport=transport,
|
||
base_url="http://test/api/v1",
|
||
timeout=30,
|
||
) as created:
|
||
yield created
|
||
|
||
|
||
class _Checkouts:
|
||
def __init__(self) -> None:
|
||
self.count = 0
|
||
|
||
def __call__(self, *_: object) -> None:
|
||
self.count += 1
|
||
|
||
def __enter__(self) -> "_Checkouts":
|
||
event.listen(engine.sync_engine, "checkout", self)
|
||
return self
|
||
|
||
def __exit__(self, *_: object) -> None:
|
||
event.remove(engine.sync_engine, "checkout", self)
|
||
|
||
|
||
async def _register_login(
|
||
client: httpx.AsyncClient,
|
||
) -> tuple[dict[str, str], str]:
|
||
username = f"u{uuid4().hex[:12]}"
|
||
response = await client.post(
|
||
"/users",
|
||
json={
|
||
"username": username,
|
||
"display_name": "User",
|
||
"password": PASSWORD,
|
||
},
|
||
)
|
||
assert response.status_code == 201, response.text
|
||
user_id = response.json()["id"]
|
||
response = await client.post("/auth/sessions", auth=(username, PASSWORD))
|
||
assert response.status_code == 201, response.text
|
||
token = response.json()["token"]
|
||
return {"Authorization": f"Bearer {token}"}, user_id
|
||
|
||
|
||
async def _chat(
|
||
client: httpx.AsyncClient,
|
||
headers: dict[str, str],
|
||
user_id: str,
|
||
) -> str:
|
||
response = await client.post(
|
||
"/chats",
|
||
headers=headers,
|
||
json={"title": "T", "member_ids": [user_id]},
|
||
)
|
||
assert response.status_code == 201, response.text
|
||
return response.json()["id"]
|
||
|
||
|
||
async def test_response_without_held_connection(
|
||
client: httpx.AsyncClient,
|
||
recording: _Recording,
|
||
) -> None:
|
||
"""Сценарий «Ответ без занятого соединения»."""
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
recording.at_response_start.clear()
|
||
with _Checkouts() as checkouts:
|
||
response = await client.get(
|
||
f"/chats/{chat_id}/messages",
|
||
headers=headers,
|
||
)
|
||
assert response.status_code == 200
|
||
assert checkouts.count == 2
|
||
assert recording.at_response_start == [0]
|
||
|
||
|
||
async def test_pool_of_one_connection(client: httpx.AsyncClient) -> None:
|
||
"""Сценарий «Пул из одного соединения»."""
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
assert engine.pool.size() == 1
|
||
responses = await asyncio.gather(
|
||
*(
|
||
client.get(f"/chats/{chat_id}/messages", headers=headers)
|
||
for _ in range(2)
|
||
),
|
||
)
|
||
assert [item.status_code for item in responses] == [200, 200]
|
||
|
||
|
||
async def test_login_does_not_hold_connection_on_hash(
|
||
client: httpx.AsyncClient,
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
"""Сценарий «Вход не держит соединение на хэш»."""
|
||
headers, _ = await _register_login(client)
|
||
username = (await client.get("/users/me", headers=headers)).json()[
|
||
"username"
|
||
]
|
||
verifying = asyncio.Event()
|
||
release = asyncio.Event()
|
||
|
||
async def verify_password(
|
||
*,
|
||
password: str,
|
||
password_hash: str | None,
|
||
) -> PasswordCheck:
|
||
verifying.set()
|
||
await release.wait()
|
||
return PasswordCheck(valid=True)
|
||
|
||
monkeypatch.setattr(session_module, "verify_password", verify_password)
|
||
login = asyncio.create_task(
|
||
client.post("/auth/sessions", auth=(username, PASSWORD)),
|
||
)
|
||
await verifying.wait()
|
||
assert engine.pool.checkedout() == 0
|
||
other = await client.get("/users/me", headers=headers)
|
||
assert other.status_code == 200
|
||
release.set()
|
||
assert (await login).status_code == 201
|
||
|
||
|
||
async def test_block_without_postgres_takes_no_connection(
|
||
client: httpx.AsyncClient,
|
||
fake_clients,
|
||
) -> None:
|
||
"""Сценарий «Блок без обращения к PostgreSQL»: только вход выдаёт."""
|
||
cache, _ = fake_clients
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
# карточка уже в кэше после создания
|
||
found = await chat_cache.get_chat(cache, chat_id=chat_id)
|
||
assert isinstance(found, ChatFullDTO)
|
||
with _Checkouts() as checkouts:
|
||
response = await client.get(f"/chats/{chat_id}", headers=headers)
|
||
assert response.status_code == 200
|
||
assert checkouts.count == 1
|
||
|
||
|
||
async def test_two_slow_steps_fit_single_deadline(
|
||
client: httpx.AsyncClient,
|
||
) -> None:
|
||
"""Сценарий «Два медленных шага»: 503 через ~T, а не через сумму сроков."""
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
sessions_lock = await admin()
|
||
messages_lock = await admin()
|
||
sessions_tx = sessions_lock.transaction()
|
||
messages_tx = messages_lock.transaction()
|
||
await sessions_tx.start()
|
||
await messages_tx.start()
|
||
try:
|
||
await sessions_lock.execute(
|
||
"LOCK TABLE sessions IN ACCESS EXCLUSIVE MODE",
|
||
)
|
||
await messages_lock.execute(
|
||
"LOCK TABLE messages IN ACCESS EXCLUSIVE MODE",
|
||
)
|
||
started = time.monotonic()
|
||
request = asyncio.create_task(
|
||
client.get(f"/chats/{chat_id}/messages", headers=headers),
|
||
)
|
||
# проверка токена занимает больше половины срока
|
||
await asyncio.sleep(BLOCK_SECONDS * 0.6)
|
||
await sessions_tx.rollback()
|
||
response = await request
|
||
elapsed = time.monotonic() - started
|
||
finally:
|
||
await messages_tx.rollback()
|
||
await sessions_lock.close()
|
||
await messages_lock.close()
|
||
assert response.status_code == 503
|
||
assert response.json()["error"]["code"] == "unavailable"
|
||
assert elapsed < BLOCK_SECONDS + MARGIN
|
||
|
||
|
||
class _SessionRecords:
|
||
"""Записи логгера `session_service`: событие и поля."""
|
||
|
||
def __init__(self) -> None:
|
||
self.records: list[tuple[str, dict[str, object]]] = []
|
||
|
||
def bind(self, **_: object) -> "_SessionRecords":
|
||
return self
|
||
|
||
def _record(self, event_name: str, **fields: object) -> None:
|
||
self.records.append((event_name, fields))
|
||
|
||
debug = info = warning = error = _record
|
||
|
||
|
||
async def test_hash_queue_gets_deadline(
|
||
client: httpx.AsyncClient,
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
"""Сценарий «Очередь хэшей» (с change auth-hardening — дельта спека
|
||
`unit-of-work`): очередь бюджета хэшей ограничена, не успевшие — 429
|
||
`rate_limited` с `Retry-After` не позже срока ожидания места (меньше T),
|
||
а не ждут очередь без срока; 503 — только от пула (один на «тесном»
|
||
API), не от очереди хэшей. Предел неудачных входов выключен: 200
|
||
одновременных попыток одной пары он остановил бы резервом раньше
|
||
бюджета (это его сценарий, не этот).
|
||
|
||
Время в очереди 429 (verify S4) — `waited_ms` записи «Password hashing
|
||
is busy» на каждый 429: не больше `kdf_wait_timeout_seconds` с запасом.
|
||
Полное время ответа 429 включает ещё блок чтения хэша, а он на «тесном»
|
||
API ждёт единственное соединение пула до `pool_timeout_seconds`, — по
|
||
нему отдельно ограничено только общим сроком T."""
|
||
limiter = dependencies.login_limiter
|
||
monkeypatch.setattr(limiter, "max_failures_per_account", 0)
|
||
monkeypatch.setattr(limiter, "max_failures_per_address", 0)
|
||
wait_timeout = dependencies.settings.auth.kdf_wait_timeout_seconds
|
||
assert wait_timeout < BLOCK_SECONDS
|
||
headers, _ = await _register_login(client)
|
||
username = (await client.get("/users/me", headers=headers)).json()[
|
||
"username"
|
||
]
|
||
|
||
async def login() -> tuple[int, float, str | None]:
|
||
started = time.monotonic()
|
||
response = await client.post(
|
||
"/auth/sessions",
|
||
auth=(username, PASSWORD),
|
||
)
|
||
return (
|
||
response.status_code,
|
||
time.monotonic() - started,
|
||
response.headers.get("Retry-After"),
|
||
)
|
||
|
||
records = _SessionRecords()
|
||
monkeypatch.setattr(session_module, "logger", records)
|
||
results = await asyncio.gather(*(login() for _ in range(200)))
|
||
statuses = {status for status, _, _ in results}
|
||
assert 429 in statuses
|
||
assert statuses <= {201, 429, 503}
|
||
assert {after for status, _, after in results if status == 429} == {"1"}
|
||
assert max(elapsed for _, elapsed, _ in results) < BLOCK_SECONDS + MARGIN
|
||
waits = [
|
||
fields["waited_ms"]
|
||
for event_name, fields in records.records
|
||
if event_name == "Password hashing is busy"
|
||
]
|
||
rejected = sum(1 for status, _, _ in results if status == 429)
|
||
assert len(waits) == rejected
|
||
assert max(waits) <= (wait_timeout + MARGIN) * 1000
|
||
# «очередь полна» — 0 мс; отказ по сроку (если был: очередь могла
|
||
# разойтись раньше срока) ждал не меньше срока
|
||
timed_out = [wait for wait in waits if wait > 0]
|
||
assert all(wait >= wait_timeout * 1000 * 0.9 for wait in timed_out)
|
||
|
||
|
||
class _PoolRecords(logging.Handler):
|
||
"""Записи пула: только текст, `exc_info` с кадрами не хранится."""
|
||
|
||
def __init__(self) -> None:
|
||
super().__init__(level=logging.WARNING)
|
||
self.messages: list[str] = []
|
||
|
||
def emit(self, record: logging.LogRecord) -> None:
|
||
self.messages.append(record.getMessage())
|
||
|
||
|
||
async def test_deadline_in_second_block_single_record(
|
||
client: httpx.AsyncClient,
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
proxy_target: tuple[str, int],
|
||
warnings: DatabaseWarnings,
|
||
) -> None:
|
||
"""Сценарий «Срок запроса истёк во втором блоке: одна запись, без ERROR
|
||
пула»: заморозка между входом и операцией, зазор сроков — настоящая
|
||
длительность блока входа."""
|
||
headers, _ = await _register_login(client)
|
||
records = _PoolRecords()
|
||
pool_logger = logging.getLogger("sqlalchemy.pool")
|
||
pool_logger.addHandler(records)
|
||
async with TcpProxy(*proxy_target) as proxy:
|
||
proxied = create_engine(
|
||
# секция роли api с паролем — подстановка conftest (change
|
||
# openbao-secrets): в settings пароля процесса нет
|
||
process_secrets("api").database.model_copy(
|
||
update={"host": "localhost", "port": proxy.port},
|
||
),
|
||
ca_file=dependencies.settings.client_tls.ca_file,
|
||
)
|
||
monkeypatch.setattr(
|
||
dependencies,
|
||
"session_factory",
|
||
create_session_factory(proxied),
|
||
)
|
||
real_list = ChatService.list_user_chats
|
||
|
||
async def frozen_list(self, **kwargs):
|
||
proxy.freeze()
|
||
return await real_list(self, **kwargs)
|
||
|
||
monkeypatch.setattr(ChatService, "list_user_chats", frozen_list)
|
||
try:
|
||
started = time.monotonic()
|
||
response = await client.get("/chats", headers=headers)
|
||
elapsed = time.monotonic() - started
|
||
checked_out = proxied.pool.checkedout()
|
||
finally:
|
||
pool_logger.removeHandler(records)
|
||
proxy.thaw()
|
||
await proxied.dispose()
|
||
assert response.status_code == 503
|
||
assert response.json()["error"]["code"] == "unavailable"
|
||
assert elapsed < BLOCK_SECONDS + DB_CLOSE_TIMEOUT_SECONDS + MARGIN
|
||
assert warnings.events == ["Database is unavailable"]
|
||
assert records.messages == []
|
||
assert checked_out == 0
|
||
|
||
|
||
async def test_upload_longer_than_block(
|
||
client: httpx.AsyncClient,
|
||
fake_clients,
|
||
) -> None:
|
||
"""Сценарий «Вложение дольше срока блока»: загрузка."""
|
||
_, storage = fake_clients
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
|
||
async def slow_body() -> AsyncIterator[bytes]:
|
||
yield SAMPLE[:100]
|
||
await asyncio.sleep(BLOCK_SECONDS + 0.3)
|
||
yield SAMPLE[100:]
|
||
|
||
started = time.monotonic()
|
||
response = await client.post(
|
||
f"/chats/{chat_id}/attachments",
|
||
params={"filename": "a.png"},
|
||
headers={**headers, "Content-Type": "image/png"},
|
||
content=slow_body(),
|
||
)
|
||
assert response.status_code == 202, response.text
|
||
assert time.monotonic() - started > BLOCK_SECONDS
|
||
assert storage.objects
|
||
|
||
|
||
async def test_download_longer_than_block(
|
||
client: httpx.AsyncClient,
|
||
fake_clients,
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
"""Тот же сценарий: скачивание потоком."""
|
||
_, storage = fake_clients
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
row = attachment_dto(
|
||
chat_id=chat_id,
|
||
owner_id=user_id,
|
||
status=AttachmentStatus.READY,
|
||
created_at=utcnow(),
|
||
namespace=storage.namespace,
|
||
)
|
||
async with SqlAlchemyUnitOfWork(
|
||
session_factory,
|
||
timeout_seconds=BLOCK_SECONDS,
|
||
) as uow:
|
||
await uow.attachments.insert_attachment(attachment=row)
|
||
await uow.commit()
|
||
storage.seed(row.object_key, b"content", content_type="image/png")
|
||
real_open = storage.open
|
||
|
||
class _Slow:
|
||
def __init__(self, inner) -> None:
|
||
self.inner = inner
|
||
|
||
async def __aenter__(self):
|
||
stored = await self.inner.__aenter__()
|
||
real_chunks = stored.iter_chunks
|
||
|
||
async def iter_chunks(chunk_bytes: int = 1024):
|
||
await asyncio.sleep(BLOCK_SECONDS + 0.3)
|
||
async for chunk in real_chunks(chunk_bytes):
|
||
yield chunk
|
||
|
||
stored.iter_chunks = iter_chunks
|
||
return stored
|
||
|
||
async def __aexit__(self, *exc_info: object):
|
||
return await self.inner.__aexit__(*exc_info)
|
||
|
||
monkeypatch.setattr(storage, "open", lambda key: _Slow(real_open(key)))
|
||
started = time.monotonic()
|
||
response = await client.get(
|
||
f"/attachments/{row.id}/content",
|
||
headers=headers,
|
||
)
|
||
assert response.status_code == 200
|
||
assert response.content == b"content"
|
||
assert time.monotonic() - started > BLOCK_SECONDS
|
||
|
||
|
||
async def test_code_order_with_postgres_down(
|
||
client: httpx.AsyncClient,
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
"""Вход идёт раньше валидации, битый JSON — раньше входа; отказ БД на входе —
|
||
503 до 400 (design, Context, п. 8)."""
|
||
headers, user_id = await _register_login(client)
|
||
chat_id = await _chat(client, headers, user_id)
|
||
down_settings = dependencies.settings.database.model_copy(
|
||
update={"port": 1, "connect_timeout_seconds": 1},
|
||
)
|
||
down = create_engine(
|
||
down_settings,
|
||
ca_file=dependencies.settings.client_tls.ca_file,
|
||
)
|
||
monkeypatch.setattr(
|
||
dependencies,
|
||
"session_factory",
|
||
create_session_factory(down),
|
||
)
|
||
url = f"/chats/{chat_id}/messages"
|
||
try:
|
||
no_token = await client.post(url, json={"text": "x"})
|
||
assert no_token.status_code == 401
|
||
broken = await client.post(
|
||
url,
|
||
headers={**headers, "Content-Type": "application/json"},
|
||
content=b"{not json",
|
||
)
|
||
assert broken.status_code == 400
|
||
extra = await client.post(
|
||
url,
|
||
headers={**headers, "Idempotency-Key": "k"},
|
||
json={"text": "x", "extra": 1},
|
||
)
|
||
assert extra.status_code == 503
|
||
assert extra.json()["error"]["code"] == "unavailable"
|
||
history = await client.get(url, headers=headers)
|
||
assert history.status_code == 503
|
||
finally:
|
||
await down.dispose()
|