forked from mtusi2026/mtusi2026lab3
Compare commits
1
Commits
025beab4ca
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ab3a341aa |
-22
@@ -1,22 +0,0 @@
|
|||||||
# syntax=docker/dockerfile:1
|
|
||||||
FROM python:3.11.9-slim
|
|
||||||
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
|
||||||
WEB_CONCURRENCY=2
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY requirements.txt .
|
|
||||||
RUN pip install -r requirements.txt
|
|
||||||
|
|
||||||
COPY main.py .
|
|
||||||
COPY app ./app
|
|
||||||
|
|
||||||
RUN useradd --system --uid 10001 --no-create-home --shell /usr/sbin/nologin appuser
|
|
||||||
USER 10001
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=10 \
|
|
||||||
CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/live', timeout=2)"]
|
|
||||||
|
|
||||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "5"]
|
|
||||||
@@ -28,6 +28,3 @@ test-persistence:
|
|||||||
test-failover:
|
test-failover:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
test-failover:
|
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
"""Тонкая обёртка над Redis/Valkey."""
|
|
||||||
import logging
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
|
|
||||||
import redis
|
|
||||||
|
|
||||||
from . import config
|
|
||||||
|
|
||||||
log = logging.getLogger("cache")
|
|
||||||
_client = None
|
|
||||||
_down_until = 0.0
|
|
||||||
_lock = threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def _get_client():
|
|
||||||
global _client
|
|
||||||
if not config.CACHE_URL:
|
|
||||||
return None
|
|
||||||
if _client is None:
|
|
||||||
with _lock:
|
|
||||||
if _client is None:
|
|
||||||
_client = redis.Redis.from_url(
|
|
||||||
config.CACHE_URL, socket_timeout=config.CACHE_TIMEOUT,
|
|
||||||
socket_connect_timeout=config.CACHE_TIMEOUT, max_connections=20,
|
|
||||||
decode_responses=True, retry_on_timeout=False)
|
|
||||||
return _client
|
|
||||||
|
|
||||||
|
|
||||||
def _call(fn):
|
|
||||||
global _down_until
|
|
||||||
client = _get_client()
|
|
||||||
if client is None or time.monotonic() < _down_until:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
return fn(client)
|
|
||||||
except redis.RedisError as exc:
|
|
||||||
_down_until = time.monotonic() + config.CACHE_BACKOFF
|
|
||||||
log.warning("cache unavailable (%s); bypassing for %.0fs", type(exc).__name__, config.CACHE_BACKOFF)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get(key):
|
|
||||||
return _call(lambda c: c.get(key))
|
|
||||||
|
|
||||||
|
|
||||||
def set_if_absent(key, value, ttl_ms):
|
|
||||||
return _call(lambda c: c.set(key, value, nx=True, px=ttl_ms))
|
|
||||||
|
|
||||||
|
|
||||||
def set_always(key, value, ttl_ms):
|
|
||||||
return _call(lambda c: c.set(key, value, px=ttl_ms))
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
"""Конфигурация из переменных окружения. Секреты читаются из файлов (*_FILE), а не из env."""
|
|
||||||
import os
|
|
||||||
import socket
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def _read(name):
|
|
||||||
"""NAME_FILE (путь к секрету) имеет приоритет над NAME (прямое значение для локальной отладки)."""
|
|
||||||
path = os.environ.get(name + "_FILE")
|
|
||||||
if path:
|
|
||||||
return Path(path).read_text(encoding="utf-8").strip()
|
|
||||||
return os.environ.get(name) or None
|
|
||||||
|
|
||||||
|
|
||||||
def _int(name, default, low, high):
|
|
||||||
value = int(os.environ.get(name, default))
|
|
||||||
if not low <= value <= high:
|
|
||||||
raise ValueError("%s must be in %d..%d" % (name, low, high))
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
APP_HOST = os.environ.get("APP_HOST", "0.0.0.0")
|
|
||||||
APP_PORT = _int("APP_PORT", 8080, 1, 65535)
|
|
||||||
APP_ENV = os.environ.get("APP_ENV", "lab")
|
|
||||||
|
|
||||||
SESSION_TTL_SECONDS = _int("SESSION_TTL_SECONDS", 3600, 1, 30 * 24 * 3600)
|
|
||||||
SESSION_STORE = os.environ.get("SESSION_STORE", "postgres+cache")
|
|
||||||
SESSION_CACHE_TTL_SECONDS = _int("SESSION_CACHE_TTL_SECONDS", 60, 1, 3600)
|
|
||||||
|
|
||||||
DATABASE_URL = _read("DATABASE_URL")
|
|
||||||
CACHE_URL = _read("CACHE_URL") if SESSION_STORE == "postgres+cache" else None
|
|
||||||
|
|
||||||
DB_POOL_MIN = _int("DB_POOL_MIN", 1, 0, 50)
|
|
||||||
DB_POOL_MAX = _int("DB_POOL_MAX", 16, 1, 50)
|
|
||||||
DB_POOL_TIMEOUT = float(os.environ.get("DB_POOL_TIMEOUT", "3"))
|
|
||||||
DB_STATEMENT_TIMEOUT_MS = _int("DB_STATEMENT_TIMEOUT_MS", 5000, 100, 60000)
|
|
||||||
CACHE_TIMEOUT = float(os.environ.get("CACHE_TIMEOUT", "0.25"))
|
|
||||||
CACHE_BACKOFF = float(os.environ.get("CACHE_BACKOFF", "2"))
|
|
||||||
|
|
||||||
ARGON2_MEMORY_KIB = _int("ARGON2_MEMORY_KIB", 19456, 8, 1048576)
|
|
||||||
ARGON2_TIME_COST = _int("ARGON2_TIME_COST", 2, 1, 10)
|
|
||||||
MAX_BODY_BYTES = 64 * 1024
|
|
||||||
THREAD_LIMIT = _int("THREAD_LIMIT", 24, 1, 200)
|
|
||||||
HASH_CONCURRENCY = _int("HASH_CONCURRENCY", 4, 1, 32)
|
|
||||||
|
|
||||||
# --- Новое в lab3 ---
|
|
||||||
# ID реплики для заголовка X-Instance-Id. Если не задан, берем имя хоста контейнера.
|
|
||||||
INSTANCE_ID = os.environ.get("INSTANCE_ID") or socket.gethostname()
|
|
||||||
# Время жизни ключей идемпотентности (24 часа)
|
|
||||||
IDEMPOTENCY_TTL_SECONDS = _int("IDEMPOTENCY_TTL_SECONDS", 86400, 60, 86400 * 7)
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
"""Непрозрачный keyset-курсор (created_at, id)."""
|
|
||||||
import base64
|
|
||||||
import json
|
|
||||||
import uuid
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
from .errors import bad_request
|
|
||||||
|
|
||||||
|
|
||||||
def encode(kind, scope, created_at, row_id):
|
|
||||||
payload = {"k": kind, "s": str(scope), "t": created_at.astimezone(timezone.utc).isoformat(), "i": str(row_id)}
|
|
||||||
return base64.urlsafe_b64encode(json.dumps(payload, separators=(",", ":")).encode()).rstrip(b"=").decode()
|
|
||||||
|
|
||||||
|
|
||||||
def decode(value, kind, scope):
|
|
||||||
if not value:
|
|
||||||
raise bad_request("Некорректный cursor")
|
|
||||||
try:
|
|
||||||
raw = base64.urlsafe_b64decode(value + "=" * (-len(value) % 4))
|
|
||||||
payload = json.loads(raw.decode("utf-8"))
|
|
||||||
created_at = datetime.fromisoformat(payload["t"])
|
|
||||||
row_id = uuid.UUID(payload["i"])
|
|
||||||
if payload["k"] != kind or payload["s"] != str(scope) or created_at.tzinfo is None:
|
|
||||||
raise ValueError("scope mismatch")
|
|
||||||
return created_at, row_id
|
|
||||||
except (ValueError, KeyError, TypeError, UnicodeDecodeError):
|
|
||||||
raise bad_request("Некорректный cursor")
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
"""Пул соединений PostgreSQL."""
|
|
||||||
import threading
|
|
||||||
|
|
||||||
import psycopg
|
|
||||||
from psycopg.rows import dict_row
|
|
||||||
from psycopg_pool import ConnectionPool, PoolClosed, PoolTimeout
|
|
||||||
|
|
||||||
from . import config
|
|
||||||
|
|
||||||
TRANSIENT = (psycopg.OperationalError, PoolTimeout, PoolClosed, psycopg.errors.QueryCanceled,
|
|
||||||
psycopg.errors.LockNotAvailable)
|
|
||||||
|
|
||||||
_pool = None
|
|
||||||
_lock = threading.Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def get_pool():
|
|
||||||
global _pool
|
|
||||||
if _pool is None:
|
|
||||||
with _lock:
|
|
||||||
if _pool is None:
|
|
||||||
if not config.DATABASE_URL:
|
|
||||||
raise RuntimeError("DATABASE_URL(_FILE) is not configured")
|
|
||||||
options = ("-c statement_timeout=%d -c lock_timeout=3000 "
|
|
||||||
"-c idle_in_transaction_session_timeout=10000" % config.DB_STATEMENT_TIMEOUT_MS)
|
|
||||||
pool = ConnectionPool(
|
|
||||||
config.DATABASE_URL,
|
|
||||||
min_size=config.DB_POOL_MIN, max_size=config.DB_POOL_MAX,
|
|
||||||
timeout=config.DB_POOL_TIMEOUT, max_lifetime=1800, max_idle=300,
|
|
||||||
reconnect_timeout=5,
|
|
||||||
check=ConnectionPool.check_connection,
|
|
||||||
kwargs={"row_factory": dict_row, "connect_timeout": 3, "options": options,
|
|
||||||
"application_name": "messenger-api"},
|
|
||||||
open=False)
|
|
||||||
pool.open(wait=False)
|
|
||||||
_pool = pool
|
|
||||||
return _pool
|
|
||||||
|
|
||||||
|
|
||||||
def connection(timeout=None):
|
|
||||||
return get_pool().connection(timeout=timeout)
|
|
||||||
|
|
||||||
|
|
||||||
def ping():
|
|
||||||
with connection(timeout=1.5) as conn:
|
|
||||||
conn.execute("SELECT 1")
|
|
||||||
|
|
||||||
|
|
||||||
def close():
|
|
||||||
global _pool
|
|
||||||
if _pool is not None:
|
|
||||||
_pool.close(timeout=5)
|
|
||||||
_pool = None
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
class ApiError(Exception):
|
|
||||||
def __init__(self, status, code, message, headers=None):
|
|
||||||
super().__init__(message)
|
|
||||||
self.status, self.code, self.message, self.headers = status, code, message, headers or {}
|
|
||||||
|
|
||||||
|
|
||||||
BASIC_CHALLENGE = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
|
|
||||||
BEARER_CHALLENGE = {"WWW-Authenticate": 'Bearer realm="messenger"'}
|
|
||||||
|
|
||||||
|
|
||||||
def bad_request(message="Некорректный запрос"):
|
|
||||||
return ApiError(400, "invalid_request", message)
|
|
||||||
|
|
||||||
|
|
||||||
def unauthorized(challenge):
|
|
||||||
return ApiError(401, "unauthorized", "Требуется аутентификация", challenge)
|
|
||||||
|
|
||||||
|
|
||||||
def not_found():
|
|
||||||
return ApiError(404, "not_found", "Объект не найден")
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
"""Логика идемпотентности для POST запросов."""
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
from .errors import ApiError, bad_request
|
|
||||||
|
|
||||||
|
|
||||||
def get_request_hash(data: dict) -> str:
|
|
||||||
"""Хеш тела запроса, независимый от порядка ключей."""
|
|
||||||
return hashlib.sha256(json.dumps(data, sort_keys=True).encode("utf-8")).hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
def check_and_lock(conn, user_id: str, method: str, path: str, key: str, req_hash: str):
|
|
||||||
"""
|
|
||||||
Блокирует ключ идемпотентности через advisory lock в PostgreSQL.
|
|
||||||
"""
|
|
||||||
if not key:
|
|
||||||
raise bad_request("Missing Idempotency-Key")
|
|
||||||
if not isinstance(key, str) or not (1 <= len(key) <= 128):
|
|
||||||
raise bad_request("Invalid Idempotency-Key format")
|
|
||||||
|
|
||||||
# Берем блокировку на уровне БД по хешу ключа.
|
|
||||||
lock_id = f"{user_id}:{method}:{path}:{key}"
|
|
||||||
conn.execute("SELECT pg_advisory_xact_lock(hashtext(%s))", (lock_id,))
|
|
||||||
|
|
||||||
# Проверяем, есть ли уже такой ключ
|
|
||||||
row = conn.execute("""
|
|
||||||
SELECT request_hash, status_code, response_body
|
|
||||||
FROM idempotency_keys
|
|
||||||
WHERE user_id = %s AND method = %s AND path = %s AND key = %s
|
|
||||||
""", (user_id, method, path, key)).fetchone()
|
|
||||||
|
|
||||||
if row:
|
|
||||||
# Ключ уже существует
|
|
||||||
if row["request_hash"] != req_hash:
|
|
||||||
raise ApiError(409, "conflict", "Idempotency-Key conflict: body differs")
|
|
||||||
|
|
||||||
# Если запрос уже был успешно обработан, возвращаем сохраненный ответ
|
|
||||||
if row["status_code"] is not None and row["response_body"] is not None:
|
|
||||||
return False, json.loads(row["response_body"])
|
|
||||||
|
|
||||||
# Если status_code пустой, значит первая транзакция упала. Разрешаем перезапись.
|
|
||||||
return True, None
|
|
||||||
|
|
||||||
return True, None
|
|
||||||
|
|
||||||
|
|
||||||
def save_result(conn, user_id: str, method: str, path: str, key: str, req_hash: str, status_code: int, response_body: dict):
|
|
||||||
"""Сохраняет результат выполнения запроса для будущих повторных вызовов."""
|
|
||||||
conn.execute("""
|
|
||||||
INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, status_code, response_body)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
||||||
ON CONFLICT (user_id, method, path, key) DO UPDATE
|
|
||||||
SET status_code = EXCLUDED.status_code, response_body = EXCLUDED.response_body
|
|
||||||
""", (user_id, method, path, key, req_hash, status_code, json.dumps(response_body)))
|
|
||||||
|
|
||||||
|
|
||||||
def cleanup(conn, ttl_seconds: int):
|
|
||||||
"""Оппортунистическая очистка старых ключей (через подзапрос, т.к. DELETE LIMIT в PG нет)."""
|
|
||||||
conn.execute("""
|
|
||||||
DELETE FROM idempotency_keys
|
|
||||||
WHERE (user_id, method, path, key) IN (
|
|
||||||
SELECT user_id, method, path, key
|
|
||||||
FROM idempotency_keys
|
|
||||||
WHERE created_at < now() - make_interval(secs => %s)
|
|
||||||
LIMIT 100
|
|
||||||
)
|
|
||||||
""", (ttl_seconds,))
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
"""Версионируемые миграции."""
|
|
||||||
import hashlib
|
|
||||||
import logging
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import psycopg
|
|
||||||
|
|
||||||
from . import config
|
|
||||||
|
|
||||||
LOCK_ID = 727_002_001
|
|
||||||
log = logging.getLogger("migrate")
|
|
||||||
|
|
||||||
|
|
||||||
def migration_files():
|
|
||||||
return sorted((Path(__file__).parent / "migrations").glob("[0-9][0-9][0-9][0-9]_*.sql"))
|
|
||||||
|
|
||||||
|
|
||||||
def apply_all(conninfo):
|
|
||||||
files = migration_files()
|
|
||||||
with psycopg.connect(conninfo, connect_timeout=3) as conn:
|
|
||||||
conn.execute("SELECT pg_advisory_xact_lock(%s)", (LOCK_ID,))
|
|
||||||
conn.execute("""CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
||||||
version text PRIMARY KEY,
|
|
||||||
checksum text NOT NULL,
|
|
||||||
applied_at timestamptz NOT NULL DEFAULT now())""")
|
|
||||||
applied = dict(conn.execute("SELECT version, checksum FROM schema_migrations").fetchall())
|
|
||||||
done = []
|
|
||||||
for path in files:
|
|
||||||
sql = path.read_text(encoding="utf-8")
|
|
||||||
checksum = hashlib.sha256(sql.encode()).hexdigest()
|
|
||||||
if path.stem in applied:
|
|
||||||
if applied[path.stem] != checksum:
|
|
||||||
raise RuntimeError("migration %s was modified after being applied" % path.stem)
|
|
||||||
continue
|
|
||||||
conn.execute(sql)
|
|
||||||
conn.execute("INSERT INTO schema_migrations (version, checksum) VALUES (%s, %s)", (path.stem, checksum))
|
|
||||||
done.append(path.stem)
|
|
||||||
return done
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(message)s")
|
|
||||||
if not config.DATABASE_URL:
|
|
||||||
sys.exit("DATABASE_URL(_FILE) is not configured")
|
|
||||||
deadline = time.monotonic() + 60
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
done = apply_all(config.DATABASE_URL)
|
|
||||||
break
|
|
||||||
except psycopg.OperationalError as exc:
|
|
||||||
if time.monotonic() > deadline:
|
|
||||||
raise
|
|
||||||
log.info("database not ready (%s), retrying", type(exc).__name__)
|
|
||||||
time.sleep(1)
|
|
||||||
log.info("applied: %s", ", ".join(done) if done else "nothing to do (schema is up to date)")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
CREATE TABLE users (
|
|
||||||
id uuid PRIMARY KEY,
|
|
||||||
username text NOT NULL,
|
|
||||||
display_name text NOT NULL,
|
|
||||||
password_hash text NOT NULL,
|
|
||||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
CONSTRAINT users_username_key UNIQUE (username),
|
|
||||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
|
||||||
CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE chats (
|
|
||||||
id uuid PRIMARY KEY,
|
|
||||||
title text NOT NULL,
|
|
||||||
created_by uuid NOT NULL REFERENCES users (id),
|
|
||||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100)
|
|
||||||
);
|
|
||||||
CREATE INDEX chats_created_at_id_idx ON chats (created_at, id);
|
|
||||||
|
|
||||||
CREATE TABLE chat_members (
|
|
||||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
|
||||||
user_id uuid NOT NULL REFERENCES users (id),
|
|
||||||
added_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
PRIMARY KEY (chat_id, user_id)
|
|
||||||
);
|
|
||||||
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
|
|
||||||
|
|
||||||
CREATE TABLE messages (
|
|
||||||
id uuid PRIMARY KEY,
|
|
||||||
chat_id uuid NOT NULL,
|
|
||||||
sender_id uuid NOT NULL,
|
|
||||||
text text NOT NULL,
|
|
||||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000),
|
|
||||||
CONSTRAINT messages_sender_is_member FOREIGN KEY (chat_id, sender_id)
|
|
||||||
REFERENCES chat_members (chat_id, user_id)
|
|
||||||
);
|
|
||||||
CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id);
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
CREATE TABLE sessions (
|
|
||||||
token_hash bytea PRIMARY KEY,
|
|
||||||
user_id uuid NOT NULL REFERENCES users (id),
|
|
||||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
expires_at timestamptz NOT NULL,
|
|
||||||
CONSTRAINT sessions_hash_length CHECK (octet_length(token_hash) = 32),
|
|
||||||
CONSTRAINT sessions_expiry_after_creation CHECK (expires_at > created_at)
|
|
||||||
);
|
|
||||||
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);
|
|
||||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
CREATE TABLE idempotency_keys (
|
|
||||||
user_id uuid NOT NULL REFERENCES users(id),
|
|
||||||
method text NOT NULL,
|
|
||||||
path text NOT NULL,
|
|
||||||
key text NOT NULL,
|
|
||||||
request_hash text NOT NULL,
|
|
||||||
status_code int,
|
|
||||||
response_body text,
|
|
||||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
||||||
PRIMARY KEY (user_id, method, path, key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idempotency_keys_created_at_idx ON idempotency_keys (created_at);
|
|
||||||
-98
@@ -1,98 +0,0 @@
|
|||||||
"""Доступ к данным."""
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from . import cursor, security
|
|
||||||
from .errors import bad_request
|
|
||||||
from .sessions import fmt_ts
|
|
||||||
|
|
||||||
|
|
||||||
def username_exists(conn, username):
|
|
||||||
return conn.execute("SELECT 1 FROM users WHERE username = %s", (username,)).fetchone() is not None
|
|
||||||
|
|
||||||
|
|
||||||
def insert_user(conn, username, display_name, password_hash):
|
|
||||||
return conn.execute(
|
|
||||||
"""INSERT INTO users (id, username, display_name, password_hash) VALUES (%s, %s, %s, %s)
|
|
||||||
RETURNING id, username, display_name, created_at""",
|
|
||||||
(uuid.uuid4(), username, display_name, password_hash)).fetchone()
|
|
||||||
|
|
||||||
|
|
||||||
def user_for_login(conn, username):
|
|
||||||
return conn.execute("SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s",
|
|
||||||
(username,)).fetchone()
|
|
||||||
|
|
||||||
|
|
||||||
def update_password_hash(conn, user_id, new_hash):
|
|
||||||
conn.execute("UPDATE users SET password_hash = %s WHERE id = %s", (new_hash, user_id))
|
|
||||||
|
|
||||||
|
|
||||||
def chat_json(row):
|
|
||||||
return {"id": str(row["id"]), "title": row["title"], "member_ids": [str(x) for x in row["member_ids"]],
|
|
||||||
"created_at": fmt_ts(row["created_at"])}
|
|
||||||
|
|
||||||
|
|
||||||
def create_chat(conn, creator_id, title, member_ids, check_members=True):
|
|
||||||
if check_members:
|
|
||||||
found = conn.execute("SELECT count(*) AS n FROM users WHERE id = ANY(%s)", (member_ids,)).fetchone()["n"]
|
|
||||||
if found != len(member_ids):
|
|
||||||
raise bad_request("Неизвестный участник чата")
|
|
||||||
chat = conn.execute(
|
|
||||||
"INSERT INTO chats (id, title, created_by) VALUES (%s, %s, %s) RETURNING id, title, created_at",
|
|
||||||
(uuid.uuid4(), title, creator_id)).fetchone()
|
|
||||||
conn.execute("INSERT INTO chat_members (chat_id, user_id) SELECT %s, unnest(%s::uuid[])",
|
|
||||||
(chat["id"], member_ids))
|
|
||||||
chat["member_ids"] = sorted(member_ids)
|
|
||||||
return chat
|
|
||||||
|
|
||||||
|
|
||||||
def get_chat(conn, chat_id, user_id):
|
|
||||||
return conn.execute(
|
|
||||||
"""SELECT c.id, c.title, c.created_at,
|
|
||||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) AS member_ids
|
|
||||||
FROM chats c
|
|
||||||
WHERE c.id = %s AND EXISTS (SELECT 1 FROM chat_members me WHERE me.chat_id = c.id AND me.user_id = %s)""",
|
|
||||||
(chat_id, user_id)).fetchone()
|
|
||||||
|
|
||||||
|
|
||||||
def is_member(conn, chat_id, user_id):
|
|
||||||
return conn.execute("SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
|
|
||||||
(chat_id, user_id)).fetchone() is not None
|
|
||||||
|
|
||||||
|
|
||||||
def list_chats(conn, user_id, after, limit):
|
|
||||||
base = """SELECT c.id, c.title, c.created_at,
|
|
||||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) AS member_ids
|
|
||||||
FROM chat_members me JOIN chats c ON c.id = me.chat_id
|
|
||||||
WHERE me.user_id = %s"""
|
|
||||||
params = [user_id]
|
|
||||||
if after:
|
|
||||||
base += " AND (c.created_at, c.id) > (%s, %s)"
|
|
||||||
params += list(after)
|
|
||||||
rows = conn.execute(base + " ORDER BY c.created_at, c.id LIMIT %s", params + [limit + 1]).fetchall()
|
|
||||||
page, more = rows[:limit], len(rows) > limit
|
|
||||||
nxt = cursor.encode("chats", user_id, page[-1]["created_at"], page[-1]["id"]) if more else None
|
|
||||||
return [chat_json(r) for r in page], nxt
|
|
||||||
|
|
||||||
|
|
||||||
def message_json(row):
|
|
||||||
return {"id": str(row["id"]), "chat_id": str(row["chat_id"]), "sender_id": str(row["sender_id"]),
|
|
||||||
"text": row["text"], "created_at": fmt_ts(row["created_at"])}
|
|
||||||
|
|
||||||
|
|
||||||
def insert_message(conn, chat_id, sender_id, text):
|
|
||||||
return conn.execute(
|
|
||||||
"""INSERT INTO messages (id, chat_id, sender_id, text) VALUES (%s, %s, %s, %s)
|
|
||||||
RETURNING id, chat_id, sender_id, text, created_at""",
|
|
||||||
(uuid.uuid4(), chat_id, sender_id, text)).fetchone()
|
|
||||||
|
|
||||||
|
|
||||||
def list_messages(conn, chat_id, after, limit):
|
|
||||||
sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = %s"
|
|
||||||
params = [chat_id]
|
|
||||||
if after:
|
|
||||||
sql += " AND (created_at, id) > (%s, %s)"
|
|
||||||
params += list(after)
|
|
||||||
rows = conn.execute(sql + " ORDER BY created_at, id LIMIT %s", params + [limit + 1]).fetchall()
|
|
||||||
page, more = rows[:limit], len(rows) > limit
|
|
||||||
nxt = cursor.encode("messages", chat_id, page[-1]["created_at"], page[-1]["id"]) if more else None
|
|
||||||
return [message_json(r) for r in page], nxt
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
"""Пароли (Argon2id), токены, разбор заголовка Authorization."""
|
|
||||||
import base64
|
|
||||||
import binascii
|
|
||||||
import hashlib
|
|
||||||
import re
|
|
||||||
import secrets
|
|
||||||
import threading
|
|
||||||
|
|
||||||
from argon2 import PasswordHasher, Type
|
|
||||||
from argon2.exceptions import InvalidHashError, VerificationError
|
|
||||||
|
|
||||||
from . import config
|
|
||||||
|
|
||||||
_hasher = PasswordHasher(time_cost=config.ARGON2_TIME_COST, memory_cost=config.ARGON2_MEMORY_KIB,
|
|
||||||
parallelism=1, hash_len=32, salt_len=16, type=Type.ID)
|
|
||||||
_DUMMY_HASH = _hasher.hash("dummy-password-for-constant-time")
|
|
||||||
_hash_slots = threading.BoundedSemaphore(config.HASH_CONCURRENCY)
|
|
||||||
_TOKEN_RE = re.compile(r"[A-Za-z0-9._~+/=-]{16,512}")
|
|
||||||
|
|
||||||
|
|
||||||
def hash_password(password):
|
|
||||||
with _hash_slots:
|
|
||||||
return _hasher.hash(password)
|
|
||||||
|
|
||||||
|
|
||||||
def verify_password(stored_hash, password):
|
|
||||||
with _hash_slots:
|
|
||||||
try:
|
|
||||||
_hasher.verify(stored_hash, password)
|
|
||||||
except (VerificationError, InvalidHashError):
|
|
||||||
return False, None
|
|
||||||
return True, (_hasher.hash(password) if _hasher.check_needs_rehash(stored_hash) else None)
|
|
||||||
|
|
||||||
|
|
||||||
def burn_verify(password):
|
|
||||||
with _hash_slots:
|
|
||||||
try:
|
|
||||||
_hasher.verify(_DUMMY_HASH, password)
|
|
||||||
except VerificationError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def new_token():
|
|
||||||
return secrets.token_urlsafe(32)
|
|
||||||
|
|
||||||
|
|
||||||
def token_hash(token):
|
|
||||||
return hashlib.sha256(token.encode("ascii")).digest()
|
|
||||||
|
|
||||||
|
|
||||||
def parse_basic(header):
|
|
||||||
if not header:
|
|
||||||
return None
|
|
||||||
scheme, _, rest = header.partition(" ")
|
|
||||||
if scheme.lower() != "basic":
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
text = base64.b64decode(rest.strip(), validate=True).decode("utf-8")
|
|
||||||
except (binascii.Error, UnicodeDecodeError, ValueError):
|
|
||||||
return None
|
|
||||||
username, sep, password = text.partition(":")
|
|
||||||
return (username, password) if sep else None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_bearer(header):
|
|
||||||
if not header:
|
|
||||||
return None
|
|
||||||
scheme, _, rest = header.partition(" ")
|
|
||||||
token = rest.strip()
|
|
||||||
if scheme.lower() != "bearer" or not _TOKEN_RE.fullmatch(token):
|
|
||||||
return None
|
|
||||||
return token
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
"""Сессии: PostgreSQL — источник истины, Redis/Valkey — read-through кэш."""
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
from datetime import timezone
|
|
||||||
|
|
||||||
from . import cache, config, db, security
|
|
||||||
from .errors import ApiError
|
|
||||||
|
|
||||||
TOMBSTONE = "revoked"
|
|
||||||
_KEY = "sess:v1:"
|
|
||||||
|
|
||||||
|
|
||||||
def fmt_ts(value):
|
|
||||||
return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
|
||||||
|
|
||||||
|
|
||||||
def user_json(row):
|
|
||||||
return {"id": str(row["id"]), "username": row["username"], "display_name": row["display_name"],
|
|
||||||
"created_at": fmt_ts(row["created_at"])}
|
|
||||||
|
|
||||||
|
|
||||||
def _ttl_ms(expires_epoch):
|
|
||||||
remaining = expires_epoch - time.time()
|
|
||||||
return int(min(config.SESSION_CACHE_TTL_SECONDS, remaining) * 1000) if remaining > 0 else 0
|
|
||||||
|
|
||||||
|
|
||||||
def create(conn, user_row):
|
|
||||||
token = security.new_token()
|
|
||||||
row = conn.execute(
|
|
||||||
"""INSERT INTO sessions (token_hash, user_id, expires_at)
|
|
||||||
VALUES (%s, %s, clock_timestamp() + make_interval(secs => %s))
|
|
||||||
RETURNING expires_at""",
|
|
||||||
(security.token_hash(token), user_row["id"], config.SESSION_TTL_SECONDS)).fetchone()
|
|
||||||
conn.execute("""DELETE FROM sessions WHERE token_hash IN (
|
|
||||||
SELECT token_hash FROM sessions WHERE expires_at < now() - interval '1 hour' LIMIT 100)""")
|
|
||||||
return token, row["expires_at"]
|
|
||||||
|
|
||||||
|
|
||||||
def warm(token, user, expires_at):
|
|
||||||
entry = {"user": user, "exp": expires_at.timestamp()}
|
|
||||||
ttl = _ttl_ms(entry["exp"])
|
|
||||||
if ttl > 0:
|
|
||||||
cache.set_if_absent(_KEY + security.token_hash(token).hex(), json.dumps(entry), ttl)
|
|
||||||
|
|
||||||
|
|
||||||
def lookup(token):
|
|
||||||
digest = security.token_hash(token)
|
|
||||||
key = _KEY + digest.hex()
|
|
||||||
cached = cache.get(key)
|
|
||||||
if cached == TOMBSTONE:
|
|
||||||
return None
|
|
||||||
if cached:
|
|
||||||
try:
|
|
||||||
entry = json.loads(cached)
|
|
||||||
if entry["exp"] > time.time():
|
|
||||||
return {"user": entry["user"], "digest": digest}
|
|
||||||
return None
|
|
||||||
except (ValueError, KeyError, TypeError):
|
|
||||||
pass
|
|
||||||
with db.connection() as conn:
|
|
||||||
row = conn.execute(
|
|
||||||
"""SELECT u.id, u.username, u.display_name, u.created_at,
|
|
||||||
extract(epoch FROM s.expires_at) AS exp
|
|
||||||
FROM sessions s JOIN users u ON u.id = s.user_id
|
|
||||||
WHERE s.token_hash = %s AND s.expires_at > clock_timestamp()""", (digest,)).fetchone()
|
|
||||||
if row is None:
|
|
||||||
return None
|
|
||||||
user = user_json(row)
|
|
||||||
ttl = _ttl_ms(float(row["exp"]))
|
|
||||||
if ttl > 0:
|
|
||||||
cache.set_if_absent(key, json.dumps({"user": user, "exp": float(row["exp"])}), ttl)
|
|
||||||
return {"user": user, "digest": digest}
|
|
||||||
|
|
||||||
|
|
||||||
def revoke(digest):
|
|
||||||
if config.CACHE_URL:
|
|
||||||
wrote = cache.set_always(_KEY + digest.hex(), TOMBSTONE, config.SESSION_CACHE_TTL_SECONDS * 1000)
|
|
||||||
if not wrote:
|
|
||||||
raise ApiError(503, "unavailable", "Сервис временно недоступен")
|
|
||||||
with db.connection() as conn:
|
|
||||||
conn.execute("DELETE FROM sessions WHERE token_hash = %s", (digest,))
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
"""Строгая валидация входа."""
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from .errors import ApiError, bad_request
|
|
||||||
|
|
||||||
_UUID_RE = re.compile(r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
|
|
||||||
_USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
|
|
||||||
_INT_RE = re.compile(r"[0-9]{1,6}")
|
|
||||||
|
|
||||||
|
|
||||||
def parse_uuid(value):
|
|
||||||
if isinstance(value, str) and _UUID_RE.fullmatch(value):
|
|
||||||
return uuid.UUID(value)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def require_uuid(value, name="id"):
|
|
||||||
parsed = parse_uuid(value)
|
|
||||||
if parsed is None:
|
|
||||||
raise bad_request("Некорректный %s" % name)
|
|
||||||
return parsed
|
|
||||||
|
|
||||||
|
|
||||||
def _reject_constant(name):
|
|
||||||
raise ValueError("non-finite number")
|
|
||||||
|
|
||||||
|
|
||||||
def _no_duplicates(pairs):
|
|
||||||
result = {}
|
|
||||||
for key, value in pairs:
|
|
||||||
if key in result:
|
|
||||||
raise ValueError("duplicate key")
|
|
||||||
result[key] = value
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def json_body(content_type, raw, allowed, required):
|
|
||||||
if content_type.split(";")[0].strip().lower() != "application/json":
|
|
||||||
raise ApiError(415, "unsupported_media_type", "Ожидается Content-Type: application/json")
|
|
||||||
try:
|
|
||||||
data = json.loads(raw.decode("utf-8"), parse_constant=_reject_constant, object_pairs_hook=_no_duplicates)
|
|
||||||
except (ValueError, UnicodeDecodeError, RecursionError):
|
|
||||||
raise bad_request("Некорректный JSON")
|
|
||||||
if not isinstance(data, dict) or set(data) - set(allowed) or not set(required) <= set(data):
|
|
||||||
raise bad_request("Некорректный набор полей")
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def text_field(value, name, low, high, allow_blank=False):
|
|
||||||
if not isinstance(value, str):
|
|
||||||
raise bad_request("Поле %s должно быть строкой" % name)
|
|
||||||
if "\x00" in value or not _encodable(value):
|
|
||||||
raise bad_request("Поле %s содержит недопустимые символы" % name)
|
|
||||||
if not low <= len(value) <= high:
|
|
||||||
raise bad_request("Поле %s: длина должна быть %d–%d" % (name, low, high))
|
|
||||||
if not allow_blank and not value.strip():
|
|
||||||
raise bad_request("Поле %s не должно состоять только из пробелов" % name)
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def _encodable(value):
|
|
||||||
try:
|
|
||||||
value.encode("utf-8")
|
|
||||||
return True
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def username_field(value):
|
|
||||||
if not isinstance(value, str) or not _USERNAME_RE.fullmatch(value):
|
|
||||||
raise bad_request("username должен соответствовать [a-z0-9_]{3,32}")
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
def limit_param(params):
|
|
||||||
raw = params.get("limit")
|
|
||||||
if raw is None:
|
|
||||||
return 50
|
|
||||||
if not _INT_RE.fullmatch(raw) or not 1 <= int(raw) <= 100:
|
|
||||||
raise bad_request("limit должен быть целым числом 1–100")
|
|
||||||
return int(raw)
|
|
||||||
-114
@@ -1,114 +0,0 @@
|
|||||||
name: messenger-lab3
|
|
||||||
|
|
||||||
x-app-env: &app-env
|
|
||||||
APP_HOST: "0.0.0.0"
|
|
||||||
APP_PORT: "8080"
|
|
||||||
APP_ENV: ${APP_ENV:-lab}
|
|
||||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
|
||||||
SESSION_STORE: ${SESSION_STORE:-postgres+cache}
|
|
||||||
SESSION_CACHE_TTL_SECONDS: ${SESSION_CACHE_TTL_SECONDS:-60}
|
|
||||||
DATABASE_URL_FILE: /run/secrets/database_url
|
|
||||||
CACHE_URL_FILE: /run/secrets/cache_url
|
|
||||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
|
||||||
|
|
||||||
services:
|
|
||||||
postgres:
|
|
||||||
image: postgres:17.6-alpine
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
|
||||||
POSTGRES_DB: messenger
|
|
||||||
POSTGRES_USER: messenger
|
|
||||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
|
||||||
secrets: [postgres_password]
|
|
||||||
command: ["postgres", "-c", "fsync=on", "-c", "synchronous_commit=on", "-c", "full_page_writes=on", "-c", "max_connections=100"]
|
|
||||||
volumes:
|
|
||||||
- pgdata:/var/lib/postgresql/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
|
||||||
interval: 3s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 20
|
|
||||||
stop_grace_period: 30s
|
|
||||||
networks: [backend]
|
|
||||||
|
|
||||||
valkey:
|
|
||||||
image: valkey/valkey:8.1.3-alpine
|
|
||||||
restart: unless-stopped
|
|
||||||
secrets: [valkey_password]
|
|
||||||
command:
|
|
||||||
- sh
|
|
||||||
- -c
|
|
||||||
- >-
|
|
||||||
exec valkey-server --requirepass "$$(cat /run/secrets/valkey_password)"
|
|
||||||
--appendonly yes --appendfsync everysec --save ""
|
|
||||||
--maxmemory 64mb --maxmemory-policy volatile-ttl
|
|
||||||
volumes:
|
|
||||||
- valkeydata:/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$(cat /run/secrets/valkey_password)\" valkey-cli ping | grep -q PONG"]
|
|
||||||
interval: 3s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 20
|
|
||||||
networks: [backend]
|
|
||||||
|
|
||||||
migrate:
|
|
||||||
build: .
|
|
||||||
command: ["python", "-m", "app.migrate"]
|
|
||||||
environment: *app-env
|
|
||||||
secrets: [database_url, cache_url]
|
|
||||||
depends_on:
|
|
||||||
postgres: {condition: service_healthy}
|
|
||||||
restart: "no"
|
|
||||||
networks: [backend]
|
|
||||||
|
|
||||||
api:
|
|
||||||
build: .
|
|
||||||
restart: unless-stopped
|
|
||||||
environment: *app-env
|
|
||||||
secrets: [database_url, cache_url]
|
|
||||||
depends_on:
|
|
||||||
migrate: {condition: service_completed_successfully}
|
|
||||||
postgres: {condition: service_healthy}
|
|
||||||
valkey: {condition: service_started}
|
|
||||||
read_only: true
|
|
||||||
tmpfs: ["/tmp", "/dev/shm"]
|
|
||||||
cap_drop: [ALL]
|
|
||||||
security_opt: ["no-new-privileges:true"]
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "python", "-c", "import urllib.request as u; u.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"]
|
|
||||||
interval: 5s
|
|
||||||
timeout: 3s
|
|
||||||
retries: 20
|
|
||||||
start_period: 5s
|
|
||||||
stop_grace_period: 25s
|
|
||||||
networks: [backend]
|
|
||||||
deploy:
|
|
||||||
replicas: 2
|
|
||||||
resources:
|
|
||||||
limits:
|
|
||||||
cpus: '0.50'
|
|
||||||
memory: 512M
|
|
||||||
|
|
||||||
nginx:
|
|
||||||
image: nginx:1.27-alpine
|
|
||||||
ports:
|
|
||||||
- "127.0.0.1:8080:80"
|
|
||||||
volumes:
|
|
||||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
|
||||||
depends_on:
|
|
||||||
api:
|
|
||||||
condition: service_healthy
|
|
||||||
networks: [backend]
|
|
||||||
|
|
||||||
networks:
|
|
||||||
backend: {}
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
pgdata: {}
|
|
||||||
valkeydata: {}
|
|
||||||
|
|
||||||
secrets:
|
|
||||||
postgres_password: {file: ./secrets/postgres_password}
|
|
||||||
valkey_password: {file: ./secrets/valkey_password}
|
|
||||||
database_url: {file: ./secrets/database_url}
|
|
||||||
cache_url: {file: ./secrets/cache_url}
|
|
||||||
@@ -1,275 +0,0 @@
|
|||||||
"""Мессенджер, лабораторная 3: балансировка, X-Instance-Id, идемпотентность."""
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
import uuid
|
|
||||||
from contextlib import asynccontextmanager
|
|
||||||
from typing import NamedTuple
|
|
||||||
|
|
||||||
import anyio.to_thread
|
|
||||||
import psycopg
|
|
||||||
from fastapi import Depends, FastAPI, Request
|
|
||||||
from fastapi.exceptions import RequestValidationError
|
|
||||||
from fastapi.responses import JSONResponse, Response
|
|
||||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
||||||
|
|
||||||
from app import config, cursor, db, idempotency, repo, security, sessions, validation
|
|
||||||
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, ApiError, bad_request, not_found, unauthorized
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
|
|
||||||
log = logging.getLogger("api")
|
|
||||||
|
|
||||||
_REQUEST_ID_RE = re.compile(r"[A-Za-z0-9._-]{1,64}")
|
|
||||||
|
|
||||||
# Уникальный ID инстанса для X-Instance-Id
|
|
||||||
INSTANCE_ID = config.INSTANCE_ID
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
|
||||||
async def lifespan(_app):
|
|
||||||
anyio.to_thread.current_default_thread_limiter().total_tokens = config.THREAD_LIMIT
|
|
||||||
yield
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
|
|
||||||
|
|
||||||
|
|
||||||
def make_error(code, message, request_id):
|
|
||||||
return {"error": {"code": code, "message": message, "request_id": request_id}}
|
|
||||||
|
|
||||||
|
|
||||||
def _rid(request):
|
|
||||||
return getattr(request.state, "request_id", None) or uuid.uuid4().hex
|
|
||||||
|
|
||||||
|
|
||||||
def error_response(request, status, code, message, headers=None):
|
|
||||||
rid = _rid(request)
|
|
||||||
out = {"X-Request-Id": rid, "Cache-Control": "no-store"}
|
|
||||||
out.update(headers or {})
|
|
||||||
return JSONResponse(status_code=status, content=make_error(code, message, rid), headers=out)
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("http")
|
|
||||||
async def add_request_id(request: Request, call_next):
|
|
||||||
incoming = request.headers.get("X-Request-Id", "")
|
|
||||||
rid = incoming if _REQUEST_ID_RE.fullmatch(incoming) else uuid.uuid4().hex
|
|
||||||
request.state.request_id = rid
|
|
||||||
response = await call_next(request)
|
|
||||||
response.headers["X-Request-Id"] = rid
|
|
||||||
response.headers["X-Instance-Id"] = INSTANCE_ID # <--- НОВОЕ В LAB3
|
|
||||||
response.headers["Cache-Control"] = "no-store"
|
|
||||||
return response
|
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(ApiError)
|
|
||||||
async def api_error_handler(request: Request, exc: ApiError):
|
|
||||||
return error_response(request, exc.status, exc.code, exc.message, exc.headers)
|
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(RequestValidationError)
|
|
||||||
async def validation_handler(request: Request, exc):
|
|
||||||
return error_response(request, 400, "invalid_request", "Некорректный запрос")
|
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(StarletteHTTPException)
|
|
||||||
async def http_exc_handler(request: Request, exc: StarletteHTTPException):
|
|
||||||
mapping = {404: ("not_found", "Объект не найден"), 405: ("invalid_request", "Метод не поддерживается")}
|
|
||||||
code, message = mapping.get(exc.status_code, ("invalid_request", "Ошибка запроса"))
|
|
||||||
return error_response(request, exc.status_code, code, message)
|
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(Exception)
|
|
||||||
async def generic_handler(request: Request, exc: Exception):
|
|
||||||
if isinstance(exc, db.TRANSIENT):
|
|
||||||
log.warning("dependency unavailable [%s]: %s", _rid(request), type(exc).__name__)
|
|
||||||
return error_response(request, 503, "unavailable", "Сервис временно недоступен")
|
|
||||||
log.error("unhandled error [%s]", _rid(request), exc_info=exc)
|
|
||||||
return error_response(request, 500, "internal_error", "Внутренняя ошибка сервера")
|
|
||||||
|
|
||||||
|
|
||||||
class Auth(NamedTuple):
|
|
||||||
user: dict
|
|
||||||
user_id: uuid.UUID
|
|
||||||
digest: bytes
|
|
||||||
|
|
||||||
|
|
||||||
def authenticated(request: Request) -> Auth:
|
|
||||||
token = security.parse_bearer(request.headers.get("Authorization"))
|
|
||||||
if token is None:
|
|
||||||
raise unauthorized(BEARER_CHALLENGE)
|
|
||||||
found = sessions.lookup(token)
|
|
||||||
if found is None:
|
|
||||||
raise unauthorized(BEARER_CHALLENGE)
|
|
||||||
return Auth(found["user"], uuid.UUID(found["user"]["id"]), found["digest"])
|
|
||||||
|
|
||||||
|
|
||||||
class RawBody(NamedTuple):
|
|
||||||
content_type: str
|
|
||||||
data: bytes
|
|
||||||
|
|
||||||
|
|
||||||
async def raw_body(request: Request) -> RawBody:
|
|
||||||
declared = request.headers.get("content-length")
|
|
||||||
if declared is not None and declared.isdigit() and int(declared) > config.MAX_BODY_BYTES:
|
|
||||||
raise ApiError(413, "payload_too_large", "Тело запроса слишком большое")
|
|
||||||
chunks, size = [], 0
|
|
||||||
async for chunk in request.stream():
|
|
||||||
size += len(chunk)
|
|
||||||
if size > config.MAX_BODY_BYTES:
|
|
||||||
raise ApiError(413, "payload_too_large", "Тело запроса слишком большое")
|
|
||||||
chunks.append(chunk)
|
|
||||||
return RawBody(request.headers.get("content-type", ""), b"".join(chunks))
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health/live")
|
|
||||||
def live():
|
|
||||||
return {"status": "ok"}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health/ready")
|
|
||||||
def ready():
|
|
||||||
db.ping()
|
|
||||||
return {"status": "ok"}
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/v1/users", status_code=201)
|
|
||||||
def create_user(body: RawBody = Depends(raw_body)):
|
|
||||||
data = validation.json_body(body.content_type, body.data,
|
|
||||||
{"username", "display_name", "password"}, {"username", "display_name", "password"})
|
|
||||||
username = validation.username_field(data["username"])
|
|
||||||
display_name = validation.text_field(data["display_name"], "display_name", 1, 100)
|
|
||||||
password = validation.text_field(data["password"], "password", 12, 128)
|
|
||||||
with db.connection() as conn:
|
|
||||||
if repo.username_exists(conn, username):
|
|
||||||
raise ApiError(409, "conflict", "Имя пользователя занято")
|
|
||||||
password_hash = security.hash_password(password)
|
|
||||||
try:
|
|
||||||
with db.connection() as conn:
|
|
||||||
row = repo.insert_user(conn, username, display_name, password_hash)
|
|
||||||
except psycopg.errors.UniqueViolation:
|
|
||||||
raise ApiError(409, "conflict", "Имя пользователя занято")
|
|
||||||
return sessions.user_json(row)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/users/me")
|
|
||||||
def get_me(auth: Auth = Depends(authenticated)):
|
|
||||||
return auth.user
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/v1/auth/sessions", status_code=201)
|
|
||||||
def login(request: Request):
|
|
||||||
creds = security.parse_basic(request.headers.get("Authorization"))
|
|
||||||
if creds is None:
|
|
||||||
raise unauthorized(BASIC_CHALLENGE)
|
|
||||||
username, password = creds
|
|
||||||
with db.connection() as conn:
|
|
||||||
user = repo.user_for_login(conn, username) if validation._USERNAME_RE.fullmatch(username) else None
|
|
||||||
if user is None:
|
|
||||||
security.burn_verify(password)
|
|
||||||
raise unauthorized(BASIC_CHALLENGE)
|
|
||||||
ok, new_hash = security.verify_password(user["password_hash"], password)
|
|
||||||
if not ok:
|
|
||||||
raise unauthorized(BASIC_CHALLENGE)
|
|
||||||
with db.connection() as conn:
|
|
||||||
if new_hash:
|
|
||||||
repo.update_password_hash(conn, user["id"], new_hash)
|
|
||||||
token, expires_at = sessions.create(conn, user)
|
|
||||||
user_json = sessions.user_json(user)
|
|
||||||
sessions.warm(token, user_json, expires_at)
|
|
||||||
return {"token": token, "token_type": "Bearer", "expires_at": sessions.fmt_ts(expires_at), "user": user_json}
|
|
||||||
|
|
||||||
|
|
||||||
@app.delete("/api/v1/auth/sessions/current")
|
|
||||||
def logout(auth: Auth = Depends(authenticated)):
|
|
||||||
sessions.revoke(auth.digest)
|
|
||||||
return Response(status_code=204)
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/v1/chats", status_code=201)
|
|
||||||
def create_chat(auth: Auth = Depends(authenticated), body: RawBody = Depends(raw_body)):
|
|
||||||
data = validation.json_body(body.content_type, body.data, {"title", "member_ids"}, {"title", "member_ids"})
|
|
||||||
title = validation.text_field(data["title"], "title", 1, 100)
|
|
||||||
raw = data["member_ids"]
|
|
||||||
if not isinstance(raw, list) or not 1 <= len(raw) <= 100:
|
|
||||||
raise bad_request("member_ids: от 1 до 100 элементов")
|
|
||||||
members = [validation.require_uuid(x, "member_id") for x in raw]
|
|
||||||
if len(set(members)) != len(members) or auth.user_id not in members:
|
|
||||||
raise bad_request("member_ids должны быть уникальны и включать вызывающего")
|
|
||||||
with db.connection() as conn:
|
|
||||||
chat = repo.create_chat(conn, auth.user_id, title, members)
|
|
||||||
return repo.chat_json(chat)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/chats")
|
|
||||||
def list_chats(request: Request, auth: Auth = Depends(authenticated)):
|
|
||||||
limit = validation.limit_param(request.query_params)
|
|
||||||
raw = request.query_params.get("cursor")
|
|
||||||
after = cursor.decode(raw, "chats", auth.user_id) if raw is not None else None
|
|
||||||
with db.connection() as conn:
|
|
||||||
items, nxt = repo.list_chats(conn, auth.user_id, after, limit)
|
|
||||||
return {"items": items, "next_cursor": nxt}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/chats/{chat_id}")
|
|
||||||
def get_chat(chat_id: str, auth: Auth = Depends(authenticated)):
|
|
||||||
cid = validation.require_uuid(chat_id, "chat_id")
|
|
||||||
with db.connection() as conn:
|
|
||||||
chat = repo.get_chat(conn, cid, auth.user_id)
|
|
||||||
if chat is None:
|
|
||||||
raise not_found()
|
|
||||||
return repo.chat_json(chat)
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
|
||||||
def create_message(chat_id: str, request: Request, auth: Auth = Depends(authenticated), body: RawBody = Depends(raw_body)):
|
|
||||||
cid = validation.require_uuid(chat_id, "chat_id")
|
|
||||||
|
|
||||||
# 1. Проверка доступа (до блокировок, чтобы не держать лок зря)
|
|
||||||
with db.connection() as conn:
|
|
||||||
if not repo.is_member(conn, cid, auth.user_id):
|
|
||||||
raise not_found()
|
|
||||||
|
|
||||||
# 2. Валидация тела
|
|
||||||
data = validation.json_body(body.content_type, body.data, {"text"}, {"text"})
|
|
||||||
text = validation.text_field(data["text"], "text", 1, 4000)
|
|
||||||
|
|
||||||
# 3. Идемпотентность
|
|
||||||
idem_key = request.headers.get("Idempotency-Key")
|
|
||||||
path = f"/api/v1/chats/{chat_id}/messages"
|
|
||||||
req_hash = idempotency.get_request_hash(data)
|
|
||||||
|
|
||||||
with db.connection() as conn:
|
|
||||||
# Очистка старых ключей
|
|
||||||
idempotency.cleanup(conn, config.IDEMPOTENCY_TTL_SECONDS)
|
|
||||||
|
|
||||||
# Проверка и захват блокировки (pg_advisory_xact_lock)
|
|
||||||
need_exec, cached_resp = idempotency.check_and_lock(
|
|
||||||
conn, str(auth.user_id), "POST", path, idem_key, req_hash
|
|
||||||
)
|
|
||||||
|
|
||||||
if not need_exec:
|
|
||||||
return JSONResponse(status_code=201, content=cached_resp)
|
|
||||||
|
|
||||||
# Создание сообщения
|
|
||||||
msg = repo.insert_message(conn, cid, auth.user_id, text)
|
|
||||||
msg_json = repo.message_json(msg)
|
|
||||||
|
|
||||||
# Сохранение ответа для будущих повторов
|
|
||||||
idempotency.save_result(
|
|
||||||
conn, str(auth.user_id), "POST", path, idem_key, req_hash, 201, msg_json
|
|
||||||
)
|
|
||||||
|
|
||||||
return msg_json
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
|
||||||
def list_messages(chat_id: str, request: Request, auth: Auth = Depends(authenticated)):
|
|
||||||
cid = validation.require_uuid(chat_id, "chat_id")
|
|
||||||
limit = validation.limit_param(request.query_params)
|
|
||||||
raw = request.query_params.get("cursor")
|
|
||||||
after = cursor.decode(raw, "messages", cid) if raw is not None else None
|
|
||||||
with db.connection() as conn:
|
|
||||||
if not repo.is_member(conn, cid, auth.user_id):
|
|
||||||
raise not_found()
|
|
||||||
items, nxt = repo.list_messages(conn, cid, after, limit)
|
|
||||||
return {"items": items, "next_cursor": nxt}
|
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
events {
|
|
||||||
worker_connections 1024;
|
|
||||||
}
|
|
||||||
|
|
||||||
http {
|
|
||||||
# Docker DNS-сервер
|
|
||||||
resolver 127.0.0.11 valid=5s;
|
|
||||||
|
|
||||||
server {
|
|
||||||
listen 80;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
# Используем переменную, чтобы Nginx перезапрашивал DNS
|
|
||||||
set $api_upstream api:8080;
|
|
||||||
proxy_pass http://$api_upstream;
|
|
||||||
|
|
||||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
|
||||||
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
|
# Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib.
|
||||||
openapi-spec-validator==0.7.2
|
openapi-spec-validator==0.7.2
|
||||||
pytest==8.3.3
|
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
annotated-types==0.8.0
|
|
||||||
anyio==4.15.1
|
|
||||||
argon2-cffi==25.1.0
|
|
||||||
argon2-cffi-bindings==26.1.0
|
|
||||||
cffi==2.1.1
|
|
||||||
click==8.5.0
|
|
||||||
fastapi==0.115.0
|
|
||||||
h11==0.16.0
|
|
||||||
httptools==0.9.0
|
|
||||||
idna==3.20
|
|
||||||
psycopg==3.2.9
|
|
||||||
psycopg-binary==3.2.9
|
|
||||||
psycopg-pool==3.2.6
|
|
||||||
pycparser==3.11
|
|
||||||
pydantic==2.14.1
|
|
||||||
pydantic_core==2.50.1
|
|
||||||
python-dotenv==1.2.4
|
|
||||||
PyYAML==6.0.3
|
|
||||||
redis==6.4.0
|
|
||||||
starlette==0.38.6
|
|
||||||
typing-inspection==0.4.4
|
|
||||||
typing_extensions==4.16.0
|
|
||||||
uvicorn==0.30.6
|
|
||||||
uvloop==0.23.0
|
|
||||||
watchfiles==1.3.0
|
|
||||||
websockets==17.2
|
|
||||||
Regular → Executable
@@ -1,17 +0,0 @@
|
|||||||
@echo off
|
|
||||||
if "%TARGET_INSTANCE_ID%"=="" (
|
|
||||||
echo TARGET_INSTANCE_ID is not set
|
|
||||||
exit /b 1
|
|
||||||
)
|
|
||||||
|
|
||||||
for /f "tokens=*" %%i in ('docker ps -q --filter "name=lab3-api"') do (
|
|
||||||
for /f "tokens=*" %%h in ('docker inspect -f "{{.Config.Hostname}}" %%i') do (
|
|
||||||
if "%%h"=="%TARGET_INSTANCE_ID%" (
|
|
||||||
echo Stopping container %%i
|
|
||||||
docker stop %%i
|
|
||||||
exit /b 0
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
echo Container not found
|
|
||||||
exit /b 1
|
|
||||||
Regular → Executable
@@ -1,30 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# Скрипт останавливает конкретную реплику API по её X-Instance-Id.
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
if [ -z "${TARGET_INSTANCE_ID:-}" ]; then
|
|
||||||
echo "TARGET_INSTANCE_ID is not set" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Так как INSTANCE_ID = socket.gethostname(), в Docker это короткий ID контейнера.
|
|
||||||
cid=$(docker ps -q --filter "id=$TARGET_INSTANCE_ID" || true)
|
|
||||||
|
|
||||||
if [ -z "$cid" ]; then
|
|
||||||
# Если по ID не нашли, ищем по имени проекта и проверяем hostname
|
|
||||||
for c in $(docker ps -q --filter "name=lab3-api"); do
|
|
||||||
h=$(docker inspect -f '{{.Config.Hostname}}' "$c")
|
|
||||||
if [ "$h" = "$TARGET_INSTANCE_ID" ]; then
|
|
||||||
cid="$c"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$cid" ]; then
|
|
||||||
echo "Stopping container $cid (Instance ID: $TARGET_INSTANCE_ID)"
|
|
||||||
docker stop "$cid"
|
|
||||||
else
|
|
||||||
echo "Container with instance ID $TARGET_INSTANCE_ID not found" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
Reference in New Issue
Block a user