forked from mtusi2026/mtusi2026lab3
feat: add replicated API with transactional message idempotency
This commit is contained in:
@@ -18,3 +18,16 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
**/__pycache__
|
||||
**/*.py[cod]
|
||||
.work
|
||||
.pytest_cache
|
||||
.ruff_cache
|
||||
tests
|
||||
student-tests
|
||||
research
|
||||
scripts
|
||||
contracts
|
||||
*.md
|
||||
Makefile
|
||||
compose.yaml
|
||||
|
||||
@@ -11,3 +11,8 @@ SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
|
||||
PUBLIC_PORT=8083
|
||||
API_CPUS=0.5
|
||||
API_MEMORY=384m
|
||||
DB_POOL_SIZE=10
|
||||
|
||||
@@ -30,3 +30,7 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
|
||||
.work/
|
||||
.pytest_cache/
|
||||
.ruff_cache/
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
3.14.7
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv
|
||||
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS builder
|
||||
COPY --from=uv /uv /usr/local/bin/uv
|
||||
WORKDIR /app
|
||||
ENV UV_PYTHON_DOWNLOADS=never UV_LINK_MODE=copy
|
||||
COPY pyproject.toml uv.lock ./
|
||||
RUN uv sync --locked --no-dev --no-install-project
|
||||
|
||||
FROM python:3.14.7-slim-trixie@sha256:51dafde81dbdb6ebde285137a295cf18a47ca95234fe388a343719cb97305b3d AS runtime
|
||||
ARG VCS_REF=unknown
|
||||
LABEL org.opencontainers.image.revision=$VCS_REF
|
||||
WORKDIR /app
|
||||
ENV PATH="/app/.venv/bin:$PATH" PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 APP_HOST=0.0.0.0 APP_PORT=8080
|
||||
RUN groupadd --gid 10001 app && useradd --uid 10001 --gid app --no-create-home app
|
||||
COPY --from=builder /app/.venv /app/.venv
|
||||
COPY src /app/src
|
||||
COPY migrations /app/migrations
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --start-interval=1s --retries=3 CMD ["python", "-c", "import os,urllib.request; urllib.request.urlopen('http://127.0.0.1:'+os.getenv('APP_PORT','8080')+'/health/ready',timeout=1).read()"]
|
||||
STOPSIGNAL SIGTERM
|
||||
CMD ["python", "-m", "src"]
|
||||
@@ -28,3 +28,27 @@ test-persistence:
|
||||
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)
|
||||
|
||||
UV ?= uv
|
||||
VERIFY_OUTPUT ?= evidence/cluster-repeat.json
|
||||
RESEARCH_OUTPUT ?= evidence/scaling-repeat
|
||||
.PHONY: bootstrap up down image lint test-student verify research rolling-update
|
||||
bootstrap:
|
||||
$(PYTHON) scripts/bootstrap.py
|
||||
up: bootstrap
|
||||
VCS_REF=$$(git rev-parse HEAD) docker compose up -d --build --wait
|
||||
down:
|
||||
docker compose down
|
||||
image:
|
||||
docker build --build-arg VCS_REF=$$(git rev-parse HEAD) -t mtusi-messenger:lab3 .
|
||||
lint:
|
||||
$(UV) run --locked ruff format src student-tests research scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_*.py
|
||||
$(UV) run --locked ruff check src student-tests research scripts/bootstrap.py scripts/stand.py scripts/cluster.py scripts/verify_*.py
|
||||
test-student:
|
||||
BASE_URL="$(BASE_URL)" $(UV) run --locked pytest student-tests
|
||||
verify:
|
||||
$(UV) run --locked python -m scripts.verify_cluster --output "$(VERIFY_OUTPUT)"
|
||||
research:
|
||||
$(UV) run --locked python -m research.run --output "$(RESEARCH_OUTPUT)"
|
||||
rolling-update:
|
||||
scripts/rolling-update.sh
|
||||
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
name: mtusi-lab3-maslov
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:18.3@sha256:7e32e9833a6fb1c92c32552794cb6ed569d51b445a54907d35fc112ef39684db
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
PGDATA: /var/lib/postgresql/data/pgdata
|
||||
secrets: [postgres_password]
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
cpus: 1
|
||||
mem_limit: 512m
|
||||
memswap_limit: 512m
|
||||
cache:
|
||||
image: valkey/valkey:8.1.3@sha256:fea8b3e67b15729d4bb70589eb03367bab9ad1ee89c876f54327fc7c6e618571
|
||||
command: [valkey-server, /run/secrets/valkey.conf]
|
||||
secrets:
|
||||
- cache_password
|
||||
- source: valkey_config
|
||||
target: valkey.conf
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'REDISCLI_AUTH="$$(cat /run/secrets/cache_password)" valkey-cli ping']
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 20
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
memswap_limit: 128m
|
||||
migrate:
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
pull_policy: never
|
||||
command: [python, -m, src.migrate]
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
secrets: [database_url]
|
||||
depends_on:
|
||||
postgres: {condition: service_healthy}
|
||||
cpus: 0.5
|
||||
mem_limit: 256m
|
||||
read_only: true
|
||||
api:
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
VCS_REF: ${VCS_REF:-unknown}
|
||||
scale: ${API_REPLICAS:-2}
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL_FILE: /run/secrets/cache_url
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DB_POOL_SIZE: ${DB_POOL_SIZE:-10}
|
||||
secrets: [database_url, cache_url]
|
||||
depends_on:
|
||||
migrate: {condition: service_completed_successfully}
|
||||
cache: {condition: service_healthy}
|
||||
cpus: ${API_CPUS:-0.5}
|
||||
mem_limit: ${API_MEMORY:-384m}
|
||||
memswap_limit: ${API_MEMORY:-384m}
|
||||
pids_limit: 128
|
||||
read_only: true
|
||||
tmpfs: ["/tmp:rw,noexec,nosuid,size=16m"]
|
||||
cap_drop: [ALL]
|
||||
security_opt: [no-new-privileges:true]
|
||||
stop_grace_period: 10s
|
||||
lb:
|
||||
image: haproxy:3.2.1-alpine@sha256:ac79fe145f2bb6626ff26b584a2d0a34e791906c01015f2ae037aa3137b683d9
|
||||
ports:
|
||||
- "127.0.0.1:${PUBLIC_PORT:-8083}:8080"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api: {condition: service_healthy}
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
memswap_limit: 128m
|
||||
read_only: true
|
||||
tmpfs: ["/tmp:rw,nosuid,size=4m"]
|
||||
security_opt: [no-new-privileges:true]
|
||||
stop_grace_period: 10s
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
cache_password:
|
||||
file: ./secrets/cache_password
|
||||
cache_url:
|
||||
file: ./secrets/cache_url
|
||||
valkey_config:
|
||||
file: ./secrets/valkey.conf
|
||||
name: valkey.conf
|
||||
volumes:
|
||||
postgres_data:
|
||||
@@ -0,0 +1,36 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 512
|
||||
stats socket /tmp/admin.sock mode 600 level admin
|
||||
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 15s
|
||||
timeout server 8s
|
||||
timeout http-request 10s
|
||||
timeout http-keep-alive 5s
|
||||
retries 0
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend messenger
|
||||
|
||||
backend messenger
|
||||
balance roundrobin
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fastinter 250ms fall 2 rise 1 resolvers docker init-addr libc,none resolve-prefer ipv4
|
||||
server-template api 1-8 api:8080
|
||||
@@ -0,0 +1,36 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
position integer NOT NULL CHECK (position BETWEEN 0 AND 99),
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
UNIQUE (chat_id, position)
|
||||
);
|
||||
CREATE INDEX members_user_chat_idx ON members(user_id,chat_id);
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL,
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES members(chat_id,user_id)
|
||||
);
|
||||
CREATE INDEX messages_history_idx ON messages(chat_id,created_at,id);
|
||||
CREATE TABLE sessions (
|
||||
token_digest bytea PRIMARY KEY CHECK (octet_length(token_digest)=32),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
created_at timestamptz NOT NULL,
|
||||
expires_at timestamptz NOT NULL CHECK (expires_at > created_at)
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE idempotency (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
body_hash bytea NOT NULL CHECK (octet_length(body_hash)=32),
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY(user_id, chat_id, key)
|
||||
);
|
||||
CREATE INDEX idempotency_expiry_idx ON idempotency(expires_at);
|
||||
@@ -0,0 +1,31 @@
|
||||
[project]
|
||||
name = "mtusi-messenger-lab3"
|
||||
version = "1.0.0"
|
||||
requires-python = ">=3.14,<3.15"
|
||||
dependencies = [
|
||||
"fastapi==0.143.3", "pydantic==2.14.1", "uvicorn==0.54.0",
|
||||
"psycopg[binary,pool]==3.3.6", "argon2-cffi>=25,<26", "redis==8.1.0",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["pytest>=8,<10", "httpx>=0.28,<1", "ruff>=0.14,<1", "openapi-spec-validator==0.7.2", "psutil>=7,<8", "matplotlib>=3.10,<4"]
|
||||
|
||||
[tool.ruff]
|
||||
line-length = 79
|
||||
target-version = "py314"
|
||||
include = ["src/**/*.py", "student-tests/**/*.py", "research/**/*.py", "scripts/verify_*.py"]
|
||||
|
||||
[tool.ruff.lint]
|
||||
select = ["E", "F", "I", "B", "UP"]
|
||||
|
||||
[tool.ruff.per-file-target-version]
|
||||
"scripts/*.py" = "py39"
|
||||
|
||||
# SQL и генерируемые Markdown-таблицы в инструментах не разрываем.
|
||||
[tool.ruff.lint.per-file-ignores]
|
||||
"research/*.py" = ["E501"]
|
||||
"scripts/*.py" = ["E501"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["student-tests"]
|
||||
addopts = "-ra"
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Создаёт только отсутствующие локальные файлы; секреты не печатает."""
|
||||
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def create(path, value):
|
||||
try:
|
||||
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
|
||||
except FileExistsError:
|
||||
return
|
||||
with os.fdopen(fd, "w") as stream:
|
||||
stream.write(value + "\n")
|
||||
|
||||
|
||||
def main():
|
||||
directory = ROOT / "secrets"
|
||||
directory.mkdir(mode=0o700, exist_ok=True)
|
||||
pg = directory / "postgres_password"
|
||||
cache = directory / "cache_password"
|
||||
create(pg, secrets.token_urlsafe(32))
|
||||
create(cache, secrets.token_urlsafe(32))
|
||||
create(
|
||||
directory / "database_url",
|
||||
"postgresql://messenger:"
|
||||
+ pg.read_text().strip()
|
||||
+ "@postgres:5432/messenger",
|
||||
)
|
||||
create(
|
||||
directory / "cache_url",
|
||||
"redis://:" + cache.read_text().strip() + "@cache:6379/0",
|
||||
)
|
||||
create(
|
||||
directory / "valkey.conf",
|
||||
"bind 0.0.0.0\nprotected-mode yes\nport 6379\nmaxmemory 64mb\n"
|
||||
'maxmemory-policy allkeys-lru\nsave ""\nappendonly no\nrequirepass '
|
||||
+ cache.read_text().strip(),
|
||||
)
|
||||
create(ROOT / ".env", (ROOT / ".env.example").read_text().rstrip())
|
||||
print("Local configuration ready; existing files preserved")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Явные операторские действия только над API текущего Compose-проекта."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(args, cwd=ROOT, text=True).strip()
|
||||
|
||||
|
||||
def containers():
|
||||
ids = command("docker", "compose", "ps", "-q", "api").split()
|
||||
return json.loads(command("docker", "inspect", *ids)) if ids else []
|
||||
|
||||
|
||||
def alias(cid):
|
||||
code = (
|
||||
"import urllib.request; r=urllib.request.urlopen("
|
||||
"'http://127.0.0.1:8080/health/live',timeout=2); "
|
||||
"print(r.headers['X-Instance-Id'])"
|
||||
)
|
||||
return command("docker", "exec", cid, "python", "-c", code)
|
||||
|
||||
|
||||
def wait_count(count):
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
data = containers()
|
||||
if len(data) == count and all(
|
||||
c["State"].get("Health", {}).get("Status") == "healthy"
|
||||
for c in data
|
||||
):
|
||||
# DNS и проверки HAProxy должны успеть увидеть новый адрес.
|
||||
time.sleep(3)
|
||||
return
|
||||
time.sleep(0.5)
|
||||
raise TimeoutError("Replica readiness")
|
||||
|
||||
|
||||
def main():
|
||||
action = sys.argv[1]
|
||||
data = containers()
|
||||
if action == "stop":
|
||||
target = os.environ["TARGET_INSTANCE_ID"]
|
||||
matches = [c for c in data if alias(c["Id"]) == target]
|
||||
if len(matches) != 1 or len(data) < 2:
|
||||
raise RuntimeError("Target must identify one running replica")
|
||||
command("docker", "stop", "--time", "10", matches[0]["Id"])
|
||||
print("Selected API stopped")
|
||||
elif action == "rolling":
|
||||
count = len(data)
|
||||
if count < 2:
|
||||
raise RuntimeError("Need at least two replicas")
|
||||
# Сначала добавляется готовая реплика, затем удаляется одна старая.
|
||||
for old in data:
|
||||
command(
|
||||
"docker",
|
||||
"compose",
|
||||
"up",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--no-recreate",
|
||||
"--scale",
|
||||
f"api={count + 1}",
|
||||
"api",
|
||||
)
|
||||
wait_count(count + 1)
|
||||
command("docker", "stop", "--time", "10", old["Id"])
|
||||
command("docker", "rm", old["Id"])
|
||||
wait_count(count)
|
||||
print("Sequential replacement completed")
|
||||
else:
|
||||
raise ValueError("Unknown action")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
docker compose restart api postgres cache lb
|
||||
docker compose up -d --no-build --no-recreate --wait
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
exec python3 scripts/cluster.py rolling
|
||||
@@ -0,0 +1,137 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8083")
|
||||
|
||||
|
||||
def command(args, *, input=None):
|
||||
result = subprocess.run(
|
||||
args, cwd=ROOT, input=input, capture_output=True, text=True
|
||||
)
|
||||
if result.returncode:
|
||||
raise RuntimeError(
|
||||
"Command failed: "
|
||||
+ args[0]
|
||||
+ "; "
|
||||
+ result.stderr.replace(str(ROOT), "<repository>")
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def compose(*args):
|
||||
return command(["docker", "compose", *args])
|
||||
|
||||
|
||||
def sql(query, *, database="messenger"):
|
||||
return compose(
|
||||
"exec",
|
||||
"-T",
|
||||
"postgres",
|
||||
"psql",
|
||||
"-U",
|
||||
"messenger",
|
||||
"-d",
|
||||
database,
|
||||
"-A",
|
||||
"-t",
|
||||
"-v",
|
||||
"ON_ERROR_STOP=1",
|
||||
"-c",
|
||||
query,
|
||||
)
|
||||
|
||||
|
||||
def cache(command_name, *args):
|
||||
script = (
|
||||
'export REDISCLI_AUTH="$(cat /run/secrets/cache_password)"; '
|
||||
'exec valkey-cli "$@"'
|
||||
)
|
||||
return compose(
|
||||
"exec", "-T", "cache", "sh", "-c", script, "sh", command_name, *args
|
||||
)
|
||||
|
||||
|
||||
def ready(base=BASE, *, timeout=30):
|
||||
end = time.monotonic() + timeout
|
||||
with httpx.Client(base_url=base, trust_env=False, timeout=6) as client:
|
||||
while time.monotonic() < end:
|
||||
try:
|
||||
if client.get("/health/ready").status_code == 200:
|
||||
return
|
||||
except httpx.HTTPError:
|
||||
pass
|
||||
time.sleep(0.2)
|
||||
raise TimeoutError("API readiness")
|
||||
|
||||
|
||||
def actor(client):
|
||||
username = "verify_" + uuid4().hex[:20]
|
||||
password = "Synthetic-" + uuid4().hex + ":🌍"
|
||||
result = client.post(
|
||||
"/api/v1/users",
|
||||
json={
|
||||
"username": username,
|
||||
"display_name": "Synthetic",
|
||||
"password": password,
|
||||
},
|
||||
)
|
||||
assert result.status_code == 201
|
||||
user = result.json()
|
||||
session = client.post(
|
||||
"/api/v1/auth/sessions", auth=httpx.BasicAuth(username, password)
|
||||
)
|
||||
assert session.status_code == 201
|
||||
return user, password, session.json()
|
||||
|
||||
|
||||
def auth(session):
|
||||
return {"Authorization": "Bearer " + session["token"]}
|
||||
|
||||
|
||||
def extra_api(*, ttl=3600, pool_size=1):
|
||||
name = "mtusi-lab3-test-" + uuid4().hex[:10]
|
||||
compose(
|
||||
"run",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
"--name",
|
||||
name,
|
||||
"-e",
|
||||
"SESSION_TTL_SECONDS=" + str(ttl),
|
||||
"-e",
|
||||
"DB_POOL_SIZE=" + str(pool_size),
|
||||
"-p",
|
||||
"127.0.0.1::8080",
|
||||
"api",
|
||||
)
|
||||
try:
|
||||
info = json.loads(command(["docker", "inspect", name]))[0]
|
||||
base = (
|
||||
"http://127.0.0.1:"
|
||||
+ info["NetworkSettings"]["Ports"]["8080/tcp"][0]["HostPort"]
|
||||
)
|
||||
ready(base)
|
||||
return name, base
|
||||
except Exception:
|
||||
command(["docker", "rm", "-f", name])
|
||||
raise
|
||||
|
||||
|
||||
def instances():
|
||||
data = {}
|
||||
for role in ["api", "postgres", "cache"]:
|
||||
cid = compose("ps", "-q", role)
|
||||
info = json.loads(command(["docker", "inspect", cid]))[0]
|
||||
data[role] = {
|
||||
"container": cid[:12],
|
||||
"pid": info["State"]["Pid"],
|
||||
"started_at": info["State"]["StartedAt"],
|
||||
}
|
||||
return data
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
exec python3 scripts/cluster.py stop
|
||||
@@ -0,0 +1,292 @@
|
||||
"""Проверки реальных процессов, failover, rolling и общей идемпотентности."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
|
||||
from scripts import cluster
|
||||
from scripts.stand import BASE, ROOT, actor, auth, command, compose, ready
|
||||
|
||||
|
||||
def snapshot():
|
||||
ids = compose("ps", "-q").split()
|
||||
result = []
|
||||
for c in json.loads(command(["docker", "inspect", *ids])):
|
||||
role = c["Config"]["Labels"]["com.docker.compose.service"]
|
||||
entry = {
|
||||
"role": role,
|
||||
"container": c["Id"][:12],
|
||||
"pid": c["State"]["Pid"],
|
||||
"started_at": c["State"]["StartedAt"],
|
||||
"ports": c["HostConfig"]["PortBindings"],
|
||||
"cpu_nano": c["HostConfig"]["NanoCpus"],
|
||||
"memory_limit": c["HostConfig"]["Memory"],
|
||||
}
|
||||
if role == "api":
|
||||
entry["instance"] = cluster.alias(c["Id"])
|
||||
assert not entry["ports"]
|
||||
result.append(entry)
|
||||
return result
|
||||
|
||||
|
||||
def exercise(client, session, chat, action, *, seconds=15):
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
observations, confirmed, errors, reads = [], {}, 0, 0
|
||||
started = time.monotonic()
|
||||
finished = threading.Event()
|
||||
action_result = {}
|
||||
|
||||
def operate():
|
||||
time.sleep(0.5)
|
||||
try:
|
||||
action()
|
||||
except BaseException as exc:
|
||||
action_result["error"] = type(exc).__name__
|
||||
finally:
|
||||
action_result["finished"] = round(time.monotonic() - started, 3)
|
||||
finished.set()
|
||||
|
||||
thread = threading.Thread(target=operate)
|
||||
thread.start()
|
||||
key = uuid4().hex
|
||||
try:
|
||||
while time.monotonic() - started < seconds or not finished.is_set():
|
||||
h = {**auth(session), "Idempotency-Key": key}
|
||||
try:
|
||||
r = client.post(path, headers=h, json={"text": key})
|
||||
if r.status_code != 201:
|
||||
errors += 1
|
||||
continue
|
||||
m = r.json()
|
||||
confirmed[m["id"]] = m
|
||||
observations.append(
|
||||
{
|
||||
"at": round(time.monotonic() - started, 3),
|
||||
"instance": r.headers["x-instance-id"],
|
||||
}
|
||||
)
|
||||
key = uuid4().hex
|
||||
read = client.get(
|
||||
path, headers=auth(session), params={"limit": 100}
|
||||
)
|
||||
reads += read.status_code == 200
|
||||
errors += read.status_code != 200
|
||||
except httpx.HTTPError:
|
||||
errors += 1
|
||||
time.sleep(0.04)
|
||||
finally:
|
||||
thread.join(timeout=90)
|
||||
assert not action_result.get("error"), action_result
|
||||
found, cursor = [], None
|
||||
while True:
|
||||
r = client.get(
|
||||
path,
|
||||
headers=auth(session),
|
||||
params={"limit": 100, **({"cursor": cursor} if cursor else {})},
|
||||
)
|
||||
assert r.status_code == 200
|
||||
page = r.json()
|
||||
found.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if not cursor:
|
||||
break
|
||||
by_id = {m["id"]: m for m in found}
|
||||
assert all(by_id.get(k) == v for k, v in confirmed.items())
|
||||
texts = [m["text"] for m in found]
|
||||
assert len(texts) == len(set(texts))
|
||||
recovery = next(
|
||||
(
|
||||
x["at"]
|
||||
for x in observations
|
||||
if x["at"] >= action_result["finished"]
|
||||
),
|
||||
None,
|
||||
)
|
||||
assert recovery is not None and (seconds > 20 or recovery <= 10)
|
||||
elapsed = time.monotonic() - started
|
||||
return {
|
||||
"confirmed_writes": len(confirmed),
|
||||
"successful_reads": reads,
|
||||
"errors": errors,
|
||||
"successful_rps": round((len(confirmed) + reads) / elapsed, 2),
|
||||
"first_success_after_action_seconds": recovery,
|
||||
"action": action_result,
|
||||
"observations": observations,
|
||||
"confirmed_preserved": True,
|
||||
"duplicate_texts": False,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--output", default="evidence/cluster-repeat.json")
|
||||
args = parser.parse_args()
|
||||
report = {
|
||||
"date": datetime.now(timezone.utc).isoformat(),
|
||||
"source_commit": command(["git", "rev-parse", "HEAD"]),
|
||||
"complete": False,
|
||||
}
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
ready()
|
||||
initial = snapshot()
|
||||
apis = [c for c in initial if c["role"] == "api"]
|
||||
assert len(apis) == 2 and len({c["pid"] for c in apis}) == 2
|
||||
report["before"] = initial
|
||||
with httpx.Client(base_url=BASE, trust_env=False, timeout=8) as client:
|
||||
user, _, session = actor(client)
|
||||
r = client.post(
|
||||
"/api/v1/chats",
|
||||
headers=auth(session),
|
||||
json={
|
||||
"title": "Cluster verification",
|
||||
"member_ids": [user["id"]],
|
||||
},
|
||||
)
|
||||
assert r.status_code == 201
|
||||
chat = r.json()
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
key = uuid4().hex
|
||||
|
||||
def repeat(_):
|
||||
with httpx.Client(
|
||||
base_url=BASE, trust_env=False, timeout=8
|
||||
) as c:
|
||||
r = c.post(
|
||||
path,
|
||||
headers={**auth(session), "Idempotency-Key": key},
|
||||
json={"text": "Same concurrent request"},
|
||||
)
|
||||
assert r.status_code == 201
|
||||
return r.json(), r.headers["x-instance-id"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
repeated = list(pool.map(repeat, range(30)))
|
||||
assert len({m[0]["id"] for m in repeated}) == 1
|
||||
assert len({m[1] for m in repeated}) == 2
|
||||
report["parallel_replay"] = {
|
||||
"requests": 30,
|
||||
"messages": 1,
|
||||
"instances": sorted({m[1] for m in repeated}),
|
||||
}
|
||||
# Отдельный чат каждой серии: проверки дубликатов не смешивают тексты.
|
||||
report["failovers"] = []
|
||||
for api in apis:
|
||||
|
||||
def stop(target=api["instance"]):
|
||||
env = {**os.environ, "TARGET_INSTANCE_ID": target}
|
||||
subprocess.run(
|
||||
["scripts/stop-one.sh"],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
r = client.post(
|
||||
"/api/v1/chats",
|
||||
headers=auth(session),
|
||||
json={"title": "Failover", "member_ids": [user["id"]]},
|
||||
)
|
||||
result = exercise(client, session, r.json(), stop)
|
||||
assert api["instance"] not in [
|
||||
o["instance"] for o in result["observations"][-5:]
|
||||
]
|
||||
assert (
|
||||
command(
|
||||
[
|
||||
"docker",
|
||||
"inspect",
|
||||
"-f",
|
||||
"{{.State.Running}}",
|
||||
api["container"],
|
||||
]
|
||||
)
|
||||
== "false"
|
||||
)
|
||||
result["stopped"] = api
|
||||
report["failovers"].append(result)
|
||||
compose("up", "-d", "--no-build", "--wait")
|
||||
ready()
|
||||
r = client.post(
|
||||
"/api/v1/chats",
|
||||
headers=auth(session),
|
||||
json={"title": "Rolling", "member_ids": [user["id"]]},
|
||||
)
|
||||
before_roll = snapshot()
|
||||
report["rolling"] = exercise(
|
||||
client,
|
||||
session,
|
||||
r.json(),
|
||||
lambda: command(["scripts/rolling-update.sh"]),
|
||||
seconds=45,
|
||||
)
|
||||
after_roll = snapshot()
|
||||
assert not (
|
||||
{c["container"] for c in before_roll if c["role"] == "api"}
|
||||
& {c["container"] for c in after_roll if c["role"] == "api"}
|
||||
)
|
||||
report["rolling"]["before"] = before_roll
|
||||
report["rolling"]["after"] = after_roll
|
||||
replay = repeat(0)[0]
|
||||
assert replay == repeated[0][0]
|
||||
report["replay_after_replacement"] = True
|
||||
before = snapshot()
|
||||
env = {
|
||||
**os.environ,
|
||||
"NO_PROXY": "localhost,127.0.0.1",
|
||||
"no_proxy": "localhost,127.0.0.1",
|
||||
}
|
||||
for mode, hook in [
|
||||
("failover", "scripts/stop-one.sh"),
|
||||
("persistence", "scripts/restart.sh"),
|
||||
]:
|
||||
if mode == "persistence":
|
||||
compose("up", "-d", "--no-build", "--wait")
|
||||
ready()
|
||||
before = snapshot()
|
||||
run = subprocess.run(
|
||||
[
|
||||
".venv/bin/python",
|
||||
"tests/resilience.py",
|
||||
"--mode",
|
||||
mode,
|
||||
"--action-script",
|
||||
hook,
|
||||
"--base-url",
|
||||
BASE,
|
||||
],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
assert run.returncode == 0, run.stdout + run.stderr
|
||||
report["public_" + mode] = run.stdout.strip()
|
||||
after = snapshot()
|
||||
for role in ["postgres", "cache", "lb"]:
|
||||
old = next(c for c in before if c["role"] == role)
|
||||
new = next(c for c in after if c["role"] == role)
|
||||
assert old["started_at"] != new["started_at"]
|
||||
report["after_persistence"] = after
|
||||
report["complete"] = True
|
||||
finally:
|
||||
output.write_text(
|
||||
json.dumps(report, ensure_ascii=False, indent=2) + "\n"
|
||||
)
|
||||
print("PASS: process failover, rolling, replay and full persistence")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,44 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
|
||||
|
||||
def early_stop(signum, _frame):
|
||||
print(
|
||||
json.dumps({"event": "stopped_before_startup", "signal": signum}),
|
||||
flush=True,
|
||||
)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
signal.signal(signal.SIGTERM, early_stop)
|
||||
signal.signal(signal.SIGINT, early_stop)
|
||||
|
||||
import uvicorn # noqa: E402
|
||||
|
||||
from src.logging_config import JsonFormatter # noqa: E402
|
||||
|
||||
|
||||
def main() -> None:
|
||||
host = os.environ.get("APP_HOST", "127.0.0.1")
|
||||
port = int(os.environ.get("APP_PORT", "8080"))
|
||||
if not 1 <= port <= 65535:
|
||||
raise ValueError("APP_PORT must be between 1 and 65535")
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(JsonFormatter())
|
||||
logging.basicConfig(level=logging.INFO, handlers=[handler], force=True)
|
||||
uvicorn.run(
|
||||
"src.api:app",
|
||||
host=host,
|
||||
port=port,
|
||||
workers=1,
|
||||
access_log=False,
|
||||
timeout_graceful_shutdown=5,
|
||||
log_config=None,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+301
@@ -0,0 +1,301 @@
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from anyio import to_thread
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from pydantic import ValidationError
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import JSONResponse, Response
|
||||
|
||||
from src.cache import UserCache
|
||||
from src.cursor import parse_uuid
|
||||
from src.database import Database
|
||||
from src.domain import timestamp
|
||||
from src.errors import DomainError, invalid
|
||||
from src.password import Passwords
|
||||
from src.repository import PostgresRepository
|
||||
from src.schemas import CreateChat, CreateMessage, CreateUser, Input
|
||||
from src.service import MessengerService
|
||||
from src.settings import Settings
|
||||
|
||||
logger = logging.getLogger("messenger")
|
||||
INSTANCE_ID = uuid4().hex
|
||||
|
||||
|
||||
def response(data: object, *, status: int = 200) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
jsonable_encoder(
|
||||
data, custom_encoder={datetime: timestamp, UUID: str}
|
||||
),
|
||||
status_code=status,
|
||||
)
|
||||
|
||||
|
||||
async def body(request: Request, model: type[Input]) -> Input:
|
||||
try:
|
||||
data = await request.body()
|
||||
return model.model_validate(
|
||||
json.loads(
|
||||
data.decode("utf-8"),
|
||||
parse_constant=lambda _: (_ for _ in ()).throw(ValueError()),
|
||||
)
|
||||
)
|
||||
except ValueError, ValidationError, UnicodeError, RecursionError:
|
||||
raise invalid() from None
|
||||
|
||||
|
||||
def pagination(request: Request) -> tuple[int, str | None]:
|
||||
limits = request.query_params.getlist("limit")
|
||||
cursors = request.query_params.getlist("cursor")
|
||||
if len(limits) > 1 or len(cursors) > 1:
|
||||
raise invalid()
|
||||
raw = limits[0] if limits else "50"
|
||||
if not raw.isascii() or not raw.isdecimal():
|
||||
raise invalid()
|
||||
raw = raw.lstrip("0") or "0"
|
||||
if len(raw) > 3:
|
||||
raise invalid()
|
||||
limit = int(raw)
|
||||
if not 1 <= limit <= 100:
|
||||
raise invalid()
|
||||
return limit, cursors[0] if cursors else None
|
||||
|
||||
|
||||
def service(request: Request) -> MessengerService:
|
||||
return request.app.state.service
|
||||
|
||||
|
||||
async def run_io(function, **kwargs):
|
||||
return await to_thread.run_sync(partial(function, **kwargs))
|
||||
|
||||
|
||||
async def actor(request: Request):
|
||||
return await run_io(
|
||||
service(request).identify, value=request.headers.get("authorization")
|
||||
)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
settings = Settings.load()
|
||||
database = Database(url=settings.database_url, size=settings.pool_size)
|
||||
cache = UserCache(url=settings.cache_url)
|
||||
try:
|
||||
await run_io(database.open)
|
||||
passwords = await run_io(Passwords)
|
||||
app.state.database = database
|
||||
app.state.service = MessengerService(
|
||||
repository=PostgresRepository(database=database, cache=cache),
|
||||
passwords=passwords,
|
||||
session_ttl=settings.session_ttl,
|
||||
)
|
||||
app.state.ready = True
|
||||
yield
|
||||
finally:
|
||||
app.state.ready = False
|
||||
await run_io(cache.close)
|
||||
await run_io(database.close)
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_context(request: Request, call_next):
|
||||
request_id = str(uuid4())
|
||||
request.state.request_id = request_id
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
result = await call_next(request)
|
||||
except Exception:
|
||||
logger.error(
|
||||
json.dumps({"event": "internal_error", "request_id": request_id})
|
||||
)
|
||||
result = response(
|
||||
{
|
||||
"error": {
|
||||
"code": "internal_error",
|
||||
"message": "Internal error",
|
||||
"request_id": request_id,
|
||||
}
|
||||
},
|
||||
status=500,
|
||||
)
|
||||
result.headers["X-Request-Id"] = request_id
|
||||
result.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
route = request.scope.get("route")
|
||||
logger.info(
|
||||
json.dumps(
|
||||
{
|
||||
"event": "request",
|
||||
"request_id": request_id,
|
||||
"method": request.method,
|
||||
"route": getattr(route, "path", "unmatched"),
|
||||
"status": result.status_code,
|
||||
"elapsed_ms": round((time.perf_counter() - started) * 1000, 3),
|
||||
}
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@app.exception_handler(DomainError)
|
||||
async def domain_error(request: Request, error: DomainError):
|
||||
result = response(
|
||||
{
|
||||
"error": {
|
||||
"code": error.code,
|
||||
"message": error.message,
|
||||
"request_id": request.state.request_id,
|
||||
}
|
||||
},
|
||||
status=error.status,
|
||||
)
|
||||
result.headers.update(error.headers)
|
||||
return result
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_error(request: Request, error: HTTPException):
|
||||
code = "not_found" if error.status_code == 404 else "invalid_request"
|
||||
return response(
|
||||
{
|
||||
"error": {
|
||||
"code": code,
|
||||
"message": "Resource not found"
|
||||
if error.status_code == 404
|
||||
else "Invalid request",
|
||||
"request_id": request.state.request_id,
|
||||
}
|
||||
},
|
||||
status=error.status_code,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/health/live")
|
||||
async def live():
|
||||
return response({"status": "ok"})
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def ready():
|
||||
if not getattr(app.state, "ready", False):
|
||||
raise DomainError(503, "unavailable", "Not ready")
|
||||
await run_io(app.state.database.ping)
|
||||
return response({"status": "ok"})
|
||||
|
||||
|
||||
@app.post("/api/v1/users")
|
||||
async def register(request: Request):
|
||||
data = await body(request, CreateUser)
|
||||
return response(
|
||||
await run_io(service(request).register, **data.model_dump()),
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/users/me")
|
||||
async def current(request: Request):
|
||||
return response(await actor(request))
|
||||
|
||||
|
||||
@app.post("/api/v1/chats")
|
||||
async def create_chat(request: Request):
|
||||
user = await actor(request)
|
||||
data = await body(request, CreateChat)
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).create_chat, actor_id=user.id, **data.model_dump()
|
||||
),
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats")
|
||||
async def list_chats(request: Request):
|
||||
user = await actor(request)
|
||||
limit, cursor = pagination(request)
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).chats,
|
||||
actor_id=user.id,
|
||||
limit=limit,
|
||||
cursor=cursor,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}")
|
||||
async def get_chat(request: Request, chat_id: str):
|
||||
user = await actor(request)
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).get_chat,
|
||||
actor_id=user.id,
|
||||
chat_id=parse_uuid(chat_id),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/chats/{chat_id}/messages")
|
||||
async def send(request: Request, chat_id: str):
|
||||
user = await actor(request)
|
||||
cid = parse_uuid(chat_id)
|
||||
await run_io(service(request).get_chat, actor_id=user.id, chat_id=cid)
|
||||
data = await body(request, CreateMessage)
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).send,
|
||||
actor_id=user.id,
|
||||
chat_id=cid,
|
||||
idempotency_key=request.headers.get("idempotency-key"),
|
||||
**data.model_dump(),
|
||||
),
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/chats/{chat_id}/messages")
|
||||
async def history(request: Request, chat_id: str):
|
||||
user = await actor(request)
|
||||
cid = parse_uuid(chat_id)
|
||||
await run_io(service(request).get_chat, actor_id=user.id, chat_id=cid)
|
||||
limit, cursor = pagination(request)
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).history,
|
||||
actor_id=user.id,
|
||||
chat_id=cid,
|
||||
limit=limit,
|
||||
cursor=cursor,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/auth/sessions")
|
||||
async def login(request: Request):
|
||||
return response(
|
||||
await run_io(
|
||||
service(request).login,
|
||||
authorization=request.headers.get("authorization"),
|
||||
),
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
@app.delete("/api/v1/auth/sessions/current")
|
||||
async def logout(request: Request):
|
||||
await run_io(
|
||||
service(request).logout,
|
||||
authorization=request.headers.get("authorization"),
|
||||
)
|
||||
return Response(status_code=204)
|
||||
@@ -0,0 +1,66 @@
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
import redis
|
||||
from redis.backoff import NoBackoff
|
||||
from redis.retry import Retry
|
||||
|
||||
from src.domain import User, timestamp
|
||||
|
||||
|
||||
class UserCache:
|
||||
def __init__(self, *, url: str):
|
||||
self.client = redis.Redis.from_url(
|
||||
url,
|
||||
socket_connect_timeout=0.15,
|
||||
socket_timeout=0.15,
|
||||
max_connections=16,
|
||||
retry=Retry(NoBackoff(), 0),
|
||||
decode_responses=True,
|
||||
)
|
||||
self.disabled_until = 0.0
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def close(self):
|
||||
self.client.close()
|
||||
|
||||
def _command(self, function, *args, **kwargs):
|
||||
if time.monotonic() < self.disabled_until:
|
||||
return None
|
||||
try:
|
||||
return function(*args, **kwargs)
|
||||
except redis.RedisError, OSError:
|
||||
with self.lock:
|
||||
self.disabled_until = time.monotonic() + 5
|
||||
return None
|
||||
|
||||
def get(self, *, user_id: UUID) -> User | None:
|
||||
raw = self._command(self.client.get, "user:" + str(user_id))
|
||||
if raw is None:
|
||||
return None
|
||||
try:
|
||||
row = json.loads(raw)
|
||||
if UUID(row["id"]) != user_id:
|
||||
return None
|
||||
return User(
|
||||
user_id,
|
||||
row["username"],
|
||||
row["display_name"],
|
||||
datetime.fromisoformat(row["created_at"]),
|
||||
)
|
||||
except ValueError, TypeError, KeyError:
|
||||
return None
|
||||
|
||||
def put(self, *, user: User):
|
||||
raw = json.dumps(
|
||||
{
|
||||
"id": str(user.id),
|
||||
"username": user.username,
|
||||
"display_name": user.display_name,
|
||||
"created_at": timestamp(user.created_at),
|
||||
}
|
||||
)
|
||||
self._command(self.client.set, "user:" + str(user.id), raw, ex=30)
|
||||
@@ -0,0 +1,64 @@
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from src.domain import Key, timestamp
|
||||
from src.errors import invalid
|
||||
|
||||
|
||||
def parse_uuid(value: str) -> UUID:
|
||||
if not isinstance(value, str) or not re.fullmatch(
|
||||
r"[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}", value
|
||||
):
|
||||
raise invalid()
|
||||
return UUID(value)
|
||||
|
||||
|
||||
def encode_cursor(*, scope: str, key: Key) -> str:
|
||||
payload = {
|
||||
"v": 1,
|
||||
"scope": scope,
|
||||
"at": timestamp(key[0]),
|
||||
"id": str(key[1]),
|
||||
}
|
||||
return (
|
||||
base64.urlsafe_b64encode(
|
||||
json.dumps(payload, separators=(",", ":")).encode()
|
||||
)
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
|
||||
|
||||
def decode_cursor(*, value: str | None, scope: str) -> Key | None:
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
if (
|
||||
not value
|
||||
or len(value) > 2048
|
||||
or not re.fullmatch(r"[A-Za-z0-9_-]+", value)
|
||||
):
|
||||
raise ValueError
|
||||
data = json.loads(
|
||||
base64.b64decode(
|
||||
value + "=" * (-len(value) % 4), altchars=b"-_", validate=True
|
||||
)
|
||||
)
|
||||
if (
|
||||
not isinstance(data, dict)
|
||||
or type(data.get("v")) is not int
|
||||
or data["v"] != 1
|
||||
or data.get("scope") != scope
|
||||
or not isinstance(data.get("at"), str)
|
||||
):
|
||||
raise ValueError
|
||||
at = datetime.fromisoformat(data["at"])
|
||||
if at.tzinfo is None or timestamp(at) != data["at"]:
|
||||
raise ValueError
|
||||
return at, parse_uuid(data["id"])
|
||||
except ValueError, TypeError, KeyError, binascii.Error, RecursionError:
|
||||
raise invalid() from None
|
||||
@@ -0,0 +1,69 @@
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
|
||||
import psycopg
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool, PoolTimeout
|
||||
|
||||
from src.errors import DomainError
|
||||
|
||||
|
||||
class DeadlineConnection(psycopg.Connection):
|
||||
"""Клиентский срок действует и при замороженном PostgreSQL."""
|
||||
|
||||
def wait(self, gen, interval=0.1, timeout=None):
|
||||
deadline = getattr(self, "request_deadline", None)
|
||||
remaining = (
|
||||
max(0.001, deadline - time.monotonic()) if deadline else 3.0
|
||||
)
|
||||
bound = min(remaining, timeout) if timeout is not None else remaining
|
||||
try:
|
||||
return super().wait(gen, interval=interval, timeout=bound)
|
||||
except psycopg.OperationalError:
|
||||
self.close()
|
||||
raise
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self, *, url: str, size: int):
|
||||
self.pool = ConnectionPool(
|
||||
url,
|
||||
connection_class=DeadlineConnection,
|
||||
kwargs={
|
||||
"autocommit": True,
|
||||
"row_factory": dict_row,
|
||||
"connect_timeout": 2,
|
||||
"options": (
|
||||
"-c statement_timeout=2500 -c lock_timeout=1000 "
|
||||
"-c idle_in_transaction_session_timeout=6000"
|
||||
),
|
||||
},
|
||||
min_size=1,
|
||||
max_size=size,
|
||||
timeout=1,
|
||||
open=False,
|
||||
)
|
||||
|
||||
def open(self):
|
||||
self.pool.open(wait=True, timeout=10)
|
||||
|
||||
def close(self):
|
||||
self.pool.close(timeout=2)
|
||||
|
||||
@contextmanager
|
||||
def connection(self):
|
||||
try:
|
||||
with self.pool.connection(timeout=1) as conn:
|
||||
conn.request_deadline = time.monotonic() + 4
|
||||
try:
|
||||
yield conn
|
||||
finally:
|
||||
conn.request_deadline = None
|
||||
except psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout:
|
||||
raise DomainError(
|
||||
503, "unavailable", "Database unavailable"
|
||||
) from None
|
||||
|
||||
def ping(self):
|
||||
with self.connection() as conn:
|
||||
conn.execute("SELECT 1").fetchone()
|
||||
@@ -0,0 +1,44 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
def timestamp(value: datetime) -> str:
|
||||
return value.isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class User:
|
||||
id: UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Chat:
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: tuple[UUID, ...]
|
||||
created_at: datetime
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Message:
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: datetime
|
||||
|
||||
|
||||
type Record = User | Chat | Message
|
||||
type Key = tuple[datetime, UUID]
|
||||
|
||||
|
||||
def order_key(record: Record) -> Key:
|
||||
return record.created_at, record.id
|
||||
@@ -0,0 +1,16 @@
|
||||
class DomainError(Exception):
|
||||
def __init__(
|
||||
self, status: int, code: str, message: str, headers=None
|
||||
) -> None:
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers or {}
|
||||
|
||||
|
||||
def invalid() -> DomainError:
|
||||
return DomainError(400, "invalid_request", "Invalid request")
|
||||
|
||||
|
||||
def not_found() -> DomainError:
|
||||
return DomainError(404, "not_found", "Resource not found")
|
||||
@@ -0,0 +1,17 @@
|
||||
import json
|
||||
import logging
|
||||
from datetime import UTC, datetime
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
message = record.getMessage()
|
||||
try:
|
||||
event = json.loads(message)
|
||||
if not isinstance(event, dict):
|
||||
event = {"event": message}
|
||||
except ValueError:
|
||||
event = {"event": message}
|
||||
event["timestamp"] = datetime.now(UTC).isoformat()
|
||||
event["level"] = record.levelname.lower()
|
||||
return json.dumps(event, ensure_ascii=False)
|
||||
@@ -0,0 +1,51 @@
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import psycopg
|
||||
|
||||
from src.settings import secret
|
||||
|
||||
|
||||
def main():
|
||||
with psycopg.connect(
|
||||
secret("DATABASE_URL"), autocommit=True, connect_timeout=3
|
||||
) as conn:
|
||||
conn.execute("SELECT pg_advisory_lock(2301002)")
|
||||
try:
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS schema_migrations "
|
||||
"(name text PRIMARY KEY, checksum text NOT NULL, "
|
||||
"applied_at timestamptz NOT NULL DEFAULT now())"
|
||||
)
|
||||
for path in sorted(Path("migrations").glob("*.sql")):
|
||||
content = path.read_bytes()
|
||||
checksum = hashlib.sha256(content).hexdigest()
|
||||
row = conn.execute(
|
||||
"SELECT checksum FROM schema_migrations WHERE name=%s",
|
||||
(path.name,),
|
||||
).fetchone()
|
||||
if row:
|
||||
if row[0] != checksum:
|
||||
raise RuntimeError(
|
||||
"Applied migration checksum mismatch"
|
||||
)
|
||||
continue
|
||||
with conn.transaction():
|
||||
conn.execute(content.decode())
|
||||
conn.execute(
|
||||
"INSERT INTO schema_migrations(name,checksum) "
|
||||
"VALUES(%s,%s)",
|
||||
(path.name, checksum),
|
||||
)
|
||||
print(
|
||||
json.dumps(
|
||||
{"event": "migration_applied", "name": path.name}
|
||||
)
|
||||
)
|
||||
finally:
|
||||
conn.execute("SELECT pg_advisory_unlock(2301002)")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,36 @@
|
||||
import threading
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
from argon2.low_level import Type
|
||||
|
||||
from src.errors import DomainError
|
||||
|
||||
|
||||
class Passwords:
|
||||
def __init__(self):
|
||||
self.hasher = PasswordHasher(
|
||||
time_cost=3, memory_cost=65536, parallelism=4, type=Type.ID
|
||||
)
|
||||
self.slots = threading.BoundedSemaphore(2)
|
||||
self.dummy = self.hasher.hash("Synthetic-dummy-password-not-for-login")
|
||||
|
||||
def _run(self, function, *args):
|
||||
if not self.slots.acquire(timeout=5):
|
||||
raise DomainError(503, "unavailable", "Authentication busy")
|
||||
try:
|
||||
return function(*args)
|
||||
finally:
|
||||
self.slots.release()
|
||||
|
||||
def hash(self, *, password: str) -> str:
|
||||
return self._run(self.hasher.hash, password)
|
||||
|
||||
def verify(self, *, password: str, encoded: str | None) -> bool:
|
||||
try:
|
||||
return (
|
||||
self._run(self.hasher.verify, encoded or self.dummy, password)
|
||||
and encoded is not None
|
||||
)
|
||||
except VerificationError, InvalidHashError:
|
||||
return False
|
||||
@@ -0,0 +1,251 @@
|
||||
import hashlib
|
||||
import json
|
||||
from uuid import UUID
|
||||
|
||||
from psycopg.errors import ForeignKeyViolation, UniqueViolation
|
||||
|
||||
from src.cache import UserCache
|
||||
from src.database import Database
|
||||
from src.domain import Chat, Key, Message, User
|
||||
from src.errors import DomainError, invalid, not_found
|
||||
|
||||
USER_COLUMNS = "id, username, display_name, created_at"
|
||||
|
||||
|
||||
def user_dto(row) -> User:
|
||||
return User(
|
||||
row["id"], row["username"], row["display_name"], row["created_at"]
|
||||
)
|
||||
|
||||
|
||||
def chat_dto(row) -> Chat:
|
||||
return Chat(
|
||||
row["id"], row["title"], tuple(row["member_ids"]), row["created_at"]
|
||||
)
|
||||
|
||||
|
||||
def message_dto(row) -> Message:
|
||||
return Message(
|
||||
row["id"],
|
||||
row["chat_id"],
|
||||
row["sender_id"],
|
||||
row["text"],
|
||||
row["created_at"],
|
||||
)
|
||||
|
||||
|
||||
class PostgresRepository:
|
||||
def __init__(self, *, database: Database, cache: UserCache):
|
||||
self.database = database
|
||||
self.cache = cache
|
||||
|
||||
def add_user(self, *, user: User, password_hash: str) -> User:
|
||||
try:
|
||||
with self.database.connection() as conn:
|
||||
conn.execute(
|
||||
"INSERT INTO users(id,username,display_name,"
|
||||
"password_hash,created_at) VALUES(%s,%s,%s,%s,%s)",
|
||||
(
|
||||
user.id,
|
||||
user.username,
|
||||
user.display_name,
|
||||
password_hash,
|
||||
user.created_at,
|
||||
),
|
||||
)
|
||||
except UniqueViolation:
|
||||
raise DomainError(
|
||||
409, "conflict", "Username already exists"
|
||||
) from None
|
||||
return user
|
||||
|
||||
def get_user(self, *, user_id: UUID) -> User | None:
|
||||
cached = self.cache.get(user_id=user_id)
|
||||
if cached is not None:
|
||||
return cached
|
||||
with self.database.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT " + USER_COLUMNS + " FROM users WHERE id=%s",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
user = user_dto(row)
|
||||
self.cache.put(user=user)
|
||||
return user
|
||||
|
||||
def credentials(self, *, username: str):
|
||||
with self.database.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT "
|
||||
+ USER_COLUMNS
|
||||
+ ", password_hash FROM users WHERE username=%s",
|
||||
(username,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None, None
|
||||
return user_dto(row), row["password_hash"]
|
||||
|
||||
def add_session(
|
||||
self, *, digest: bytes, user_id: UUID, created_at, expires_at
|
||||
):
|
||||
with self.database.connection() as conn:
|
||||
conn.execute(
|
||||
"INSERT INTO sessions(token_digest,user_id,created_at,"
|
||||
"expires_at) VALUES(%s,%s,%s,%s)",
|
||||
(digest, user_id, created_at, expires_at),
|
||||
)
|
||||
|
||||
def session_user(self, *, digest: bytes) -> UUID | None:
|
||||
with self.database.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT user_id FROM sessions WHERE token_digest=%s "
|
||||
"AND expires_at>clock_timestamp()",
|
||||
(digest,),
|
||||
).fetchone()
|
||||
return row["user_id"] if row else None
|
||||
|
||||
def delete_session(self, *, digest: bytes):
|
||||
with self.database.connection() as conn:
|
||||
conn.execute(
|
||||
"DELETE FROM sessions WHERE token_digest=%s", (digest,)
|
||||
)
|
||||
|
||||
def add_chat(self, *, chat: Chat, actor_id: UUID) -> Chat:
|
||||
members = chat.member_ids
|
||||
if actor_id not in members or len(set(members)) != len(members):
|
||||
raise invalid()
|
||||
try:
|
||||
with self.database.connection() as conn, conn.transaction():
|
||||
conn.execute(
|
||||
"INSERT INTO chats(id,title,created_at) VALUES(%s,%s,%s)",
|
||||
(chat.id, chat.title, chat.created_at),
|
||||
)
|
||||
with conn.cursor() as cursor:
|
||||
cursor.executemany(
|
||||
"INSERT INTO members(chat_id,user_id,position) "
|
||||
"VALUES(%s,%s,%s)",
|
||||
[
|
||||
(chat.id, user_id, position)
|
||||
for position, user_id in enumerate(members)
|
||||
],
|
||||
)
|
||||
except ForeignKeyViolation:
|
||||
raise invalid() from None
|
||||
return chat
|
||||
|
||||
def get_chat(self, *, chat_id: UUID, actor_id: UUID) -> Chat:
|
||||
with self.database.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT c.id,c.title,c.created_at,"
|
||||
"array_agg(m.user_id ORDER BY m.position) member_ids "
|
||||
"FROM chats c JOIN members m ON m.chat_id=c.id "
|
||||
"WHERE c.id=%s AND EXISTS(SELECT 1 FROM members own "
|
||||
"WHERE own.chat_id=c.id AND own.user_id=%s) GROUP BY c.id",
|
||||
(chat_id, actor_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
return chat_dto(row)
|
||||
|
||||
def add_message(
|
||||
self, *, message: Message, idempotency_key: str
|
||||
) -> Message:
|
||||
# Область включает пользователя, метод и нормализованный путь чата.
|
||||
scope = f"{message.sender_id}:POST:{message.chat_id}:{idempotency_key}"
|
||||
lock = int.from_bytes(
|
||||
hashlib.sha256(scope.encode()).digest()[:8], "big", signed=True
|
||||
)
|
||||
fingerprint = hashlib.sha256(
|
||||
json.dumps(
|
||||
{"text": message.text},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
).encode()
|
||||
).digest()
|
||||
try:
|
||||
with self.database.connection() as conn, conn.transaction():
|
||||
conn.execute("SELECT pg_advisory_xact_lock(%s)", (lock,))
|
||||
prior = conn.execute(
|
||||
"SELECT i.body_hash,i.message_id,"
|
||||
"i.expires_at>clock_timestamp() active "
|
||||
"FROM idempotency i WHERE user_id=%s AND chat_id=%s "
|
||||
"AND key=%s FOR UPDATE",
|
||||
(message.sender_id, message.chat_id, idempotency_key),
|
||||
).fetchone()
|
||||
if prior and prior["active"]:
|
||||
if bytes(prior["body_hash"]) != fingerprint:
|
||||
raise DomainError(409, "conflict", "Key already used")
|
||||
row = conn.execute(
|
||||
"SELECT * FROM messages WHERE id=%s",
|
||||
(prior["message_id"],),
|
||||
).fetchone()
|
||||
return message_dto(row)
|
||||
conn.execute(
|
||||
"INSERT INTO messages(id,chat_id,sender_id,"
|
||||
"text,created_at) "
|
||||
"VALUES(%s,%s,%s,%s,%s)",
|
||||
(
|
||||
message.id,
|
||||
message.chat_id,
|
||||
message.sender_id,
|
||||
message.text,
|
||||
message.created_at,
|
||||
),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO idempotency(user_id,chat_id,key,body_hash,"
|
||||
"message_id,expires_at) VALUES(%s,%s,%s,%s,%s,"
|
||||
"clock_timestamp()+interval '24 hours') "
|
||||
"ON CONFLICT(user_id,chat_id,key) DO UPDATE SET "
|
||||
"body_hash=excluded.body_hash,message_id=excluded.message_id,"
|
||||
"expires_at=excluded.expires_at",
|
||||
(
|
||||
message.sender_id,
|
||||
message.chat_id,
|
||||
idempotency_key,
|
||||
fingerprint,
|
||||
message.id,
|
||||
),
|
||||
)
|
||||
except ForeignKeyViolation:
|
||||
raise not_found() from None
|
||||
return message
|
||||
|
||||
def list_chats(
|
||||
self, *, actor_id: UUID, after: Key | None, limit: int
|
||||
) -> list[Chat]:
|
||||
condition = "AND (c.created_at,c.id)>(%s,%s)" if after else ""
|
||||
parameters = (
|
||||
[actor_id, *after, limit + 1] if after else [actor_id, limit + 1]
|
||||
)
|
||||
with self.database.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id "
|
||||
"FROM members WHERE chat_id=c.id "
|
||||
"ORDER BY position) member_ids "
|
||||
"FROM chats c JOIN members own ON own.chat_id=c.id "
|
||||
"WHERE own.user_id=%s "
|
||||
+ condition
|
||||
+ " ORDER BY c.created_at,c.id LIMIT %s",
|
||||
parameters,
|
||||
).fetchall()
|
||||
return [chat_dto(row) for row in rows]
|
||||
|
||||
def list_messages(
|
||||
self, *, chat_id: UUID, actor_id: UUID, after: Key | None, limit: int
|
||||
) -> list[Message]:
|
||||
condition = "AND (created_at,id)>(%s,%s)" if after else ""
|
||||
parameters = (
|
||||
[chat_id, *after, limit + 1] if after else [chat_id, limit + 1]
|
||||
)
|
||||
with self.database.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT id,chat_id,sender_id,text,created_at FROM messages "
|
||||
"WHERE chat_id=%s "
|
||||
+ condition
|
||||
+ " ORDER BY created_at,id LIMIT %s",
|
||||
parameters,
|
||||
).fetchall()
|
||||
return [message_dto(row) for row in rows]
|
||||
@@ -0,0 +1,51 @@
|
||||
import re
|
||||
from typing import Annotated
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import (
|
||||
BaseModel,
|
||||
BeforeValidator,
|
||||
ConfigDict,
|
||||
Field,
|
||||
StrictStr,
|
||||
field_validator,
|
||||
)
|
||||
|
||||
from src.cursor import parse_uuid
|
||||
|
||||
UuidIn = Annotated[UUID, BeforeValidator(parse_uuid)]
|
||||
|
||||
|
||||
class Input(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
@field_validator("*", mode="after")
|
||||
@classmethod
|
||||
def nonblank(cls, value: object) -> object:
|
||||
if isinstance(value, str) and (
|
||||
not value.strip() or any(0xD800 <= ord(c) <= 0xDFFF for c in value)
|
||||
):
|
||||
raise ValueError("Invalid text")
|
||||
return value
|
||||
|
||||
|
||||
class CreateUser(Input):
|
||||
username: StrictStr
|
||||
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||
password: StrictStr = Field(min_length=12, max_length=128)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def username_format(cls, value: str) -> str:
|
||||
if not re.fullmatch(r"[a-z0-9_]{3,32}", value):
|
||||
raise ValueError("Invalid username")
|
||||
return value
|
||||
|
||||
|
||||
class CreateChat(Input):
|
||||
title: StrictStr = Field(min_length=1, max_length=100)
|
||||
member_ids: list[UuidIn] = Field(min_length=1, max_length=100, strict=True)
|
||||
|
||||
|
||||
class CreateMessage(Input):
|
||||
text: StrictStr = Field(min_length=1, max_length=4000)
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import re
|
||||
import secrets
|
||||
from datetime import timedelta
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from src.cursor import decode_cursor, encode_cursor
|
||||
from src.domain import Chat, Message, Record, User, order_key, utcnow
|
||||
from src.errors import DomainError, invalid
|
||||
from src.password import Passwords
|
||||
from src.repository import PostgresRepository
|
||||
|
||||
|
||||
def page(*, rows: list[Record], limit: int, scope: str) -> dict:
|
||||
items = rows[:limit]
|
||||
cursor = (
|
||||
encode_cursor(scope=scope, key=order_key(items[-1]))
|
||||
if len(rows) > limit
|
||||
else None
|
||||
)
|
||||
return {"items": items, "next_cursor": cursor}
|
||||
|
||||
|
||||
def unauthorized(*, basic=False):
|
||||
challenge = (
|
||||
'Basic realm="messenger", charset="UTF-8"'
|
||||
if basic
|
||||
else 'Bearer realm="messenger"'
|
||||
)
|
||||
return DomainError(
|
||||
401,
|
||||
"unauthorized",
|
||||
"Authentication required",
|
||||
{"WWW-Authenticate": challenge},
|
||||
)
|
||||
|
||||
|
||||
def bearer(value):
|
||||
if not value:
|
||||
raise unauthorized()
|
||||
scheme, _, token = value.partition(" ")
|
||||
if scheme.lower() != "bearer" or not re.fullmatch(
|
||||
r"[A-Za-z0-9_-]{32,256}", token
|
||||
):
|
||||
raise unauthorized()
|
||||
return token
|
||||
|
||||
|
||||
def token_digest(value):
|
||||
return hashlib.sha256(value.encode()).digest()
|
||||
|
||||
|
||||
class MessengerService:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
repository: PostgresRepository,
|
||||
passwords: Passwords,
|
||||
session_ttl: int,
|
||||
) -> None:
|
||||
self.repository = repository
|
||||
self.passwords = passwords
|
||||
self.session_ttl = session_ttl
|
||||
|
||||
def identify(self, *, value: str | None) -> User:
|
||||
token = bearer(value)
|
||||
user_id = self.repository.session_user(digest=token_digest(token))
|
||||
user = self.repository.get_user(user_id=user_id) if user_id else None
|
||||
if user is None:
|
||||
raise unauthorized()
|
||||
return user
|
||||
|
||||
def register(
|
||||
self, *, username: str, display_name: str, password: str
|
||||
) -> User:
|
||||
encoded = self.passwords.hash(password=password)
|
||||
return self.repository.add_user(
|
||||
user=User(uuid4(), username, display_name, utcnow()),
|
||||
password_hash=encoded,
|
||||
)
|
||||
|
||||
def login(self, *, authorization: str | None):
|
||||
try:
|
||||
scheme, _, encoded = (authorization or "").partition(" ")
|
||||
if scheme.lower() != "basic":
|
||||
raise ValueError
|
||||
plain = base64.b64decode(encoded, validate=True).decode("utf-8")
|
||||
username, separator, password = plain.partition(":")
|
||||
if (
|
||||
not separator
|
||||
or not re.fullmatch(r"[a-z0-9_]{3,32}", username)
|
||||
or not 12 <= len(password) <= 128
|
||||
or not password.strip()
|
||||
):
|
||||
raise ValueError
|
||||
except ValueError, UnicodeError:
|
||||
raise unauthorized(basic=True) from None
|
||||
user, password_hash = self.repository.credentials(username=username)
|
||||
if not self.passwords.verify(password=password, encoded=password_hash):
|
||||
raise unauthorized(basic=True)
|
||||
now = utcnow()
|
||||
expires = now + timedelta(seconds=self.session_ttl)
|
||||
token = secrets.token_urlsafe(32)
|
||||
self.repository.add_session(
|
||||
digest=token_digest(token),
|
||||
user_id=user.id,
|
||||
created_at=now,
|
||||
expires_at=expires,
|
||||
)
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": expires,
|
||||
"user": user,
|
||||
}
|
||||
|
||||
def logout(self, *, authorization: str | None):
|
||||
self.identify(value=authorization)
|
||||
self.repository.delete_session(
|
||||
digest=token_digest(bearer(authorization))
|
||||
)
|
||||
|
||||
def create_chat(
|
||||
self, *, actor_id: UUID, title: str, member_ids: list[UUID]
|
||||
) -> Chat:
|
||||
return self.repository.add_chat(
|
||||
chat=Chat(uuid4(), title, tuple(member_ids), utcnow()),
|
||||
actor_id=actor_id,
|
||||
)
|
||||
|
||||
def get_chat(self, *, actor_id: UUID, chat_id: UUID) -> Chat:
|
||||
return self.repository.get_chat(actor_id=actor_id, chat_id=chat_id)
|
||||
|
||||
def send(
|
||||
self,
|
||||
*,
|
||||
actor_id: UUID,
|
||||
chat_id: UUID,
|
||||
text: str,
|
||||
idempotency_key: str | None,
|
||||
) -> Message:
|
||||
if not idempotency_key or not re.fullmatch(
|
||||
r"[A-Za-z0-9._:-]{1,128}", idempotency_key
|
||||
):
|
||||
raise invalid()
|
||||
return self.repository.add_message(
|
||||
message=Message(uuid4(), chat_id, actor_id, text, utcnow()),
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
|
||||
def chats(self, *, actor_id: UUID, limit: int, cursor: str | None) -> dict:
|
||||
scope = f"chats:{actor_id}"
|
||||
after = decode_cursor(value=cursor, scope=scope)
|
||||
rows = self.repository.list_chats(
|
||||
actor_id=actor_id, after=after, limit=limit
|
||||
)
|
||||
return page(rows=rows, limit=limit, scope=scope)
|
||||
|
||||
def history(
|
||||
self, *, actor_id: UUID, chat_id: UUID, limit: int, cursor: str | None
|
||||
) -> dict:
|
||||
self.get_chat(actor_id=actor_id, chat_id=chat_id)
|
||||
scope = f"messages:{chat_id}"
|
||||
after = decode_cursor(value=cursor, scope=scope)
|
||||
rows = self.repository.list_messages(
|
||||
actor_id=actor_id, chat_id=chat_id, after=after, limit=limit
|
||||
)
|
||||
return page(rows=rows, limit=limit, scope=scope)
|
||||
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def secret(name: str) -> str:
|
||||
file = os.environ.get(name + "_FILE")
|
||||
if file:
|
||||
return Path(file).read_text().strip()
|
||||
value = os.environ.get(name)
|
||||
if not value:
|
||||
raise ValueError("Missing required secret setting: " + name)
|
||||
return value
|
||||
|
||||
|
||||
def integer(name: str, default: int, low: int, high: int) -> int:
|
||||
value = int(os.environ.get(name, str(default)))
|
||||
if not low <= value <= high:
|
||||
raise ValueError("Invalid setting: " + name)
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
database_url: str = field(repr=False)
|
||||
cache_url: str = field(repr=False)
|
||||
session_ttl: int = 3600
|
||||
pool_size: int = 10
|
||||
|
||||
@classmethod
|
||||
def load(cls):
|
||||
return cls(
|
||||
secret("DATABASE_URL"),
|
||||
secret("CACHE_URL"),
|
||||
integer("SESSION_TTL_SECONDS", 3600, 1, 86400),
|
||||
integer("DB_POOL_SIZE", 10, 1, 32),
|
||||
)
|
||||
@@ -0,0 +1,115 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from jsonschema import Draft202012Validator, FormatChecker
|
||||
|
||||
CONTRACT = json.loads(
|
||||
(Path(__file__).parents[1] / "contracts/openapi.json").read_text()
|
||||
)
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8083")
|
||||
PASSWORD = "Synthetic-Password-🌍-123"
|
||||
TOKENS = {}
|
||||
|
||||
|
||||
def check(result: httpx.Response, status: int, schema: str) -> dict:
|
||||
assert result.status_code == status, result.text
|
||||
assert result.headers["x-request-id"]
|
||||
assert result.headers["x-instance-id"]
|
||||
assert result.headers["content-type"].startswith("application/json")
|
||||
payload = result.json()
|
||||
spec = {"$ref": f"#/components/schemas/{schema}", **CONTRACT}
|
||||
Draft202012Validator(spec, format_checker=FormatChecker()).validate(
|
||||
payload
|
||||
)
|
||||
if schema == "Error":
|
||||
assert payload["error"]["request_id"] == result.headers["x-request-id"]
|
||||
assert (
|
||||
payload["error"]["code"]
|
||||
== {
|
||||
400: "invalid_request",
|
||||
401: "unauthorized",
|
||||
404: "not_found",
|
||||
409: "conflict",
|
||||
500: "internal_error",
|
||||
}[status]
|
||||
)
|
||||
return payload
|
||||
|
||||
|
||||
def register(client: httpx.Client, **overrides) -> dict:
|
||||
data = {
|
||||
"username": "u_" + uuid4().hex[:24],
|
||||
"display_name": "Алиса 🌍",
|
||||
"password": PASSWORD,
|
||||
}
|
||||
data.update(overrides)
|
||||
user = check(client.post("/api/v1/users", json=data), 201, "User")
|
||||
session = check(
|
||||
client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
auth=httpx.BasicAuth(data["username"], data["password"]),
|
||||
),
|
||||
201,
|
||||
"Session",
|
||||
)
|
||||
TOKENS[user["id"]] = session["token"]
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with httpx.Client(
|
||||
base_url=BASE_URL, timeout=15, trust_env=False
|
||||
) as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def actors(client):
|
||||
return register(client), register(client), register(client)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def chat(client, actors):
|
||||
alice, bob, _ = actors
|
||||
return check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers=headers(alice),
|
||||
json={"title": "Учёба 🌍", "member_ids": [alice["id"], bob["id"]]},
|
||||
),
|
||||
201,
|
||||
"Chat",
|
||||
)
|
||||
|
||||
|
||||
def headers(actor: dict) -> dict:
|
||||
return {
|
||||
"Authorization": "Bearer " + TOKENS[actor["id"]],
|
||||
"Idempotency-Key": uuid4().hex,
|
||||
}
|
||||
|
||||
|
||||
def history(client, actor, chat_id, limit=7):
|
||||
found, cursor = [], None
|
||||
while True:
|
||||
params = {"limit": limit}
|
||||
if cursor:
|
||||
params["cursor"] = cursor
|
||||
page = check(
|
||||
client.get(
|
||||
f"/api/v1/chats/{chat_id}/messages",
|
||||
headers=headers(actor),
|
||||
params=params,
|
||||
),
|
||||
200,
|
||||
"MessagePage",
|
||||
)
|
||||
found.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
return found
|
||||
@@ -0,0 +1,412 @@
|
||||
import base64
|
||||
import json
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from conftest import BASE_URL, PASSWORD, check, headers, history, register
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path", ["/health/live", "/health/ready"])
|
||||
def test_health(client, path):
|
||||
assert check(client.get(path), 200, "Health") == {"status": "ok"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value", [None, "not-a-uuid", str(uuid4()), uuid4().hex]
|
||||
)
|
||||
def test_identification(client, value):
|
||||
check(
|
||||
client.get(
|
||||
"/api/v1/users/me", headers={"X-User-Id": value} if value else {}
|
||||
),
|
||||
401,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_identity(client, actors):
|
||||
assert (
|
||||
check(
|
||||
client.get("/api/v1/users/me", headers=headers(actors[0])),
|
||||
200,
|
||||
"User",
|
||||
)
|
||||
== actors[0]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data",
|
||||
[
|
||||
{},
|
||||
[],
|
||||
None,
|
||||
{"username": "ABC", "display_name": "A"},
|
||||
{"username": "abc\n", "display_name": "A"},
|
||||
{"username": 123, "display_name": "A"},
|
||||
{"username": "abc", "display_name": " "},
|
||||
{"username": "abc", "display_name": "x" * 101},
|
||||
{"username": "abc", "display_name": "A", "extra": True},
|
||||
{"username": "abc", "display_name": "\ud800"},
|
||||
],
|
||||
)
|
||||
def test_invalid_user(client, data):
|
||||
if isinstance(data, dict):
|
||||
data = {"password": PASSWORD, **data}
|
||||
check(client.post("/api/v1/users", content=json.dumps(data)), 400, "Error")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw", [b"{", b"NaN", b'{"username":"abc", "display_name":NaN}', b"\xff"]
|
||||
)
|
||||
def test_malformed_json(client, raw):
|
||||
check(client.post("/api/v1/users", content=raw), 400, "Error")
|
||||
|
||||
|
||||
def test_auth_before_json(client):
|
||||
check(client.post("/api/v1/chats", content=b"{"), 401, "Error")
|
||||
|
||||
|
||||
def test_duplicate_username(client, actors):
|
||||
check(
|
||||
client.post(
|
||||
"/api/v1/users",
|
||||
json={
|
||||
"username": actors[0]["username"],
|
||||
"display_name": "B",
|
||||
"password": PASSWORD,
|
||||
},
|
||||
),
|
||||
409,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_unicode_codepoints(client, actors, chat):
|
||||
user = register(client, display_name="🌍" * 100)
|
||||
assert user["display_name"] == "🌍" * 100
|
||||
text = "🌍" * 4000
|
||||
result = check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
json={"text": text},
|
||||
),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
assert result["text"] == text
|
||||
assert result["sender_id"] == actors[0]["id"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method,suffix,body",
|
||||
[
|
||||
("GET", "", None),
|
||||
("GET", "/messages", None),
|
||||
("POST", "/messages", {"text": "hello"}),
|
||||
],
|
||||
)
|
||||
def test_acl(client, actors, chat, method, suffix, body):
|
||||
outsider = headers(actors[2])
|
||||
actual = check(
|
||||
client.request(
|
||||
method,
|
||||
f"/api/v1/chats/{chat['id']}{suffix}",
|
||||
headers=outsider,
|
||||
json=body,
|
||||
),
|
||||
404,
|
||||
"Error",
|
||||
)
|
||||
missing = check(
|
||||
client.request(
|
||||
method,
|
||||
f"/api/v1/chats/{uuid4()}{suffix}",
|
||||
headers=outsider,
|
||||
json=body,
|
||||
),
|
||||
404,
|
||||
"Error",
|
||||
)
|
||||
assert actual["error"]["message"] == missing["error"]["message"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["", " \t\n", 42, True, None, "a" * 4001])
|
||||
def test_bad_message(client, actors, chat, value):
|
||||
check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
json={"text": value},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_sender_not_client_controlled(client, actors, chat):
|
||||
check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
json={"text": "Hi", "sender_id": actors[1]["id"]},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"case",
|
||||
[
|
||||
"duplicate",
|
||||
"unknown",
|
||||
"no_caller",
|
||||
"empty",
|
||||
"wrong_type",
|
||||
"bad_uuid",
|
||||
"too_many",
|
||||
"blank_title",
|
||||
],
|
||||
)
|
||||
def test_invalid_chat(client, actors, case):
|
||||
a, b, _ = actors
|
||||
members = {
|
||||
"duplicate": [a["id"], a["id"]],
|
||||
"unknown": [a["id"], str(uuid4())],
|
||||
"no_caller": [b["id"]],
|
||||
"empty": [],
|
||||
"wrong_type": a["id"],
|
||||
"bad_uuid": ["broken"],
|
||||
"too_many": [str(uuid4()) for _ in range(101)],
|
||||
"blank_title": [a["id"]],
|
||||
}[case]
|
||||
check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers=headers(a),
|
||||
json={
|
||||
"title": " " if case == "blank_title" else "Title",
|
||||
"member_ids": members,
|
||||
},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"limit", ["0", "101", "-1", "1.5", "true", "1e1", "", "12"]
|
||||
)
|
||||
def test_bad_limit(client, actors, chat, limit):
|
||||
check(
|
||||
client.get(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
params={"limit": limit},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"cursor", ["", "broken", "W10", "e30", "*", "x" * 2049]
|
||||
)
|
||||
def test_bad_cursor(client, actors, chat, cursor):
|
||||
check(
|
||||
client.get(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=headers(actors[0]),
|
||||
params={"cursor": cursor},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_pagination(client, actors, chat):
|
||||
a, b, outsider = actors
|
||||
sent = []
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
for i in range(23):
|
||||
sent.append(
|
||||
check(
|
||||
client.post(
|
||||
path,
|
||||
headers=headers(a if i % 2 else b),
|
||||
json={"text": f"Msg {i}"},
|
||||
),
|
||||
201,
|
||||
"Message",
|
||||
)
|
||||
)
|
||||
got = history(client, a, chat["id"])
|
||||
assert got == sorted(
|
||||
sent, key=lambda m: (m["created_at"], UUID(m["id"]).int)
|
||||
)
|
||||
first = check(
|
||||
client.get(path, headers=headers(a), params={"limit": 1}),
|
||||
200,
|
||||
"MessagePage",
|
||||
)
|
||||
second = check(
|
||||
client.get(
|
||||
path,
|
||||
headers=headers(b),
|
||||
params={"limit": 22, "cursor": first["next_cursor"]},
|
||||
),
|
||||
200,
|
||||
"MessagePage",
|
||||
)
|
||||
assert second["items"] == got[1:]
|
||||
assert second["next_cursor"] is None
|
||||
chats = check(
|
||||
client.get("/api/v1/chats", headers=headers(outsider)), 200, "ChatPage"
|
||||
)
|
||||
assert chats["items"] == []
|
||||
other = check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers=headers(a),
|
||||
json={"title": "Other", "member_ids": [a["id"]]},
|
||||
),
|
||||
201,
|
||||
"Chat",
|
||||
)
|
||||
check(
|
||||
client.get(
|
||||
f"/api/v1/chats/{other['id']}/messages",
|
||||
headers=headers(a),
|
||||
params={"cursor": first["next_cursor"]},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
# Валидный base64/JSON с неверным типом ключа тоже даёт 400.
|
||||
data = json.loads(
|
||||
base64.urlsafe_b64decode(
|
||||
first["next_cursor"] + "=" * (-len(first["next_cursor"]) % 4)
|
||||
)
|
||||
)
|
||||
data["at"] = 17
|
||||
forged = (
|
||||
base64.urlsafe_b64encode(json.dumps(data).encode())
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
check(
|
||||
client.get(path, headers=headers(a), params={"cursor": forged}),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_chat_pagination_scope(client, actors):
|
||||
a, b, _ = actors
|
||||
created = []
|
||||
for i in range(4):
|
||||
created.append(
|
||||
check(
|
||||
client.post(
|
||||
"/api/v1/chats",
|
||||
headers=headers(a),
|
||||
json={"title": str(i), "member_ids": [a["id"], b["id"]]},
|
||||
),
|
||||
201,
|
||||
"Chat",
|
||||
)
|
||||
)
|
||||
page = check(
|
||||
client.get("/api/v1/chats", headers=headers(a), params={"limit": 2}),
|
||||
200,
|
||||
"ChatPage",
|
||||
)
|
||||
assert page["items"] == created[:2]
|
||||
last = check(
|
||||
client.get(
|
||||
"/api/v1/chats",
|
||||
headers=headers(a),
|
||||
params={"limit": 2, "cursor": page["next_cursor"]},
|
||||
),
|
||||
200,
|
||||
"ChatPage",
|
||||
)
|
||||
assert last["items"] == created[2:]
|
||||
assert last["next_cursor"] is None
|
||||
check(
|
||||
client.get(
|
||||
"/api/v1/chats",
|
||||
headers=headers(b),
|
||||
params={"cursor": page["next_cursor"]},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_concurrent_messages(client, actors, chat):
|
||||
barrier = threading.Barrier(10)
|
||||
a = actors[0]
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
|
||||
def worker(i):
|
||||
with httpx.Client(
|
||||
base_url=BASE_URL, timeout=15, trust_env=False
|
||||
) as conn:
|
||||
barrier.wait(timeout=15)
|
||||
return [
|
||||
check(
|
||||
conn.post(
|
||||
path, headers=headers(a), json={"text": f"{i}:{j}"}
|
||||
),
|
||||
201,
|
||||
"Message",
|
||||
)["id"]
|
||||
for j in range(10)
|
||||
]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
ids = [item for batch in pool.map(worker, range(10)) for item in batch]
|
||||
rows = history(client, a, chat["id"])
|
||||
assert len(rows) == len(ids) == len(set(ids)) == 100
|
||||
assert set(ids) == {row["id"] for row in rows}
|
||||
assert len({row["text"] for row in rows}) == 100
|
||||
|
||||
|
||||
def test_registration_race(client):
|
||||
username = "race_" + uuid4().hex[:20]
|
||||
barrier = threading.Barrier(10)
|
||||
|
||||
def worker(_):
|
||||
with httpx.Client(
|
||||
base_url=BASE_URL, timeout=15, trust_env=False
|
||||
) as conn:
|
||||
barrier.wait(timeout=15)
|
||||
result = conn.post(
|
||||
"/api/v1/users",
|
||||
json={
|
||||
"username": username,
|
||||
"display_name": "Test",
|
||||
"password": PASSWORD,
|
||||
},
|
||||
)
|
||||
check(
|
||||
result,
|
||||
result.status_code,
|
||||
"User" if result.status_code == 201 else "Error",
|
||||
)
|
||||
return result.status_code
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
statuses = list(pool.map(worker, range(10)))
|
||||
assert statuses.count(201) == 1
|
||||
assert statuses.count(409) == 9
|
||||
|
||||
|
||||
def test_unknown_path(client):
|
||||
check(client.get("/does-not-exist"), 404, "Error")
|
||||
@@ -0,0 +1,26 @@
|
||||
import json
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from conftest import check, headers
|
||||
|
||||
|
||||
@pytest.mark.parametrize("encoding", ["utf-16", "utf-32"])
|
||||
def test_only_utf8_json(client, encoding):
|
||||
payload = {"username": "u_" + uuid4().hex[:20], "display_name": "UTF"}
|
||||
payload["password"] = "Synthetic-encoding-test-123"
|
||||
result = client.post(
|
||||
"/api/v1/users",
|
||||
content=json.dumps(payload).encode(encoding),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
check(result, 400, "Error")
|
||||
|
||||
|
||||
def test_leading_zero_limit(client, actors, chat):
|
||||
result = client.get(
|
||||
"/api/v1/chats/" + chat["id"] + "/messages",
|
||||
headers=headers(actors[0]),
|
||||
params={"limit": "0001"},
|
||||
)
|
||||
check(result, 200, "MessagePage")
|
||||
@@ -0,0 +1,75 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from conftest import BASE_URL, check, headers, history
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", [None, "", "a b", "x" * 129, "a/b"])
|
||||
def test_key_required_and_valid(client, actors, chat, key):
|
||||
h = headers(actors[0])
|
||||
h.pop("Idempotency-Key")
|
||||
if key is not None:
|
||||
h["Idempotency-Key"] = key
|
||||
check(
|
||||
client.post(
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
headers=h,
|
||||
json={"text": "Key"},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_parallel_replay_across_instances(client, actors, chat):
|
||||
h = headers(actors[0])
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
|
||||
def send(_):
|
||||
with httpx.Client(base_url=BASE_URL, trust_env=False, timeout=15) as c:
|
||||
r = c.post(path, headers=h, json={"text": "Один результат 🌍"})
|
||||
return check(r, 201, "Message"), r.headers["x-instance-id"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
results = list(pool.map(send, range(30)))
|
||||
assert len({r[0]["id"] for r in results}) == 1
|
||||
assert len({r[1] for r in results}) >= 2
|
||||
assert history(client, actors[0], chat["id"]) == [results[0][0]]
|
||||
check(
|
||||
client.post(path, headers=h, json={"text": "Другой текст"}),
|
||||
409,
|
||||
"Error",
|
||||
)
|
||||
# Другой пользователь может независимо использовать тот же ключ.
|
||||
other = headers(actors[1])
|
||||
other["Idempotency-Key"] = h["Idempotency-Key"]
|
||||
second = check(
|
||||
client.post(path, headers=other, json={"text": "Боб"}), 201, "Message"
|
||||
)
|
||||
assert second["id"] != results[0][0]["id"]
|
||||
|
||||
|
||||
def test_replay_rechecks_acl(client, actors, chat):
|
||||
h = headers(actors[0])
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
check(
|
||||
client.post(path, headers=h, json={"text": "Existing"}), 201, "Message"
|
||||
)
|
||||
outsider = headers(actors[2])
|
||||
outsider["Idempotency-Key"] = h["Idempotency-Key"]
|
||||
check(
|
||||
client.post(path, headers=outsider, json={"text": "Existing"}),
|
||||
404,
|
||||
"Error",
|
||||
)
|
||||
check(
|
||||
client.post(
|
||||
path,
|
||||
headers={"Idempotency-Key": uuid4().hex},
|
||||
json={"text": "Existing"},
|
||||
),
|
||||
401,
|
||||
"Error",
|
||||
)
|
||||
@@ -0,0 +1,114 @@
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from conftest import PASSWORD, TOKENS, check, headers, register
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"password", ["", "x" * 11, "x" * 129, " " * 12, 42, True, None]
|
||||
)
|
||||
def test_password_validation(client, password):
|
||||
check(
|
||||
client.post(
|
||||
"/api/v1/users",
|
||||
json={
|
||||
"username": "pwd_" + uuid4().hex[:20],
|
||||
"display_name": "Test",
|
||||
"password": password,
|
||||
},
|
||||
),
|
||||
400,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_full_utf8_password(client):
|
||||
password = "🌍" * 127 + ":"
|
||||
user = register(client, password=password)
|
||||
assert (
|
||||
check(
|
||||
client.get("/api/v1/users/me", headers=headers(user)), 200, "User"
|
||||
)
|
||||
== user
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"authorization", [None, "Basic broken", "Bearer unknown", "Basic /w=="]
|
||||
)
|
||||
def test_bad_basic(client, authorization):
|
||||
result = client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
headers={"Authorization": authorization} if authorization else {},
|
||||
)
|
||||
check(result, 401, "Error")
|
||||
assert (
|
||||
result.headers["www-authenticate"]
|
||||
== 'Basic realm="messenger", charset="UTF-8"'
|
||||
)
|
||||
|
||||
|
||||
def test_wrong_password_and_unknown_user(client):
|
||||
user = register(client)
|
||||
for username in [user["username"], "missing_" + uuid4().hex[:20]]:
|
||||
result = client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
auth=httpx.BasicAuth(username, PASSWORD + "wrong"),
|
||||
)
|
||||
data = check(result, 401, "Error")
|
||||
assert data["error"]["message"] == "Authentication required"
|
||||
|
||||
|
||||
def test_logout_is_not_revived_by_cache(client):
|
||||
user = register(client)
|
||||
auth = headers(user)
|
||||
check(client.get("/api/v1/users/me", headers=auth), 200, "User")
|
||||
result = client.delete("/api/v1/auth/sessions/current", headers=auth)
|
||||
assert result.status_code == 204 and result.content == b""
|
||||
assert result.headers["x-request-id"]
|
||||
check(client.get("/api/v1/users/me", headers=auth), 401, "Error")
|
||||
check(
|
||||
client.delete("/api/v1/auth/sessions/current", headers=auth),
|
||||
401,
|
||||
"Error",
|
||||
)
|
||||
|
||||
|
||||
def test_only_current_session_revoked(client):
|
||||
user = register(client)
|
||||
another = check(
|
||||
client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
auth=httpx.BasicAuth(user["username"], PASSWORD),
|
||||
),
|
||||
201,
|
||||
"Session",
|
||||
)
|
||||
assert another["token"] != TOKENS[user["id"]]
|
||||
assert len(another["token"]) >= 32
|
||||
assert (
|
||||
client.delete(
|
||||
"/api/v1/auth/sessions/current", headers=headers(user)
|
||||
).status_code
|
||||
== 204
|
||||
)
|
||||
result = client.get(
|
||||
"/api/v1/users/me",
|
||||
headers={"Authorization": "Bearer " + another["token"]},
|
||||
)
|
||||
assert check(result, 200, "User") == user
|
||||
|
||||
|
||||
def test_bearer_source_and_ignored_user_header(client, actors):
|
||||
alice, bob, _ = actors
|
||||
auth = {**headers(alice), "X-User-Id": bob["id"]}
|
||||
assert (
|
||||
check(client.get("/api/v1/users/me", headers=auth), 200, "User")["id"]
|
||||
== alice["id"]
|
||||
)
|
||||
result = client.get(
|
||||
"/api/v1/users/me", auth=httpx.BasicAuth(alice["username"], PASSWORD)
|
||||
)
|
||||
check(result, 401, "Error")
|
||||
assert result.headers["www-authenticate"] == 'Bearer realm="messenger"'
|
||||
Reference in New Issue
Block a user