From e9ef559c349cf8b6fc32138fed0ea7a7600ce1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=82=D0=B2=D0=B5=D0=B9?= Date: Tue, 20 Oct 2026 17:24:32 +0300 Subject: [PATCH] add level 3 --- .env.example | 2 +- Makefile | 13 ++ REPORT.md | 172 ++++++++++++++----------- compose.yaml | 61 ++++++++- evidence/attachment-failover.txt | 1 + evidence/attachments.txt | 4 + evidence/bootstrap.txt | 3 + evidence/contracts.txt | 4 + evidence/environment.json | 107 +++++++++++++++ evidence/failover.txt | 4 + evidence/lab3-regressions.txt | 2 + evidence/persistence.txt | 5 + evidence/private-bucket.txt | 1 + evidence/s3-outage.txt | 2 + evidence/smoke.txt | 2 + infra/filer.toml | 3 + migrations/004_attachments.sql | 18 +++ pyproject.toml | 2 + scripts/init-secrets.py | 34 +++++ scripts/record-environment.py | 44 +++++++ scripts/restart.sh | 4 +- scripts/verify-persistence.py | 27 ++++ src/attachments.py | 104 +++++++++++++++ src/bootstrap_s3.py | 38 ++++++ src/config.py | 4 + src/main.py | 9 +- src/models.py | 40 ++++-- src/routes.py | 2 + src/storage/postgres.py | 52 +++++++- src/storage/s3.py | 85 ++++++++++++ student-tests/attachment_resilience.py | 115 +++++++++++++++++ student-tests/attachments.py | 111 ++++++++++++++++ uv.lock | 159 +++++++++++++++++++++++ 33 files changed, 1138 insertions(+), 96 deletions(-) create mode 100644 evidence/attachment-failover.txt create mode 100644 evidence/attachments.txt create mode 100644 evidence/bootstrap.txt create mode 100644 evidence/contracts.txt create mode 100644 evidence/environment.json create mode 100644 evidence/failover.txt create mode 100644 evidence/lab3-regressions.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/private-bucket.txt create mode 100644 evidence/s3-outage.txt create mode 100644 evidence/smoke.txt create mode 100644 infra/filer.toml create mode 100644 migrations/004_attachments.sql create mode 100644 scripts/init-secrets.py create mode 100644 scripts/record-environment.py create mode 100644 scripts/verify-persistence.py create mode 100644 src/attachments.py create mode 100644 src/bootstrap_s3.py create mode 100644 src/storage/s3.py create mode 100644 student-tests/attachment_resilience.py create mode 100644 student-tests/attachments.py diff --git a/.env.example b/.env.example index 217c94be..21836d11 100644 --- a/.env.example +++ b/.env.example @@ -12,7 +12,7 @@ CACHE_URL_FILE=/run/secrets/cache_url API_REPLICAS=2 IDEMPOTENCY_TTL_SECONDS=86400 CURSOR_SECRET_FILE=/run/secrets/cursor_secret -S3_ENDPOINT=http://s3:9000 +S3_ENDPOINT=http://s3:8333 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id diff --git a/Makefile b/Makefile index 5d86d171..fc7400ef 100644 --- a/Makefile +++ b/Makefile @@ -28,3 +28,16 @@ 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) + +.PHONY: test-attachments test-private test-upload-failover test-s3-outage +test-attachments: + $(PYTHON) student-tests/attachments.py --base-url "$(BASE_URL)" + +test-private: + $(PYTHON) student-tests/attachment_resilience.py --mode private --base-url "$(BASE_URL)" + +test-upload-failover: + $(PYTHON) student-tests/attachment_resilience.py --mode failover --base-url "$(BASE_URL)" + +test-s3-outage: + $(PYTHON) student-tests/attachment_resilience.py --mode s3-outage --base-url "$(BASE_URL)" diff --git a/REPORT.md b/REPORT.md index 02c40569..b14dfe68 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,100 +1,122 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Матвей Козырев, БВТ2301. +- Целевая оценка: **3 — работающий минимум**. +- Ветка: `solution/BVT2301-Kozyrev`. +- Проверки: 20 октября 2026 года. Базовый commit и SHA-256 исходников проверенного рабочего дерева — [evidence/environment.json](evidence/environment.json). +- Использованные библиотеки: FastAPI, Uvicorn, asyncpg, argon2-cffi, boto3, Pillow. Помощь ИИ: Codex — перенос основы lab3, реализация вложений, тесты и документация. ## Запуск из чистого clone -На текущем этапе перенесена база lab3; S3 и вложения ещё не реализованы. -Инструкция ниже запускает только эту базу. - -Нужны Docker Engine с Compose v2 и Python 3 для создания локальных секретов. -Версии: Python 3.12.12 и uv 0.11.28 в Dockerfile, PostgreSQL 17.6-alpine, -HAProxy 3.0.11-alpine в compose.yaml; зависимости Python закреплены в uv.lock. - -Из корня репозитория: +Нужны Docker Engine с Compose v2, Python 3.12+ и свободный порт 8080. ```sh -python3 - <<'PY' -from pathlib import Path -import secrets - -directory = Path("secrets") -directory.mkdir(mode=0o700, exist_ok=True) -names = ("postgres_password", "database_url", "cursor_secret") -if any((directory / name).exists() for name in names): - raise SystemExit("Секреты уже существуют: сохраните их для существующей БД.") -password = secrets.token_hex(32) -values = ( - password, - f"postgresql://messenger:{password}@postgres:5432/messenger", - secrets.token_hex(32), -) -for name, value in zip(names, values): - path = directory / name - with path.open("x", encoding="utf-8") as stream: - path.chmod(0o600) - stream.write(value + "\n") -PY -docker compose --env-file .env.example config --quiet -docker compose --env-file .env.example up -d --build --wait +python3 scripts/init-secrets.py +docker compose config --quiet +docker compose up -d --build --wait curl --fail http://localhost:8080/health/ready -make check -python3 student-tests/review_regressions.py +make test ``` -Сервис `migrate` применяет SQL-миграции до запуска API. -Вход через HAProxy: `http://localhost:8080`; порты API и PostgreSQL на host не опубликованы. -Порт 8080 должен быть свободен. Compose использует отдельный проект и volume lab4; -секреты и данные lab3 не переносились. +`init-secrets.py` создаёт локальные случайные credentials в игнорируемом каталоге `secrets/`, не выводит значения и сохраняет существующие файлы при повторном запуске. `.env` не требуется для стандартного Compose: параметры и пути к secrets заданы в [compose.yaml](compose.yaml). Для переопределения TTL можно скопировать `.env.example` в `.env`. S3 endpoint, bucket и лимиты стандартного стенда заданы непосредственно в Compose; `API_REPLICAS`, `SESSION_STORE` и `CACHE_URL_FILE` из шаблона не управляют этой топологией. Реплик две, сессии в PostgreSQL, Redis не используется. -Остановка с сохранением данных: `docker compose down`. -Намеренное удаление данных этого стенда: `docker compose down -v`. +Последовательность запуска: PostgreSQL → `migrate` → API; SeaweedFS → `s3-init` → API; здоровые API → HAProxy. Миграции и bootstrap — одноразовые сервисы; их успешное состояние — `Exited (0)`. Bucket `messenger-attachments` создаётся bootstrap-сервисом, ему устанавливается private ACL и удаляется публичная policy, если она существовала. Повторный bootstrap безопасен: -Полный `make test` пока не проходит: модель Message перенесена из lab3 без -`attachment_ids`, endpoint вложений отсутствуют. Публичные тесты и контракт lab4 -сохранены. Проверки `test-persistence`, `test-failover` и собственная проверка -`idempotency_concurrency.py` используют контракт lab4 и требуют дальнейшей -реализации. Скрипты restart/stop-one перенесены для текущей топологии; -при добавлении S3 restart необходимо дополнить перезапуском этого хранилища. +```sh +docker compose run --rm --no-deps s3-init +``` + +Вход только через `http://127.0.0.1:8080`. Порты PostgreSQL, API и SeaweedFS на host не публикуются. Остановка с сохранением данных: `docker compose down`; повторный запуск — `docker compose up -d --wait`. **Намеренное удаление всех учебных данных lab4:** `docker compose down -v`. Секреты следует сохранять вместе с volumes, иначе существующая БД может перестать принимать подключение. + +Версии runtime и зависимостей закреплены в [Dockerfile](Dockerfile), [pyproject.toml](pyproject.toml) и [uv.lock](uv.lock). Python контейнера — 3.12.12, uv — 0.11.28; boto3 — 1.43.115, Pillow — 13.0.0. ## Архитектура и решения -Перенесено из локального проекта `mtusi2026lab3`: `src/`, миграции -`001`–`003`, Dockerfile, зависимости и lockfile, основной Compose, конфигурация -HAProxy, restart/failover hooks и собственные проверки сессий, идемпотентности, -readiness и ошибок API. Исследовательский benchmark lab3 и старые результаты -экспериментов не входят в этот перенос. +```text +Клиент → HAProxy → api-a / api-b → PostgreSQL (метаданные) + → SeaweedFS S3 (оригиналы) +``` -Текущая схема: клиент → HAProxy → api-a / api-b → PostgreSQL. -PostgreSQL хранит пользователей, сессии, чаты, сообщения и записи идемпотентности. -Общий `CURSOR_SECRET_FILE` задаёт ключ подписи курсоров для обеих реплик. -Исходники API пока сохраняют поведение и версию 1.3.0 из lab3. -Параметры S3 из `.env.example` будут подключены на следующем этапе. +| Роль / service | Версия | Готовность | Persistent storage | Доступ и credentials | +| --- | --- | --- | --- | --- | +| API / api-a, api-b | Python 3.12.12, исходники репозитория | `/health/ready`: PostgreSQL SELECT + S3 HeadBucket | Нет | Внутренний 8080; DB URL, cursor secret, отдельные S3 credentials из файлов | +| PostgreSQL / postgres | 17.6-alpine | pg_isready | postgres_data | Внутренний 5432; password file | +| LB / load_balancer | HAProxy 3.0.11-alpine | readiness API, активные backend checks | Нет | Host 127.0.0.1:8080 | +| S3 / s3 | SeaweedFS 4.17, digest в Compose | master cluster status; bootstrap дополнительно проверяет S3 | s3_data: файлы и LevelDB filer | Внутренний 8333; identity config из secret | +| Bootstrap / s3-init | Тот же Python-образ | exit 0 | Нет | Отдельные административные S3 credentials | -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +SeaweedFS закреплён digest `sha256:186de7ef977a20343ee9a5544073f081976a29e2d29ecf8379891e7bf177fbe9`. Настройка filer LevelDB в [infra/filer.toml](infra/filer.toml) сохраняет его метаданные в `/data/filer` на том же volume. S3 identity `api` имеет Read/Write/List для одного bucket; anonymous identity отсутствует. Административные credentials доступны bootstrap, но не API. Внутренняя Docker-сеть считается доверенной; это не защита от компрометации контейнера. -## Доказательства критериев +Лимиты Compose: каждая API — 0,5 CPU / 512 MiB; PostgreSQL — 0,75 CPU / 512 MiB; SeaweedFS — 1 CPU / 512 MiB; HAProxy — 0,25 CPU / 128 MiB. Это учебный стенд на одном Docker-host, без высокой доступности БД, S3, LB и самого host. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +### Перенос lab3 и модель вложений -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Из lab3 сохранены пользователи, чаты, общие PostgreSQL-сессии, Argon2 password hashing, keyset-пагинация, подпись cursor общим ключом, `X-Request-Id`, `X-Instance-Id`, идемпотентность сообщений и две реплики за HAProxy. Исходные публичные `tests/` и `contracts/` lab4 не изменены. -## Эксперименты и отказы +Миграция [004_attachments.sql](migrations/004_attachments.sql) добавляет таблицу Attachment: ID, чат, владелец, отображаемое имя, MIME, размер, SHA-256, внутренний object key и время. Байты в PostgreSQL не записываются. `messages.attachment_ids` — массив UUID, по умолчанию пустой. При создании сообщения в одной транзакции проверяются членство в чате и принадлежность всех вложений этому чату, затем записываются сообщение и результат идемпотентности. У массива нет отдельного внешнего ключа: связь проверяет приложение; удаления вложений и изменения состава чата в API нет. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Пустой текст разрешён только с вложением. Пропущенное `attachment_ids` и `[]` эквивалентны; hash текстовых сообщений сохранён совместимым с lab3. Для сообщения с вложениями hash также включает упорядоченный список нормализованных UUID. История и повтор POST возвращают те же attachment IDs. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +### Загрузка и скачивание -## Ограничения и следующие шаги +[attachments.py](src/attachments.py) принимает raw PNG/JPEG. Сначала проверяются Bearer-сессия и членство в чате. `filename` не используется как путь; запрещены `/`, `\`, управляющие символы и недопустимая длина. Сервер генерирует ключ `originals/`. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Лимит **10 MiB = 10 × 1024 × 1024 = 10 485 760 байт** проверяется по Content-Length и повторно при чтении каждого блока, в том числе для chunked upload. До декодирования Pillow определяет фактический формат и размеры; максимум **40 000 000 пикселей**. Проверяются целостность PNG и декодирование пикселей PNG/JPEG. MIME должен совпадать с содержимым. Анимированный PNG не поддерживается: ответ 415. + +Во время приёма используется удаляемый временный файл; он не является хранилищем оригиналов и не используется при скачивании. На одну API-реплику допускается одна одновременная загрузка/проверка изображения, ожидание вместе с загрузкой ограничено 30 секундами. SDK и декодирование выполняются вне event loop. S3 connect timeout — 2 с, read timeout — 5 с, автоматические повторы выключены. Это ограничения отдельного запроса, не доказательство устойчивости при произвольной нагрузке. + +Порядок фиксации: валидация → S3 PUT → INSERT метаданных PostgreSQL → **201, status=ready, variants=[], error_code=null**. GET метаданных и оригинала снова проверяет членство. Для постороннего — 404; без сессии — 401. GET content читает оригинал из S3, проверяет размер и SHA-256 и возвращает байты через API без redirect. Object key и credentials не входят в публичную модель. + +Недоступность S3: upload/download и readiness возвращают 503 в формате Error; liveness остаётся 200. HAProxy исключает unready backend, поэтому при недоступности обеих реплик сам LB может вернуть собственный 503. Проверка формата ошибки API при таком отказе выполняется напрямую внутри Docker-сети. + +## Проверки и доказательства + +Для собственных image-тестов и полной OpenAPI-валидации нужны локальные зависимости: + +```sh +uv sync --frozen +uv pip install -r requirements-dev.txt +make check validate PYTHON=.venv/bin/python +make test +make test-attachments PYTHON=.venv/bin/python +python3 student-tests/idempotency_concurrency.py +python3 student-tests/review_regressions.py +make test-private +make test-upload-failover +make test-s3-outage +python3 scripts/verify-persistence.py +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +docker compose start api-a api-b +``` + +Сценарии отказов выполняются последовательно на учебном стенде: они временно останавливают контейнеры. Собственные проверки возвращают остановленные сервисы в работу; публичный `test-failover` оставляет реплику остановленной, поэтому последняя команда нужна для восстановления. Все данные синтетические, токены не сохраняются. Тесты добавляют данные; автоматического удаления нет. + +| Критерий | Команда / наблюдение | Результат | +| --- | --- | --- | +| Структура и OpenAPI | `make check validate PYTHON=.venv/bin/python` | [PASS](evidence/contracts.txt) | +| Обязательный API lab4, checksum, ACL | `make test` | [PASS](evidence/smoke.txt) | +| PNG/JPEG, сообщение с вложением, чужой чат, неверный MIME/filename, лимиты, chunked | `make test-attachments PYTHON=.venv/bin/python` | [PASS](evidence/attachments.txt) | +| Приватность bucket вне API | `make test-private`: anonymous GET существующего объекта → 403 | [PASS](evidence/private-bucket.txt) | +| Отказ именно загрузившей API | `make test-upload-failover`: загрузка через api-b, остановка api-b, те же байты через другую реплику | [PASS](evidence/attachment-failover.txt) | +| Недоступность S3 | `make test-s3-outage`: 503, восстановление, прежний оригинал | [PASS](evidence/s3-outage.txt) | +| Перезапуск всех компонентов, сохранение действующей сессии, метаданных и файла | `python3 scripts/verify-persistence.py`; StartedAt каждого сервиса изменился | [PASS](evidence/persistence.txt) | +| Публичный failover, сообщения без дублей и сохранение файла | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | [Результат](evidence/failover.txt) | +| Конкурентная идемпотентность и регрессии lab3 | Собственные проверки lab3 | [Результат](evidence/lab3-regressions.txt) | +| Повторный bootstrap bucket | `docker compose run --rm --no-deps s3-init` | [Результат](evidence/bootstrap.txt) | + +Точные границы проверены отдельно: PNG с trailing bytes доведён до 10 485 760 байт и скачан без изменений; PNG 8000 × 5000 = 40 000 000 пикселей принят; заголовок 8001 × 5000 = 40 005 000 пикселей отклонён до декодирования. Это функциональные тесты, не исследование RPS или памяти уровня 4/5. + +## Ограничения + +- Между S3 PUT и COMMIT PostgreSQL нет общей транзакции. Сбой после PUT может оставить orphan object. Автоматическая сверка/очистка с grace period относится к следующему уровню и здесь не реализована. При неопределённом исходе COMMIT объект намеренно не удаляется, чтобы не повредить возможную подтверждённую запись. +- API буферизует скачивание не более 10 MiB, чтобы проверить checksum до ответа. Ограничение общего количества скачиваний и нагрузочный эксперимент для 10 одновременных upload не выполнены; оценка 4 не заявляется. +- Readiness проверяет доступность БД и bucket, но не наличие каждого отдельного объекта. Потеря одного объекта при доступном S3 обнаруживается при скачивании как 503. +- Перезапуск с volumes и отказ API подтверждены на одном Docker-host. Потеря volumes/host не покрывается, резервное копирование не реализовано. +- HTTP предназначен для локального учебного стенда. Presigned flow, обработка thumbnail и брокер не реализованы. + +## Источники + +- [Контракт API](contracts/README.md), [OpenAPI](contracts/openapi.json), [критерии задания](README.md). +- [SeaweedFS исходники и запуск](https://github.com/seaweedfs/seaweedfs), [релиз 4.17](https://github.com/seaweedfs/seaweedfs/releases/tag/4.17). Параметры закреплённого образа дополнительно проверены через `docker run --rm chrislusf/seaweedfs:4.17 server -help`. +- [Pillow Image API](https://pillow.readthedocs.io/en/stable/reference/Image.html): lazy open, размеры, verify/load и decompression bomb limits. +- [Boto3 S3 PutObject](https://docs.aws.amazon.com/boto3/latest/reference/services/s3/client/put_object.html): запись оригинала в S3. diff --git a/compose.yaml b/compose.yaml index f447ec0e..4e5a0c42 100644 --- a/compose.yaml +++ b/compose.yaml @@ -9,10 +9,20 @@ x-api: &api SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400} CURSOR_SECRET_FILE: /run/secrets/cursor_secret + S3_ENDPOINT: http://s3:8333 + S3_BUCKET: messenger-attachments + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + MAX_UPLOAD_BYTES: "10485760" + MAX_IMAGE_PIXELS: "40000000" secrets: - database_url - cursor_secret + - s3_access_key_id + - s3_secret_access_key depends_on: + s3-init: + condition: service_completed_successfully migrate: condition: service_completed_successfully expose: @@ -28,9 +38,45 @@ x-api: &api resources: limits: cpus: "0.50" - memory: 256M + memory: 512M services: + s3: + image: chrislusf/seaweedfs:4.17@sha256:186de7ef977a20343ee9a5544073f081976a29e2d29ecf8379891e7bf177fbe9 + command: ["server", "-dir=/data", "-ip=s3", "-ip.bind=0.0.0.0", "-s3", "-s3.config=/run/secrets/s3_config", "-master.volumeSizeLimitMB=128", "-volume.max=8", "-s3.port.iceberg=0", "-filer.exposeDirectoryData=false"] + secrets: + - s3_config + volumes: + - s3_data:/data + - ./infra/filer.toml:/etc/seaweedfs/filer.toml:ro + healthcheck: + test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://localhost:9333/cluster/status"] + interval: 5s + timeout: 3s + retries: 20 + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M + + s3-init: + build: + context: . + command: ["/app/.venv/bin/python", "-m", "src.bootstrap_s3"] + environment: + S3_ENDPOINT: http://s3:8333 + S3_BUCKET: messenger-attachments + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_key + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret + secrets: + - s3_admin_key + - s3_admin_secret + depends_on: + s3: + condition: service_healthy + restart: "no" + postgres: image: postgres:17.6-alpine environment: @@ -82,7 +128,7 @@ services: load_balancer: image: haproxy:3.0.11-alpine ports: - - "8080:8080" + - "127.0.0.1:8080:8080" volumes: - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro depends_on: @@ -103,6 +149,16 @@ services: memory: 128M secrets: + s3_config: + file: ./secrets/s3.json + s3_admin_key: + file: ./secrets/s3_admin_key + s3_admin_secret: + file: ./secrets/s3_admin_secret + s3_access_key_id: + file: ./secrets/s3_access_key_id + s3_secret_access_key: + file: ./secrets/s3_secret_access_key postgres_password: file: ./secrets/postgres_password database_url: @@ -111,4 +167,5 @@ secrets: file: ./secrets/cursor_secret volumes: + s3_data: postgres_data: diff --git a/evidence/attachment-failover.txt b/evidence/attachment-failover.txt new file mode 100644 index 00000000..d353ffc9 --- /dev/null +++ b/evidence/attachment-failover.txt @@ -0,0 +1 @@ +PASS: uploaded via api-b; stopped that container; original downloaded through another replica diff --git a/evidence/attachments.txt b/evidence/attachments.txt new file mode 100644 index 00000000..0c7d7fe1 --- /dev/null +++ b/evidence/attachments.txt @@ -0,0 +1,4 @@ +PASS: PNG/JPEG originals, metadata, attachment-only messages and ACL +PASS: filename, MIME, malformed image, byte/pixel and chunked limits +PASS: attachment scope, duplicates and idempotency normalization +PASS: inclusive 10 MiB and 40 million pixel boundaries diff --git a/evidence/bootstrap.txt b/evidence/bootstrap.txt new file mode 100644 index 00000000..99b2bb8a --- /dev/null +++ b/evidence/bootstrap.txt @@ -0,0 +1,3 @@ + Container mtusi2026lab4-s3-init-run-459a847a8091 Creating + Container mtusi2026lab4-s3-init-run-459a847a8091 Created +PASS: private S3 bucket ready diff --git a/evidence/contracts.txt b/evidence/contracts.txt new file mode 100644 index 00000000..ed9cd475 --- /dev/null +++ b/evidence/contracts.txt @@ -0,0 +1,4 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. diff --git a/evidence/environment.json b/evidence/environment.json new file mode 100644 index 00000000..6394fdf7 --- /dev/null +++ b/evidence/environment.json @@ -0,0 +1,107 @@ +{ + "recorded_at_utc": "2026-10-20T14:23:50.123531+00:00", + "base_commit": "3b52b91fb76d8acdeb7cbec010fd5542fd8990f7", + "branch": "solution/BVT2301-Kozyrev", + "note": "Results apply to this working tree, not only the base commit. Functional tests; no load benchmark.", + "host": "macOS-26.1-arm64-arm-64bit-Mach-O", + "docker": { + "server": "29.8.0", + "cpus": 10, + "memory_bytes": 10416644096 + }, + "compose_version": "5.5.1", + "sources_sha256": { + "Dockerfile": "efede941e2ccf21cd1e70ebbf8af7647209bd030b2962f9446bee497a2ed6a8c", + "Makefile": "bdb2dade89d84f516b20097ee8f33db7b85dd6bfa431c41881177a323cea845e", + "compose.yaml": "47d7c77d070097cef515279c3e46435bf0925d9b82dd96ef88fa38534e217328", + "infra/README.md": "0e804fa51debba4c38d3cc84abf4704897665e0335b272060363f03aadd6354f", + "infra/filer.toml": "ae3c01bf7708f5d4a319fdb454f753be0f137fe7c31269f1c413cbccc860f5c7", + "infra/haproxy.cfg": "23c9b869170633e3c6a8c41bbd3f1102bc97bf213e0d7cfed9d461af91216fd3", + "migrations/001_initial.sql": "062e58f5535d1ab6cd89d2347479a2a6b08a09e04665f7428ce41f89b39a19d1", + "migrations/002_history_cache_version.sql": "826c8243bc148caaa7339070826a7c2928073315a40dba7fa5a3d7421d980a4d", + "migrations/003_idempotency.sql": "d650ea7c1379928a013e035977a77026d2b8b09ad5b2ea97eecb545e7450353a", + "migrations/004_attachments.sql": "d760453ba130c94c403f5b04222649fc131ab4d091edd0267ede901fdf82a79e", + "pyproject.toml": "d3962edc7a683a62c915c8edec91e85c647c8ae6fe6432603e30b595bd6c3804", + "scripts/check_contract.py": "7c4402c2a6ab80d3b4676e1866d6ecc87b6daea14c080bcb9be9a7e8b959d671", + "scripts/init-secrets.py": "fc89fc740da189b65be1b69980ff512e2c233dd09997c8ffb0198d9368118386", + "scripts/record-environment.py": "716fa7b8edd05d7ab6781fc346338b2254f82186953c6dbf525785c723eee4a1", + "scripts/restart.example.sh": "0328246ba75defbced992104e986fde973f63e3830d83251eced5953263e944b", + "scripts/restart.sh": "0a64716273eb5c79011d99e4734b18ed5a3a28f41b8b1d4ea6b24d0a4ffc702f", + "scripts/stop-one.example.sh": "1bcf34394520b89d95b7605ec56675be799eea920e83cfbba306d5dc0a6d9811", + "scripts/stop-one.sh": "c69fdb1891e0c49f41d3892f2d108ecd38b2f59d505c8fe991023bb5c9b08a5e", + "scripts/validate_openapi.py": "4e981acbfc10dac58817bf922d5896e996798551fedc1b2385130e1cddb13d54", + "scripts/verify-persistence.py": "6c366bf4d85802bcffe65ae3d45d00736b184033313dabb60b80e1270d8874f1", + "src/__init__.py": "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b", + "src/attachments.py": "b377227a623bc41585f2eca487356bbd38c59a0ada56f425b8fca11abd93c085", + "src/bootstrap_s3.py": "bd0bafad4f5443bd37ab0d14c7b49453ae77fda059076f59cc02e9abc08bbc65", + "src/config.py": "6cb85b839e2947c3334c52f24d94ceb5290103e323ba33e6ab59f10f3ae3a53a", + "src/cursor.py": "a9192f2445042b8d4ddac57031718ef558990a52eb2922827074479b7f686775", + "src/errors.py": "07f8860b45cadd242d0d9f268c4b806e35353f33b11bbde0ab3d50acda86a80a", + "src/main.py": "abb5d7a856bff6d19ada7cf17addd59ef7400a26cf795ec01ea8b7d76f44527a", + "src/migrate.py": "1d164c8921df2b5369e30b1f8936c2b71858c5279070f0fd593f62f66e1f0b7d", + "src/models.py": "b94e6b26559afc5fd90f58d34d906c77bf4b4a78c716ac552358795496ad1d1b", + "src/routes.py": "3c21149a30ea7c2f695547c42133dcebcfbacbd6544bcb73c353be46fa35a2e6", + "src/storage/__init__.py": "05ef66318c4c436eaafc493072640817219ab37bce464a265a2306233389064a", + "src/storage/postgres.py": "2ff296490b06729c8e56eb97dbdc8d1ca91c94e313375ea5a88ffec99b6f838d", + "src/storage/s3.py": "433247b86837a9a1212900da9d3241105e191b36d8fcac8a92c50100f5f4b946", + "student-tests/attachment_resilience.py": "c72c431b355dda919c1b6b854d260f8c7420672ca9f86bf0848692da35dcb0ac", + "student-tests/attachments.py": "a948fabfe83b73eb8e494c80fb9d1f2a949e2a95b7a21809a05975d65d68fc15", + "student-tests/idempotency_concurrency.py": "db5d139009e4660ac1801af45d10e24665aa81cba737731e527c0f5b4152231e", + "student-tests/readiness_failover.py": "0ef8355d85be66597d7d44eb7308dcb58c6c9a174d18f360b2d6d22fdcfbed14", + "student-tests/readiness_unavailable.py": "46e3f680a3ff9cb79e95568004d97f1752dbdcd88fd4b5f160a9cc38af65a237", + "student-tests/review_regressions.py": "de58f63b2c794c2775247ddd3fccc523870ccbe673ffa404fdd566274d1d41de", + "student-tests/session_expiry.py": "ba8ead4356e4d12c4047ac2b5704d3d3342f71c37b3c13f65a0a9d7c37186ad6", + "uv.lock": "fdbeb5ba0c5eedced1a6e5b62c6a57b2d22f293cf77b9487a806c6e1a8b992a0" + }, + "services": { + "postgres": { + "image": "postgres:17.6-alpine", + "image_id": "sha256:ef257d85f76e48da1c64832459b59fcaba1a4dac97bf5d7450c77753542eee94", + "state": "running", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus_limit": 750000000, + "volumes": [ + "mtusi2026lab4_postgres_data" + ] + }, + "s3": { + "image": "chrislusf/seaweedfs:4.17@sha256:186de7ef977a20343ee9a5544073f081976a29e2d29ecf8379891e7bf177fbe9", + "image_id": "sha256:186de7ef977a20343ee9a5544073f081976a29e2d29ecf8379891e7bf177fbe9", + "state": "running", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus_limit": 1000000000, + "volumes": [ + "mtusi2026lab4_s3_data" + ] + }, + "api-a": { + "image": "mtusi2026lab4-api-a", + "image_id": "sha256:3a32a3f312aff9b46c6de73815627556af6f7cbee4b22783c1f4185d407f01b3", + "state": "running", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus_limit": 500000000, + "volumes": [] + }, + "api-b": { + "image": "mtusi2026lab4-api-b", + "image_id": "sha256:0e3c794618677c4a6305586bf44165fb1b3dbe668ce77b583df97e9e72721aa1", + "state": "running", + "health": "healthy", + "memory_limit_bytes": 536870912, + "nano_cpus_limit": 500000000, + "volumes": [] + }, + "load_balancer": { + "image": "haproxy:3.0.11-alpine", + "image_id": "sha256:e49b7beb96f4dba79f9a671515b469a6e516a2b88ff0681a820b184bba1effbd", + "state": "running", + "health": "healthy", + "memory_limit_bytes": 134217728, + "nano_cpus_limit": 250000000, + "volumes": [] + } + } +} diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 00000000..ee4b8fed --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,4 @@ +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 111, "failed_attempts": 1, "first_recovery_seconds": 2.791} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. diff --git a/evidence/lab3-regressions.txt b/evidence/lab3-regressions.txt new file mode 100644 index 00000000..9abf5c19 --- /dev/null +++ b/evidence/lab3-regressions.txt @@ -0,0 +1,2 @@ +PASS: concurrent idempotency through load balancer +PASS: non-ASCII Bearer returns 401/challenge; malformed JSON returns contract Error/400 diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..0d58f0c1 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,5 @@ +Before: {"api-a": {"container": "eca67c29fa27", "started_at": "2026-10-20T14:17:13.97672451Z"}, "api-b": {"container": "71c5a224f05f", "started_at": "2026-10-20T14:19:07.971967673Z"}, "load_balancer": {"container": "3e69e9c37346", "started_at": "2026-10-20T14:17:19.597909846Z"}, "postgres": {"container": "b756a119c924", "started_at": "2026-10-20T14:17:06.843762048Z"}, "s3": {"container": "59fd3a3bca31", "started_at": "2026-10-20T14:19:38.783873424Z"}} +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +After: {"api-a": {"container": "eca67c29fa27", "started_at": "2026-10-20T14:20:18.95842697Z"}, "api-b": {"container": "71c5a224f05f", "started_at": "2026-10-20T14:20:18.82218322Z"}, "load_balancer": {"container": "3e69e9c37346", "started_at": "2026-10-20T14:20:18.598004429Z"}, "postgres": {"container": "b756a119c924", "started_at": "2026-10-20T14:20:10.227233258Z"}, "s3": {"container": "59fd3a3bca31", "started_at": "2026-10-20T14:20:13.232762718Z"}} +PASS: PostgreSQL, S3, both API replicas and LB actually restarted; volumes preserved diff --git a/evidence/private-bucket.txt b/evidence/private-bucket.txt new file mode 100644 index 00000000..c64957f4 --- /dev/null +++ b/evidence/private-bucket.txt @@ -0,0 +1 @@ +PASS: anonymous S3 GET returns 403 for an existing object diff --git a/evidence/s3-outage.txt b/evidence/s3-outage.txt new file mode 100644 index 00000000..89f5204a --- /dev/null +++ b/evidence/s3-outage.txt @@ -0,0 +1,2 @@ +PASS: S3 outage gives 503 for readiness, download and upload; liveness stays 200 +PASS: original available again after S3 restart diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..1e7b9ff4 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,2 @@ +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/infra/filer.toml b/infra/filer.toml new file mode 100644 index 00000000..85b2149c --- /dev/null +++ b/infra/filer.toml @@ -0,0 +1,3 @@ +[leveldb2] +enabled = true +dir = "/data/filer" diff --git a/migrations/004_attachments.sql b/migrations/004_attachments.sql new file mode 100644 index 00000000..2fe0f9e2 --- /dev/null +++ b/migrations/004_attachments.sql @@ -0,0 +1,18 @@ +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL REFERENCES users(id), + filename varchar(255) NOT NULL, + content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 char(64) NOT NULL, + object_key text NOT NULL UNIQUE, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now(), + UNIQUE (id, chat_id) +); +ALTER TABLE messages DROP CONSTRAINT messages_text_check; +ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}'; +ALTER TABLE messages ADD CONSTRAINT messages_content_check + CHECK (char_length(btrim(text)) > 0 OR cardinality(attachment_ids) > 0); +CREATE INDEX attachments_chat_idx ON attachments(chat_id); diff --git a/pyproject.toml b/pyproject.toml index 7de48cee..4c35468f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,8 @@ requires-python = ">=3.12" dependencies = [ "argon2-cffi==25.1.0", "asyncpg==0.31.0", + "boto3==1.43.115", "fastapi==0.141.1", + "pillow==13.0.0", "uvicorn==0.40.0", ] diff --git a/scripts/init-secrets.py b/scripts/init-secrets.py new file mode 100644 index 00000000..1e1f6954 --- /dev/null +++ b/scripts/init-secrets.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Create local credentials without printing or overwriting existing values.""" +import json +from pathlib import Path +import secrets + +root = Path(__file__).resolve().parents[1] / "secrets" +root.mkdir(mode=0o700, exist_ok=True) + + +def write(name, value): + path = root / name + if path.exists(): + return path.read_text().strip() + with path.open("x") as stream: + path.chmod(0o600) + stream.write(value + "\n") + return value + + +password = write("postgres_password", secrets.token_hex(32)) +write("database_url", f"postgresql://messenger:{password}@postgres:5432/messenger") +write("cursor_secret", secrets.token_hex(32)) +admin_key = write("s3_admin_key", secrets.token_hex(16)) +admin_secret = write("s3_admin_secret", secrets.token_hex(32)) +app_key = write("s3_access_key_id", secrets.token_hex(16)) +app_secret = write("s3_secret_access_key", secrets.token_hex(32)) +write("s3.json", json.dumps({"identities": [ + {"name": "bootstrap", "credentials": [{"accessKey": admin_key, "secretKey": admin_secret}], + "actions": ["Admin", "Read", "Write", "List", "Tagging"]}, + {"name": "api", "credentials": [{"accessKey": app_key, "secretKey": app_secret}], + "actions": ["Read:messenger-attachments", "Write:messenger-attachments", "List:messenger-attachments"]}, +]}, indent=2)) +print("Local secrets ready; existing values preserved.") diff --git a/scripts/record-environment.py b/scripts/record-environment.py new file mode 100644 index 00000000..e983ce94 --- /dev/null +++ b/scripts/record-environment.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Sanitized environment and source fingerprint for local test evidence.""" +from datetime import datetime, timezone +from hashlib import sha256 +import json +from pathlib import Path +import platform +import subprocess + +root = Path(__file__).resolve().parents[1] + + +def command(*args): + return subprocess.check_output(args, cwd=root, text=True).strip() + + +paths = [root / name for name in ('Dockerfile', 'compose.yaml', 'pyproject.toml', 'uv.lock', 'Makefile')] +for directory in ('src', 'migrations', 'infra', 'scripts', 'student-tests'): + paths.extend(path for path in (root / directory).rglob('*') + if path.is_file() and '__pycache__' not in path.parts) +report = { + 'recorded_at_utc': datetime.now(timezone.utc).isoformat(), + 'base_commit': command('git', 'rev-parse', 'HEAD'), + 'branch': command('git', 'branch', '--show-current'), + 'note': 'Results apply to this working tree, not only the base commit. Functional tests; no load benchmark.', + 'host': platform.platform(), + 'docker': json.loads(command('docker', 'info', '--format', + '{"server":"{{.ServerVersion}}","cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}')), + 'compose_version': command('docker', 'compose', 'version', '--short'), + 'sources_sha256': {str(path.relative_to(root)): sha256(path.read_bytes()).hexdigest() for path in sorted(paths)}, + 'services': {}, +} +for service in ('postgres', 's3', 'api-a', 'api-b', 'load_balancer'): + container = command('docker', 'compose', 'ps', '-q', service) + info = json.loads(command('docker', 'inspect', container))[0] + report['services'][service] = { + 'image': info['Config']['Image'], 'image_id': info['Image'], + 'state': info['State']['Status'], 'health': info['State'].get('Health', {}).get('Status'), + 'memory_limit_bytes': info['HostConfig']['Memory'], + 'nano_cpus_limit': info['HostConfig']['NanoCpus'], + 'volumes': [mount['Name'] for mount in info['Mounts'] if mount['Type'] == 'volume'], + } +(root / 'evidence/environment.json').write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') +print('PASS: sanitized environment and source hashes saved') diff --git a/scripts/restart.sh b/scripts/restart.sh index e0d9abaa..aa02c3b6 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -1,7 +1,7 @@ #!/bin/sh set -eu cd "$(dirname "$0")/.." -docker compose restart postgres -docker compose up -d --wait postgres +docker compose restart postgres s3 +docker compose up -d --wait postgres s3 docker compose restart api-a api-b load_balancer docker compose up -d --wait api-a api-b load_balancer diff --git a/scripts/verify-persistence.py b/scripts/verify-persistence.py new file mode 100644 index 00000000..5bfc5a44 --- /dev/null +++ b/scripts/verify-persistence.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +"""Public persistence test plus proof of actual container restarts.""" +import json +from pathlib import Path +import subprocess +import sys + +ROOT = Path(__file__).resolve().parents[1] + + +def snapshot(): + result = {} + for service in ('postgres', 's3', 'api-a', 'api-b', 'load_balancer'): + container = subprocess.check_output(['docker', 'compose', 'ps', '-q', service], cwd=ROOT, text=True).strip() + started = subprocess.check_output(['docker', 'inspect', '--format', '{{.State.StartedAt}}', container], text=True).strip() + result[service] = {'container': container[:12], 'started_at': started} + return result + + +before = snapshot() +print('Before:', json.dumps(before, sort_keys=True), flush=True) +subprocess.run([sys.executable, 'tests/resilience.py', '--mode', 'persistence', + '--action-script', 'scripts/restart.sh'], cwd=ROOT, check=True) +after = snapshot() +print('After:', json.dumps(after, sort_keys=True), flush=True) +assert all(before[key]['started_at'] != after[key]['started_at'] for key in before) +print('PASS: PostgreSQL, S3, both API replicas and LB actually restarted; volumes preserved') diff --git a/src/attachments.py b/src/attachments.py new file mode 100644 index 00000000..a078a7c2 --- /dev/null +++ b/src/attachments.py @@ -0,0 +1,104 @@ +import asyncio +from hashlib import sha256 +from tempfile import TemporaryFile +import unicodedata +from uuid import UUID, uuid4 +import warnings + +from fastapi import APIRouter, Depends, Query, Request, Response +from PIL import Image, UnidentifiedImageError +from starlette.concurrency import run_in_threadpool + +from .errors import ApiError +from .models import Attachment +from .routes import get_current_user, get_store +from .storage.postgres import PostgresStore + +router = APIRouter() +Image.MAX_IMAGE_PIXELS = 40000000 +warnings.filterwarnings("error", category=Image.DecompressionBombWarning) + + +def inspect_image(file, content_type, max_pixels): + try: + file.seek(0) + with Image.open(file, formats=("PNG", "JPEG")) as image: + if image.width * image.height > max_pixels: + raise ApiError(413, "payload_too_large", "Слишком много пикселей") + actual = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format] + if actual != content_type or getattr(image, "n_frames", 1) != 1: + raise ApiError(415, "unsupported_media_type", "Нужен обычный PNG или JPEG") + image.verify() + # verify checks PNG CRC; load also catches truncated pixel streams/JPEGs. + file.seek(0) + with Image.open(file, formats=("PNG", "JPEG")) as image: + image.load() + except (Image.DecompressionBombError, Image.DecompressionBombWarning) as error: + raise ApiError(413, "payload_too_large", "Слишком много пикселей") from error + except (UnidentifiedImageError, OSError, ValueError, SyntaxError) as error: + raise ApiError(415, "unsupported_media_type", "Повреждённое изображение") from error + file.seek(0) + + +@router.post("/api/v1/chats/{chat_id}/attachments", response_model=Attachment, status_code=201) +async def upload(chat_id: UUID, request: Request, filename: str = Query(...), + user: dict = Depends(get_current_user), store: PostgresStore = Depends(get_store)): + await store.get_chat_for_member(chat_id, user["id"]) + if (not 1 <= len(filename) <= 255 or not filename.strip() or + any(c in "/\\" or unicodedata.category(c) in {"Cc", "Cs"} for c in filename)): + raise ApiError(400, "invalid_request", "Некорректное имя файла") + content_type = request.headers.get("content-type", "").lower() + if content_type not in {"image/png", "image/jpeg"}: + raise ApiError(415, "unsupported_media_type", "Нужен PNG или JPEG") + s3 = request.app.state.s3 + limit = s3.settings.max_bytes + if "content-length" in request.headers: + try: + length = int(request.headers["content-length"]) + if length < 0: + raise ValueError() + except ValueError: + raise ApiError(400, "invalid_request", "Некорректная длина") from None + if length > limit: + raise ApiError(413, "payload_too_large", "Файл больше лимита") + try: + async with asyncio.timeout(30): + async with request.app.state.upload_slot: + # Temporary scratch space only; deleted on every exit, never used for downloads. + with TemporaryFile() as file: + size = 0 + digest = sha256() + async for chunk in request.stream(): + size += len(chunk) + if size > limit: + raise ApiError(413, "payload_too_large", "Файл больше лимита") + digest.update(chunk) + await run_in_threadpool(file.write, chunk) + await run_in_threadpool(inspect_image, file, content_type, s3.settings.max_pixels) + attachment_id = uuid4() + key = f"originals/{attachment_id}" + await s3.put(key, file, size, content_type) + return await store.create_attachment( + attachment_id, chat_id, user["id"], filename, content_type, + size, digest.hexdigest(), key, + ) + except TimeoutError: + raise ApiError(503, "unavailable", "Время загрузки истекло") from None + + +@router.get("/api/v1/attachments/{attachment_id}", response_model=Attachment) +async def metadata(attachment_id: UUID, user: dict = Depends(get_current_user), + store: PostgresStore = Depends(get_store)): + return await store.attachment_for_member(attachment_id, user["id"]) + + +@router.get("/api/v1/attachments/{attachment_id}/content") +async def download(attachment_id: UUID, request: Request, + variant: str = Query("original", pattern="^(original|thumbnail|crop|optimized)$"), + user: dict = Depends(get_current_user), store: PostgresStore = Depends(get_store)): + attachment = await store.attachment_for_member(attachment_id, user["id"]) + if variant != "original": + raise ApiError(404, "not_found", "Вариант не найден") + data = await request.app.state.s3.download(attachment) + return Response(data, media_type=attachment["content_type"], + headers={"X-Content-Type-Options": "nosniff", "Cache-Control": "private, no-store"}) diff --git a/src/bootstrap_s3.py b/src/bootstrap_s3.py new file mode 100644 index 00000000..5f90eebe --- /dev/null +++ b/src/bootstrap_s3.py @@ -0,0 +1,38 @@ +"""Idempotent private bucket bootstrap; executed with separate admin credentials.""" +import time +from botocore.exceptions import BotoCoreError, ClientError +from .storage.s3 import S3Settings, S3Store + + +def main(): + store = S3Store(S3Settings.load()) + try: + for attempt in range(60): + try: + store.client.list_buckets() + break + except (BotoCoreError, ClientError): + if attempt == 59: + raise RuntimeError("S3 startup timed out") from None + time.sleep(1) + bucket = store.settings.bucket + try: + store.client.head_bucket(Bucket=bucket) + except ClientError as error: + if str(error.response["Error"]["Code"]) not in {"404", "NoSuchBucket", "NotFound"}: + raise + store.client.create_bucket(Bucket=bucket) + # No anonymous identity; revoke any previously configured public grants/policy. + store.client.put_bucket_acl(Bucket=bucket, ACL="private") + try: + store.client.delete_bucket_policy(Bucket=bucket) + except ClientError as error: + if error.response["Error"]["Code"] != "NoSuchBucketPolicy": + raise + print("PASS: private S3 bucket ready") + finally: + store.client.close() + + +if __name__ == "__main__": + main() diff --git a/src/config.py b/src/config.py index f68a8b0c..091c861b 100644 --- a/src/config.py +++ b/src/config.py @@ -15,6 +15,10 @@ _CONFIGURATION_KEYS = { "INSTANCE_ID", "CURSOR_SECRET", "CURSOR_SECRET_FILE", + "S3_ENDPOINT", "S3_REGION", "S3_BUCKET", + "S3_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID_FILE", + "S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY_FILE", + "MAX_UPLOAD_BYTES", "MAX_IMAGE_PIXELS", } diff --git a/src/main.py b/src/main.py index cd876651..30df8478 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import asyncio import logging import sys import time @@ -15,6 +16,8 @@ from .cursor import configure_secret from .models import ErrorDetails, ErrorResponse from .config import load_settings from .routes import router +from .attachments import router as attachment_router +from .storage.s3 import S3Settings, S3Store from .storage.postgres import PostgresStore @@ -41,17 +44,20 @@ async def lifespan(application: FastAPI): await store.connect() application.state.store = store application.state.settings = settings + application.state.s3 = S3Store(S3Settings.load()) + application.state.upload_slot = asyncio.Semaphore(1) application.state.accepting_requests = True try: yield finally: application.state.accepting_requests = False + application.state.s3.client.close() await store.close() app = FastAPI( title="MTUSI Messenger", - version="1.3.0", + version="1.4.0", redoc_url=None, lifespan=lifespan, ) @@ -144,6 +150,7 @@ async def handle_unexpected_error(request: Request, error: Exception) -> JSONRes app.include_router(router) +app.include_router(attachment_router) def main() -> None: diff --git a/src/models.py b/src/models.py index 095ec1a2..8dea91cf 100644 --- a/src/models.py +++ b/src/models.py @@ -1,7 +1,7 @@ from datetime import datetime from uuid import UUID -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator class CreateUserRequest(BaseModel): model_config = ConfigDict(extra="forbid", strict=True) @@ -46,16 +46,22 @@ class ChatPageResponse(BaseModel): class CreateMessageRequest(BaseModel): model_config = ConfigDict(extra="forbid", strict=True) - text: str = Field(min_length=1, max_length=4000) + text: str = Field(max_length=4000) + attachment_ids: list[str] = Field(default_factory=list, max_length=10) - @field_validator("text") + @field_validator("attachment_ids") @classmethod - def text_must_not_be_whitespace(cls, value: str) -> str: - """Не допускает сообщение, состоящее только из пробельных символов.""" + def valid_ids(cls, values: list[str]) -> list[str]: + normalized = [str(UUID(value)) for value in values] + if len(normalized) != len(set(normalized)): + raise ValueError("Повторяющиеся attachment_ids") + return normalized - if not value.strip(): - raise ValueError("text не может состоять только из пробелов") - return value + @model_validator(mode="after") + def has_content(self): + if not self.text.strip() and not self.attachment_ids: + raise ValueError("Сообщение без содержимого") + return self class MessagePageResponse(BaseModel): @@ -104,6 +110,7 @@ class Message(BaseModel): chat_id: UUID sender_id: UUID text: str + attachment_ids: list[UUID] = Field(default_factory=list) created_at: datetime @@ -114,3 +121,20 @@ class Session(BaseModel): token_type: str = "Bearer" expires_at: datetime user: User + + +class Attachment(BaseModel): + model_config = ConfigDict(extra="ignore") + + id: UUID + chat_id: UUID + owner_id: UUID + filename: str + content_type: str + size_bytes: int + sha256: str + status: str = "ready" + variants: list = Field(default_factory=list) + error_code: str | None = None + created_at: datetime + updated_at: datetime diff --git a/src/routes.py b/src/routes.py index c07e4056..5bc2a3df 100644 --- a/src/routes.py +++ b/src/routes.py @@ -83,6 +83,7 @@ async def readiness(request: Request, store: PostgresStore = Depends(get_store)) if not request.app.state.accepting_requests: raise ApiError(503, "unavailable", "API завершает работу") await store.ready() + await request.app.state.s3.ready() return {"status": "ok"} @router.post("/api/v1/users", response_model=User, status_code=201) @@ -164,6 +165,7 @@ async def create_message( payload.text, idempotency_key, request.app.state.settings.idempotency_ttl_seconds, + [UUID(value) for value in payload.attachment_ids], ) @router.get("/api/v1/chats/{chat_id}/messages", response_model=MessagePageResponse) diff --git a/src/storage/postgres.py b/src/storage/postgres.py index 67b8f815..42695ec4 100644 --- a/src/storage/postgres.py +++ b/src/storage/postgres.py @@ -83,6 +83,7 @@ class PostgresStore: "chat_id": row["chat_id"], "sender_id": row["sender_id"], "text": row["text"], + "attachment_ids": row["attachment_ids"], "created_at": row["created_at"], } @@ -216,12 +217,17 @@ class PostgresStore: return [self._chat(row) for row in rows] async def create_message_idempotent( - self, chat_id: UUID, sender_id: UUID, text: str, idempotency_key: str, ttl_seconds: int + self, chat_id: UUID, sender_id: UUID, text: str, idempotency_key: str, ttl_seconds: int, attachment_ids: list[UUID] | None = None ) -> dict: """Создаёт сообщение и фиксирует результат retry в одной транзакции.""" path = f"/api/v1/chats/{chat_id}/messages" + attachment_ids = attachment_ids or [] + # Preserve hashes of text-only requests already accepted by lab3. + canonical = {"text": text} + if attachment_ids: + canonical["attachment_ids"] = [str(value) for value in attachment_ids] request_hash = sha256( - json.dumps({"text": text}, ensure_ascii=False, separators=(",", ":"), sort_keys=True).encode("utf-8") + json.dumps(canonical, ensure_ascii=False, separators=(",", ":"), sort_keys=True).encode("utf-8") ).hexdigest() try: async with self.pool.acquire() as connection, connection.transaction(): @@ -233,6 +239,13 @@ class PostgresStore: await connection.execute( "DELETE FROM idempotency_records WHERE created_at <= now() - $1 * interval '1 second'", ttl_seconds ) + if attachment_ids: + count = await connection.fetchval( + "SELECT count(*) FROM attachments WHERE chat_id = $1 AND id = ANY($2::uuid[])", + chat_id, attachment_ids, + ) + if count != len(attachment_ids): + raise ApiError(404, "not_found", "Вложение не найдено") claimed = await connection.fetchrow( "INSERT INTO idempotency_records (user_id, method, path, idempotency_key, request_hash) " "VALUES ($1, 'POST', $2, $3, $4) ON CONFLICT DO NOTHING RETURNING message_id", @@ -251,15 +264,15 @@ class PostgresStore: if recorded["message_id"] is None: raise ApiError(503, "unavailable", "Не удалось получить результат идемпотентного запроса") row = await connection.fetchrow( - "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE id = $1", recorded["message_id"] + "SELECT id, chat_id, sender_id, text, attachment_ids, created_at FROM messages WHERE id = $1", recorded["message_id"] ) if row is None: raise ApiError(503, "unavailable", "Не удалось получить сохранённое сообщение") return self._message(row) row = await connection.fetchrow( - "INSERT INTO messages (id, chat_id, sender_id, text) VALUES ($1, $2, $3, $4) " - "RETURNING id, chat_id, sender_id, text, created_at", - uuid4(), chat_id, sender_id, text, + "INSERT INTO messages (id, chat_id, sender_id, text, attachment_ids) VALUES ($1, $2, $3, $4, $5) " + "RETURNING id, chat_id, sender_id, text, attachment_ids, created_at", + uuid4(), chat_id, sender_id, text, attachment_ids, ) await connection.execute( "UPDATE idempotency_records SET message_id = $1 WHERE user_id = $2 AND method = 'POST' " @@ -274,7 +287,7 @@ class PostgresStore: self, chat_id: UUID, user_id: UUID, after: tuple[datetime, UUID] | None, limit: int ) -> list[dict]: await self.get_chat_for_member(chat_id, user_id) - query = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = $1" + query = "SELECT id, chat_id, sender_id, text, attachment_ids, created_at FROM messages WHERE chat_id = $1" args: list = [chat_id] if after is not None: query += " AND (created_at, id) > ($2, $3)" @@ -286,3 +299,28 @@ class PostgresStore: except DB_ERRORS as error: raise ApiError(503, "unavailable", "PostgreSQL недоступен") from error return [self._message(row) for row in rows] + + + async def create_attachment(self, attachment_id, chat_id, owner_id, filename, + content_type, size_bytes, digest, object_key) -> dict: + try: + row = await self.pool.fetchrow( + "INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key) " + "VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING *", + attachment_id, chat_id, owner_id, filename, content_type, size_bytes, digest, object_key, + ) + except DB_ERRORS as error: + raise ApiError(503, "unavailable", "PostgreSQL недоступен") from error + return dict(row) + + async def attachment_for_member(self, attachment_id: UUID, user_id: UUID) -> dict: + try: + row = await self.pool.fetchrow( + "SELECT a.* FROM attachments a JOIN chat_members m ON m.chat_id = a.chat_id " + "WHERE a.id = $1 AND m.user_id = $2", attachment_id, user_id, + ) + except DB_ERRORS as error: + raise ApiError(503, "unavailable", "PostgreSQL недоступен") from error + if row is None: + raise ApiError(404, "not_found", "Вложение не найдено") + return dict(row) diff --git a/src/storage/s3.py b/src/storage/s3.py new file mode 100644 index 00000000..1fea6569 --- /dev/null +++ b/src/storage/s3.py @@ -0,0 +1,85 @@ +"""Bounded S3 operations; synchronous SDK calls run outside the event loop.""" +from dataclasses import dataclass +from hashlib import sha256 +import os +from pathlib import Path + +import boto3 +from botocore.config import Config +from botocore.exceptions import BotoCoreError, ClientError +from starlette.concurrency import run_in_threadpool + +from ..errors import ApiError + + +@dataclass(frozen=True) +class S3Settings: + endpoint: str + region: str + bucket: str + access_key: str + secret_key: str + max_bytes: int + max_pixels: int + + @classmethod + def load(cls): + def secret(name): + path = os.environ.get(name + "_FILE") + result = Path(path).read_text().strip() if path else os.environ.get(name, "") + if not result: + raise ValueError(f"Missing {name}") + return result + + max_bytes = int(os.environ.get("MAX_UPLOAD_BYTES", "10485760")) + max_pixels = int(os.environ.get("MAX_IMAGE_PIXELS", "40000000")) + if not 1 <= max_bytes <= 10485760 or not 1 <= max_pixels <= 40000000: + raise ValueError("Image limits exceed contract") + return cls(os.environ.get("S3_ENDPOINT", "http://s3:8333"), + os.environ.get("S3_REGION", "us-east-1"), + os.environ.get("S3_BUCKET", "messenger-attachments"), + secret("S3_ACCESS_KEY_ID"), secret("S3_SECRET_ACCESS_KEY"), + max_bytes, max_pixels) + + +class S3Store: + def __init__(self, settings): + self.settings = settings + self.client = boto3.client( + "s3", endpoint_url=settings.endpoint, region_name=settings.region, + aws_access_key_id=settings.access_key, aws_secret_access_key=settings.secret_key, + config=Config(signature_version="s3v4", connect_timeout=2, read_timeout=5, + retries={"total_max_attempts": 1}, + s3={"addressing_style": "path"}, + request_checksum_calculation="when_required", + response_checksum_validation="when_required"), + ) + + async def call(self, method, **kwargs): + try: + return await run_in_threadpool(getattr(self.client, method), **kwargs) + except (BotoCoreError, ClientError, OSError) as error: + raise ApiError(503, "unavailable", "S3 недоступно") from error + + async def ready(self): + await self.call("head_bucket", Bucket=self.settings.bucket) + + async def put(self, key, file, size, content_type): + file.seek(0) + await self.call("put_object", Bucket=self.settings.bucket, Key=key, + Body=file, ContentLength=size, ContentType=content_type) + + async def download(self, attachment): + response = await self.call("get_object", Bucket=self.settings.bucket, + Key=attachment["object_key"]) + body = response["Body"] + try: + data = await run_in_threadpool(body.read, self.settings.max_bytes + 1) + except (BotoCoreError, OSError) as error: + raise ApiError(503, "unavailable", "S3 недоступно") from error + finally: + body.close() + if (len(data) != attachment["size_bytes"] or + sha256(data).hexdigest() != attachment["sha256"]): + raise ApiError(503, "unavailable", "Оригинал недоступен") + return data diff --git a/student-tests/attachment_resilience.py b/student-tests/attachment_resilience.py new file mode 100644 index 00000000..83ebe368 --- /dev/null +++ b/student-tests/attachment_resilience.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 +"""Explicit local Docker tests: private bucket, uploader failover, S3 outage.""" +import json +from pathlib import Path +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, Failure, arguments, check, run + + +def compose(*args, input=None): + return subprocess.check_output(['docker', 'compose', *args], cwd=ROOT, input=input, + text=True, stderr=subprocess.DEVNULL) + + +def wait_ready(c): + deadline = time.monotonic() + 90 + while True: + try: + c.call('GET', '/health/ready', schema='Health') + return + except Failure: + check(time.monotonic() < deadline, 'stack did not recover') + time.sleep(1) + + +def main(): + def configure(parser): + parser.add_argument('--mode', required=True, choices=['private', 'failover', 's3-outage']) + args = arguments(__doc__, configure) + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + data = (ROOT / 'tests/fixtures/sample.png').read_bytes() + uploaded = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png', + 201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment') + att = uploaded['json'] + content_path = '/api/v1/attachments/' + att['id'] + '/content' + if args.mode == 'private': + script = '''import sys, urllib.request, urllib.error +url = "http://s3:8333/messenger-attachments/originals/" + sys.stdin.read().strip() +try: + urllib.request.urlopen(url, timeout=5) +except urllib.error.HTTPError as error: + assert error.code == 403, error.code + print("PASS: anonymous S3 GET returns 403 for an existing object") +else: + raise AssertionError("private object leaked") +''' + print(compose('exec', '-T', 'api-a', '/app/.venv/bin/python', '-c', script, input=att['id']).strip()) + elif args.mode == 'failover': + target = uploaded['headers']['X-Instance-Id'] + service = None + for candidate in ('api-a', 'api-b'): + container = compose('ps', '-q', candidate).strip() + hostname = subprocess.check_output(['docker', 'inspect', '--format', '{{.Config.Hostname}}', container], text=True).strip() + if hostname == target: + service = candidate + check(service is not None, 'uploading replica not found') + try: + compose('stop', service) + deadline = time.monotonic() + 15 + while True: + try: + download = c.call('GET', content_path, actor=bob) + break + except Failure: + check(time.monotonic() < deadline, 'download did not recover') + time.sleep(0.5) + check(download['headers']['X-Instance-Id'] != target, 'download used stopped uploader') + check(download['bytes'] == data, 'original changed after stopping uploader') + print('PASS: uploaded via ' + service + '; stopped that container; original downloaded through another replica') + finally: + compose('start', service) + wait_ready(c) + else: + try: + compose('stop', 's3') + # Direct API request inside the network: LB excludes unready backends. + script = '''import json, sys, urllib.request, urllib.error +payload = json.load(sys.stdin) +for method, path, body, headers in [ + ("GET", "/health/ready", None, {}), + ("GET", payload["path"], None, {"Authorization": "Bearer " + payload["token"]}), + ("POST", payload["upload_path"], bytes.fromhex(payload["bytes"]), + {"Authorization": "Bearer " + payload["token"], "Content-Type": "image/png"}), +]: + req = urllib.request.Request("http://localhost:8080" + path, data=body, headers=headers, method=method) + try: + urllib.request.urlopen(req, timeout=15) + except urllib.error.HTTPError as error: + assert error.code == 503, error.code + document = json.load(error) + assert document["error"]["code"] == "unavailable" + assert document["error"]["request_id"] == error.headers["X-Request-Id"] + else: + raise AssertionError("S3 outage was not reported") +assert urllib.request.urlopen("http://localhost:8080/health/live").status == 200 +print("PASS: S3 outage gives 503 for readiness, download and upload; liveness stays 200") +''' + payload = {'path': content_path, 'token': bob['token'], 'bytes': data.hex(), + 'upload_path': '/api/v1/chats/' + chat['id'] + '/attachments?filename=outage.png'} + print(compose('exec', '-T', 'api-a', '/app/.venv/bin/python', '-c', script, + input=json.dumps(payload)).strip()) + finally: + compose('start', 's3') + wait_ready(c) + check(c.call('GET', content_path, actor=bob)['bytes'] == data, 'original lost after S3 outage') + print('PASS: original available again after S3 restart') + + +if __name__ == '__main__': + run(main) diff --git a/student-tests/attachments.py b/student-tests/attachments.py new file mode 100644 index 00000000..81652326 --- /dev/null +++ b/student-tests/attachments.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +"""Attachment contract checks against the running lab4 stack.""" +import hashlib +import http.client +from io import BytesIO +from pathlib import Path +import struct +import sys +import uuid +import zlib +from urllib.parse import quote, urlsplit + +from PIL import Image + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client, arguments, check, run + + +def main(): + args = arguments(__doc__) + c = Client(args.base_url, args.ca_file, timeout=30) + alice, bob, eve, chat = c.bootstrap() + path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + png = (Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png').read_bytes() + + def upload(data, mime='image/png', filename='photo.png', status=201, actor=alice): + return c.call('POST', path + quote(filename, safe=''), status, actor=actor, raw=data, + headers={'Content-Type': mime}, schema='Attachment' if status == 201 else None) + + for fmt, mime in [('PNG', 'image/png'), ('JPEG', 'image/jpeg')]: + buf = BytesIO() + Image.new('RGB', (32, 20), 'blue').save(buf, fmt) + data = buf.getvalue() + att = upload(data, mime)['json'] + check(att['variants'] == [] and att['status'] == 'ready', 'wrong attachment state') + check(att['sha256'] == hashlib.sha256(data).hexdigest(), 'wrong checksum') + content_path = '/api/v1/attachments/' + att['id'] + check(c.call('GET', content_path, actor=bob, schema='Attachment')['json'] == att, + 'metadata changed') + check(c.call('GET', content_path + '/content', actor=bob)['bytes'] == data, 'bytes changed') + c.call('GET', content_path, 404, actor=eve) + c.call('GET', content_path + '/content', 404, actor=eve) + c.call('GET', content_path + '/content', 401) + c.call('GET', content_path + '/content?variant=thumbnail', 404, actor=bob) + message = c.message(bob, chat, '', attachments=[att['id']])['json'] + history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', + actor=alice, schema='MessagePage')['json']['items'] + check(message in history, 'attachment message missing in history') + print('PASS: PNG/JPEG originals, metadata, attachment-only messages and ACL') + + for name in ('../x.png', 'x/y.png', 'x\\y.png', 'x\x00.png', 'x\n.png', 'x\x7f.png', ' ', 'x' * 256): + upload(png, filename=name, status=400) + upload(png, mime='image/jpeg', status=415) + upload(b'not an image', status=415) + upload(png[:30], status=415) + upload(png, mime='application/octet-stream', status=415) + upload(png, status=404, actor=eve) + upload(b'x' * (10485760 + 1), status=413) + + # Valid PNG header with an oversized canvas: reject before attempting pixel decoding. + def chunk(kind, data): + return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data)) + huge = b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', 8001, 5000, 8, 2, 0, 0, 0)) + huge += chunk(b'IDAT', zlib.compress(b'')) + chunk(b'IEND', b'') + upload(huge, status=413) + + # No Content-Length: limits must also work for chunked requests. + url = urlsplit(c.base) + connection = http.client.HTTPConnection(url.hostname, url.port, timeout=30) + connection.request('POST', path + 'chunked.png', body=iter([b'x' * 65536] * 161), + headers={'Authorization': 'Bearer ' + alice['token'], 'Content-Type': 'image/png'}, + encode_chunked=True) + response = connection.getresponse() + check(response.status == 413, 'chunked upload bypassed byte limit') + response.read() + connection.close() + print('PASS: filename, MIME, malformed image, byte/pixel and chunked limits') + + att = upload(png)['json'] + other_chat = c.call('POST', '/api/v1/chats', 201, actor=alice, + body={'title': 'other', 'member_ids': [alice['id']]}, schema='Chat')['json'] + for target, ids, code in ((chat, [str(uuid.uuid4())], 404), + (other_chat, [att['id']], 404), + (chat, [att['id'], att['id']], 400)): + c.call('POST', '/api/v1/chats/' + target['id'] + '/messages', code, actor=alice, + body={'text': '', 'attachment_ids': ids}, headers={'Idempotency-Key': uuid.uuid4().hex}) + key = uuid.uuid4().hex + first = c.message(alice, chat, 'same', key=key)['json'] + check(c.message(alice, chat, 'same', key=key, attachments=[])['json'] == first, + 'omitted and empty attachment_ids differ') + key = uuid.uuid4().hex + first = c.message(alice, chat, '', key=key, attachments=[att['id']])['json'] + check(c.message(alice, chat, '', key=key, attachments=[att['id']])['json'] == first, 'replay differs') + c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice, + body={'text': 'changed', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}) + print('PASS: attachment scope, duplicates and idempotency normalization') + + # Exact limits are accepted; original bytes (including PNG trailing data) survive. + exact = png + b'\0' * (10485760 - len(png)) + att = upload(exact)['json'] + check(c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob)['bytes'] == exact, + '10 MiB original changed') + buf = BytesIO() + Image.new('RGB', (8000, 5000)).save(buf, 'PNG') + att = upload(buf.getvalue())['json'] + check(att['size_bytes'] == len(buf.getvalue()), '40M pixel upload failed') + print('PASS: inclusive 10 MiB and 40 million pixel boundaries') + + +if __name__ == '__main__': + run(main) diff --git a/uv.lock b/uv.lock index 3ee491eb..ace5584b 100644 --- a/uv.lock +++ b/uv.lock @@ -132,6 +132,34 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/3c/d7/8fb3044eaef08a310acfe23dae9a8e2e07d305edc29a53497e52bc76eca7/asyncpg-0.31.0-cp314-cp314t-win_amd64.whl", hash = "sha256:bd4107bb7cdd0e9e65fae66a62afd3a249663b844fa34d479f6d5b3bef9c04c3", size = 706062, upload-time = "2025-11-24T23:26:44.086Z" }, ] +[[package]] +name = "boto3" +version = "1.43.115" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "botocore" }, + { name = "jmespath" }, + { name = "s3transfer" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/0e/4a/b0ce191492a1724be73d8aacaf70d508bc8cbb9674c046d2ff7181dc51fb/boto3-1.43.115.tar.gz", hash = "sha256:c0f35e5f9cdf0edb365195e306d5586d3970b224660a11a87a4ec361ec3d7168", size = 112677, upload-time = "2026-10-19T19:19:53.093Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/be/bc/40dffae3238ef8d3415393132751fe2c88e8663ef84a31f0950e439ed2d5/boto3-1.43.115-py3-none-any.whl", hash = "sha256:6667a0c21d48dc125dbba88444bc8a601ce881a99341a0e6169f9333898e620d", size = 140044, upload-time = "2026-10-19T19:19:51.444Z" }, +] + +[[package]] +name = "botocore" +version = "1.43.115" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "jmespath" }, + { name = "python-dateutil" }, + { name = "urllib3" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/6f/10/ca525e88db50f5153ddad2a3d55908b5ace915d10f443d5e5135ba808d8d/botocore-1.43.115.tar.gz", hash = "sha256:aa4d9c4e7ef99988b7347e0cd276ba84e1eb1625510557b0e36cd57ecb19bbf2", size = 16383796, upload-time = "2026-10-19T19:19:45.382Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/77/2d/4b6b6f5704d1ec9b412c1dd9700bda0c315619dbac4a21418180a81a2780/botocore-1.43.115-py3-none-any.whl", hash = "sha256:9ceda640c868bc15f5399129e8cfdef8cbb4b350e8a99547c9b5f477cfb9619a", size = 16085101, upload-time = "2026-10-19T19:19:42.203Z" }, +] + [[package]] name = "cffi" version = "2.1.1" @@ -260,6 +288,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/58/a2/bb081bab032533a855d44de1d56f8e8426114ff1ba5d1f07a438a0a654f8/idna-3.20-py3-none-any.whl", hash = "sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c", size = 69583, upload-time = "2026-09-17T14:11:03.168Z" }, ] +[[package]] +name = "jmespath" +version = "1.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d3/59/322338183ecda247fb5d1763a6cbe46eff7222eaeebafd9fa65d4bf5cb11/jmespath-1.1.0.tar.gz", hash = "sha256:472c87d80f36026ae83c6ddd0f1d05d4e510134ed462851fd5f754c8c3cbb88d", size = 27377, upload-time = "2026-01-22T16:35:26.279Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/14/2f/967ba146e6d58cf6a652da73885f52fc68001525b4197effc174321d70b4/jmespath-1.1.0-py3-none-any.whl", hash = "sha256:a5663118de4908c91729bea0acadca56526eb2698e83de10cd116ae0f4e97c64", size = 20419, upload-time = "2026-01-22T16:35:24.919Z" }, +] + [[package]] name = "mtusi2026lab4" version = "0.1.0" @@ -267,7 +304,9 @@ source = { virtual = "." } dependencies = [ { name = "argon2-cffi" }, { name = "asyncpg" }, + { name = "boto3" }, { name = "fastapi" }, + { name = "pillow" }, { name = "uvicorn" }, ] @@ -275,10 +314,88 @@ dependencies = [ requires-dist = [ { name = "argon2-cffi", specifier = "==25.1.0" }, { name = "asyncpg", specifier = "==0.31.0" }, + { name = "boto3", specifier = "==1.43.115" }, { name = "fastapi", specifier = "==0.141.1" }, + { name = "pillow", specifier = "==13.0.0" }, { name = "uvicorn", specifier = "==0.40.0" }, ] +[[package]] +name = "pillow" +version = "13.0.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ce/a4/9cc30690a394a8305e8be4c9036aa16d0dfddadd09079860b582f562496d/pillow-13.0.0.tar.gz", hash = "sha256:cdcd1ba258178f7356d74170c9a81ba4eab6a5d55748bc402a5a2a6fe6be3510", size = 47058631, upload-time = "2026-10-16T22:18:09.718Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7a/b8/58d79332229482eb0d27c82973850808c19d37e5bdc14443d3417bd373f5/pillow-13.0.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:67c8d44ad89fa7eb88de5d6ef6199b1e03dd24974f85a67639fc8badb801fe79", size = 5568643, upload-time = "2026-10-16T22:15:44.474Z" }, + { url = "https://files.pythonhosted.org/packages/39/4b/f2ed53d83aa44ac19599ac0a54897f867fafb95f406980d1e24083f8cbe3/pillow-13.0.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:fc481f742ccda20e9ed760b8f978dff50f2a4e9bc253ac167fb0a05b0783ef52", size = 4852329, upload-time = "2026-10-16T22:15:46.284Z" }, + { url = "https://files.pythonhosted.org/packages/3b/bb/9e30e8ed18c20800ee327a12107d5249ad8dc444867b3adfcc21654e3c71/pillow-13.0.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8bf295fe459343bc6093c8e5e0df60bfa189e0fcb775a22e2c2488fdeccc6c0d", size = 6519309, upload-time = "2026-10-16T22:15:48.14Z" }, + { url = "https://files.pythonhosted.org/packages/e6/99/fdf65cc539e266ab93546c261bdf98411fe165c6e4e13f9dbf597097192b/pillow-13.0.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:42d63be3af4f9b951b199b0a9d23365d37ab14687e6d77b74940432c8515c4ca", size = 7144542, upload-time = "2026-10-16T22:15:50.093Z" }, + { url = "https://files.pythonhosted.org/packages/26/9b/f38b7c4d8f599537e2d1a6d546cec9beef7536ff8f5c49a3f9c78179d8ac/pillow-13.0.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:4bd024d567eca931b668978d672dba914d4b8e2628e2c3073a2ad178de8a2620", size = 6618042, upload-time = "2026-10-16T22:15:52.054Z" }, + { url = "https://files.pythonhosted.org/packages/a8/7c/45ee28531f89ee4582967d8a17f7a26d71ca64cd902b93c269590cd60a00/pillow-13.0.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:23edf4ec4b6f4f240e8df6e4985e9ee2ced86474632433e55fb1e2548744bf40", size = 7228137, upload-time = "2026-10-16T22:15:54.009Z" }, + { url = "https://files.pythonhosted.org/packages/01/bc/9c2a969cb1b70201d04fa17fd5aedf02a732870b8da86bceaaa30b0ca385/pillow-13.0.0-cp312-cp312-win32.whl", hash = "sha256:02464da86f561f410e13b061ec37c3068a9cb557a095494157742d07b3c24a27", size = 6515423, upload-time = "2026-10-16T22:15:56.25Z" }, + { url = "https://files.pythonhosted.org/packages/ff/cd/73a79ecdc822076002eb24b5e1e4a530b0bb5310fcb6d239abbec877110b/pillow-13.0.0-cp312-cp312-win_amd64.whl", hash = "sha256:ff3fe9eb621fbb9db4c4be33ab946f6bac5f98ab50ca8d3260503653656c961b", size = 7272166, upload-time = "2026-10-16T22:15:58.597Z" }, + { url = "https://files.pythonhosted.org/packages/4a/a3/be5a1236efd085b3984e4305da8f179ae9dab80c991d2ad78f2bd5c4c843/pillow-13.0.0-cp312-cp312-win_arm64.whl", hash = "sha256:c3bc8ee0271387e0b1e3ad0ff893645990a656e79bf9c42f4f734a3642b11d51", size = 2698943, upload-time = "2026-10-16T22:16:00.637Z" }, + { url = "https://files.pythonhosted.org/packages/82/e2/9132d38df320e8099e2642d9fa538f18a7dfebf68a875a31725dc2a115aa/pillow-13.0.0-cp313-cp313-ios_13_0_arm64_iphoneos.whl", hash = "sha256:e98b4f91f99b24b1da72e10621ba181f7281922384a7403094debb4199de6c00", size = 4190620, upload-time = "2026-10-16T22:16:02.527Z" }, + { url = "https://files.pythonhosted.org/packages/cb/be/436dac0996876f65ef1916ffb711c5ef4af2ebc57414b6f16642e07f6b47/pillow-13.0.0-cp313-cp313-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:34fb3db1149c879343861831728c243c432d4a5afc8f1878ca7612ef8de2a033", size = 4290286, upload-time = "2026-10-16T22:16:04.861Z" }, + { url = "https://files.pythonhosted.org/packages/60/16/7c1007162e4169ea0d5bec47e2e0b2174f56c50cf5b8c02a1dc23e41037e/pillow-13.0.0-cp313-cp313-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:2d50500a4a3bc6469adcdb319cf05d688b08ca3bf0f22a05339f3b25b433a599", size = 3723169, upload-time = "2026-10-16T22:16:06.956Z" }, + { url = "https://files.pythonhosted.org/packages/08/bc/578ab2eaa04705a8978bd9be0fe15c3d43a8fc95097c4f7e39540e79e4fa/pillow-13.0.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:ef5fe6b7db3f501c56000ba8fe7c7e1a35ca1d45824fbeb95a67daa85cff4dbe", size = 5568469, upload-time = "2026-10-16T22:16:08.664Z" }, + { url = "https://files.pythonhosted.org/packages/98/c8/ccede3dcc4f274b80fb49199eed1ef34ba05566c8b71976b9d1f111da2e3/pillow-13.0.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f11a3c1358bb3c68012a7b1f41b223c3db9f7bfde90b481df028cfe1c3363313", size = 4852187, upload-time = "2026-10-16T22:16:10.693Z" }, + { url = "https://files.pythonhosted.org/packages/fb/3c/cd0770e96d20bfd990db024bcbd43897ff3e88da1f4782cf90ea231b1d4f/pillow-13.0.0-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:19b66ca0a4d25cc0200b381ce824c7ea090db61427315e73cfb05dbf1b35c369", size = 6515908, upload-time = "2026-10-16T22:16:12.896Z" }, + { url = "https://files.pythonhosted.org/packages/ef/fe/f5cdaa2d1c4086e757430f368c271a58ee29cfe7378e7ae7b9d964015c25/pillow-13.0.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5f56499c1a74d2b862beb0c33b8016f99d095858455df9d21583bbbc1c15b582", size = 7142544, upload-time = "2026-10-16T22:16:14.844Z" }, + { url = "https://files.pythonhosted.org/packages/fb/b5/a54f6d44adbcd23737c8c12953f42be107e4cca64e247c0ca3a5b563061c/pillow-13.0.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a2a14cd73d5e6c927f644b0110ee81f59c4ef506259ec9183904747f2caacc81", size = 6613097, upload-time = "2026-10-16T22:16:16.893Z" }, + { url = "https://files.pythonhosted.org/packages/e6/b4/9e6c1207c21b2e91db0d15c16d30f3701ce5add1bd4da0e787761e66cb6e/pillow-13.0.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:08b826a9bf1265c76de04a42061d61ea6b109418b7826f28ff1ecdfaa26c5502", size = 7224846, upload-time = "2026-10-16T22:16:19.195Z" }, + { url = "https://files.pythonhosted.org/packages/78/9f/d3c3cb0518f3dd1eee0ef501535f53d15597a53c5c4118d178086143397a/pillow-13.0.0-cp313-cp313-win32.whl", hash = "sha256:12b0fa15879fe5a9d9eebe66e4e2d74128a773b1a4448010c9c714f45e04b9d8", size = 6516013, upload-time = "2026-10-16T22:16:21.389Z" }, + { url = "https://files.pythonhosted.org/packages/d7/ed/1b16fb66a37e1b9e41370b93b2c614c7da761a5b2a41dc333f7af4639962/pillow-13.0.0-cp313-cp313-win_amd64.whl", hash = "sha256:9dda0788a9932776a75d8bcc020076edc308ce2508b51f6739a45e3158b3fc42", size = 7284777, upload-time = "2026-10-16T22:16:23.393Z" }, + { url = "https://files.pythonhosted.org/packages/87/7d/8d02eba9f7ecc87f8e90f731e032cc4773802f2a843763afd0c66a535ff4/pillow-13.0.0-cp313-cp313-win_arm64.whl", hash = "sha256:40a266371ece581e5de13b6635ef78d7487824af80561b6ca27694a88c19bc0f", size = 2698826, upload-time = "2026-10-16T22:16:25.382Z" }, + { url = "https://files.pythonhosted.org/packages/8e/45/fbad9c7f5e2f1745c373a434d46072bc84eddfefaa28b852425ac957fda6/pillow-13.0.0-cp314-cp314-ios_13_0_arm64_iphoneos.whl", hash = "sha256:44629d4498c9cb6b2051806155f22141df0d8a929c56134b166655232dfb2f8c", size = 4190721, upload-time = "2026-10-16T22:16:27.524Z" }, + { url = "https://files.pythonhosted.org/packages/a2/bf/2074902a259a90692c5dbf132885d21671644cbbd3f690047516243a55db/pillow-13.0.0-cp314-cp314-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:bf97ba1b5542050e9b0c74e632323b6d8136cc99d64e193abebee22fe6013c76", size = 4290236, upload-time = "2026-10-16T22:16:29.562Z" }, + { url = "https://files.pythonhosted.org/packages/ff/a5/3d1bb93b8ea38d15d21356c1b8e9f50971ac592aaa248f7fb71739094c0e/pillow-13.0.0-cp314-cp314-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:aa694c49787040943570d61c9b8fc01271bd2a2cfdd55f0d896f8aecdce1a981", size = 3723093, upload-time = "2026-10-16T22:16:31.249Z" }, + { url = "https://files.pythonhosted.org/packages/84/a1/be060135d85187b25fa8b2573786c0e45c62ae27527fff5257675eb97f4a/pillow-13.0.0-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:49c12af515073e9bf511e4ef736743bf70d531a643668722279314b937742ba4", size = 5572253, upload-time = "2026-10-16T22:16:33.103Z" }, + { url = "https://files.pythonhosted.org/packages/b5/f4/0bf6f1ac9ba7ee2ffe9914aeb410a1035a0c1a3c42f122388d7fc950bc4b/pillow-13.0.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:d9b0a2bee3f3e03e9f29057aedb807dbf1e1d96ee1b9d0f422d9db6c8583271d", size = 4852269, upload-time = "2026-10-16T22:16:35.102Z" }, + { url = "https://files.pythonhosted.org/packages/83/16/b603274617b3e6728da3a0133d4fba1ea8d7fbe0c4ea5fef9cbffa398b8b/pillow-13.0.0-cp314-cp314-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5933fcea5a5ba64c686f55c886a115cbfd92f2900460784a1e79909fdbafd7b6", size = 6516058, upload-time = "2026-10-16T22:16:37.228Z" }, + { url = "https://files.pythonhosted.org/packages/1d/f2/1371707ce438ba9a43f9d24089828494ec6f9396e28cd7903aa704dd6306/pillow-13.0.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e91c2b1cbeae768c98752e67e717e283d68dae45717b41f2d91eaee648e37b7d", size = 7142574, upload-time = "2026-10-16T22:16:39.172Z" }, + { url = "https://files.pythonhosted.org/packages/b9/a9/62cf68587f910bc87c5951bcb6e9430c7643a65076adf5f18b4bf350e2bd/pillow-13.0.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7f6299e494e46253d40c34653cac369412f44ca53b5f85d848d3f95b1ed0546c", size = 6614295, upload-time = "2026-10-16T22:16:41.117Z" }, + { url = "https://files.pythonhosted.org/packages/6c/c9/9e9b48178649730299be0bafbf5f810a5b6977538f5a92d47e4ba5f5d608/pillow-13.0.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:f9bd7d260d986f3553518c5d723544346fa48031f8aef3265d786aeb59845bc0", size = 7224841, upload-time = "2026-10-16T22:16:43.118Z" }, + { url = "https://files.pythonhosted.org/packages/8f/48/c44e91384b1b6c072a714a63d00608acc16a6a968735e478e4be4384e5ac/pillow-13.0.0-cp314-cp314-win32.whl", hash = "sha256:2e31c4e68cffd7b1f116127626813b82fcba19edf6b940a7b0c266d6484d0055", size = 6508694, upload-time = "2026-10-16T22:16:45.883Z" }, + { url = "https://files.pythonhosted.org/packages/50/e2/7ad17747517e4c813dca78257d6bb593466f8231baf1384bc98d92745edb/pillow-13.0.0-cp314-cp314-win_amd64.whl", hash = "sha256:f7cbb7a64e7ec664775195e4f81923bc8a93d9451b963f844e59118f63147a7a", size = 7281830, upload-time = "2026-10-16T22:16:47.725Z" }, + { url = "https://files.pythonhosted.org/packages/41/9f/e5b51f2ce8ef4563215334035403ce61a244f93a2e65c24a940131618f87/pillow-13.0.0-cp314-cp314-win_arm64.whl", hash = "sha256:a6aaa7871aebca081eb5c45f83e845bbd9712ae99fa9286dbfdcee835e63f4b4", size = 2695468, upload-time = "2026-10-16T22:16:49.515Z" }, + { url = "https://files.pythonhosted.org/packages/97/73/a53502f731f6c84805a4ce87a8881e86d07a65aee64728b900d112dc703e/pillow-13.0.0-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:7b999816b5850a35ada08443e6e03945f1f6488144a83afe2d37b240a552aafa", size = 5578196, upload-time = "2026-10-16T22:16:51.291Z" }, + { url = "https://files.pythonhosted.org/packages/21/50/7e949959a56b5559c78ef172b70fc6025706acb289f3f648a15329e4fa6f/pillow-13.0.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:67871a13cf47275201553b536c025da777f997fa562a19eb798af4ad4b97c18f", size = 4857276, upload-time = "2026-10-16T22:16:53.399Z" }, + { url = "https://files.pythonhosted.org/packages/c9/52/43ded0ac08e77e39d17bd6d894162572201b5e8fdc4a022b14c55a1960a4/pillow-13.0.0-cp314-cp314t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e8ca8f69b7c77fa96dad99f8638c1fc4f1c0bed4acaa1baf7a3c31bdb80a0ab1", size = 6607008, upload-time = "2026-10-16T22:16:55.525Z" }, + { url = "https://files.pythonhosted.org/packages/0d/2e/4a57ed216d5bf877387cba4d44c02aa87abe51ad2e06404aa6b597c4da39/pillow-13.0.0-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:6bce6a29c3898191f4c9998e4d4e8f69790caeabde46e54da4bc235ce5bde683", size = 7224281, upload-time = "2026-10-16T22:16:57.738Z" }, + { url = "https://files.pythonhosted.org/packages/5c/3d/2929b248e135752d63aaeabf53f6de797e905b7b6263f9c1e2adade20aba/pillow-13.0.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:816752a5ef76a80484c8c441c85593b7ea9304f6231fc5776ad1fbff7e773058", size = 6695600, upload-time = "2026-10-16T22:16:59.842Z" }, + { url = "https://files.pythonhosted.org/packages/bc/33/fb0da992cf084ed1ee6ee05e96b6191e35c351eb707ca2df874feb24ce47/pillow-13.0.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:5bdb38ba220b61b4ec31dd1046dea6b016d3c6850a23f43ed8b58332b2fc668e", size = 7300589, upload-time = "2026-10-16T22:17:01.686Z" }, + { url = "https://files.pythonhosted.org/packages/bb/af/c30dd9ef6a3dce84ecb1b2393cac629047a32e66279056c6cb46c3ffd65c/pillow-13.0.0-cp314-cp314t-win32.whl", hash = "sha256:06903aac074d00c988e77a1fc435286dc498506d4ca8d6f69df9babddf4ecc15", size = 6517204, upload-time = "2026-10-16T22:17:03.722Z" }, + { url = "https://files.pythonhosted.org/packages/85/d5/ef3a8ae7609c8ab26570abf18194882534b480d245a9148b895a2262c004/pillow-13.0.0-cp314-cp314t-win_amd64.whl", hash = "sha256:9a635068d9793c7f0d929f39f18b58f1bd86c236e8e7b64167b86af9232ebca0", size = 7286695, upload-time = "2026-10-16T22:17:05.543Z" }, + { url = "https://files.pythonhosted.org/packages/84/3c/0861db0c69a03689ebe3e7599d35668a414deb5aaafd8255215e4e62e3d3/pillow-13.0.0-cp314-cp314t-win_arm64.whl", hash = "sha256:0d1a9cf7bc6917538b20f8d06b9ab2147e542f68849e96fc58eb66bdf6acc692", size = 2698098, upload-time = "2026-10-16T22:17:07.674Z" }, + { url = "https://files.pythonhosted.org/packages/6e/eb/e86a41962a3a31ad495a3d6a912d9002b01bba4a6fa71c28703abdca7f69/pillow-13.0.0-cp315-cp315-ios_13_0_arm64_iphoneos.whl", hash = "sha256:7d5e097eabd5d4915c27ac9f6e9325bb1e01a7c94d7348b5c4451d3fc30e8521", size = 4191213, upload-time = "2026-10-16T22:17:09.449Z" }, + { url = "https://files.pythonhosted.org/packages/dd/04/7e7445e706afec84ef06dc25b2c77f9cecff242c23730c62928ad1089d4a/pillow-13.0.0-cp315-cp315-ios_13_0_arm64_iphonesimulator.whl", hash = "sha256:51ebb529fc5e8abdd55302f6c3f8943037348ec2384329a3e55677f69ae2fdfc", size = 4290650, upload-time = "2026-10-16T22:17:11.196Z" }, + { url = "https://files.pythonhosted.org/packages/46/85/d534089068ac40c0f61f650389a296c812eab745a8a733f81feec39ae955/pillow-13.0.0-cp315-cp315-ios_13_0_x86_64_iphonesimulator.whl", hash = "sha256:c87a04e7f152ee4db5dbe15f0bfa3fe628a1aeedbe66b81c43b87f52c813f9ac", size = 3723078, upload-time = "2026-10-16T22:17:12.772Z" }, + { url = "https://files.pythonhosted.org/packages/ce/2b/00b5c3b401df538b5dbe311d3899730fae5ba1e21cb8b80a128887ac6747/pillow-13.0.0-cp315-cp315-macosx_10_15_x86_64.whl", hash = "sha256:20722cd5375533ad754053363a80b8fb30c79d37930a6f7228030e237ce3bd5d", size = 5572292, upload-time = "2026-10-16T22:17:14.539Z" }, + { url = "https://files.pythonhosted.org/packages/72/f5/a6b0610f946e9b54ae888ba7ad29d6e2f8d9b11f69e212d70416b7186c09/pillow-13.0.0-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:04a63720f79e22fd54d897c233a68314c50ca0603360f54ce2bd5db8f0f43782", size = 4852222, upload-time = "2026-10-16T22:17:16.521Z" }, + { url = "https://files.pythonhosted.org/packages/1e/d0/c0ff25bb0427ec70f32aac995453321fc3c4494a46c7292ff02d9d260b04/pillow-13.0.0-cp315-cp315-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4f8db54177035a2b5f717f94475b74907e12a96dc74f6d487f3966b0a4694071", size = 6527349, upload-time = "2026-10-16T22:17:18.534Z" }, + { url = "https://files.pythonhosted.org/packages/85/11/e46fa0bcc0fc117f1eacc206b3d51f04d583fab328b4b740672d0438d83f/pillow-13.0.0-cp315-cp315-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8f36aa77f74b14a2103622b196402c71330e4e7cf4122ee45c854e9a01e0eecf", size = 7152873, upload-time = "2026-10-16T22:17:20.415Z" }, + { url = "https://files.pythonhosted.org/packages/7c/53/2bdeca705f3a58679fa9201233193c86a30238340ca29b3a152c3fcc719b/pillow-13.0.0-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:149a1c323ba366f90b48e6f1a6db6582eb50512df374588de56fc909abc0f0f3", size = 6626871, upload-time = "2026-10-16T22:17:22.361Z" }, + { url = "https://files.pythonhosted.org/packages/14/de/3829030f209722ade3753bb456b805a0e7f885cf885b483407365c82e9ca/pillow-13.0.0-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:e892354a424b1e82a78ba16785822bdd40de06f19446a3d4a28047df1a4f8932", size = 7236439, upload-time = "2026-10-16T22:17:24.352Z" }, + { url = "https://files.pythonhosted.org/packages/44/b1/67fc37388382bc036a74f04da5bb6fcbf90ec3ac1f45893e06176c373fd3/pillow-13.0.0-cp315-cp315-win32.whl", hash = "sha256:8e459b613cf31f8980f584387b4fe47bf08e076e288d61aa8eba1f7fd4ddbb81", size = 6508688, upload-time = "2026-10-16T22:17:26.314Z" }, + { url = "https://files.pythonhosted.org/packages/44/05/fdb6a79aee10861165526b41b7b440e36ee1ff58613b2fbefe3acbf06d4f/pillow-13.0.0-cp315-cp315-win_amd64.whl", hash = "sha256:d4737f50d23ddd11d6685faacbb9252887dde0bc7997127757247983d74399d6", size = 7281107, upload-time = "2026-10-16T22:17:28.198Z" }, + { url = "https://files.pythonhosted.org/packages/e6/9c/6c6f7dc2a566467baf121394e83de25ac287cffe34cc030726beb4be0224/pillow-13.0.0-cp315-cp315-win_arm64.whl", hash = "sha256:2310e2adfc13c6d457d7d0f2ffdd5c56a74a317650cf765887df8d515a647ac0", size = 2695513, upload-time = "2026-10-16T22:17:30.013Z" }, + { url = "https://files.pythonhosted.org/packages/23/16/e9120defdb8bff693b582d035312acc6f530cdc2bc5238a87f410efccd82/pillow-13.0.0-cp315-cp315t-macosx_10_15_x86_64.whl", hash = "sha256:21c32061a5512dc13f87cc7303e120e4d77c2f70df89a35af0e3ce246e5ac57f", size = 5578129, upload-time = "2026-10-16T22:17:31.642Z" }, + { url = "https://files.pythonhosted.org/packages/47/22/76faf2bb2448f48a84cf894c1e2bb1090d7d2b1aa521ece131cb2f05d631/pillow-13.0.0-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:1c774b9c0ba0d0b356f71530677b39028878a2cae5a93e0a13779cfd17120f88", size = 4857428, upload-time = "2026-10-16T22:17:33.43Z" }, + { url = "https://files.pythonhosted.org/packages/b3/64/0861ea4e03c50b14af9ed9dd8b9f388ab42f445c623a9fcbab19aef7aa55/pillow-13.0.0-cp315-cp315t-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:20822140c9a2ba9c5e42df342b0a892ab2e102fc0e3db967a133bb9ce3f898bb", size = 6612582, upload-time = "2026-10-16T22:17:35.415Z" }, + { url = "https://files.pythonhosted.org/packages/5c/20/cd1e9b3f60a9f3d5ebe46da36955c26004092a501f7f2c42f9fcc26611bc/pillow-13.0.0-cp315-cp315t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ffea6b2986a797c84b6d1d27d4f43e0b83c73fc16281a930891cd237cf0d08fe", size = 7230176, upload-time = "2026-10-16T22:17:37.362Z" }, + { url = "https://files.pythonhosted.org/packages/b6/ef/ffb0a2fc85690714fb7ac662956949645d57f15bf4426f520da361e7b6e3/pillow-13.0.0-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:05cd2cd39055d0b9650060e4513ef4a9a0aea870083950bd046a0d6490f93a22", size = 6703640, upload-time = "2026-10-16T22:17:39.313Z" }, + { url = "https://files.pythonhosted.org/packages/e0/c2/6402ee3ccb3363aeed8fdb3f9fe56703ac5377a539f0b81b53999d7e121f/pillow-13.0.0-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:c6fd031333dedcad7ce3e03d244093f43504f209e86925ebdb2b868fa4037630", size = 7309231, upload-time = "2026-10-16T22:17:41.021Z" }, + { url = "https://files.pythonhosted.org/packages/fc/96/d6b82e8b8c06ad143b66b12e0b31051e4bf489b4fabca1f390a14fe1ebde/pillow-13.0.0-cp315-cp315t-win32.whl", hash = "sha256:c50c80e3591e5cf9a8dfa6007d4f10732f02e32263850999286e56ccd650cfb2", size = 6517200, upload-time = "2026-10-16T22:17:42.886Z" }, + { url = "https://files.pythonhosted.org/packages/60/d4/0062e6005e9b270b165015aafd343364ac378ec5a685431ea42baad33357/pillow-13.0.0-cp315-cp315t-win_amd64.whl", hash = "sha256:adb3c2124c68e480f4632d06cb7caa3b48a51678475f483b4edb1c6dee29ea43", size = 7287488, upload-time = "2026-10-16T22:17:45.56Z" }, + { url = "https://files.pythonhosted.org/packages/e4/81/b8eb349746be7bce47dd95337c6634e84910af91b59aaa9159c034c614fa/pillow-13.0.0-cp315-cp315t-win_arm64.whl", hash = "sha256:4a25756244bdcce3b50580baadf320fa48297f0eeb8e114ba64139a40d4d0fc8", size = 2698073, upload-time = "2026-10-16T22:17:47.556Z" }, + { url = "https://files.pythonhosted.org/packages/60/da/f6d644e631c5b370c5d0b899f3ccadd2780aab070b29a110527cf4e1fd2c/pillow-13.0.0-pp312-pypy312_pp80-macosx_10_15_x86_64.whl", hash = "sha256:8619685cad8efc4c38c66d3b36aad854523297c43d748b3b6f0cc03c90ba3463", size = 5523188, upload-time = "2026-10-16T22:17:59.334Z" }, + { url = "https://files.pythonhosted.org/packages/e4/b0/2594f202d743a35d4fe80682bfd96f277e5ca7aecb27cf64ef5bfc1d000b/pillow-13.0.0-pp312-pypy312_pp80-macosx_11_0_arm64.whl", hash = "sha256:7e91e97a929232cdb828889ee6ad0e48d7f40f4ec38818f4e4a8968b2b1af22e", size = 4806808, upload-time = "2026-10-16T22:18:01.188Z" }, + { url = "https://files.pythonhosted.org/packages/8e/ed/d504165ef79af6fafe25c76726cf30f544cc236002f7ed054f1d51adc4ff/pillow-13.0.0-pp312-pypy312_pp80-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:93ff9c9c3663ce23aaa5d0b6b6abac55359e2a0c06c7e30f3771829af816be63", size = 5464358, upload-time = "2026-10-16T22:18:02.779Z" }, + { url = "https://files.pythonhosted.org/packages/7b/fe/a10aba5503ca13e9e009b8bd0a1a4ce5584bdce246245fd6f3a83701369c/pillow-13.0.0-pp312-pypy312_pp80-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e65dd6e75de67b2362a4fa3e16aa627595f65054dc4ec73d64680c84d592fa61", size = 6043514, upload-time = "2026-10-16T22:18:04.538Z" }, + { url = "https://files.pythonhosted.org/packages/c0/37/66b12dc476041fe523bca29c9d58fbf1ea92a4eaf73acfb9e993948f1191/pillow-13.0.0-pp312-pypy312_pp80-win_amd64.whl", hash = "sha256:567f20f1c75897d7663830da6ea8af719e03d712b1d9ee7f813319f9ece42e5a", size = 7366030, upload-time = "2026-10-16T22:18:06.638Z" }, +] + [[package]] name = "pycparser" version = "3.1" @@ -378,6 +495,39 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/fa/04/c81d4841331c2178b6fb09ae225425e110ed72d990c9fe556c4ec03d1013/pydantic_core-2.46.5-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e24d8f05fa2d28513d94e877e9c75ad66175376209b3977f916e240e623193c", size = 2111034, upload-time = "2026-08-28T10:01:07.345Z" }, ] +[[package]] +name = "python-dateutil" +version = "2.9.0.post0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "six" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/66/c0/0c8b6ad9f17a802ee498c46e004a0eb49bc148f2fd230864601a86dcf6db/python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 342432, upload-time = "2024-03-01T18:36:20.211Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892, upload-time = "2024-03-01T18:36:18.57Z" }, +] + +[[package]] +name = "s3transfer" +version = "0.19.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "botocore" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/49/c5/0a5ce655109cb96b8dfad36908a4e108d15f5dc2eff8fbc3146c4312e704/s3transfer-0.19.3.tar.gz", hash = "sha256:8326acc3d056d7f7b8a37110c7d2aea41f67e83f6ae79cd3b5fbcca34c726406", size = 166087, upload-time = "2026-10-19T19:19:49.751Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e2/a4/88f9e10498759121060b8050a1554753dfc04df39bbbcc7aad9ecc8d9440/s3transfer-0.19.3-py3-none-any.whl", hash = "sha256:24eb7535804306355e31c952a90022bf5b2f9d760f9a11062c7493a217c08553", size = 90252, upload-time = "2026-10-19T19:19:48.309Z" }, +] + +[[package]] +name = "six" +version = "1.17.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/94/e7/b2c673351809dca68a0e064b6af791aa332cf192da575fd474ed7d6f16a2/six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81", size = 34031, upload-time = "2024-12-04T17:35:28.174Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050, upload-time = "2024-12-04T17:35:26.475Z" }, +] + [[package]] name = "starlette" version = "1.7.0" @@ -412,6 +562,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/67/81/4add07e5172b7ac40d8ed5ff580409a7801a4fe26d529bdd915401dabfbe/typing_inspection-0.4.4-py3-none-any.whl", hash = "sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147", size = 14750, upload-time = "2026-08-12T12:37:24.648Z" }, ] +[[package]] +name = "urllib3" +version = "2.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/e3/05/b17359e1cefb4f909b5e40b1b90a496d987258916dbbf88e842c729f510e/urllib3-2.8.0.tar.gz", hash = "sha256:63bf2ead4c879426ebf22ef2a781eeb4aa3b4ae798a0435506f8687fd5bb9b63", size = 458972, upload-time = "2026-09-15T19:29:36.253Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/92/9d/c4e665119135114480843e7ab388fa94d8480650450e6f8e26b70d323a4c/urllib3-2.8.0-py3-none-any.whl", hash = "sha256:0cf3cae568d36aa9576b28dfb35f11328f1cb974ca7647d9475ebb86c75ac6e3", size = 135717, upload-time = "2026-09-15T19:29:34.577Z" }, +] + [[package]] name = "uvicorn" version = "0.40.0"