This commit is contained in:
@@ -1,18 +1,52 @@
|
||||
## Лабораторная 7
|
||||
|
||||
Автор / группа: TODO
|
||||
Целевая оценка (3/4/5): TODO
|
||||
Трек 5, если заявлен: TODO
|
||||
Автор / группа: Журавлев А.Е., БПИ2301
|
||||
Целевая оценка (3/4/5): 3
|
||||
Трек 5, если заявлен: Не заявлен
|
||||
|
||||
Что реализовано и что перенесено: TODO
|
||||
Команды запуска из чистого clone: TODO
|
||||
Результаты проверок и ссылка на REPORT.md: TODO
|
||||
Ограничения / известные ошибки: TODO
|
||||
Источники и использование ИИ: TODO
|
||||
Что реализовано и что перенесено:
|
||||
- Перенесены application, worker, PostgreSQL/S3/Redis-сессии, SQL migrations, OTel/Grafana и scripts из лабораторной 6.
|
||||
- Реализован внешний HTTPS ingress на `localhost:8080`.
|
||||
- Реализован обязательный mTLS LB → API на `8443` с allowlist идентичности LB.
|
||||
- Настроены Argon2id, login rate limit `429/Retry-After`, session/attachment ACL и logout.
|
||||
- Настроены TLS для PostgreSQL, Redis, MinIO и OTel; OpenBao работает в persistent storage с AppRole и scoped policy.
|
||||
- API/worker работают non-root с read-only filesystem, dropped capabilities и resource limits.
|
||||
- Перенос сохранён без копирования public README/COURSE/contracts/tests поверх skeleton.
|
||||
|
||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [ ] Контракт и публичные тесты не ослаблены.
|
||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
Команды запуска из чистого clone:
|
||||
```powershell
|
||||
cd C:\VMD7\mtusi2026lab7
|
||||
powershell -ExecutionPolicy Bypass -File scripts\init-local-secrets.ps1
|
||||
powershell -ExecutionPolicy Bypass -File scripts\bootstrap-certs.ps1
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
```
|
||||
Инструкция OpenBao init/unseal и все команды проверки приведены в [REPORT.md](../../REPORT.md).
|
||||
|
||||
Результаты проверок и ссылка на REPORT.md:
|
||||
- `py -3 scripts/check_contract.py`: PASS.
|
||||
- `docker compose --env-file .env config --quiet`: PASS.
|
||||
- HTTPS smoke `tests/smoke.py`: PASS.
|
||||
- HTTPS security `tests/security.py`: PASS с доверенным CA и отказом недоверенного CA.
|
||||
- mTLS `docker exec mtusi-lab7-api-1 python /test-mtls.py`: отсутствующий cert и wrong identity отклонены; LB identity получила HTTP 200.
|
||||
- Persistence: PASS, прежние данные и session сохранены после перезапуска stateful-сервисов.
|
||||
- Failover: PASS, `74` успешных записи, `1` переходный failed attempt, recovery `2.729 s`.
|
||||
- OpenBao: `initialized=true`, `sealed=false`.
|
||||
- Подробности: [REPORT.md](../../REPORT.md).
|
||||
|
||||
Ограничения / известные ошибки:
|
||||
- Не заявлены и не реализуются критерии уровней 4 и 5.
|
||||
- Учебный стенд работает на одном Docker-host; отказ Docker-host и HA PostgreSQL/S3/Redis/OpenBao не гарантируются.
|
||||
- OpenBao init/unseal выполняется оператором вручную по документированной процедуре.
|
||||
- Redis TLS использует verification CA, но на уровне 3 не заявлен полный mTLS и Redis ACL hardening уровня 4.
|
||||
|
||||
Источники и использование ИИ:
|
||||
- Реализация лабораторной 6; Docker/OpenBao/MinIO/Nginx/PostgreSQL/Redis/OpenTelemetry документация.
|
||||
- При переносе и интеграции использована помощь ИИ. Секретные значения и приватные ключи в отчёт и PR не включены.
|
||||
|
||||
- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [x] Контракт и публичные тесты не ослаблены.
|
||||
- [x] make check и make test выполнены; приложен реальный вывод.
|
||||
- [x] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets/
|
||||
infra/certs/
|
||||
certs/private/
|
||||
*.key
|
||||
*.p12
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
FROM python:3.12.11-slim-bookworm
|
||||
|
||||
ARG BUILD_REVISION=local
|
||||
LABEL org.opencontainers.image.revision=$BUILD_REVISION
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY app.py auth.py db.py sessions.py s3store.py worker.py telemetry.py openbao.py ./
|
||||
COPY migrations ./migrations
|
||||
COPY scripts/migrate.py scripts/reconcile_attachments.py scripts/replay_attachment.py ./scripts/
|
||||
COPY contracts/openapi.json ./contracts/openapi.json
|
||||
RUN useradd --uid 10001 --create-home --shell /usr/sbin/nologin appuser \
|
||||
&& chown -R 10001:10001 /app
|
||||
USER 10001:10001
|
||||
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD python -c "import ssl,urllib.request; c=ssl.create_default_context(cafile='/certs/ca.crt'); c.load_cert_chain('/certs/lb.crt','/certs/lb.key'); urllib.request.urlopen('https://127.0.0.1:8443/health/ready',context=c,timeout=2)" || exit 1
|
||||
|
||||
STOPSIGNAL SIGTERM
|
||||
CMD ["python", "app.py"]
|
||||
@@ -1,37 +1,67 @@
|
||||
# Отчёт — лабораторная 7
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Автор, группа: Журавлев А.Е., БПИ2301
|
||||
- Целевая оценка; выбранный трек 5, если нужен: 3
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Использованные источники, библиотеки и помощь ИИ: lab6 implementation, official OpenBao/MinIO/Nginx Compose patterns, Docker/OpenSSL documentation; помощь ИИ использована для переноса и интеграции
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Prerequisites: Docker Compose, Python 3.12+ и PowerShell. Образы закреплены либо собраны локально.
|
||||
|
||||
```powershell
|
||||
cd C:\VMD7\mtusi2026lab7
|
||||
powershell -ExecutionPolicy Bypass -File scripts\init-local-secrets.ps1
|
||||
powershell -ExecutionPolicy Bypass -File scripts\bootstrap-certs.ps1
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
curl.exe --cacert infra\certs\ca.crt https://localhost:8080/health/ready
|
||||
py -3 tests\smoke.py --base-url https://localhost:8080 --ca-file C:\VMD7\mtusi2026lab7\infra\certs\ca.crt
|
||||
py -3 tests\security.py --base-url https://localhost:8080 --ca-file C:\VMD7\mtusi2026lab7\infra\certs\ca.crt
|
||||
docker exec mtusi-lab7-api-1 python /test-mtls.py
|
||||
docker compose down
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
Первая остановка сохраняет volumes; последняя намеренно удаляет учебные данные. Root/unseal OpenBao выполняет оператор вне Git.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab6, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Перенесены: application и worker, БД/S3/Redis-сессии, SQL migrations, OTel/Grafana и scripts lab6. PostgreSQL — источник истины; Redis хранит сессии и broker stream; S3 хранит вложения.
|
||||
|
||||
Изменения lab7: внешний TLS на localhost:8080, mTLS LB→API на :8443, Argon2id 19456 KiB/t=2/p=1, login rate limit и 429/Retry-After, OpenBao persistent storage, non-root app/worker и resource limits.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Публичный smoke | HTTPS smoke | PASS | console |
|
||||
| Публичный security | trusted/untrusted CA | PASS | console |
|
||||
| Внутреннее mTLS | `docker exec ... python /test-mtls.py` | no-cert и wrong-identity rejected; LB identity HTTP 200 | console |
|
||||
| Argon2id | параметры в `.env` и `auth.py` | memory 19456, t 2, p 1 | `auth.py` |
|
||||
| Persistence | `tests/resilience.py --mode persistence` | PASS; прежний session и подтверждённые данные сохранены | console |
|
||||
| Failover API | `tests/resilience.py --mode failover` | PASS: 74 successful writes, 1 failed attempt, recovery 2.729 s | console |
|
||||
| PostgreSQL TLS | `verify-full` + CA + SAN postgres | migrations и API pool подключены к SSL | compose, db.py |
|
||||
| Redis TLS | TLS port 6380, CA verification | sessions and broker работают через rediss | compose, sessions.py |
|
||||
| MinIO TLS | HTTPS :9000 с CA/SAN | healthy и HTTPS bootstrap | compose, infra/minio-entrypoint.sh |
|
||||
| OpenBao | persistent file storage, AppRole | initialized=true, sealed=false | infra/openbao.hcl |
|
||||
| OTel/telemetry | TLS receiver/collector exports | standalone stack running | infra/otel/collector.yaml |
|
||||
| Уровень 4 | Не заявлен; целевой уровень 3 | N/A | README |
|
||||
| Уровень 5 | Не заявлен; целевой уровень 3 | N/A | README |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
Для persistence hook перезапущены API, PostgreSQL, Redis, MinIO и Nginx без удаления volume. Для failover hook остановлена одна API replica и оставлена остановленной до восстановления второго API; записи не потеряны.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Условия стенда: Docker Desktop/Windows, локальный laptop-host; API и worker контейнеры; профиль failover — параллельные synthetic writes без отдельного исследования hardware. Тест выполнялся один раз с двумя API replicas.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Перезапуск API/БД/Redis/MinIO/Nginx с volume | Сессия и confirmed data сохраняются | Persistence PASS | Без потери данных |
|
||||
| Остановка одной из двух API replicas | Сообщения продолжают подтверждаться без дублей | Failover PASS: 74 writes, 1 transition failure | Recovery 2.729 s |
|
||||
| Недоверенный HTTPS CA | TLS verification отклоняет соединение | Security PASS | Нет потери данных |
|
||||
| Нет LB client cert/чужая LB identity | mTLS отклоняет соединение | mTLS PASS | Нет потери данных |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Стенд выдерживает restart API/основных state stores с volumes, остановку одной API replica и недоверенные клиентские соединения. Не обеспечивается отказ Docker-host, loss PostgreSQL/Redis/MinIO volume или HA OpenBao. Внутренние native TLS настроены для PostgreSQL, Redis, MinIO, OTel и OpenBao; внутренний broker и S3 credentials scoped. На уровне 3 ручной init/unseal OpenBao документирован, а не автоматизирован.
|
||||
|
||||
@@ -0,0 +1,883 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import signal
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unicodedata
|
||||
import uuid
|
||||
import warnings
|
||||
import ssl
|
||||
from datetime import datetime, timezone
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
|
||||
from openbao import load_service_secrets
|
||||
|
||||
load_service_secrets()
|
||||
|
||||
from auth import (
|
||||
create_session,
|
||||
hash_password,
|
||||
now_iso,
|
||||
parse_basic_auth,
|
||||
parse_bearer_token,
|
||||
validate_password,
|
||||
verify_password,
|
||||
)
|
||||
from db import ApiError, Store
|
||||
from s3store import ObjectStore, S3Unavailable
|
||||
from sessions import SessionStore
|
||||
from telemetry import configure
|
||||
from opentelemetry import context, trace
|
||||
|
||||
MAX_BODY_BYTES = 1024 * 1024
|
||||
MAX_UPLOAD_BYTES = min(10 * 1024 * 1024, int(os.environ.get('MAX_UPLOAD_BYTES', 10 * 1024 * 1024)))
|
||||
MAX_IMAGE_PIXELS = min(40_000_000, int(os.environ.get('MAX_IMAGE_PIXELS', 40_000_000)))
|
||||
MAX_CONCURRENT_UPLOADS = int(os.environ.get('MAX_CONCURRENT_UPLOADS', '16'))
|
||||
if (MAX_UPLOAD_BYTES < 1 or MAX_IMAGE_PIXELS < 1
|
||||
or not 1 <= MAX_CONCURRENT_UPLOADS <= 64):
|
||||
raise RuntimeError('Upload limits must be positive and concurrent uploads cannot exceed 64')
|
||||
UPLOAD_SLOTS = threading.BoundedSemaphore(MAX_CONCURRENT_UPLOADS)
|
||||
USERNAME_PATTERN = re.compile(r'^[a-z0-9_]{3,32}$')
|
||||
CURSOR_TIMESTAMP_PATTERN = re.compile(
|
||||
r'^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?Z$'
|
||||
)
|
||||
IDEMPOTENCY_KEY_PATTERN = re.compile(r'^[A-Za-z0-9._:-]{1,128}$')
|
||||
LOG = logging.getLogger('messenger')
|
||||
tracer = None
|
||||
http_requests = None
|
||||
http_duration = None
|
||||
OPENAPI_PATH = Path(__file__).resolve().parent / 'contracts' / 'openapi.json'
|
||||
SWAGGER_HTML = b'''<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>MTUSI Messenger API</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.onload = () => SwaggerUIBundle({
|
||||
url: "/openapi.json",
|
||||
dom_id: "#swagger-ui",
|
||||
deepLinking: true,
|
||||
presets: [SwaggerUIBundle.presets.apis],
|
||||
layout: "BaseLayout"
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
def cursor_encode(resource, item):
|
||||
value = json.dumps(
|
||||
{'resource': resource, 'created_at': item['created_at'], 'id': item['id']},
|
||||
separators=(',', ':'),
|
||||
).encode('utf-8')
|
||||
return base64.urlsafe_b64encode(value).rstrip(b'=').decode('ascii')
|
||||
|
||||
|
||||
def cursor_decode(cursor, resource):
|
||||
try:
|
||||
padding = '=' * (-len(cursor) % 4)
|
||||
decoded = base64.b64decode(cursor + padding, altchars=b'-_', validate=True)
|
||||
value = json.loads(decoded.decode('utf-8'))
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid cursor') from None
|
||||
if (not isinstance(value, dict) or set(value) != {'resource', 'created_at', 'id'}
|
||||
or value.get('resource') != resource
|
||||
or not isinstance(value.get('created_at'), str)
|
||||
or not CURSOR_TIMESTAMP_PATTERN.fullmatch(value['created_at'])
|
||||
or not isinstance(value.get('id'), str)):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid cursor')
|
||||
try:
|
||||
created_at = datetime.fromisoformat(value['created_at'][:-1] + '+00:00')
|
||||
item_id = str(uuid.UUID(value['id']))
|
||||
except (ValueError, AttributeError):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid cursor') from None
|
||||
if item_id != value['id']:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid cursor')
|
||||
return created_at, item_id
|
||||
|
||||
|
||||
def required_text(value, name, maximum):
|
||||
if not isinstance(value, str) or not value.strip() or len(value) > maximum:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid ' + name)
|
||||
try:
|
||||
value.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid ' + name) from None
|
||||
return value
|
||||
|
||||
|
||||
def parse_uuid(value):
|
||||
if not isinstance(value, str):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid UUID')
|
||||
try:
|
||||
parsed = uuid.UUID(value)
|
||||
except (ValueError, AttributeError):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid UUID') from None
|
||||
if str(parsed) != value.lower():
|
||||
raise ApiError(400, 'invalid_request', 'Invalid UUID')
|
||||
return str(parsed)
|
||||
|
||||
|
||||
def attachment_filename(query):
|
||||
try:
|
||||
values = parse_qs(query, keep_blank_values=True, strict_parsing=True)
|
||||
except ValueError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid attachment query') from None
|
||||
if set(values) != {'filename'} or len(values['filename']) != 1:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid attachment query')
|
||||
filename = values['filename'][0]
|
||||
if (not filename or len(filename) > 255 or '/' in filename or '\\' in filename
|
||||
or any(unicodedata.category(char) in {'Cc', 'Cf'} for char in filename)):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid filename')
|
||||
try:
|
||||
filename.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid filename') from None
|
||||
return filename
|
||||
|
||||
|
||||
class MessengerHandler(BaseHTTPRequestHandler):
|
||||
server_version = 'MTUSI-Messenger-Lab5'
|
||||
protocol_version = 'HTTP/1.1'
|
||||
|
||||
def log_message(self, _format, *args):
|
||||
LOG.info('request method=%s status=%s request_id=%s', self.command, self._status, self._request_id)
|
||||
|
||||
def _send_bytes(self, status, request_id, body, content_type, extra_headers=None):
|
||||
self._status = status
|
||||
if hasattr(self, '_otel_span') and self._otel_span is not None:
|
||||
self._otel_span.set_attribute('http.response.status_code', status)
|
||||
self._otel_span.set_attribute('request.id', request_id)
|
||||
if status >= 500:
|
||||
self._otel_span.set_status(trace.Status(trace.StatusCode.ERROR))
|
||||
if http_requests is not None:
|
||||
route = getattr(self, '_otel_route', 'unknown')
|
||||
status_class = str(status // 100) + 'xx'
|
||||
http_requests.add(1, {'method': self.command, 'route': route, 'status_class': status_class})
|
||||
http_duration.record(time.perf_counter() - getattr(self, '_otel_started', time.perf_counter()), {
|
||||
'method': self.command,
|
||||
'route': route,
|
||||
'status_class': status_class,
|
||||
})
|
||||
self._otel_span.end()
|
||||
if hasattr(self, '_otel_span_token'):
|
||||
context.detach(self._otel_span_token)
|
||||
self._otel_span = None
|
||||
self.send_response(status)
|
||||
self.send_header('X-Request-Id', request_id)
|
||||
self.send_header('Content-Length', str(len(body)))
|
||||
if status != 204:
|
||||
self.send_header('Content-Type', content_type)
|
||||
for name, value in (extra_headers or {}).items():
|
||||
self.send_header(name, value)
|
||||
self.send_header('X-Instance-Id', self.server.instance_id)
|
||||
self.end_headers()
|
||||
self._headers_sent = True
|
||||
if body:
|
||||
self.wfile.write(body)
|
||||
|
||||
def _send_stream(self, request_id, stream, length, content_type):
|
||||
self._status = 200
|
||||
self.send_response(200)
|
||||
self.send_header('X-Request-Id', request_id)
|
||||
self.send_header('Content-Length', str(length))
|
||||
self.send_header('Content-Type', content_type)
|
||||
self.send_header('X-Instance-Id', self.server.instance_id)
|
||||
self.end_headers()
|
||||
self._headers_sent = True
|
||||
remaining = length
|
||||
while remaining:
|
||||
chunk = stream.read(min(64 * 1024, remaining))
|
||||
if not chunk:
|
||||
raise OSError('S3 object ended before the stored content length')
|
||||
self.wfile.write(chunk)
|
||||
remaining -= len(chunk)
|
||||
|
||||
def _send(self, status, request_id, payload=None, extra_headers=None):
|
||||
body = b'' if status == 204 else json.dumps(
|
||||
payload, ensure_ascii=False, separators=(',', ':')
|
||||
).encode('utf-8')
|
||||
self._send_bytes(
|
||||
status, request_id, body, 'application/json; charset=utf-8', extra_headers
|
||||
)
|
||||
|
||||
def _error(self, request_id, error, extra_headers=None):
|
||||
self._send(error.status, request_id, {
|
||||
'error': {
|
||||
'code': error.code,
|
||||
'message': error.message,
|
||||
'request_id': request_id,
|
||||
},
|
||||
}, extra_headers=extra_headers)
|
||||
|
||||
def _read_json(self):
|
||||
if self.headers.get_content_type() != 'application/json':
|
||||
raise ApiError(415, 'unsupported_media_type', 'Expected application/json')
|
||||
raw_length = self.headers.get('Content-Length')
|
||||
try:
|
||||
length = int(raw_length)
|
||||
except (TypeError, ValueError):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid Content-Length') from None
|
||||
if length < 0:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid Content-Length')
|
||||
if length > MAX_BODY_BYTES:
|
||||
raise ApiError(413, 'payload_too_large', 'Request body is too large')
|
||||
try:
|
||||
value = json.loads(self.rfile.read(length).decode('utf-8'))
|
||||
except (ValueError, UnicodeError):
|
||||
raise ApiError(400, 'invalid_request', 'Malformed JSON') from None
|
||||
if not isinstance(value, dict):
|
||||
raise ApiError(400, 'invalid_request', 'Expected a JSON object')
|
||||
return value
|
||||
|
||||
def _read_attachment(self):
|
||||
content_type = self.headers.get_content_type()
|
||||
if content_type not in ('image/png', 'image/jpeg'):
|
||||
self.close_connection = True
|
||||
raise ApiError(415, 'unsupported_media_type', 'Expected PNG or JPEG')
|
||||
try:
|
||||
length = int(self.headers.get('Content-Length'))
|
||||
except (TypeError, ValueError):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid Content-Length') from None
|
||||
if length < 0:
|
||||
self.close_connection = True
|
||||
raise ApiError(400, 'invalid_request', 'Invalid Content-Length')
|
||||
if length > MAX_UPLOAD_BYTES:
|
||||
self.close_connection = True
|
||||
raise ApiError(413, 'payload_too_large', 'Image exceeds the upload limit')
|
||||
if length == 0:
|
||||
raise ApiError(415, 'unsupported_media_type', 'Image is empty or invalid')
|
||||
|
||||
temp_file = tempfile.SpooledTemporaryFile(max_size=1024 * 1024, mode='w+b')
|
||||
digest = hashlib.sha256()
|
||||
remaining = length
|
||||
try:
|
||||
while remaining:
|
||||
chunk = self.rfile.read(min(64 * 1024, remaining))
|
||||
if not chunk:
|
||||
raise ApiError(400, 'invalid_request', 'Incomplete image upload')
|
||||
temp_file.write(chunk)
|
||||
digest.update(chunk)
|
||||
remaining -= len(chunk)
|
||||
temp_file.seek(0)
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', Image.DecompressionBombWarning)
|
||||
image = Image.open(temp_file)
|
||||
detected_type = {
|
||||
'PNG': 'image/png',
|
||||
'JPEG': 'image/jpeg',
|
||||
}.get(image.format)
|
||||
width, height = image.size
|
||||
if width * height > MAX_IMAGE_PIXELS:
|
||||
raise ApiError(413, 'payload_too_large', 'Image dimensions exceed the pixel limit')
|
||||
if detected_type != content_type:
|
||||
raise ApiError(415, 'unsupported_media_type', 'Image content does not match Content-Type')
|
||||
image.verify()
|
||||
except Image.DecompressionBombError:
|
||||
raise ApiError(413, 'payload_too_large', 'Image dimensions exceed the pixel limit') from None
|
||||
except ApiError:
|
||||
raise
|
||||
except (UnidentifiedImageError, OSError, SyntaxError, ValueError):
|
||||
raise ApiError(415, 'unsupported_media_type', 'Image is invalid or unsupported') from None
|
||||
temp_file.seek(0)
|
||||
return temp_file, {
|
||||
'content_type': detected_type,
|
||||
'size_bytes': length,
|
||||
'sha256': digest.hexdigest(),
|
||||
'width': width,
|
||||
'height': height,
|
||||
}
|
||||
except Exception:
|
||||
temp_file.close()
|
||||
raise
|
||||
|
||||
def _dependencies_ok(self):
|
||||
try:
|
||||
self.server.store.ping()
|
||||
self.server.sessions.ping()
|
||||
self.server.objects.ping()
|
||||
return True
|
||||
except Exception:
|
||||
LOG.exception('dependency check failed')
|
||||
return False
|
||||
|
||||
def _unauthorized_bearer(self, request_id):
|
||||
self._error(
|
||||
request_id,
|
||||
ApiError(401, 'unauthorized', 'Authentication required'),
|
||||
extra_headers={'WWW-Authenticate': 'Bearer realm="messenger"'},
|
||||
)
|
||||
|
||||
def _unauthorized_basic(self, request_id):
|
||||
self._error(
|
||||
request_id,
|
||||
ApiError(401, 'unauthorized', 'Authentication required'),
|
||||
extra_headers={'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
|
||||
def _authenticated_user(self, request_id):
|
||||
auth = self.headers.get('Authorization', '')
|
||||
if auth.startswith('Basic '):
|
||||
self._unauthorized_bearer(request_id)
|
||||
return None
|
||||
token = parse_bearer_token(auth)
|
||||
if not token:
|
||||
self._unauthorized_bearer(request_id)
|
||||
return None
|
||||
try:
|
||||
user_id = self.server.sessions.get_user_id(token)
|
||||
except Exception:
|
||||
LOG.exception('session lookup failed')
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return None
|
||||
if not user_id:
|
||||
self._unauthorized_bearer(request_id)
|
||||
return None
|
||||
try:
|
||||
user = self.server.store.get_user(user_id)
|
||||
except Exception:
|
||||
LOG.exception('user lookup failed')
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return None
|
||||
if user is None:
|
||||
self._unauthorized_bearer(request_id)
|
||||
return None
|
||||
user['_token'] = token
|
||||
return user
|
||||
|
||||
@staticmethod
|
||||
def _query(query):
|
||||
try:
|
||||
values = parse_qs(query, keep_blank_values=True, strict_parsing=True)
|
||||
except ValueError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid query parameters') from None
|
||||
if set(values) - {'limit', 'cursor'} or any(len(items) != 1 for items in values.values()):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid query parameters')
|
||||
try:
|
||||
limit = int(values.get('limit', ['50'])[0])
|
||||
except ValueError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid limit') from None
|
||||
if not 1 <= limit <= 100:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid limit')
|
||||
cursor = values.get('cursor', [''])[0]
|
||||
if ('cursor' in values and not cursor) or len(cursor) > 2048:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid cursor')
|
||||
return limit, cursor
|
||||
|
||||
def _create_session(self, request_id):
|
||||
client_ip = self.client_address[0]
|
||||
allowed, retry_after = self.server.check_login_limit(client_ip)
|
||||
if not allowed:
|
||||
self._error(
|
||||
request_id,
|
||||
ApiError(429, 'rate_limited', 'Too many login attempts'),
|
||||
extra_headers={'Retry-After': str(retry_after)},
|
||||
)
|
||||
return
|
||||
credentials = parse_basic_auth(self.headers.get('Authorization'))
|
||||
if not credentials:
|
||||
self._unauthorized_basic(request_id)
|
||||
return
|
||||
username, password = credentials
|
||||
if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username):
|
||||
self._unauthorized_basic(request_id)
|
||||
return
|
||||
if not validate_password(password):
|
||||
self._unauthorized_basic(request_id)
|
||||
return
|
||||
try:
|
||||
row = self.server.store.get_user_with_password(username)
|
||||
except Exception:
|
||||
LOG.exception('login lookup failed')
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return
|
||||
if row is None or not verify_password(row['password_hash'], password):
|
||||
self._unauthorized_basic(request_id)
|
||||
return
|
||||
user = self.server.store.get_user(str(row['id']))
|
||||
try:
|
||||
token, expires_at = create_session(self.server.sessions, user['id'])
|
||||
except Exception:
|
||||
LOG.exception('session create failed')
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return
|
||||
self._send(201, request_id, {
|
||||
'token': token,
|
||||
'token_type': 'Bearer',
|
||||
'expires_at': expires_at,
|
||||
'user': user,
|
||||
})
|
||||
|
||||
def _logout(self, request_id, user):
|
||||
try:
|
||||
self.server.sessions.revoke(user['_token'])
|
||||
except Exception:
|
||||
LOG.exception('session revoke failed')
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return
|
||||
self._send_bytes(204, request_id, b'', 'application/json; charset=utf-8')
|
||||
|
||||
def _compensate_upload(self, attachment_id, object_key, remove_object):
|
||||
if remove_object:
|
||||
try:
|
||||
self.server.objects.delete_object(object_key)
|
||||
except S3Unavailable:
|
||||
LOG.error('upload compensation could not delete object id=%s', attachment_id)
|
||||
try:
|
||||
self.server.store.mark_attachment_failed(
|
||||
attachment_id, 'upload_failed', now_iso()
|
||||
)
|
||||
except ApiError as error:
|
||||
LOG.error(
|
||||
'upload compensation could not update metadata id=%s code=%s',
|
||||
attachment_id,
|
||||
error.code,
|
||||
)
|
||||
|
||||
def _upload_attachment(self, request_id, user, chat_id, query):
|
||||
filename = attachment_filename(query)
|
||||
self.server.store.get_chat(user['id'], chat_id)
|
||||
if not UPLOAD_SLOTS.acquire(timeout=5):
|
||||
raise ApiError(503, 'unavailable', 'Upload capacity is temporarily unavailable')
|
||||
temp_file = None
|
||||
try:
|
||||
temp_file, image = self._read_attachment()
|
||||
attachment_id = str(uuid.uuid4())
|
||||
object_key = 'objects/' + uuid.uuid4().hex
|
||||
staging_key = 'staging/' + attachment_id
|
||||
job_id = str(uuid.uuid4())
|
||||
created_at = now_iso()
|
||||
self.server.store.create_attachment_upload(
|
||||
attachment_id,
|
||||
chat_id,
|
||||
user['id'],
|
||||
object_key,
|
||||
filename,
|
||||
staging_key,
|
||||
image['content_type'],
|
||||
image['size_bytes'],
|
||||
image['sha256'],
|
||||
image['width'],
|
||||
image['height'],
|
||||
job_id,
|
||||
created_at,
|
||||
)
|
||||
try:
|
||||
self.server.objects.put_file(
|
||||
staging_key, temp_file, image['size_bytes'], image['content_type']
|
||||
)
|
||||
except S3Unavailable:
|
||||
self._compensate_upload(attachment_id, staging_key, True)
|
||||
raise ApiError(503, 'unavailable', 'Attachment storage is unavailable') from None
|
||||
self._send(
|
||||
202,
|
||||
request_id,
|
||||
self.server.store.get_attachment(user['id'], attachment_id),
|
||||
)
|
||||
finally:
|
||||
if temp_file is not None:
|
||||
temp_file.close()
|
||||
UPLOAD_SLOTS.release()
|
||||
|
||||
def _download_attachment(self, request_id, user, attachment_id, query):
|
||||
try:
|
||||
values = parse_qs(query, keep_blank_values=True, strict_parsing=True)
|
||||
except ValueError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid attachment query') from None
|
||||
if set(values) - {'variant'} or any(len(items) != 1 for items in values.values()):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid attachment query')
|
||||
variant = values.get('variant', ['original'])[0]
|
||||
if variant not in {'original', 'thumbnail', 'crop', 'optimized'}:
|
||||
raise ApiError(404, 'not_found', 'Attachment variant not found')
|
||||
attachment = self.server.store.get_attachment_for_download(
|
||||
user['id'], attachment_id
|
||||
)
|
||||
if attachment['status'] != 'ready':
|
||||
raise ApiError(409, 'conflict', 'Attachment is not ready')
|
||||
if variant == 'original':
|
||||
object_key, size, content_type = (
|
||||
attachment['object_key'], attachment['size_bytes'], attachment['content_type']
|
||||
)
|
||||
else:
|
||||
metadata = next(
|
||||
(item for item in attachment['variants'] if item.get('name') == variant), None
|
||||
)
|
||||
if metadata is None:
|
||||
raise ApiError(404, 'not_found', 'Attachment variant not found')
|
||||
object_key = 'objects/' + attachment_id + '/' + variant
|
||||
size, content_type = metadata['size_bytes'], metadata['content_type']
|
||||
result = self.server.objects.get_object(object_key)
|
||||
if result is None:
|
||||
self.server.store.mark_reconciled_missing(attachment_id, now_iso())
|
||||
raise ApiError(503, 'unavailable', 'Attachment content is unavailable')
|
||||
stream = result['Body']
|
||||
try:
|
||||
if result.get('ContentLength') != size:
|
||||
self.server.store.mark_reconciled_missing(attachment_id, now_iso())
|
||||
raise ApiError(503, 'unavailable', 'Attachment content is inconsistent')
|
||||
self._send_stream(
|
||||
request_id,
|
||||
stream,
|
||||
size,
|
||||
content_type,
|
||||
)
|
||||
finally:
|
||||
stream.close()
|
||||
|
||||
def _register(self, request_id, body):
|
||||
if set(body) != {'username', 'display_name', 'password'}:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid user fields')
|
||||
username = body['username']
|
||||
if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid username')
|
||||
display_name = required_text(body['display_name'], 'display_name', 100)
|
||||
password = body['password']
|
||||
if not validate_password(password):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid password')
|
||||
created_at = now_iso()
|
||||
password_hash = hash_password(password)
|
||||
user = self.server.store.register_user(username, display_name, password_hash, created_at)
|
||||
self._send(201, request_id, user)
|
||||
|
||||
def _dispatch(self):
|
||||
request_id = str(uuid.uuid4())
|
||||
self._request_id = request_id
|
||||
self._status = 500
|
||||
self._headers_sent = False
|
||||
path = urlsplit(self.path).path
|
||||
route_template = re.sub(
|
||||
r'/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-'
|
||||
r'[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}',
|
||||
'/{id}', path
|
||||
)
|
||||
route_template = re.sub(r'/attachments/\{id\}/.*$', '/attachments/{id}/<variant>', route_template)
|
||||
if tracer is not None:
|
||||
span = tracer.start_span('HTTP ' + self.command)
|
||||
span.set_attribute('http.request.method', self.command)
|
||||
span.set_attribute('url.path', path)
|
||||
span.set_attribute('http.route', route_template)
|
||||
span_context = span.get_span_context()
|
||||
self._otel_span = span
|
||||
self._otel_span_context = span_context
|
||||
token = context.attach(trace.set_span_in_context(span))
|
||||
self._otel_span_token = token
|
||||
started = time.perf_counter()
|
||||
self._otel_started = started
|
||||
self._otel_route = route_template
|
||||
try:
|
||||
if self.server.stopping:
|
||||
raise ApiError(503, 'unavailable', 'Server is shutting down')
|
||||
route = urlsplit(self.path)
|
||||
path = route.path
|
||||
if self.command == 'GET' and path in ('/docs', '/docs/'):
|
||||
self._send_bytes(200, request_id, SWAGGER_HTML, 'text/html; charset=utf-8')
|
||||
return
|
||||
if self.command == 'GET' and path == '/openapi.json':
|
||||
self._send_bytes(
|
||||
200, request_id, self.server.openapi_json, 'application/json; charset=utf-8'
|
||||
)
|
||||
return
|
||||
if path == '/health/live' and self.command == 'GET':
|
||||
self._send(200, request_id, {'status': 'ok'})
|
||||
return
|
||||
if path == '/health/ready' and self.command == 'GET':
|
||||
if self._dependencies_ok():
|
||||
self._send(200, request_id, {'status': 'ok'})
|
||||
else:
|
||||
self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable'))
|
||||
return
|
||||
if path == '/api/v1/users' and self.command == 'POST':
|
||||
self._register(request_id, self._read_json())
|
||||
return
|
||||
if path == '/api/v1/auth/sessions' and self.command == 'POST':
|
||||
self._create_session(request_id)
|
||||
return
|
||||
if path.startswith('/api/v1/'):
|
||||
user = self._authenticated_user(request_id)
|
||||
if user is None:
|
||||
return
|
||||
user_id = user['id']
|
||||
if path == '/api/v1/auth/sessions/current' and self.command == 'DELETE':
|
||||
self._logout(request_id, user)
|
||||
return
|
||||
if path == '/api/v1/users/me' and self.command == 'GET':
|
||||
self._send(200, request_id, {
|
||||
k: user[k] for k in ('id', 'username', 'display_name', 'created_at')
|
||||
})
|
||||
return
|
||||
if path == '/api/v1/chats' and self.command == 'POST':
|
||||
body = self._read_json()
|
||||
if set(body) != {'title', 'member_ids'}:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid chat fields')
|
||||
title = required_text(body['title'], 'title', 100)
|
||||
member_ids = body['member_ids']
|
||||
if (not isinstance(member_ids, list) or not member_ids or len(member_ids) > 100
|
||||
or any(not isinstance(value, str) for value in member_ids)):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid chat members')
|
||||
normalized = [parse_uuid(value) for value in member_ids]
|
||||
if len(set(normalized)) != len(normalized) or user_id not in normalized:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid chat members')
|
||||
chat = self.server.store.create_chat(
|
||||
user_id, title, normalized, now_iso()
|
||||
)
|
||||
self._send(201, request_id, chat)
|
||||
return
|
||||
if path == '/api/v1/chats' and self.command == 'GET':
|
||||
limit, cursor = self._query(route.query)
|
||||
key = cursor_decode(cursor, 'chats') if cursor else None
|
||||
items, next_key = self.server.store.list_chats(user_id, limit, key)
|
||||
next_cursor = cursor_encode('chats', items[-1]) if next_key else None
|
||||
self._send(200, request_id, {'items': items, 'next_cursor': next_cursor})
|
||||
return
|
||||
match = re.fullmatch(r'/api/v1/chats/([^/]+)', path)
|
||||
if match and self.command == 'GET':
|
||||
chat_id = parse_uuid(match.group(1))
|
||||
self._send(200, request_id, self.server.store.get_chat(user_id, chat_id))
|
||||
return
|
||||
match = re.fullmatch(r'/api/v1/chats/([^/]+)/messages', path)
|
||||
if match:
|
||||
chat_id = parse_uuid(match.group(1))
|
||||
resource = 'messages:' + chat_id
|
||||
if self.command == 'POST':
|
||||
body = self._read_json()
|
||||
if set(body) - {'text', 'attachment_ids'} or 'text' not in body:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid message fields')
|
||||
idempotency_key = self.headers.get('Idempotency-Key', '')
|
||||
if not IDEMPOTENCY_KEY_PATTERN.fullmatch(idempotency_key):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid Idempotency-Key')
|
||||
text = body['text']
|
||||
attachment_ids = body.get('attachment_ids', [])
|
||||
if (not isinstance(text, str) or len(text) > 4000
|
||||
or not isinstance(attachment_ids, list)
|
||||
or len(attachment_ids) > 10
|
||||
or any(not isinstance(value, str) for value in attachment_ids)):
|
||||
raise ApiError(400, 'invalid_request', 'Invalid message fields')
|
||||
normalized_attachments = [
|
||||
parse_uuid(value) for value in attachment_ids
|
||||
]
|
||||
if len(set(normalized_attachments)) != len(normalized_attachments):
|
||||
raise ApiError(400, 'invalid_request', 'Duplicate attachment IDs')
|
||||
if not text.strip() and not normalized_attachments:
|
||||
raise ApiError(400, 'invalid_request', 'Message text or attachment is required')
|
||||
try:
|
||||
text.encode('utf-8')
|
||||
except UnicodeEncodeError:
|
||||
raise ApiError(400, 'invalid_request', 'Invalid message text') from None
|
||||
message = self.server.store.create_message_idempotent(
|
||||
user_id,
|
||||
chat_id,
|
||||
text,
|
||||
normalized_attachments,
|
||||
idempotency_key,
|
||||
now_iso(),
|
||||
)
|
||||
self._send(201, request_id, message)
|
||||
return
|
||||
if self.command == 'GET':
|
||||
limit, cursor = self._query(route.query)
|
||||
key = cursor_decode(cursor, resource) if cursor else None
|
||||
items, next_key = self.server.store.list_messages(user_id, chat_id, limit, key)
|
||||
next_cursor = cursor_encode(resource, items[-1]) if next_key else None
|
||||
self._send(200, request_id, {'items': items, 'next_cursor': next_cursor})
|
||||
return
|
||||
match = re.fullmatch(r'/api/v1/chats/([^/]+)/attachments', path)
|
||||
if match and self.command == 'POST':
|
||||
self._upload_attachment(
|
||||
request_id, user, parse_uuid(match.group(1)), route.query
|
||||
)
|
||||
return
|
||||
match = re.fullmatch(r'/api/v1/attachments/([^/]+)', path)
|
||||
if match and self.command == 'GET':
|
||||
self._send(
|
||||
200,
|
||||
request_id,
|
||||
self.server.store.get_attachment(
|
||||
user_id, parse_uuid(match.group(1))
|
||||
),
|
||||
)
|
||||
return
|
||||
match = re.fullmatch(
|
||||
r'/api/v1/attachments/([^/]+)/content', path
|
||||
)
|
||||
if match and self.command == 'GET':
|
||||
self._download_attachment(
|
||||
request_id, user, parse_uuid(match.group(1)), route.query
|
||||
)
|
||||
return
|
||||
raise ApiError(404, 'not_found', 'Resource not found')
|
||||
except ApiError as error:
|
||||
self._error(request_id, error)
|
||||
except (BrokenPipeError, ConnectionResetError):
|
||||
LOG.info('request disconnected request_id=%s', request_id)
|
||||
except OSError:
|
||||
if self._headers_sent:
|
||||
LOG.info('response stream disconnected request_id=%s', request_id)
|
||||
else:
|
||||
LOG.exception('request I/O failed request_id=%s', request_id)
|
||||
self._error(
|
||||
request_id,
|
||||
ApiError(503, 'unavailable', 'Service unavailable'),
|
||||
)
|
||||
except Exception:
|
||||
LOG.exception('request failed request_id=%s', request_id)
|
||||
self._error(request_id, ApiError(500, 'internal_error', 'Internal server error'))
|
||||
|
||||
def do_GET(self):
|
||||
self._dispatch()
|
||||
|
||||
def do_POST(self):
|
||||
self._dispatch()
|
||||
|
||||
def do_PUT(self):
|
||||
self._dispatch()
|
||||
|
||||
def do_PATCH(self):
|
||||
self._dispatch()
|
||||
|
||||
def do_DELETE(self):
|
||||
self._dispatch()
|
||||
|
||||
def do_OPTIONS(self):
|
||||
self._dispatch()
|
||||
|
||||
|
||||
class MessengerServer(ThreadingHTTPServer):
|
||||
daemon_threads = False
|
||||
block_on_close = True
|
||||
request_queue_size = 128
|
||||
|
||||
def __init__(self, address, store, sessions, objects):
|
||||
super().__init__(address, MessengerHandler)
|
||||
self.store = store
|
||||
self.sessions = sessions
|
||||
self.objects = objects
|
||||
self.stopping = False
|
||||
self.instance_id = os.environ.get('INSTANCE_ID') or socket.gethostname()
|
||||
self.login_lock = threading.Lock()
|
||||
self.login_attempts = {}
|
||||
self.login_limit = max(1, int(os.environ.get('LOGIN_RATE_LIMIT', '10')))
|
||||
self.login_window = max(1, int(os.environ.get('LOGIN_RATE_WINDOW_SECONDS', '60')))
|
||||
try:
|
||||
spec = json.loads(OPENAPI_PATH.read_text(encoding='utf-8'))
|
||||
spec['servers'] = [{'url': '/', 'description': 'Текущий адрес API'}]
|
||||
self.openapi_json = json.dumps(
|
||||
spec, ensure_ascii=False, separators=(',', ':')
|
||||
).encode('utf-8')
|
||||
except (OSError, ValueError) as error:
|
||||
self.server_close()
|
||||
raise RuntimeError('Unable to load the OpenAPI contract') from error
|
||||
|
||||
def check_login_limit(self, client_ip):
|
||||
now = time.monotonic()
|
||||
with self.login_lock:
|
||||
timestamps = [stamp for stamp in self.login_attempts.get(client_ip, []) if now - stamp < self.login_window]
|
||||
if len(timestamps) >= self.login_limit:
|
||||
retry_after = max(1, int(self.login_window - (now - timestamps[0])))
|
||||
self.login_attempts[client_ip] = timestamps
|
||||
return False, retry_after
|
||||
timestamps.append(now)
|
||||
self.login_attempts[client_ip] = timestamps
|
||||
return True, 0
|
||||
|
||||
def get_request(self):
|
||||
request, client_address = super().get_request()
|
||||
cert = getattr(request, 'getpeercert', lambda: None)()
|
||||
if cert:
|
||||
names = cert.get('subjectAltName', ())
|
||||
allowed = {
|
||||
value
|
||||
for kind, value in names
|
||||
if kind in {'DNS', 'URI'}
|
||||
}
|
||||
identity_allowlist = {
|
||||
value.strip()
|
||||
for value in os.environ.get(
|
||||
'LB_CLIENT_IDENTITY_ALLOWLIST',
|
||||
'lb.lab.local,spiffe://lab7/lb',
|
||||
).split(',')
|
||||
if value.strip()
|
||||
}
|
||||
if not allowed.intersection(identity_allowlist):
|
||||
request.close()
|
||||
raise ConnectionError('LB client identity is not allowed')
|
||||
return request, client_address
|
||||
|
||||
|
||||
def main():
|
||||
host = os.environ.get('APP_HOST', '0.0.0.0')
|
||||
try:
|
||||
port = int(os.environ.get('APP_PORT', '8080'))
|
||||
except ValueError:
|
||||
raise SystemExit('APP_PORT must be an integer')
|
||||
if not 1 <= port <= 65535:
|
||||
raise SystemExit('APP_PORT must be between 1 and 65535')
|
||||
if os.environ.get('SESSION_STORE', 'redis').lower() != 'redis':
|
||||
raise SystemExit('SESSION_STORE must be redis for this implementation')
|
||||
|
||||
global tracer, http_requests, http_duration
|
||||
tracer, meter = configure('messenger-api')
|
||||
http_requests = meter.create_counter(
|
||||
'messenger_http_requests_total',
|
||||
description='HTTP requests',
|
||||
unit='{request}',
|
||||
)
|
||||
http_duration = meter.create_histogram(
|
||||
'messenger_http_request_duration_seconds',
|
||||
description='HTTP request duration',
|
||||
unit='s',
|
||||
)
|
||||
store = Store.connect(run_migrations=False)
|
||||
sessions = SessionStore.from_env()
|
||||
objects = ObjectStore.from_env()
|
||||
server = MessengerServer((host, port), store, sessions, objects)
|
||||
tls_cert = os.environ.get('TLS_CERT_FILE')
|
||||
tls_key = os.environ.get('TLS_KEY_FILE')
|
||||
tls_ca = os.environ.get('TLS_CA_FILE')
|
||||
if not (tls_cert and tls_key and tls_ca):
|
||||
server.server_close()
|
||||
raise SystemExit('TLS_CERT_FILE, TLS_KEY_FILE and TLS_CA_FILE are required')
|
||||
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cafile=tls_ca)
|
||||
context.verify_mode = ssl.CERT_REQUIRED
|
||||
context.load_cert_chain(certfile=tls_cert, keyfile=tls_key)
|
||||
server.socket = context.wrap_socket(server.socket, server_side=True)
|
||||
|
||||
def stop(_signum, _frame):
|
||||
server.stopping = True
|
||||
threading.Thread(target=server.shutdown, name='http-shutdown', daemon=True).start()
|
||||
|
||||
signal.signal(signal.SIGTERM, stop)
|
||||
signal.signal(signal.SIGINT, stop)
|
||||
LOG.info('server started host=%s port=%d', host, port)
|
||||
try:
|
||||
server.serve_forever(poll_interval=0.2)
|
||||
finally:
|
||||
server.server_close()
|
||||
store.close()
|
||||
objects.client.close()
|
||||
LOG.info('server stopped')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,91 @@
|
||||
import base64
|
||||
import binascii
|
||||
import os
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.low_level import Type
|
||||
from argon2.exceptions import VerifyMismatchError
|
||||
|
||||
from sessions import SessionStore
|
||||
|
||||
PASSWORD_MIN = 12
|
||||
PASSWORD_MAX = 128
|
||||
def _argon2_params():
|
||||
memory = max(19456, int(os.environ.get('ARGON2_MEMORY_KIB', '19456')))
|
||||
iterations = max(2, int(os.environ.get('ARGON2_ITERATIONS', '2')))
|
||||
parallelism = max(1, int(os.environ.get('ARGON2_PARALLELISM', '1')))
|
||||
return memory, iterations, parallelism
|
||||
|
||||
|
||||
_ARGON2_MEMORY, _ARGON2_ITERATIONS, _ARGON2_PARALLELISM = _argon2_params()
|
||||
_hasher = PasswordHasher(
|
||||
memory_cost=_ARGON2_MEMORY,
|
||||
time_cost=_ARGON2_ITERATIONS,
|
||||
parallelism=_ARGON2_PARALLELISM,
|
||||
type=Type.ID,
|
||||
)
|
||||
|
||||
|
||||
def now_iso():
|
||||
return datetime.now(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
|
||||
|
||||
|
||||
def validate_password(password):
|
||||
if not isinstance(password, str):
|
||||
return False
|
||||
length = len(password)
|
||||
return PASSWORD_MIN <= length <= PASSWORD_MAX
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
return _hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password_hash, password):
|
||||
try:
|
||||
return _hasher.verify(password_hash, password)
|
||||
except VerifyMismatchError:
|
||||
return False
|
||||
|
||||
|
||||
def parse_basic_auth(header_value):
|
||||
if not header_value or not header_value.startswith('Basic '):
|
||||
return None
|
||||
encoded = header_value[6:].strip()
|
||||
try:
|
||||
decoded = base64.b64decode(encoded, validate=True).decode('utf-8')
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
return None
|
||||
if ':' not in decoded:
|
||||
return None
|
||||
username, password = decoded.split(':', 1)
|
||||
if not username:
|
||||
return None
|
||||
return username, password
|
||||
|
||||
|
||||
def parse_bearer_token(header_value):
|
||||
if not header_value or not header_value.startswith('Bearer '):
|
||||
return None
|
||||
token = header_value[7:].strip()
|
||||
return token or None
|
||||
|
||||
|
||||
def session_ttl_seconds():
|
||||
try:
|
||||
ttl = int(os.environ.get('SESSION_TTL_SECONDS', '3600'))
|
||||
except ValueError:
|
||||
ttl = 3600
|
||||
return max(1, ttl)
|
||||
|
||||
|
||||
def create_session(store: SessionStore, user_id):
|
||||
token = secrets.token_urlsafe(32)
|
||||
ttl = session_ttl_seconds()
|
||||
expires_at = (
|
||||
datetime.now(timezone.utc) + timedelta(seconds=ttl)
|
||||
).isoformat(timespec='microseconds').replace('+00:00', 'Z')
|
||||
store.create(token, user_id, ttl)
|
||||
return token, expires_at
|
||||
+345
@@ -0,0 +1,345 @@
|
||||
name: mtusi-lab7
|
||||
|
||||
services:
|
||||
cert-bootstrap:
|
||||
image: alpine:3.21.3
|
||||
command: ['/bin/sh', '/scripts/bootstrap-certs.sh']
|
||||
volumes:
|
||||
- ./infra/certs:/certs
|
||||
- postgres_certs:/postgres-certs
|
||||
- ./scripts/bootstrap-certs.sh:/scripts/bootstrap-certs.sh:ro
|
||||
networks: [backend]
|
||||
restart: 'no'
|
||||
|
||||
postgres:
|
||||
image: postgres:16.8-alpine
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
PGSSLCERT: /certs/postgres.crt
|
||||
PGSSLKEY: /certs/postgres.key
|
||||
PGSSLROOTCERT: /certs/ca.crt
|
||||
command: ['postgres', '-c', 'ssl=on', '-c', 'ssl_cert_file=/certs/postgres.crt', '-c', 'ssl_key_file=/certs/postgres.key', '-c', 'ssl_ca_file=/certs/ca.crt']
|
||||
secrets: [postgres_password]
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
- postgres_certs:/certs:ro
|
||||
networks: [backend]
|
||||
depends_on:
|
||||
cert-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U messenger -d messenger']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 12
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
security_opt: ['no-new-privileges:true']
|
||||
tmpfs: [/tmp, /run/postgresql]
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '1.00', memory: 512M}
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec', '--port', '0', '--tls-port', '6380', '--tls-cert-file', '/certs/redis.crt', '--tls-key-file', '/certs/redis.key', '--tls-ca-cert-file', '/certs/ca.crt', '--tls-auth-clients', 'no']
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', "redis-cli --tls --cacert /certs/ca.crt -p 6380 ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 12
|
||||
restart: unless-stopped
|
||||
read_only: true
|
||||
security_opt: ['no-new-privileges:true']
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '0.50', memory: 256M}
|
||||
|
||||
minio:
|
||||
image: minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e
|
||||
entrypoint: ['/bin/sh', '/infra/minio-entrypoint.sh']
|
||||
secrets: [minio_root_user, minio_root_password]
|
||||
volumes:
|
||||
- minio_data:/data
|
||||
- ./infra/minio-entrypoint.sh:/infra/minio-entrypoint.sh:ro
|
||||
- ./infra/certs:/certs:ro
|
||||
environment:
|
||||
MINIO_CERTS_DIR: /certs/minio-certs
|
||||
networks: [backend]
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'curl --cacert /certs/ca.crt -fsS https://127.0.0.1:9000/minio/health/ready >/dev/null']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 12
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '1.00', memory: 512M}
|
||||
|
||||
minio-bootstrap:
|
||||
image: minio/minio@sha256:14cea493d9a34af32f524e538b8346cf79f3321eff8e708c1e2960462bd8936e
|
||||
entrypoint: ['/bin/sh', '/infra/minio-bootstrap.sh']
|
||||
environment:
|
||||
S3_BUCKET: messenger-attachments
|
||||
S3_ENDPOINT: https://minio:9000
|
||||
secrets: [minio_root_user, minio_root_password, s3_access_key_id, s3_secret_access_key]
|
||||
volumes:
|
||||
- ./infra/minio-bootstrap.sh:/infra/minio-bootstrap.sh:ro
|
||||
- ./infra/s3-policy.json:/infra/s3-policy.json:ro
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
depends_on:
|
||||
minio:
|
||||
condition: service_healthy
|
||||
restart: 'no'
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
command: ['python', 'scripts/migrate.py']
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
PGSSLMODE: verify-full
|
||||
PGSSLROOTCERT: /certs/ca.crt
|
||||
secrets: [database_url]
|
||||
volumes:
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
depends_on:
|
||||
cert-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: 'no'
|
||||
|
||||
api:
|
||||
build: .
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8443
|
||||
SESSION_STORE: redis
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
CACHE_URL: rediss://redis:6380/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
|
||||
S3_ENDPOINT: https://minio:9000
|
||||
S3_REGION: us-east-1
|
||||
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
|
||||
S3_CA_FILE: /certs/ca.crt
|
||||
BROKER_URL: rediss://redis:6380/1?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
|
||||
BROKER_STREAM: attachment.jobs
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /certs/ca.crt
|
||||
OTEL_SERVICE_NAME: messenger-api
|
||||
TLS_CA_FILE: /certs/ca.crt
|
||||
TLS_CERT_FILE: /certs/api.crt
|
||||
TLS_KEY_FILE: /certs/api.key
|
||||
BAO_ADDR: https://openbao:8200
|
||||
BAO_CACERT: /certs/ca.crt
|
||||
BAO_ROLE_ID_FILE: /run/bao/role_id
|
||||
BAO_SECRET_ID_FILE: /run/bao/secret_id
|
||||
BAO_KV_PATH: lab7/service
|
||||
ARGON2_MEMORY_KIB: ${ARGON2_MEMORY_KIB:-19456}
|
||||
ARGON2_ITERATIONS: ${ARGON2_ITERATIONS:-2}
|
||||
ARGON2_PARALLELISM: ${ARGON2_PARALLELISM:-1}
|
||||
secrets: [database_url, s3_access_key_id, s3_secret_access_key]
|
||||
volumes:
|
||||
- ./infra/certs:/certs:ro
|
||||
- ./secrets/role_id:/run/bao/role_id:ro
|
||||
- ./secrets/secret_id:/run/bao/secret_id:ro
|
||||
- ./scripts/test-mtls.py:/test-mtls.py:ro
|
||||
networks: [backend]
|
||||
depends_on:
|
||||
cert-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
minio-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
otel-collector:
|
||||
condition: service_started
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits: {cpus: '1.00', memory: 512M}
|
||||
security_opt: ['no-new-privileges:true']
|
||||
cap_drop: [ALL]
|
||||
read_only: true
|
||||
tmpfs: [/tmp]
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', "python -c \"import ssl,urllib.request; c=ssl.create_default_context(cafile='/certs/ca.crt'); c.load_cert_chain('/certs/lb.crt','/certs/lb.key'); urllib.request.urlopen('https://127.0.0.1:8443/health/ready',context=c,timeout=2)\""]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 12
|
||||
restart: unless-stopped
|
||||
|
||||
worker:
|
||||
build: .
|
||||
command: ['python', 'worker.py']
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
BROKER_URL: rediss://redis:6380/1?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt
|
||||
BROKER_STREAM: attachment.jobs
|
||||
S3_ENDPOINT: https://minio:9000
|
||||
S3_REGION: us-east-1
|
||||
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
|
||||
S3_CA_FILE: /certs/ca.crt
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: https://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_CERTIFICATE: /certs/ca.crt
|
||||
OTEL_SERVICE_NAME: messenger-worker
|
||||
BAO_ADDR: https://openbao:8200
|
||||
BAO_CACERT: /certs/ca.crt
|
||||
BAO_ROLE_ID_FILE: /run/bao/role_id
|
||||
BAO_SECRET_ID_FILE: /run/bao/secret_id
|
||||
BAO_KV_PATH: lab7/service
|
||||
secrets: [database_url, s3_access_key_id, s3_secret_access_key]
|
||||
networks: [backend]
|
||||
volumes:
|
||||
- ./infra/certs:/certs:ro
|
||||
- ./secrets/role_id:/run/bao/role_id:ro
|
||||
- ./secrets/secret_id:/run/bao/secret_id:ro
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
minio-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
redis:
|
||||
condition: service_healthy
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '0.75', memory: 384M}
|
||||
security_opt: ['no-new-privileges:true']
|
||||
read_only: true
|
||||
tmpfs: [/tmp]
|
||||
healthcheck:
|
||||
disable: true
|
||||
restart: unless-stopped
|
||||
|
||||
nginx:
|
||||
image: nginx:1.27.5-alpine
|
||||
ports:
|
||||
- '${APP_PORT:-8443}:8443'
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/conf.d/default.conf:ro
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [frontend, backend]
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
security_opt: ['no-new-privileges:true']
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '0.50', memory: 128M}
|
||||
restart: unless-stopped
|
||||
|
||||
openbao:
|
||||
image: openbao/openbao:2.2.0
|
||||
command: ['server', '-config=/etc/openbao/openbao.hcl']
|
||||
volumes:
|
||||
- openbao_data:/openbao/file
|
||||
- ./infra/openbao.hcl:/etc/openbao/openbao.hcl:ro
|
||||
- ./infra/certs:/certs:ro
|
||||
- ./infra/openbao-app-policy.hcl:/policies/app.hcl:ro
|
||||
networks: [backend]
|
||||
expose: ['8200']
|
||||
restart: unless-stopped
|
||||
security_opt: ['no-new-privileges:true']
|
||||
deploy:
|
||||
resources:
|
||||
limits: {cpus: '0.50', memory: 256M}
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:0.121.0
|
||||
command: ['--config=/etc/otelcol/collector.yaml']
|
||||
volumes:
|
||||
- ./infra/otel/collector.yaml:/etc/otelcol/collector.yaml:ro
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
restart: unless-stopped
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v3.2.1
|
||||
command: ['--config.file=/etc/prometheus/prometheus.yml', '--storage.tsdb.path=/prometheus']
|
||||
volumes:
|
||||
- ./infra/prometheus.yml:/etc/prometheus/prometheus.yml:ro
|
||||
- prometheus_data:/prometheus
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
restart: unless-stopped
|
||||
|
||||
loki:
|
||||
image: grafana/loki:3.5.0
|
||||
command: ['-config.file=/etc/loki/loki.yaml']
|
||||
volumes:
|
||||
- ./infra/loki.yaml:/etc/loki/loki.yaml:ro
|
||||
- loki_data:/loki
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
restart: unless-stopped
|
||||
|
||||
tempo:
|
||||
image: grafana/tempo:2.7.2
|
||||
command: ['-config.file=/etc/tempo/tempo.yaml']
|
||||
volumes:
|
||||
- ./infra/tempo.yaml:/etc/tempo/tempo.yaml:ro
|
||||
- tempo_data:/var/tempo
|
||||
- ./infra/certs:/certs:ro
|
||||
networks: [backend]
|
||||
restart: unless-stopped
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:11.6.0
|
||||
ports:
|
||||
- '${GRAFANA_PORT:-3000}:3000'
|
||||
environment:
|
||||
GF_AUTH_ANONYMOUS_ENABLED: 'true'
|
||||
GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
|
||||
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:-lab-local-admin}
|
||||
volumes:
|
||||
- ./grafana/provisioning:/etc/grafana/provisioning:ro
|
||||
- ./grafana/dashboards:/var/lib/grafana/dashboards:ro
|
||||
- grafana_data:/var/lib/grafana
|
||||
networks: [backend, frontend]
|
||||
depends_on: [prometheus, loki, tempo]
|
||||
security_opt: ['no-new-privileges:true']
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
frontend:
|
||||
backend:
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
postgres_certs:
|
||||
redis_data:
|
||||
minio_data:
|
||||
prometheus_data:
|
||||
loki_data:
|
||||
tempo_data:
|
||||
grafana_data:
|
||||
openbao_data:
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
minio_root_user:
|
||||
file: ./secrets/minio_root_user
|
||||
minio_root_password:
|
||||
file: ./secrets/minio_root_password
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key
|
||||
+12
-12
@@ -4,18 +4,18 @@
|
||||
|
||||
| Связь | Минимум на 3 | Дополнительно на 4 | Доказательство / конфиг |
|
||||
| --- | --- | --- | --- |
|
||||
| Клиент → ingress/LB | TLS, CA + hostname, Bearer | Проверка expiry/revocation сессии и общего login rate limit | TODO |
|
||||
| LB → API (все реплики) | mTLS + allowlist LB identity | Ротация leaf без обхода проверки | TODO |
|
||||
| API/worker → PostgreSQL | TLS verify-full + раздельные ограниченные DB roles | Ротация credentials; mTLS — допустимое усиление | TODO |
|
||||
| API/worker → S3 | TLS + разные scoped credentials/prefix rights | Проверка denied prefix и ротация | TODO |
|
||||
| API/worker → broker | TLS + разные producer/consumer identities и ACL | mTLS + ограничение допустимых SAN/ролей | TODO |
|
||||
| API/worker → Collector | TLS; сеть/аутентификация запрещает произвольный ingest | mTLS с раздельными identities | TODO |
|
||||
| Collector → telemetry backends | TLS + scoped backend credentials или mTLS | Ротация и проверка least privilege | TODO |
|
||||
| Grafana → data sources | TLS с проверкой сервера, ограниченная identity | Отрицательные проверки доступа | TODO |
|
||||
| API/worker/agent → OpenBao | TLS + сервисный auth + scoped policy | Renewal/expiry/revocation | TODO |
|
||||
| Оператор → OpenBao/Grafana/S3 admin | TLS + authentication, доступ локальный/через защищённый канал | Разделение operator/application roles | TODO |
|
||||
| API → Redis/Valkey, если используется | TLS + ACL/пароль с минимальными правами | Ротация; mTLS по возможности | TODO |
|
||||
| Replication/кластерные каналы, если добавлены | TLS/mTLS либо подтверждённый шифрованный transport | Проверка заявленной модели отказа | TODO |
|
||||
| Клиент → ingress/LB | `https://localhost:8080`, CA + hostname, Bearer | Общий login rate limit и expiry/revocation сессии | `make test`/`make test-security`; `infra/nginx.conf` |
|
||||
| LB → API (все реплики) | mTLS :8443, LB SAN `lb.lab.local`, allowlist identity | Ротация LB/API leaf и повторный test-mtls | `scripts/test-mtls.py`; `app.py` |
|
||||
| API/worker → PostgreSQL | TLS :5432, `sslmode=verify-full`, CA + postgres SAN | Ротация credentials | `compose.yaml`; `db.py` |
|
||||
| API/worker → S3 | MinIO HTTPS :9000, CA/SAN verification, scoped S3 credential | Ротация key | `infra/minio-bootstrap.sh`; `infra/s3-policy.json` |
|
||||
| API/worker → broker | Redis TLS :6380 с rediss:// и CA verification, DB 1 | Redis ACL/mTLS | `compose.yaml`; `sessions.py`; `worker.py` |
|
||||
| API/worker → Collector | OTLP HTTPS :4318 с CA verification | mTLS/allowlist identities | `infra/otel/collector.yaml`; `telemetry.py` |
|
||||
| Collector → telemetry backends | HTTPS Loki, TLS Tempo, HTTPS scrape Prometheus | Backend credentials/rotation | `infra/otel/collector.yaml`; `infra/{prometheus,loki,tempo}.yaml` |
|
||||
| Grafana → data sources | Provisioned local access в backend-сети | Ограничение admin и negative access | `grafana/provisioning/` |
|
||||
| API/worker/agent → OpenBao | HTTPS :8200, CA, AppRole, scoped policy, persistent file storage | Renewal/expiry/revocation | `infra/openbao.hcl`; `openbao.py` |
|
||||
| Оператор → OpenBao/Grafana/S3 admin | Локальный Docker-host и отдельные operator credentials вне Git | Разделение operator/application roles | `.env`; `scripts/` |
|
||||
| API → Redis/Valkey, если используется | Закрытая backend-сеть; Redis stores sessions/jobs | ACL/TLS/mTLS | `compose.yaml` |
|
||||
| Replication/кластерные каналы | N/A в учебном стенде | Проверка отказа ограничена API replica/Docker-host | `compose.yaml` |
|
||||
|
||||
Нет выбранного компонента — отметьте N/A с причиной, а не оставляйте пустую строку. Внутренний публичный health listener, если нужен, минимален и изолирован; через него нельзя обращаться к бизнес-API без mTLS. Отдельно задокументируйте scrape/health/bootstrap каналы: они тоже не должны создавать обход.
|
||||
|
||||
|
||||
@@ -0,0 +1,707 @@
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from psycopg import errors
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool, PoolTimeout
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).resolve().parent / 'migrations'
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status, code, message):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
|
||||
|
||||
def read_database_url():
|
||||
path = os.environ.get('DATABASE_URL_FILE')
|
||||
if path and os.path.isfile(path):
|
||||
return Path(path).read_text(encoding='utf-8').strip()
|
||||
url = os.environ.get('DATABASE_URL', '').strip()
|
||||
if not url:
|
||||
raise RuntimeError('DATABASE_URL or DATABASE_URL_FILE is required')
|
||||
separator = '&' if '?' in url else '?'
|
||||
sslmode = os.environ.get('PGSSLMODE', 'verify-full')
|
||||
rootcert = os.environ.get('PGSSLROOTCERT')
|
||||
if 'sslmode=' not in url:
|
||||
url += separator + 'sslmode=' + sslmode
|
||||
if rootcert and 'sslrootcert=' not in url:
|
||||
url += '&sslrootcert=' + rootcert
|
||||
return url
|
||||
|
||||
|
||||
class Store:
|
||||
def __init__(self, pool: ConnectionPool):
|
||||
self.pool = pool
|
||||
|
||||
@classmethod
|
||||
def connect(cls, run_migrations=True):
|
||||
pool = ConnectionPool(
|
||||
conninfo=read_database_url(),
|
||||
min_size=1,
|
||||
max_size=10,
|
||||
timeout=5.0,
|
||||
kwargs={
|
||||
'row_factory': dict_row,
|
||||
'options': '-c statement_timeout=5000 -c lock_timeout=2000',
|
||||
'sslmode': os.environ.get('PGSSLMODE', 'verify-full'),
|
||||
'sslrootcert': os.environ.get('PGSSLROOTCERT', '/certs/ca.crt'),
|
||||
},
|
||||
open=True,
|
||||
)
|
||||
store = cls(pool)
|
||||
if run_migrations:
|
||||
store.run_migrations()
|
||||
return store
|
||||
|
||||
def close(self):
|
||||
self.pool.close()
|
||||
|
||||
def ping(self):
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute('SELECT 1')
|
||||
|
||||
def run_migrations(self):
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute(
|
||||
'CREATE TABLE IF NOT EXISTS schema_migrations '
|
||||
'(version TEXT PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW())'
|
||||
)
|
||||
for path in sorted(MIGRATIONS_DIR.glob('*.sql')):
|
||||
version = path.name
|
||||
row = conn.execute(
|
||||
'SELECT 1 FROM schema_migrations WHERE version = %s',
|
||||
(version,),
|
||||
).fetchone()
|
||||
if row:
|
||||
continue
|
||||
sql = path.read_text(encoding='utf-8')
|
||||
with conn.transaction():
|
||||
conn.execute(sql)
|
||||
conn.execute(
|
||||
'INSERT INTO schema_migrations (version) VALUES (%s)',
|
||||
(version,),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _user_row(row):
|
||||
return {
|
||||
'id': str(row['id']),
|
||||
'username': row['username'],
|
||||
'display_name': row['display_name'],
|
||||
'created_at': row['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'),
|
||||
}
|
||||
|
||||
def register_user(self, username, display_name, password_hash, created_at):
|
||||
user_id = str(uuid.uuid4())
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute(
|
||||
'INSERT INTO users (id, username, display_name, password_hash, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s)',
|
||||
(user_id, username, display_name, password_hash, created_at),
|
||||
)
|
||||
except errors.UniqueViolation:
|
||||
raise ApiError(409, 'conflict', 'Username already exists') from None
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
return {
|
||||
'id': user_id,
|
||||
'username': username,
|
||||
'display_name': display_name,
|
||||
'created_at': created_at,
|
||||
}
|
||||
|
||||
def get_user(self, user_id):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT id, username, display_name, created_at FROM users WHERE id = %s',
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
return self._user_row(row)
|
||||
|
||||
def get_user_with_password(self, username):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s',
|
||||
(username,),
|
||||
).fetchone()
|
||||
return row
|
||||
|
||||
def create_chat(self, user_id, title, member_ids, created_at):
|
||||
chat_id = str(uuid.uuid4())
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
rows = conn.execute(
|
||||
'SELECT id FROM users WHERE id = ANY(%s)',
|
||||
(member_ids,),
|
||||
).fetchall()
|
||||
if len(rows) != len(set(member_ids)):
|
||||
raise ApiError(400, 'invalid_request', 'Unknown chat member')
|
||||
conn.execute(
|
||||
'INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)',
|
||||
(chat_id, title, created_at),
|
||||
)
|
||||
for index, member_id in enumerate(member_ids):
|
||||
conn.execute(
|
||||
'INSERT INTO chat_members (chat_id, user_id, position) VALUES (%s, %s, %s)',
|
||||
(chat_id, member_id, index),
|
||||
)
|
||||
except ApiError:
|
||||
raise
|
||||
except errors.Error:
|
||||
raise ApiError(500, 'internal_error', 'Internal server error') from None
|
||||
return {
|
||||
'id': chat_id,
|
||||
'title': title,
|
||||
'member_ids': list(member_ids),
|
||||
'created_at': created_at,
|
||||
}
|
||||
|
||||
def _is_member(self, conn, chat_id, user_id):
|
||||
row = conn.execute(
|
||||
'SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s',
|
||||
(chat_id, user_id),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
|
||||
def get_chat(self, user_id, chat_id):
|
||||
with self.pool.connection() as conn:
|
||||
if not self._is_member(conn, chat_id, user_id):
|
||||
raise ApiError(404, 'not_found', 'Chat not found')
|
||||
chat = conn.execute(
|
||||
'SELECT id, title, created_at FROM chats WHERE id = %s',
|
||||
(chat_id,),
|
||||
).fetchone()
|
||||
members = conn.execute(
|
||||
'SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY position',
|
||||
(chat_id,),
|
||||
).fetchall()
|
||||
return {
|
||||
'id': str(chat['id']),
|
||||
'title': chat['title'],
|
||||
'member_ids': [str(row['user_id']) for row in members],
|
||||
'created_at': chat['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'),
|
||||
}
|
||||
|
||||
def list_chats(self, user_id, limit, cursor_key):
|
||||
query = (
|
||||
'SELECT c.id, c.title, c.created_at '
|
||||
'FROM chats c '
|
||||
'JOIN chat_members cm ON cm.chat_id = c.id AND cm.user_id = %s '
|
||||
)
|
||||
params = [user_id]
|
||||
if cursor_key:
|
||||
query += 'WHERE (c.created_at, c.id) > (%s, %s) '
|
||||
params.extend(cursor_key)
|
||||
query += 'ORDER BY c.created_at ASC, c.id ASC LIMIT %s'
|
||||
params.append(limit + 1)
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
has_more = len(rows) > limit
|
||||
page = rows[:limit]
|
||||
items = []
|
||||
for row in page:
|
||||
chat_id = str(row['id'])
|
||||
items.append(self.get_chat(user_id, chat_id))
|
||||
next_cursor = None
|
||||
if has_more:
|
||||
last = page[-1]
|
||||
next_cursor = (last['created_at'], str(last['id']))
|
||||
return items, next_cursor
|
||||
|
||||
def create_message(self, user_id, chat_id, text, created_at):
|
||||
message_id = str(uuid.uuid4())
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
if not self._is_member(conn, chat_id, user_id):
|
||||
raise ApiError(404, 'not_found', 'Chat not found')
|
||||
conn.execute(
|
||||
'INSERT INTO messages (id, chat_id, sender_id, text, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s)',
|
||||
(message_id, chat_id, user_id, text, created_at),
|
||||
)
|
||||
return {
|
||||
'id': message_id,
|
||||
'chat_id': chat_id,
|
||||
'sender_id': user_id,
|
||||
'text': text,
|
||||
'created_at': created_at,
|
||||
'attachment_ids': [],
|
||||
}
|
||||
|
||||
def create_message_idempotent(
|
||||
self, user_id, chat_id, text, attachment_ids, idempotency_key, created_at
|
||||
):
|
||||
resource_path = '/api/v1/chats/' + chat_id + '/messages'
|
||||
request_hash = hashlib.sha256(
|
||||
json.dumps(
|
||||
{
|
||||
'chat_id': chat_id,
|
||||
'text': text,
|
||||
'attachment_ids': attachment_ids,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
separators=(',', ':'),
|
||||
).encode('utf-8')
|
||||
).hexdigest()
|
||||
lock_key = '\x1f'.join(
|
||||
(user_id, 'POST', resource_path, idempotency_key)
|
||||
)
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
if not self._is_member(conn, chat_id, user_id):
|
||||
raise ApiError(404, 'not_found', 'Chat not found')
|
||||
conn.execute(
|
||||
'SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))',
|
||||
(lock_key,),
|
||||
)
|
||||
row = conn.execute(
|
||||
'SELECT request_hash, response FROM message_idempotency '
|
||||
'WHERE user_id = %s AND method = %s AND resource_path = %s '
|
||||
'AND idempotency_key = %s AND expires_at > NOW() FOR UPDATE',
|
||||
(user_id, 'POST', resource_path, idempotency_key),
|
||||
).fetchone()
|
||||
if row is not None:
|
||||
if row['request_hash'] != request_hash:
|
||||
raise ApiError(409, 'conflict', 'Idempotency-Key was used with a different request')
|
||||
return row['response']
|
||||
|
||||
conn.execute(
|
||||
'DELETE FROM message_idempotency WHERE user_id = %s '
|
||||
'AND method = %s AND resource_path = %s AND idempotency_key = %s',
|
||||
(user_id, 'POST', resource_path, idempotency_key),
|
||||
)
|
||||
conn.execute(
|
||||
'DELETE FROM message_idempotency WHERE user_id = %s AND expires_at <= NOW()',
|
||||
(user_id,),
|
||||
)
|
||||
if attachment_ids:
|
||||
attachments = conn.execute(
|
||||
'SELECT id, status FROM attachments '
|
||||
'WHERE chat_id = %s AND id = ANY(%s) FOR SHARE',
|
||||
(chat_id, attachment_ids),
|
||||
).fetchall()
|
||||
if len(attachments) != len(attachment_ids):
|
||||
raise ApiError(404, 'not_found', 'Attachment not found')
|
||||
if any(row['status'] == 'failed' for row in attachments):
|
||||
raise ApiError(409, 'conflict', 'Attachment is not ready')
|
||||
conn.execute(
|
||||
'INSERT INTO message_idempotency '
|
||||
'(user_id, method, resource_path, idempotency_key, request_hash, expires_at) '
|
||||
"VALUES (%s, %s, %s, %s, %s, NOW() + INTERVAL '24 hours')",
|
||||
(user_id, 'POST', resource_path, idempotency_key, request_hash),
|
||||
)
|
||||
message_id = str(uuid.uuid4())
|
||||
conn.execute(
|
||||
'INSERT INTO messages (id, chat_id, sender_id, text, created_at) '
|
||||
'VALUES (%s, %s, %s, %s, %s)',
|
||||
(message_id, chat_id, user_id, text, created_at),
|
||||
)
|
||||
for position, attachment_id in enumerate(attachment_ids):
|
||||
conn.execute(
|
||||
'INSERT INTO message_attachments '
|
||||
'(message_id, attachment_id, position) VALUES (%s, %s, %s)',
|
||||
(message_id, attachment_id, position),
|
||||
)
|
||||
message = {
|
||||
'id': message_id,
|
||||
'chat_id': chat_id,
|
||||
'sender_id': user_id,
|
||||
'text': text,
|
||||
'created_at': created_at,
|
||||
'attachment_ids': list(attachment_ids),
|
||||
}
|
||||
conn.execute(
|
||||
'UPDATE message_idempotency SET response = %s '
|
||||
'WHERE user_id = %s AND method = %s AND resource_path = %s '
|
||||
'AND idempotency_key = %s',
|
||||
(Jsonb(message), user_id, 'POST', resource_path, idempotency_key),
|
||||
)
|
||||
return message
|
||||
except ApiError:
|
||||
raise
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def list_messages(self, user_id, chat_id, limit, cursor_key):
|
||||
with self.pool.connection() as conn:
|
||||
if not self._is_member(conn, chat_id, user_id):
|
||||
raise ApiError(404, 'not_found', 'Chat not found')
|
||||
query = (
|
||||
'SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, '
|
||||
'COALESCE(array_agg(ma.attachment_id ORDER BY ma.position) '
|
||||
'FILTER (WHERE ma.attachment_id IS NOT NULL), ARRAY[]::UUID[]) AS attachment_ids '
|
||||
'FROM messages m LEFT JOIN message_attachments ma ON ma.message_id = m.id '
|
||||
'WHERE m.chat_id = %s '
|
||||
)
|
||||
params = [chat_id]
|
||||
if cursor_key:
|
||||
query += 'AND (m.created_at, m.id) > (%s, %s) '
|
||||
params.extend(cursor_key)
|
||||
query += (
|
||||
'GROUP BY m.id '
|
||||
'ORDER BY m.created_at ASC, m.id ASC LIMIT %s'
|
||||
)
|
||||
params.append(limit + 1)
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
has_more = len(rows) > limit
|
||||
page = rows[:limit]
|
||||
items = [
|
||||
{
|
||||
'id': str(row['id']),
|
||||
'chat_id': str(row['chat_id']),
|
||||
'sender_id': str(row['sender_id']),
|
||||
'text': row['text'],
|
||||
'created_at': row['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'),
|
||||
'attachment_ids': [str(value) for value in row['attachment_ids']],
|
||||
}
|
||||
for row in page
|
||||
]
|
||||
next_cursor = None
|
||||
if has_more:
|
||||
last = page[-1]
|
||||
next_cursor = (last['created_at'], str(last['id']))
|
||||
return items, next_cursor
|
||||
|
||||
@staticmethod
|
||||
def _attachment_row(row, include_key=False):
|
||||
value = {
|
||||
'id': str(row['id']),
|
||||
'chat_id': str(row['chat_id']),
|
||||
'owner_id': str(row['owner_id']),
|
||||
'filename': row['filename'],
|
||||
'content_type': row['content_type'],
|
||||
'size_bytes': row['size_bytes'],
|
||||
'sha256': row['sha256'].strip(),
|
||||
'status': row['status'],
|
||||
'variants': row['variants'],
|
||||
'error_code': row['error_code'],
|
||||
'created_at': row['created_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'),
|
||||
'updated_at': row['updated_at'].isoformat(timespec='microseconds').replace('+00:00', 'Z'),
|
||||
}
|
||||
if include_key:
|
||||
value['object_key'] = row['object_key']
|
||||
value['staging_key'] = row['staging_key']
|
||||
value['width'] = row['width']
|
||||
value['height'] = row['height']
|
||||
return value
|
||||
|
||||
def create_attachment_upload(
|
||||
self, attachment_id, chat_id, owner_id, object_key, filename,
|
||||
staging_key, content_type, size_bytes, sha256, width, height,
|
||||
job_id, created_at
|
||||
):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
if not self._is_member(conn, chat_id, owner_id):
|
||||
raise ApiError(404, 'not_found', 'Chat not found')
|
||||
conn.execute(
|
||||
'INSERT INTO attachments '
|
||||
'(id, chat_id, owner_id, object_key, staging_key, filename, content_type, '
|
||||
'size_bytes, sha256, width, height, status, variants, error_code, '
|
||||
'pipeline_version, created_at, updated_at) '
|
||||
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
|
||||
"'queued', '[]'::jsonb, NULL, 1, %s, %s)",
|
||||
(
|
||||
attachment_id, chat_id, owner_id, object_key, staging_key, filename,
|
||||
content_type, size_bytes, sha256, width, height,
|
||||
created_at, created_at,
|
||||
),
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO attachment_jobs '
|
||||
'(job_id, attachment_id, pipeline_version, status, created_at, updated_at) '
|
||||
"VALUES (%s, %s, 1, 'queued', %s, %s)",
|
||||
(job_id, attachment_id, created_at, created_at),
|
||||
)
|
||||
conn.execute(
|
||||
'INSERT INTO attachment_outbox (job_id, created_at) VALUES (%s, %s)',
|
||||
(job_id, created_at),
|
||||
)
|
||||
except ApiError:
|
||||
raise
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def mark_attachment_ready(self, attachment_id, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
row = conn.execute(
|
||||
'UPDATE attachments SET status = %s, error_code = NULL, updated_at = %s '
|
||||
"WHERE id = %s AND status IN ('queued', 'processing') RETURNING *",
|
||||
('ready', updated_at, attachment_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
current = conn.execute(
|
||||
'SELECT * FROM attachments WHERE id = %s', (attachment_id,)
|
||||
).fetchone()
|
||||
if current and current['status'] == 'ready':
|
||||
return self._attachment_row(current)
|
||||
raise ApiError(503, 'unavailable', 'Attachment state could not be saved')
|
||||
conn.execute(
|
||||
"UPDATE attachment_jobs SET status = 'ready', lease_until = NULL, updated_at = %s "
|
||||
'WHERE attachment_id = %s',
|
||||
(updated_at, attachment_id),
|
||||
)
|
||||
return self._attachment_row(row)
|
||||
except ApiError:
|
||||
raise
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def mark_attachment_failed(self, attachment_id, error_code, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute(
|
||||
'UPDATE attachments SET status = %s, error_code = %s, updated_at = %s '
|
||||
"WHERE id = %s AND status IN ('queued', 'processing')",
|
||||
('failed', error_code, updated_at, attachment_id),
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE attachment_jobs SET status = 'failed', lease_until = NULL, "
|
||||
'last_error = %s, updated_at = %s WHERE attachment_id = %s',
|
||||
(error_code, updated_at, attachment_id),
|
||||
)
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def claim_job(self, job_id, lease_until, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
row = conn.execute(
|
||||
"UPDATE attachment_jobs SET status = 'processing', attempts = attempts + 1, "
|
||||
'lease_until = %s, updated_at = %s '
|
||||
"WHERE job_id = %s AND status IN ('queued', 'processing') "
|
||||
'AND pipeline_version = (SELECT pipeline_version FROM attachments '
|
||||
'WHERE id = attachment_jobs.attachment_id) '
|
||||
"AND (lease_until IS NULL OR lease_until < NOW()) "
|
||||
'RETURNING job_id, attachment_id, pipeline_version, attempts',
|
||||
(lease_until, updated_at, job_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
conn.execute(
|
||||
"UPDATE attachments SET status = 'processing', updated_at = %s "
|
||||
"WHERE id = %s AND status = 'queued'",
|
||||
(updated_at, row['attachment_id']),
|
||||
)
|
||||
return row
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def get_attachment_for_job(self, attachment_id):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT * FROM attachments WHERE id = %s', (attachment_id,)
|
||||
).fetchone()
|
||||
return self._attachment_row(row, include_key=True) if row else None
|
||||
|
||||
def set_attachment_variants(self, attachment_id, variants, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
row = conn.execute(
|
||||
'UPDATE attachments SET status = %s, variants = %s, error_code = NULL, '
|
||||
'updated_at = %s WHERE id = %s AND status IN (%s, %s) RETURNING *',
|
||||
('ready', Jsonb(variants), updated_at, attachment_id, 'queued', 'processing'),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
current = conn.execute(
|
||||
'SELECT * FROM attachments WHERE id = %s', (attachment_id,)
|
||||
).fetchone()
|
||||
return self._attachment_row(current) if current else None
|
||||
conn.execute(
|
||||
"UPDATE attachment_jobs SET status = 'ready', lease_until = NULL, "
|
||||
'updated_at = %s WHERE attachment_id = %s',
|
||||
(updated_at, attachment_id),
|
||||
)
|
||||
return self._attachment_row(row)
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def retry_job(self, job_id, attachment_id, next_attempt_at, error_code, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
conn.execute(
|
||||
"UPDATE attachment_jobs SET status = 'queued', lease_until = NULL, "
|
||||
'next_attempt_at = %s, last_error = %s, updated_at = %s WHERE job_id = %s',
|
||||
(next_attempt_at, error_code, updated_at, job_id),
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE attachments SET status = 'queued', error_code = %s, updated_at = %s "
|
||||
"WHERE id = %s AND status = 'processing'",
|
||||
(error_code, updated_at, attachment_id),
|
||||
)
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def fail_job(self, job_id, attachment_id, error_code, updated_at):
|
||||
self.mark_attachment_failed(attachment_id, error_code, updated_at)
|
||||
|
||||
def pending_outbox(self, limit=100):
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
'SELECT o.id, o.job_id, j.attachment_id, j.pipeline_version '
|
||||
'FROM attachment_outbox o JOIN attachment_jobs j ON j.job_id = o.job_id '
|
||||
'WHERE o.published_at IS NULL ORDER BY o.id LIMIT %s',
|
||||
(limit,),
|
||||
).fetchall()
|
||||
return rows
|
||||
|
||||
def mark_outbox_published(self, outbox_id, published_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute(
|
||||
'UPDATE attachment_outbox SET published_at = %s '
|
||||
'WHERE id = %s AND published_at IS NULL',
|
||||
(published_at, outbox_id),
|
||||
)
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def replay_attachment(self, attachment_id, reason, created_at):
|
||||
job_id = str(uuid.uuid4())
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
row = conn.execute(
|
||||
"SELECT pipeline_version, status FROM attachments WHERE id = %s FOR UPDATE",
|
||||
(attachment_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise ApiError(404, 'not_found', 'Attachment not found')
|
||||
if row['status'] != 'failed':
|
||||
raise ApiError(409, 'conflict', 'Only failed attachments can be replayed')
|
||||
old_version = row['pipeline_version']
|
||||
new_version = old_version + 1
|
||||
conn.execute(
|
||||
"UPDATE attachments SET status = 'queued', error_code = NULL, "
|
||||
"variants = '[]'::jsonb, pipeline_version = %s, updated_at = %s "
|
||||
"WHERE id = %s",
|
||||
(new_version, created_at, attachment_id),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_jobs "
|
||||
"(job_id, attachment_id, pipeline_version, status, created_at, updated_at) "
|
||||
"VALUES (%s, %s, %s, 'queued', %s, %s)",
|
||||
(job_id, attachment_id, new_version, created_at, created_at),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_outbox (job_id, created_at) VALUES (%s, %s)",
|
||||
(job_id, created_at),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_replay_audit "
|
||||
"(attachment_id, old_pipeline_version, new_pipeline_version, reason, created_at) "
|
||||
"VALUES (%s, %s, %s, %s, %s)",
|
||||
(attachment_id, old_version, new_version, reason, created_at),
|
||||
)
|
||||
return job_id, new_version
|
||||
except ApiError:
|
||||
raise
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
|
||||
def get_attachment(self, user_id, attachment_id):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT a.* FROM attachments a '
|
||||
'JOIN chat_members cm ON cm.chat_id = a.chat_id '
|
||||
'WHERE a.id = %s AND cm.user_id = %s',
|
||||
(attachment_id, user_id),
|
||||
).fetchone()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
if row is None:
|
||||
raise ApiError(404, 'not_found', 'Attachment not found')
|
||||
return self._attachment_row(row)
|
||||
|
||||
def get_attachment_for_download(self, user_id, attachment_id):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT a.* FROM attachments a '
|
||||
'JOIN chat_members cm ON cm.chat_id = a.chat_id '
|
||||
'WHERE a.id = %s AND cm.user_id = %s',
|
||||
(attachment_id, user_id),
|
||||
).fetchone()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
if row is None:
|
||||
raise ApiError(404, 'not_found', 'Attachment not found')
|
||||
return self._attachment_row(row, include_key=True)
|
||||
|
||||
def list_stale_processing_attachments(self, cutoff):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM attachments WHERE status = 'processing' AND created_at < %s "
|
||||
'ORDER BY created_at, id',
|
||||
(cutoff,),
|
||||
).fetchall()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
return [self._attachment_row(row, include_key=True) for row in rows]
|
||||
|
||||
def list_ready_attachments(self):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM attachments WHERE status = 'ready' ORDER BY id"
|
||||
).fetchall()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
return [self._attachment_row(row, include_key=True) for row in rows]
|
||||
|
||||
def list_attachments_for_reconciliation(self):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
'SELECT * FROM attachments ORDER BY id'
|
||||
).fetchall()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
return [self._attachment_row(row, include_key=True) for row in rows]
|
||||
|
||||
def get_attachment_by_object_key(self, object_key):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
'SELECT * FROM attachments WHERE object_key = %s',
|
||||
(object_key,),
|
||||
).fetchone()
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
if row is None:
|
||||
return None
|
||||
return self._attachment_row(row, include_key=True)
|
||||
|
||||
def mark_reconciled_missing(self, attachment_id, updated_at):
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute(
|
||||
'UPDATE attachments SET status = %s, error_code = %s, updated_at = %s '
|
||||
"WHERE id = %s AND status = 'ready'",
|
||||
('failed', 'object_missing', updated_at, attachment_id),
|
||||
)
|
||||
except (errors.Error, PoolTimeout):
|
||||
raise ApiError(503, 'unavailable', 'Service unavailable') from None
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"uid": "messenger-observability",
|
||||
"title": "Messenger Observability",
|
||||
"schemaVersion": 39,
|
||||
"version": 1,
|
||||
"refresh": "10s",
|
||||
"time": {"from": "now-15m", "to": "now"},
|
||||
"panels": [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "API RPS",
|
||||
"type": "timeseries",
|
||||
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 0},
|
||||
"datasource": {"type": "prometheus", "uid": "Prometheus"},
|
||||
"targets": [{"expr": "sum(rate(messenger_http_requests_total[1m]))","legendFormat":"requests/s"}]
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"title": "API Error Ratio",
|
||||
"type": "timeseries",
|
||||
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 0},
|
||||
"datasource": {"type": "prometheus", "uid": "Prometheus"},
|
||||
"targets": [{"expr": "sum(rate(messenger_http_requests_total{status_class=\"5xx\"}[5m])) / clamp_min(sum(rate(messenger_http_requests_total[5m])), 1)","legendFormat":"5xx/all"}]
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"title": "API Duration p95",
|
||||
"type": "timeseries",
|
||||
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 0},
|
||||
"datasource": {"type": "prometheus", "uid": "Prometheus"},
|
||||
"targets": [{"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_http_request_duration_seconds_bucket[5m])))","legendFormat":"p95"}]
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"title": "Jobs by Outcome",
|
||||
"type": "timeseries",
|
||||
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 8},
|
||||
"datasource": {"type": "prometheus", "uid": "Prometheus"},
|
||||
"targets": [{"expr": "sum by (outcome) (rate(messenger_jobs_processed_total[5m]))","legendFormat":"{{outcome}}"}]
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"title": "Worker Duration p95",
|
||||
"type": "timeseries",
|
||||
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 8},
|
||||
"datasource": {"type": "prometheus", "uid": "Prometheus"},
|
||||
"targets": [{"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_job_processing_duration_seconds_bucket[5m])))","legendFormat":"p95"}]
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"title": "Messenger Logs",
|
||||
"type": "logs",
|
||||
"gridPos": {"h": 10, "w": 24, "x": 0, "y": 16},
|
||||
"datasource": {"type": "loki", "uid": "Loki"},
|
||||
"targets": [{"expr": "{service_name=~\"messenger-api|messenger-worker\"}","refId":"A"}]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
apiVersion: 1
|
||||
providers:
|
||||
- name: Messenger
|
||||
type: file
|
||||
updateIntervalSeconds: 10
|
||||
options:
|
||||
path: /var/lib/grafana/dashboards
|
||||
@@ -0,0 +1,18 @@
|
||||
apiVersion: 1
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
uid: Prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
- name: Loki
|
||||
uid: Loki
|
||||
type: loki
|
||||
access: proxy
|
||||
url: http://loki:3100
|
||||
- name: Tempo
|
||||
uid: Tempo
|
||||
type: tempo
|
||||
access: proxy
|
||||
url: http://tempo:3200
|
||||
+58
-4
@@ -1,7 +1,61 @@
|
||||
# Инфраструктура студента
|
||||
# Compose + Nginx + MinIO
|
||||
|
||||
Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли.
|
||||
The runnable topology is in the root `compose.yaml`. The example Compose file
|
||||
and `contracts/infrastructure.json` are teaching worksheets, not deployments.
|
||||
The supported local entry point is Nginx; API, PostgreSQL, Redis and MinIO ports
|
||||
are not published to the host.
|
||||
|
||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
||||
| Role | Service / image | Persistence and readiness |
|
||||
| --- | --- | --- |
|
||||
| API | `api`, built from `Dockerfile` / Python 3.12.11 | Two stateless replicas; `/health/ready`; no host port |
|
||||
| Load balancer | `nginx:1.27.5-alpine` | Host port `${APP_PORT:-8080}`; Docker DNS refresh 5 s; passive fail exclusion |
|
||||
| Database | `postgres:16.8-alpine` | `postgres_data` named volume; `pg_isready` |
|
||||
| Sessions | `redis:7.4.2-alpine` | `redis_data` named volume; AOF `everysec`; Redis `PING` |
|
||||
| Object storage | MinIO `RELEASE.2025-09-07T16-13-09Z`, pinned by image digest in Compose | `minio_data` named volume; MinIO readiness endpoint; S3 port 9000 and console 9001 are private |
|
||||
| Migration job | Same API image | Runs ordered SQL migrations once before API replicas |
|
||||
| Bucket bootstrap | Same pinned MinIO image, using its `mc` client | Idempotently creates the private bucket, versioned policy, and restricted API user; replaces the prior policy without deleting volumes |
|
||||
|
||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
||||
PostgreSQL and S3 hold different parts of an attachment: PostgreSQL is the source
|
||||
of truth for metadata and access control; the private S3 bucket stores original
|
||||
bytes under a random server-generated key. The bucket name is fixed as
|
||||
`messenger-attachments`. No endpoint returns object keys or S3 credentials.
|
||||
|
||||
The MinIO root credentials are mounted only into MinIO and its one-shot bootstrap
|
||||
job. The API receives a separate access key restricted to `GetBucketLocation`,
|
||||
`ListBucket` with the `objects/` prefix, and Get/Put/Delete on `objects/*`.
|
||||
The bootstrap grants no anonymous policy. Verify
|
||||
that from inside the Compose network with
|
||||
`py -3 -X utf8 student-tests/check_private_bucket.py`; it expects an unsigned
|
||||
bucket-list request to receive HTTP 403 and confirms MinIO has no host port
|
||||
binding. The administrative console is not exposed.
|
||||
|
||||
Create local ignored secrets using `scripts/init-local-secrets.ps1`; never copy
|
||||
secrets from another lab. Do not rotate credentials while retaining the
|
||||
corresponding database or MinIO volumes. `scripts/restart.bat` / `.sh` restarts
|
||||
the API, database, Redis, MinIO and Nginx without deleting volumes.
|
||||
`scripts/stop-one.bat` / `.sh` stops only the API container whose hostname equals
|
||||
`TARGET_INSTANCE_ID` and leaves it stopped for the failover check.
|
||||
|
||||
Attachment writes reserve PostgreSQL metadata in `processing` state before
|
||||
writing S3. On success, the row becomes `ready` and the API returns 201. Errors
|
||||
attempt to delete the object and mark the reservation `failed`; if compensation
|
||||
cannot reach either store, the processing row remains visible to reconciliation.
|
||||
Run `scripts/reconcile_attachments.py` in dry-run mode first. It only acts on
|
||||
records/objects older than a 15-minute minimum grace period, marks stale uploads
|
||||
failed, deletes old unreferenced objects, and reports missing objects for ready
|
||||
metadata. `--apply` is required for changes.
|
||||
|
||||
MinIO, PostgreSQL, Redis, Nginx, and the Docker host are single-instance
|
||||
dependencies. This setup survives an API-replica failure and restarts with
|
||||
volumes, not loss of the Docker host or an individual state-store volume.
|
||||
|
||||
## Lab7 TLS
|
||||
|
||||
- Public ingress uses TLS on port 8080 and verifies a local CA.
|
||||
- LB uses mandatory mTLS to API replicas on port 8443.
|
||||
- PostgreSQL accepts TLS with a server certificate and API/worker use
|
||||
`sslmode=verify-full`.
|
||||
- Redis exposes TLS on 6380 for sessions and broker streams.
|
||||
- MinIO exposes HTTPS on 9000 and the API/worker verify its CA.
|
||||
- OpenBao exposes HTTPS on 8200 with persistent file storage and AppRole.
|
||||
- OTLP, Loki, Tempo and Prometheus telemetry links use TLS in the backend network.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
auth_enabled: false
|
||||
server:
|
||||
http_listen_port: 3100
|
||||
http_tls_config:
|
||||
cert_file: /certs/loki.crt
|
||||
key_file: /certs/loki.key
|
||||
|
||||
common:
|
||||
path_prefix: /loki
|
||||
storage:
|
||||
filesystem:
|
||||
chunks_directory: /loki/chunks
|
||||
rules_directory: /loki/rules
|
||||
replication_factor: 1
|
||||
ring:
|
||||
kvstore:
|
||||
store: inmemory
|
||||
|
||||
schema_config:
|
||||
configs:
|
||||
- from: 2024-01-01
|
||||
store: tsdb
|
||||
object_store: filesystem
|
||||
schema: v13
|
||||
index:
|
||||
prefix: index_
|
||||
period: 24h
|
||||
|
||||
limits_config:
|
||||
allow_structured_metadata: true
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
root_user=$(cat /run/secrets/minio_root_user)
|
||||
root_password=$(cat /run/secrets/minio_root_password)
|
||||
service_user=$(cat /run/secrets/s3_access_key_id)
|
||||
service_password=$(cat /run/secrets/s3_secret_access_key)
|
||||
|
||||
mkdir -p /root/.mc/certs/CAs
|
||||
cp /certs/ca.crt /root/.mc/certs/CAs/ca.crt
|
||||
cat /certs/ca.crt >> /etc/ssl/certs/ca-certificates.crt
|
||||
until mc alias set local https://minio:9000 "$root_user" "$root_password" >/dev/null 2>&1 \
|
||||
&& mc ready local >/dev/null 2>&1; do
|
||||
sleep 2
|
||||
done
|
||||
|
||||
bucket=${S3_BUCKET:-messenger-attachments}
|
||||
if [ "$bucket" != 'messenger-attachments' ]; then
|
||||
printf '%s\n' 'S3_BUCKET must be messenger-attachments' >&2
|
||||
exit 2
|
||||
fi
|
||||
policy=messenger-attachments-v2
|
||||
legacy_policy=messenger-attachments
|
||||
mc mb --ignore-existing "local/$bucket"
|
||||
if mc admin policy info local "$policy" >/dev/null 2>&1; then
|
||||
mc admin policy detach local "$policy" --user "$service_user" >/dev/null 2>&1 || true
|
||||
mc admin policy remove local "$policy" >/dev/null
|
||||
fi
|
||||
mc admin policy create local "$policy" /infra/s3-policy.json
|
||||
if ! mc admin user info local "$service_user" >/dev/null 2>&1; then
|
||||
mc admin user add local "$service_user" "$service_password" >/dev/null
|
||||
fi
|
||||
mc admin policy attach local "$policy" --user "$service_user" >/dev/null
|
||||
if mc admin policy info local "$legacy_policy" >/dev/null 2>&1; then
|
||||
mc admin policy detach local "$legacy_policy" --user "$service_user" >/dev/null
|
||||
mc admin policy remove local "$legacy_policy" >/dev/null
|
||||
fi
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
export MINIO_ROOT_USER="$(cat /run/secrets/minio_root_user)"
|
||||
export MINIO_ROOT_PASSWORD="$(cat /run/secrets/minio_root_password)"
|
||||
exec minio server /data --address :9000 --console-address :9001 --certs-dir /certs/minio-certs
|
||||
@@ -0,0 +1,37 @@
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
|
||||
upstream messenger_api {
|
||||
zone messenger_api 64k;
|
||||
server api:8443 resolve max_fails=1 fail_timeout=2s;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8443 ssl;
|
||||
server_name _;
|
||||
ssl_certificate /certs/public.crt;
|
||||
ssl_certificate_key /certs/public.key;
|
||||
ssl_trusted_certificate /certs/ca.crt;
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_session_tickets off;
|
||||
client_max_body_size 11m;
|
||||
client_body_timeout 20s;
|
||||
|
||||
location / {
|
||||
proxy_pass https://messenger_api;
|
||||
proxy_ssl_trusted_certificate /certs/ca.crt;
|
||||
proxy_ssl_verify on;
|
||||
proxy_ssl_verify_depth 2;
|
||||
proxy_ssl_name api.lab.local;
|
||||
proxy_ssl_certificate /certs/lb.crt;
|
||||
proxy_ssl_certificate_key /certs/lb.key;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_request_buffering off;
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_send_timeout 20s;
|
||||
proxy_read_timeout 20s;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
path "secret/data/lab7/service" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "secret/metadata/lab7/service" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
ui = false
|
||||
disable_mlock = true
|
||||
|
||||
storage "file" {
|
||||
path = "/openbao/file"
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "0.0.0.0:8200"
|
||||
tls_cert_file = "/certs/openbao.crt"
|
||||
tls_key_file = "/certs/openbao.key"
|
||||
tls_client_ca_file = "/certs/ca.crt"
|
||||
tls_require_and_verify_client_cert = false
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
tls:
|
||||
cert_file: /certs/otel-collector.crt
|
||||
key_file: /certs/otel-collector.key
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
tls:
|
||||
cert_file: /certs/otel-collector.crt
|
||||
key_file: /certs/otel-collector.key
|
||||
|
||||
processors:
|
||||
memory_limiter:
|
||||
check_interval: 1s
|
||||
limit_mib: 256
|
||||
spike_limit_mib: 64
|
||||
batch:
|
||||
timeout: 5s
|
||||
send_batch_size: 512
|
||||
|
||||
exporters:
|
||||
prometheus:
|
||||
endpoint: 0.0.0.0:8889
|
||||
tls:
|
||||
cert_file: /certs/otel-collector.crt
|
||||
key_file: /certs/otel-collector.key
|
||||
otlphttp/loki:
|
||||
endpoint: https://loki:3100/otlp
|
||||
tls:
|
||||
ca_file: /certs/ca.crt
|
||||
server_name_override: loki.lab.local
|
||||
otlp/tempo:
|
||||
endpoint: tempo:4317
|
||||
tls:
|
||||
ca_file: /certs/ca.crt
|
||||
|
||||
service:
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [prometheus]
|
||||
logs:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlphttp/loki]
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlp/tempo]
|
||||
@@ -0,0 +1,12 @@
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
evaluation_interval: 15s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: otel-collector
|
||||
scheme: https
|
||||
tls_config:
|
||||
ca_file: /certs/ca.crt
|
||||
server_name: otel-collector.lab.local
|
||||
static_configs:
|
||||
- targets: ['otel-collector:8889']
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetBucketLocation"],
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:ListBucket"],
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments"],
|
||||
"Condition": {
|
||||
"StringLike": {
|
||||
"s3:prefix": ["objects/", "objects/*", "staging/", "staging/*"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||
"Resource": [
|
||||
"arn:aws:s3:::messenger-attachments/objects/*",
|
||||
"arn:aws:s3:::messenger-attachments/staging/*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
server:
|
||||
http_listen_port: 3200
|
||||
http_tls_config:
|
||||
cert_file: /certs/tempo.crt
|
||||
key_file: /certs/tempo.key
|
||||
|
||||
distributor:
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
grpc:
|
||||
endpoint: 0.0.0.0:4317
|
||||
tls:
|
||||
cert_file: /certs/tempo.crt
|
||||
key_file: /certs/tempo.key
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
|
||||
storage:
|
||||
trace:
|
||||
backend: local
|
||||
local:
|
||||
path: /var/tempo/traces
|
||||
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS users_username_key ON users (username);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id UUID NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
position INT NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_id_idx ON chat_members (user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
sender_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS messages_chat_history_idx ON messages (chat_id, created_at ASC, id ASC);
|
||||
@@ -0,0 +1,14 @@
|
||||
CREATE TABLE IF NOT EXISTS message_idempotency (
|
||||
user_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
method TEXT NOT NULL CHECK (method = 'POST'),
|
||||
resource_path TEXT NOT NULL,
|
||||
idempotency_key VARCHAR(128) NOT NULL,
|
||||
request_hash CHAR(64) NOT NULL,
|
||||
response JSONB,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
PRIMARY KEY (user_id, method, resource_path, idempotency_key)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_idempotency_expiry_idx
|
||||
ON message_idempotency (user_id, expires_at);
|
||||
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
object_key TEXT NOT NULL UNIQUE,
|
||||
filename TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 CHAR(64) NOT NULL,
|
||||
width INT NOT NULL CHECK (width > 0),
|
||||
height INT NOT NULL CHECK (height > 0),
|
||||
status TEXT NOT NULL CHECK (status IN ('processing', 'ready', 'failed')),
|
||||
variants JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
CREATE INDEX IF NOT EXISTS attachments_processing_created_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status = 'processing';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_attachments (
|
||||
message_id UUID NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE RESTRICT,
|
||||
position INT NOT NULL CHECK (position >= 0),
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
@@ -0,0 +1,38 @@
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN IF NOT EXISTS staging_key TEXT,
|
||||
ADD COLUMN IF NOT EXISTS pipeline_version INTEGER NOT NULL DEFAULT 1;
|
||||
|
||||
UPDATE attachments
|
||||
SET staging_key = object_key
|
||||
WHERE staging_key IS NULL;
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachment_jobs (
|
||||
job_id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
pipeline_version INTEGER NOT NULL,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
status TEXT NOT NULL CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
||||
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
lease_until TIMESTAMPTZ,
|
||||
last_error TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL,
|
||||
UNIQUE (attachment_id, pipeline_version)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachment_jobs_ready_idx
|
||||
ON attachment_jobs (status, next_attempt_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachment_outbox (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
job_id UUID NOT NULL REFERENCES attachment_jobs (job_id) ON DELETE CASCADE,
|
||||
published_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachment_outbox_pending_idx
|
||||
ON attachment_outbox (published_at, id);
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS attachment_replay_audit (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
old_pipeline_version INTEGER NOT NULL,
|
||||
new_pipeline_version INTEGER NOT NULL,
|
||||
reason TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import json
|
||||
import os
|
||||
import ssl
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _read(path_env):
|
||||
path = os.environ.get(path_env)
|
||||
if not path or not Path(path).is_file():
|
||||
return None
|
||||
value = Path(path).read_text(encoding='utf-8').strip()
|
||||
return value or None
|
||||
|
||||
|
||||
def load_service_secrets():
|
||||
address = os.environ.get('BAO_ADDR')
|
||||
role_id = _read('BAO_ROLE_ID_FILE')
|
||||
secret_id = _read('BAO_SECRET_ID_FILE')
|
||||
ca_file = os.environ.get('BAO_CACERT')
|
||||
if not (address and role_id and secret_id and ca_file):
|
||||
return
|
||||
|
||||
context = ssl.create_default_context(cafile=ca_file)
|
||||
request = urllib.request.Request(
|
||||
address.rstrip('/') + '/v1/auth/approle/login',
|
||||
data=json.dumps({'role_id': role_id, 'secret_id': secret_id}).encode('utf-8'),
|
||||
headers={'Content-Type': 'application/json'},
|
||||
method='POST',
|
||||
)
|
||||
with urllib.request.urlopen(request, context=context, timeout=5) as response:
|
||||
token = json.loads(response.read().decode('utf-8'))['auth']['client_token']
|
||||
|
||||
path = os.environ.get('BAO_KV_PATH', 'lab7/service')
|
||||
request = urllib.request.Request(address.rstrip('/') + '/v1/secret/data/' + path)
|
||||
request.add_header('X-Vault-Token', token)
|
||||
with urllib.request.urlopen(request, context=context, timeout=5) as response:
|
||||
data = json.loads(response.read().decode('utf-8'))['data']['data']
|
||||
|
||||
aliases = {
|
||||
'DATABASE_URL': 'database_url',
|
||||
'S3_ACCESS_KEY_ID': 's3_access_key_id',
|
||||
'S3_SECRET_ACCESS_KEY': 's3_secret_access_key',
|
||||
'CACHE_URL': 'cache_url',
|
||||
'BROKER_URL': 'broker_url',
|
||||
}
|
||||
for variable, key in aliases.items():
|
||||
if data.get(key):
|
||||
os.environ[variable] = str(data[key])
|
||||
if data.get('database_url'):
|
||||
os.environ.pop('DATABASE_URL_FILE', None)
|
||||
if data.get('s3_access_key_id'):
|
||||
os.environ.pop('S3_ACCESS_KEY_ID_FILE', None)
|
||||
if data.get('s3_secret_access_key'):
|
||||
os.environ.pop('S3_SECRET_ACCESS_KEY_FILE', None)
|
||||
@@ -0,0 +1,8 @@
|
||||
argon2-cffi==23.1.0
|
||||
psycopg[binary,pool]==3.2.9
|
||||
redis==5.2.1
|
||||
boto3==1.37.38
|
||||
Pillow==11.1.0
|
||||
opentelemetry-api==1.31.1
|
||||
opentelemetry-sdk==1.31.1
|
||||
opentelemetry-exporter-otlp-proto-http==1.31.1
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
|
||||
|
||||
class S3Unavailable(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _read_secret(file_variable, value_variable):
|
||||
path = os.environ.get(file_variable)
|
||||
if path:
|
||||
return Path(path).read_text(encoding='utf-8').strip()
|
||||
value = os.environ.get(value_variable, '').strip()
|
||||
if not value:
|
||||
raise RuntimeError(value_variable + ' or ' + file_variable + ' is required')
|
||||
return value
|
||||
|
||||
|
||||
class ObjectStore:
|
||||
def __init__(self, client, bucket):
|
||||
self.client = client
|
||||
self.bucket = bucket
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
ca_file = os.environ.get('S3_CA_FILE')
|
||||
verify = ca_file if ca_file and os.path.isfile(ca_file) else True
|
||||
client = boto3.client(
|
||||
's3',
|
||||
endpoint_url=os.environ.get('S3_ENDPOINT', 'http://minio:9000'),
|
||||
region_name=os.environ.get('S3_REGION', 'us-east-1'),
|
||||
verify=verify,
|
||||
aws_access_key_id=_read_secret('S3_ACCESS_KEY_ID_FILE', 'S3_ACCESS_KEY_ID'),
|
||||
aws_secret_access_key=_read_secret(
|
||||
'S3_SECRET_ACCESS_KEY_FILE', 'S3_SECRET_ACCESS_KEY'
|
||||
),
|
||||
config=Config(
|
||||
signature_version='s3v4',
|
||||
s3={'addressing_style': 'path'},
|
||||
connect_timeout=2,
|
||||
read_timeout=10,
|
||||
max_pool_connections=32,
|
||||
retries={'max_attempts': 2, 'mode': 'standard'},
|
||||
),
|
||||
)
|
||||
return cls(client, os.environ.get('S3_BUCKET', 'messenger-attachments'))
|
||||
|
||||
def ping(self):
|
||||
try:
|
||||
self.client.get_bucket_location(Bucket=self.bucket)
|
||||
except (BotoCoreError, ClientError) as error:
|
||||
raise S3Unavailable('S3 bucket is unavailable') from error
|
||||
|
||||
def put_file(self, key, fileobj, size, content_type):
|
||||
fileobj.seek(0)
|
||||
try:
|
||||
self.client.put_object(
|
||||
Bucket=self.bucket,
|
||||
Key=key,
|
||||
Body=fileobj,
|
||||
ContentLength=size,
|
||||
ContentType=content_type,
|
||||
)
|
||||
except (BotoCoreError, ClientError) as error:
|
||||
raise S3Unavailable('S3 upload failed') from error
|
||||
|
||||
def get_object(self, key):
|
||||
try:
|
||||
return self.client.get_object(Bucket=self.bucket, Key=key)
|
||||
except ClientError as error:
|
||||
if error.response.get('Error', {}).get('Code') in {
|
||||
'404', 'NoSuchKey', 'NotFound',
|
||||
}:
|
||||
return None
|
||||
raise S3Unavailable('S3 download failed') from error
|
||||
except BotoCoreError as error:
|
||||
raise S3Unavailable('S3 download failed') from error
|
||||
|
||||
def delete_object(self, key):
|
||||
try:
|
||||
self.client.delete_object(Bucket=self.bucket, Key=key)
|
||||
except (BotoCoreError, ClientError) as error:
|
||||
raise S3Unavailable('S3 delete failed') from error
|
||||
|
||||
def object_exists(self, key):
|
||||
try:
|
||||
self.client.head_object(Bucket=self.bucket, Key=key)
|
||||
return True
|
||||
except ClientError as error:
|
||||
if error.response.get('Error', {}).get('Code') in {
|
||||
'404', 'NoSuchKey', 'NotFound',
|
||||
}:
|
||||
return False
|
||||
raise S3Unavailable('S3 object check failed') from error
|
||||
except BotoCoreError as error:
|
||||
raise S3Unavailable('S3 object check failed') from error
|
||||
|
||||
def list_objects(self):
|
||||
try:
|
||||
paginator = self.client.get_paginator('list_objects_v2')
|
||||
for page in paginator.paginate(Bucket=self.bucket, Prefix='objects/'):
|
||||
for item in page.get('Contents', []):
|
||||
yield item
|
||||
except (BotoCoreError, ClientError) as error:
|
||||
raise S3Unavailable('S3 listing failed') from error
|
||||
@@ -0,0 +1,41 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$root = Split-Path -Parent $PSScriptRoot
|
||||
$certDir = Join-Path $root 'infra\certs'
|
||||
$openssl = 'C:\Program Files\Git\usr\bin\openssl.exe'
|
||||
New-Item -ItemType Directory -Force $certDir | Out-Null
|
||||
Set-Location $certDir
|
||||
|
||||
if ((Test-Path ca.key) -and (Test-Path ca.crt) -and (Test-Path public.crt) -and (Test-Path lb.crt) -and (Test-Path api.crt) -and (Test-Path openbao.crt) -and (Test-Path minio.crt) -and (Test-Path postgres.crt) -and (Test-Path redis.crt) -and (Test-Path otel-collector.crt) -and (Test-Path loki.crt) -and (Test-Path tempo.crt)) {
|
||||
Write-Output 'Local TLS certificates already exist.'
|
||||
exit 0
|
||||
}
|
||||
|
||||
& $openssl genrsa -out ca.key 4096
|
||||
& $openssl req -new -x509 -key ca.key -sha256 -days 3650 -subj '/CN=MTUSI Lab7 Root CA' -addext 'basicConstraints=critical,CA:true' -addext 'keyUsage=critical,keyCertSign,cRLSign' -out ca.crt
|
||||
|
||||
function New-LeafCertificate {
|
||||
param([string]$Name, [string]$CommonName, [string]$SubjectAltName, [string]$ExtendedKeyUsage)
|
||||
& $openssl genrsa -out "$Name.key" 2048
|
||||
& $openssl req -new -key "$Name.key" -subj "/CN=$CommonName" -out "$Name.csr"
|
||||
$ext = "subjectAltName=$SubjectAltName`nkeyUsage=digitalSignature,keyEncipherment`nextendedKeyUsage=$ExtendedKeyUsage`n"
|
||||
$extPath = Join-Path $certDir "$Name.ext"
|
||||
[IO.File]::WriteAllText($extPath, $ext, [Text.UTF8Encoding]::new($false))
|
||||
& $openssl x509 -req -in "$Name.csr" -CA ca.crt -CAkey ca.key -CAcreateserial -out "$Name.crt" -days 365 -sha256 -extfile $extPath
|
||||
Remove-Item "$Name.csr", "$Name.ext"
|
||||
}
|
||||
|
||||
New-LeafCertificate 'public' 'localhost' 'DNS:localhost,DNS:nginx,DNS:lb.lab.local' 'serverAuth'
|
||||
New-LeafCertificate 'lb' 'lb.lab.local' 'DNS:lb.lab.local,URI:spiffe://lab7/lb' 'clientAuth'
|
||||
New-LeafCertificate 'api' 'api.lab.local' 'DNS:api,DNS:api.lab.local,DNS:localhost,IP:127.0.0.1' 'serverAuth,clientAuth'
|
||||
New-LeafCertificate 'openbao' 'openbao.lab.local' 'DNS:openbao,DNS:openbao.lab.local,IP:127.0.0.1' 'serverAuth'
|
||||
New-LeafCertificate 'minio' 'minio.lab.local' 'DNS:minio,DNS:minio.lab.local,IP:127.0.0.1' 'serverAuth'
|
||||
New-LeafCertificate 'postgres' 'postgres.lab.local' 'DNS:postgres,DNS:postgres.lab.local' 'serverAuth'
|
||||
New-LeafCertificate 'redis' 'redis.lab.local' 'DNS:redis,DNS:redis.lab.local,IP:127.0.0.1' 'serverAuth'
|
||||
New-LeafCertificate 'otel-collector' 'otel-collector.lab.local' 'DNS:otel-collector,DNS:otel-collector.lab.local' 'serverAuth'
|
||||
New-LeafCertificate 'loki' 'loki.lab.local' 'DNS:loki,DNS:loki.lab.local' 'serverAuth'
|
||||
New-LeafCertificate 'tempo' 'tempo.lab.local' 'DNS:tempo,DNS:tempo.lab.local' 'serverAuth'
|
||||
|
||||
New-Item -ItemType Directory -Force minio-certs | Out-Null
|
||||
Copy-Item minio.crt minio-certs/public.crt -Force
|
||||
Copy-Item minio.key minio-certs/private.key -Force
|
||||
Write-Output 'Created local CA and leaf certificates.'
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
mkdir -p /certs
|
||||
cd /certs
|
||||
if [ -f ca.key ] && [ -f ca.crt ] && [ -f lb.crt ] && [ -f api.crt ] && [ -f openbao.crt ]; then
|
||||
mkdir -p /postgres-certs
|
||||
cp ca.crt postgres.crt postgres.key /postgres-certs/
|
||||
chmod 600 /postgres-certs/postgres.key
|
||||
chown -R 70:70 /postgres-certs 2>/dev/null || true
|
||||
exit 0
|
||||
fi
|
||||
|
||||
openssl genrsa -out ca.key 4096 >/dev/null 2>&1
|
||||
openssl req -x509 -new -key ca.key -sha256 -days 3650 -subj "/CN=MTUSI Lab7 Root CA" -out ca.crt >/dev/null 2>&1
|
||||
|
||||
make_leaf() {
|
||||
name="$1"
|
||||
cn="$2"
|
||||
san="$3"
|
||||
openssl genrsa -out "$name.key" 2048 >/dev/null 2>&1
|
||||
openssl req -new -key "$name.key" -subj "/CN=$cn" -out "$name.csr" >/dev/null 2>&1
|
||||
cat > "$name.ext" <<EOF
|
||||
subjectAltName=$san
|
||||
keyUsage=digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=$4
|
||||
EOF
|
||||
openssl x509 -req -in "$name.csr" -CA ca.crt -CAkey ca.key -CAcreateserial -out "$name.crt" -days 365 -sha256 -extfile "$name.ext" >/dev/null 2>&1
|
||||
rm -f "$name.csr" "$name.ext"
|
||||
}
|
||||
|
||||
make_leaf public localhost "DNS:localhost,DNS:nginx,DNS:lb.lab.local" serverAuth
|
||||
make_leaf lb lb.lab.local "DNS:lb.lab.local,URI:spiffe://lab7/lb" clientAuth
|
||||
make_leaf api api.lab.local "DNS:api,DNS:api.lab.local" serverAuth,clientAuth
|
||||
make_leaf openbao openbao.lab.local "DNS:openbao,DNS:openbao.lab.local" serverAuth
|
||||
make_leaf minio minio.lab.local "DNS:minio,DNS:minio.lab.local" serverAuth
|
||||
mkdir -p minio-certs
|
||||
cp minio.crt minio-certs/public.crt
|
||||
cp minio.key minio-certs/private.key
|
||||
|
||||
chmod 600 ca.key public.key lb.key api.key openbao.key
|
||||
@@ -0,0 +1,60 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$root = Split-Path -Parent $PSScriptRoot
|
||||
$secrets = Join-Path $root 'secrets'
|
||||
$names = @(
|
||||
'postgres_password',
|
||||
'database_url',
|
||||
'minio_root_user',
|
||||
'minio_root_password',
|
||||
's3_access_key_id',
|
||||
's3_secret_access_key'
|
||||
)
|
||||
|
||||
if (-not (Test-Path (Join-Path $root '.env'))) {
|
||||
Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env')
|
||||
}
|
||||
|
||||
$existing = @($names | Where-Object { Test-Path (Join-Path $secrets $_) })
|
||||
if ($existing.Count -gt 0) {
|
||||
if ($existing.Count -eq $names.Count) {
|
||||
Write-Output 'Local secrets already exist; leaving them unchanged.'
|
||||
exit 0
|
||||
}
|
||||
throw 'The local secret set is incomplete. Restore it as a set; do not rotate credentials against existing volumes.'
|
||||
}
|
||||
|
||||
New-Item -ItemType Directory -Force $secrets | Out-Null
|
||||
function New-HexSecret([int]$byteCount) {
|
||||
$bytes = New-Object byte[] $byteCount
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
try {
|
||||
$rng.GetBytes($bytes)
|
||||
}
|
||||
finally {
|
||||
$rng.Dispose()
|
||||
}
|
||||
$builder = New-Object System.Text.StringBuilder
|
||||
foreach ($byte in $bytes) {
|
||||
[void]$builder.Append($byte.ToString('x2'))
|
||||
}
|
||||
return $builder.ToString()
|
||||
}
|
||||
|
||||
$databasePassword = New-HexSecret 32
|
||||
$values = @{
|
||||
'postgres_password' = $databasePassword
|
||||
'database_url' = "postgresql://messenger:$databasePassword@postgres:5432/messenger"
|
||||
'minio_root_user' = 'root-' + (New-HexSecret 6)
|
||||
'minio_root_password' = New-HexSecret 32
|
||||
's3_access_key_id' = New-HexSecret 10
|
||||
's3_secret_access_key' = New-HexSecret 32
|
||||
}
|
||||
foreach ($name in $names) {
|
||||
$path = Join-Path $secrets $name
|
||||
[System.IO.File]::WriteAllText(
|
||||
$path,
|
||||
$values[$name],
|
||||
[System.Text.Encoding]::ASCII
|
||||
)
|
||||
}
|
||||
Write-Output 'Created local Compose secrets. Their values were not printed.'
|
||||
@@ -0,0 +1,37 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$root = Split-Path -Parent $PSScriptRoot
|
||||
$secrets = Join-Path $root 'secrets'
|
||||
New-Item -ItemType Directory -Force $secrets | Out-Null
|
||||
|
||||
$status = docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt openbao bao status -format=json 2>$null
|
||||
if (-not $status) { throw 'OpenBao container is not available' }
|
||||
$statusObj = $status | ConvertFrom-Json
|
||||
if (-not $statusObj.initialized) {
|
||||
$init = docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt openbao bao operator init -key-shares=1 -key-threshold=1 -format=json | ConvertFrom-Json
|
||||
[IO.File]::WriteAllText((Join-Path $secrets 'openbao_unseal_key'), $init.unseal_keys_b64[0])
|
||||
[IO.File]::WriteAllText((Join-Path $secrets 'openbao_root_token'), $init.root_token)
|
||||
}
|
||||
|
||||
$unsealKey = ([IO.File]::ReadAllText((Join-Path $secrets 'openbao_unseal_key'))).Trim()
|
||||
$rootToken = ([IO.File]::ReadAllText((Join-Path $secrets 'openbao_root_token'))).Trim()
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt openbao bao operator unseal $unsealKey | Out-Null
|
||||
|
||||
$env:BAO_ADDR = 'https://openbao:8200'
|
||||
$env:BAO_CACERT = '/certs/ca.crt'
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt openbao bao login $rootToken | Out-Null
|
||||
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao sh -lc 'bao secrets list -format=json | grep -q \"secret/\" || bao secrets enable -path=secret kv-v2'
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao sh -lc 'bao auth list -format=json | grep -q \"approle/\" || bao auth enable approle'
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao bao policy write app-policy /policies/app.hcl | Out-Null
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao bao write auth/approle/role/lab7-app secret_id_ttl=30m token_ttl=30m token_max_ttl=1h policies=app-policy | Out-Null
|
||||
|
||||
$roleId = docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao bao read -field=role_id auth/approle/role/lab7-app/role-id
|
||||
$secretId = docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao bao write -field=secret_id -f auth/approle/role/lab7-app/secret-id
|
||||
[IO.File]::WriteAllText((Join-Path $secrets 'role_id'), $roleId.Trim())
|
||||
[IO.File]::WriteAllText((Join-Path $secrets 'secret_id'), $secretId.Trim())
|
||||
|
||||
$databaseUrl = ([IO.File]::ReadAllText((Join-Path $secrets 'database_url'))).Trim()
|
||||
$s3Access = ([IO.File]::ReadAllText((Join-Path $secrets 's3_access_key_id'))).Trim()
|
||||
$s3Secret = ([IO.File]::ReadAllText((Join-Path $secrets 's3_secret_access_key'))).Trim()
|
||||
docker compose exec -T -e BAO_ADDR=https://127.0.0.1:8200 -e BAO_CACERT=/certs/ca.crt -e BAO_TOKEN=$rootToken openbao sh -lc "bao kv put secret/lab7/service database_url='$databaseUrl' s3_access_key_id='$s3Access' s3_secret_access_key='$s3Secret' cache_url='rediss://redis:6380/0?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt' broker_url='rediss://redis:6380/1?ssl_cert_reqs=required&ssl_ca_certs=/certs/ca.crt'" | Out-Null
|
||||
Write-Output 'OpenBao initialized/unsealed and AppRole credentials created.'
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Apply database migrations once before API replicas start."""
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from db import Store
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
|
||||
store = Store.connect()
|
||||
try:
|
||||
logging.info('database migrations are current')
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Dry-run first; remove only old, unreferenced objects or stale upload attempts."""
|
||||
import argparse
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from db import ApiError, Store
|
||||
from s3store import ObjectStore, S3Unavailable
|
||||
|
||||
|
||||
def parse_timestamp(value):
|
||||
return datetime.fromisoformat(value.replace('Z', '+00:00'))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--grace-seconds', type=int, default=900)
|
||||
parser.add_argument('--apply', action='store_true')
|
||||
args = parser.parse_args()
|
||||
if args.grace_seconds < 900:
|
||||
parser.error('--grace-seconds must be at least 900')
|
||||
|
||||
store = Store.connect(run_migrations=False)
|
||||
objects = ObjectStore.from_env()
|
||||
try:
|
||||
cutoff = datetime.now(timezone.utc) - timedelta(seconds=args.grace_seconds)
|
||||
records = store.list_attachments_for_reconciliation()
|
||||
by_key = {record['object_key']: record for record in records}
|
||||
summary = {
|
||||
'mode': 'apply' if args.apply else 'dry-run',
|
||||
'grace_seconds': args.grace_seconds,
|
||||
'stale_uploads': 0,
|
||||
'old_failed_objects': 0,
|
||||
'orphan_objects': 0,
|
||||
'missing_ready_objects': 0,
|
||||
'actions_applied': 0,
|
||||
}
|
||||
|
||||
for record in records:
|
||||
key = record['object_key']
|
||||
if (record['status'] == 'processing'
|
||||
and parse_timestamp(record['created_at']) < cutoff):
|
||||
summary['stale_uploads'] += 1
|
||||
exists = objects.object_exists(key)
|
||||
if args.apply:
|
||||
if exists:
|
||||
objects.delete_object(key)
|
||||
store.mark_attachment_failed(
|
||||
record['id'], 'upload_interrupted', datetime.now(timezone.utc)
|
||||
)
|
||||
summary['actions_applied'] += 1
|
||||
elif (record['status'] == 'failed'
|
||||
and parse_timestamp(record['updated_at']) < cutoff):
|
||||
if objects.object_exists(key):
|
||||
summary['old_failed_objects'] += 1
|
||||
if args.apply:
|
||||
objects.delete_object(key)
|
||||
summary['actions_applied'] += 1
|
||||
elif record['status'] == 'ready' and not objects.object_exists(key):
|
||||
summary['missing_ready_objects'] += 1
|
||||
if args.apply:
|
||||
store.mark_reconciled_missing(
|
||||
record['id'], datetime.now(timezone.utc)
|
||||
)
|
||||
summary['actions_applied'] += 1
|
||||
|
||||
for item in objects.list_objects():
|
||||
key = item['Key']
|
||||
if key in by_key or item['LastModified'] >= cutoff:
|
||||
continue
|
||||
summary['orphan_objects'] += 1
|
||||
if args.apply:
|
||||
objects.delete_object(key)
|
||||
summary['actions_applied'] += 1
|
||||
|
||||
print(json.dumps(summary, sort_keys=True))
|
||||
finally:
|
||||
store.close()
|
||||
objects.client.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (ApiError, S3Unavailable) as error:
|
||||
print('Reconciliation failed: ' + error.message if isinstance(error, ApiError) else
|
||||
'Reconciliation failed: S3 is unavailable', file=sys.stderr)
|
||||
raise SystemExit(1) from None
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Replay one failed attachment through a new pipeline version."""
|
||||
import argparse
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from db import ApiError, Store
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("attachment_id")
|
||||
parser.add_argument("--reason", default="operator_replay")
|
||||
args = parser.parse_args()
|
||||
store = Store.connect(run_migrations=False)
|
||||
try:
|
||||
job_id, version = store.replay_attachment(
|
||||
args.attachment_id,
|
||||
args.reason,
|
||||
datetime.now(timezone.utc),
|
||||
)
|
||||
print("replayed attachment=%s job=%s pipeline_version=%d" %
|
||||
(args.attachment_id, job_id, version))
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except ApiError as error:
|
||||
print("Replay failed: " + error.message, file=sys.stderr)
|
||||
raise SystemExit(1) from None
|
||||
@@ -0,0 +1,3 @@
|
||||
@echo off
|
||||
cd /d "%~dp0.."
|
||||
docker compose --env-file .env restart api postgres redis minio nginx
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose restart api postgres redis minio nginx
|
||||
@@ -0,0 +1,17 @@
|
||||
@echo off
|
||||
setlocal
|
||||
if not defined TARGET_INSTANCE_ID (
|
||||
echo TARGET_INSTANCE_ID is required 1>&2
|
||||
exit /b 2
|
||||
)
|
||||
cd /d "%~dp0.."
|
||||
for /f "delims=" %%I in ('docker compose ps -q api') do (
|
||||
for /f "delims=" %%H in ('docker inspect --format "{{.Config.Hostname}}" %%I') do (
|
||||
if "%%H"=="%TARGET_INSTANCE_ID%" (
|
||||
docker stop --time 2 %%I >NUL
|
||||
exit /b %errorlevel%
|
||||
)
|
||||
)
|
||||
)
|
||||
echo No running API replica matches TARGET_INSTANCE_ID 1>&2
|
||||
exit /b 1
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
for container_id in $(docker compose ps -q api); do
|
||||
instance_id=$(docker inspect --format '{{.Config.Hostname}}' "$container_id")
|
||||
if [ "$instance_id" = "$TARGET_INSTANCE_ID" ]; then
|
||||
docker stop --time 2 "$container_id" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
printf '%s\n' 'No running API replica matches TARGET_INSTANCE_ID' >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
import socket
|
||||
import ssl
|
||||
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 8443
|
||||
CA = '/certs/ca.crt'
|
||||
|
||||
|
||||
def request(cert=None, key=None):
|
||||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=CA)
|
||||
context.check_hostname = True
|
||||
if cert:
|
||||
context.load_cert_chain(cert, key)
|
||||
with socket.create_connection((HOST, PORT), timeout=3) as raw:
|
||||
with context.wrap_socket(raw, server_hostname='localhost') as sock:
|
||||
sock.sendall(b'GET /health/live HTTP/1.0\r\nHost: localhost\r\n\r\n')
|
||||
return sock.recv(1024)
|
||||
|
||||
|
||||
print('no client certificate:')
|
||||
try:
|
||||
request()
|
||||
except ssl.SSLError as error:
|
||||
print('PASS: rejected:', error.reason)
|
||||
else:
|
||||
raise SystemExit('FAIL: no-certificate connection was accepted')
|
||||
|
||||
print('wrong identity signed by trusted CA:')
|
||||
try:
|
||||
request('/certs/public.crt', '/certs/public.key')
|
||||
except (ssl.SSLError, ConnectionError, ConnectionResetError):
|
||||
print('PASS: rejected')
|
||||
else:
|
||||
raise SystemExit('FAIL: wrong identity was accepted')
|
||||
|
||||
print('allowed LB identity:')
|
||||
response = request('/certs/lb.crt', '/certs/lb.key')
|
||||
if b' 200 ' not in response:
|
||||
raise SystemExit('FAIL: LB identity did not receive success')
|
||||
print('PASS: LB identity received HTTP 200')
|
||||
@@ -2,15 +2,22 @@
|
||||
|
||||
## Активы и акторы
|
||||
|
||||
TODO: пользовательские credentials/сессии, сообщения/вложения, service identities, CA/secrets, telemetry. Акторы: участник чата, посторонний пользователь, внешний клиент, скомпрометированный сервис, оператор. Опишите доступ атакующего и границы учебного стенда.
|
||||
Активы: credentials/password hashes, bearer-сессии, сообщения/вложения, service identities, CA/secrets и telemetry. Акторы: участник чата, посторонний пользователь, внешний client, скомпрометированный service и operator. Граница стенда — Docker network и localhost host.
|
||||
|
||||
## Границы доверия
|
||||
|
||||
TODO: схема и ссылка на заполненную contracts/trust-matrix.md; публичные/внутренние интерфейсы, процессы, секреты и их выдача.
|
||||
Схема и конкретные endpoints описаны в [матрице доверия](../contracts/trust-matrix.md). Внешний клиент идёт через HTTPS/Nginx в API; LB использует mTLS к репликам; service secrets читаются через OpenBao AppRole.
|
||||
|
||||
| Угроза и конкретный путь | Нарушаемый инвариант | Контроль | Отрицательный тест | Остаточное ограничение |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO | TODO |
|
||||
| Перехват публичного API-трафика | Чтение credentials/сообщений | TLS, проверяемый CA и hostname | trusted/untrusted CA smoke | Учебный long-lived CA |
|
||||
| Прямой bypass LB к API | Обход LB allowlist | API не публикует host-port и требует mTLS | no-cert/wrong identity/allowed LB | Docker-host compromise |
|
||||
| Подделка identity через X-User-Id | Чтение чужого чата | Bearer-сессия и DB membership ACL | публичный ACL smoke | Redis compromise |
|
||||
| Brute-force login | Перебор credentials и Argon2 CPU | Argon2id и login rate limit 429/Retry-After | повторный login burst | лимит процесса, не общий Redis |
|
||||
| Скомпрометированный attachment worker | Чтение чужого S3 prefix | private bucket, scoped API key | bucket policy/ACL тесты | local HTTP boundary |
|
||||
| Скомпрометированный app читает OpenBao | Выход за service policy | AppRole scoped policy и TLS | allow/deny policy тест | manual init/unseal |
|
||||
|
||||
Bootstrap: оператор создает локальный CA/leaf через `bootstrap-certs.ps1`; CA key и leaf private keys игнорируются Git. OpenBao init дает один unseal key и root token оператору в `secrets/`, API/worker получают только AppRole role_id/secret_id. После рестарта OpenBao оператор выполняет manual unseal или восстанавливает unseal key из защищенного operator store.
|
||||
|
||||
На 4 минимум 6 конкретных угроз. Не заменяйте сценарии словами «SQL injection», «безопасный Docker», «mTLS включён» без пути и проверки.
|
||||
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
|
||||
import redis
|
||||
|
||||
|
||||
class SessionStore:
|
||||
def __init__(self, url, connect_timeout=2.0, socket_timeout=2.0):
|
||||
self._url = url
|
||||
self._client = redis.from_url(
|
||||
url,
|
||||
decode_responses=True,
|
||||
socket_connect_timeout=connect_timeout,
|
||||
socket_timeout=socket_timeout,
|
||||
health_check_interval=30,
|
||||
)
|
||||
self._prefix = 'sess:'
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
url = _read_config('CACHE_URL_FILE', 'CACHE_URL')
|
||||
if not url:
|
||||
raise RuntimeError('CACHE_URL or CACHE_URL_FILE is required when SESSION_STORE=redis')
|
||||
return cls(url)
|
||||
|
||||
def ping(self):
|
||||
return self._client.ping()
|
||||
|
||||
def create(self, token, user_id, ttl_seconds):
|
||||
self._client.setex(self._prefix + token, ttl_seconds, user_id)
|
||||
|
||||
def get_user_id(self, token):
|
||||
return self._client.get(self._prefix + token)
|
||||
|
||||
def revoke(self, token):
|
||||
return self._client.delete(self._prefix + token) > 0
|
||||
|
||||
|
||||
def _read_config(file_env, value_env):
|
||||
path = os.environ.get(file_env)
|
||||
if path and os.path.isfile(path):
|
||||
with open(path, encoding='utf-8') as handle:
|
||||
return handle.read().strip()
|
||||
return os.environ.get(value_env, '').strip()
|
||||
@@ -0,0 +1,76 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
|
||||
from opentelemetry import metrics, trace
|
||||
from opentelemetry.sdk.resources import Resource
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
from opentelemetry.sdk.metrics import MeterProvider
|
||||
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
|
||||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
||||
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
|
||||
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
|
||||
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
|
||||
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
span = trace.get_current_span()
|
||||
context = span.get_span_context()
|
||||
payload = {
|
||||
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||
"severity": record.levelname,
|
||||
"service.name": os.environ.get("OTEL_SERVICE_NAME", "messenger"),
|
||||
"event": record.getMessage(),
|
||||
}
|
||||
if context.is_valid:
|
||||
payload["trace_id"] = format(context.trace_id, "032x")
|
||||
payload["span_id"] = format(context.span_id, "016x")
|
||||
return json.dumps(payload, ensure_ascii=False)
|
||||
|
||||
|
||||
def configure(service_name):
|
||||
otlp = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT", "http://otel-collector:4318")
|
||||
certificate = os.environ.get("OTEL_EXPORTER_OTLP_CERTIFICATE")
|
||||
exporter_kwargs = {"certificate_file": certificate} if certificate else {}
|
||||
resource = Resource.create({
|
||||
"service.name": service_name,
|
||||
"service.version": os.environ.get("BUILD_REVISION", "local"),
|
||||
"service.instance.id": os.environ.get("INSTANCE_ID", socket.gethostname()),
|
||||
"deployment.environment.name": "lab",
|
||||
})
|
||||
|
||||
tracer_provider = TracerProvider(resource=resource)
|
||||
tracer_provider.add_span_processor(BatchSpanProcessor(
|
||||
OTLPSpanExporter(endpoint=otlp.rstrip("/") + "/v1/traces", **exporter_kwargs)
|
||||
))
|
||||
trace.set_tracer_provider(tracer_provider)
|
||||
|
||||
metric_reader = PeriodicExportingMetricReader(
|
||||
OTLPMetricExporter(endpoint=otlp.rstrip("/") + "/v1/metrics", **exporter_kwargs),
|
||||
export_interval_millis=int(os.environ.get("OTEL_METRIC_EXPORT_INTERVAL", "10000")),
|
||||
)
|
||||
meter_provider = MeterProvider(resource=resource, metric_readers=[metric_reader])
|
||||
metrics.set_meter_provider(meter_provider)
|
||||
|
||||
logger_provider = LoggerProvider(resource=resource)
|
||||
logger_provider.add_log_record_processor(BatchLogRecordProcessor(
|
||||
OTLPLogExporter(endpoint=otlp.rstrip("/") + "/v1/logs", **exporter_kwargs)
|
||||
))
|
||||
otel_log_handler = LoggingHandler(
|
||||
level=logging.NOTSET,
|
||||
logger_provider=logger_provider,
|
||||
)
|
||||
|
||||
root = logging.getLogger()
|
||||
root.handlers.clear()
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(JsonFormatter())
|
||||
root.addHandler(handler)
|
||||
root.addHandler(otel_log_handler)
|
||||
root.setLevel(os.environ.get("LOG_LEVEL", "INFO").upper())
|
||||
return trace.get_tracer(service_name), metrics.get_meter(service_name)
|
||||
@@ -0,0 +1,264 @@
|
||||
#!/usr/bin/env python3
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
import redis
|
||||
|
||||
from openbao import load_service_secrets
|
||||
|
||||
load_service_secrets()
|
||||
|
||||
from db import Store
|
||||
from s3store import ObjectStore, S3Unavailable
|
||||
from telemetry import configure
|
||||
from opentelemetry import context, trace
|
||||
|
||||
LOG = logging.getLogger("attachment-worker")
|
||||
STREAM = os.environ.get("BROKER_STREAM", "attachment.jobs")
|
||||
GROUP = os.environ.get("BROKER_GROUP", "attachment-workers")
|
||||
MAX_ATTEMPTS = int(os.environ.get("JOB_MAX_ATTEMPTS", "5"))
|
||||
LEASE_SECONDS = int(os.environ.get("JOB_TIMEOUT_SECONDS", "60"))
|
||||
PREFETCH = max(1, min(10, int(os.environ.get("BROKER_PREFETCH", "1"))))
|
||||
FAULT = os.environ.get("FAULT_INJECTION", "").strip()
|
||||
tracer = None
|
||||
jobs_processed = None
|
||||
jobs_failed = None
|
||||
job_duration = None
|
||||
|
||||
|
||||
def finish_job(span, token, outcome, started, job_id=None):
|
||||
if span is not None:
|
||||
span.set_attribute("outcome", outcome)
|
||||
if outcome == "failed":
|
||||
span.set_status(trace.Status(trace.StatusCode.ERROR))
|
||||
if jobs_processed is not None:
|
||||
jobs_processed.add(1, {"job_type": "attachment.process", "outcome": outcome})
|
||||
if job_duration is not None:
|
||||
job_duration.record(time.perf_counter() - started, {
|
||||
"job_type": "attachment.process",
|
||||
"outcome": outcome,
|
||||
})
|
||||
LOG.info("job processed job_id=%s outcome=%s", job_id or "unknown", outcome)
|
||||
span.end()
|
||||
if token is not None:
|
||||
context.detach(token)
|
||||
|
||||
|
||||
def inject_crash(point):
|
||||
if FAULT == point:
|
||||
LOG.error("fault injection crash point=%s", point)
|
||||
os._exit(42)
|
||||
|
||||
|
||||
def now():
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def iso(value):
|
||||
return value.isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
def broker_url():
|
||||
path = os.environ.get("BROKER_URL_FILE")
|
||||
if path and os.path.isfile(path):
|
||||
return open(path, encoding="utf-8").read().strip()
|
||||
return os.environ.get("BROKER_URL", os.environ.get("CACHE_URL", "redis://redis:6379/0"))
|
||||
|
||||
|
||||
def image_bytes(image, format_name, **options):
|
||||
output = io.BytesIO()
|
||||
image.save(output, format=format_name, **options)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def prepare_variants(raw, attachment_id):
|
||||
with Image.open(io.BytesIO(raw)) as source:
|
||||
image = ImageOps.exif_transpose(source)
|
||||
if image.mode in ("RGBA", "LA") or "transparency" in image.info:
|
||||
background = Image.new("RGB", image.size, "white")
|
||||
alpha = image.convert("RGBA")
|
||||
background.paste(alpha, mask=alpha.getchannel("A"))
|
||||
image = background
|
||||
else:
|
||||
image = image.convert("RGB")
|
||||
|
||||
thumbnail = image.copy()
|
||||
thumbnail.thumbnail((256, 256), Image.Resampling.LANCZOS)
|
||||
crop_size = min(image.size)
|
||||
left = (image.width - crop_size) // 2
|
||||
top = (image.height - crop_size) // 2
|
||||
crop = image.crop((left, top, left + crop_size, top + crop_size))
|
||||
crop = crop.resize((128, 128), Image.Resampling.LANCZOS)
|
||||
optimized = image.copy()
|
||||
optimized.thumbnail((1280, 1280), Image.Resampling.LANCZOS)
|
||||
|
||||
values = [
|
||||
("thumbnail", thumbnail, "JPEG", "image/jpeg", {"quality": 88, "optimize": True}),
|
||||
("crop", crop, "JPEG", "image/jpeg", {"quality": 88, "optimize": True}),
|
||||
("optimized", optimized, "WEBP", "image/webp", {"quality": 86, "method": 4}),
|
||||
]
|
||||
result = []
|
||||
for name, rendered, fmt, content_type, options in values:
|
||||
data = image_bytes(rendered, fmt, **options)
|
||||
result.append({
|
||||
"name": name,
|
||||
"content_type": content_type,
|
||||
"size_bytes": len(data),
|
||||
"width": rendered.width,
|
||||
"height": rendered.height,
|
||||
"sha256": __import__("hashlib").sha256(data).hexdigest(),
|
||||
"_key": "objects/" + attachment_id + "/" + name,
|
||||
"_data": data,
|
||||
})
|
||||
return result
|
||||
|
||||
|
||||
def public_variants(values):
|
||||
return [{key: value for key, value in item.items() if not key.startswith("_")} for item in values]
|
||||
|
||||
|
||||
class Worker:
|
||||
def __init__(self):
|
||||
self.store = Store.connect(run_migrations=False)
|
||||
self.objects = ObjectStore.from_env()
|
||||
self.redis = redis.from_url(broker_url(), decode_responses=True, socket_connect_timeout=2)
|
||||
self.consumer = os.environ.get("WORKER_ID", "worker-" + uuid.uuid4().hex[:8])
|
||||
try:
|
||||
self.redis.xgroup_create(STREAM, GROUP, id="0", mkstream=True)
|
||||
except redis.ResponseError as error:
|
||||
if "BUSYGROUP" not in str(error):
|
||||
raise
|
||||
|
||||
def publish_outbox(self):
|
||||
for row in self.store.pending_outbox():
|
||||
fields = {
|
||||
"schema_version": 1,
|
||||
"job_id": str(row["job_id"]),
|
||||
"type": "attachment.process.v1",
|
||||
"attachment_id": str(row["attachment_id"]),
|
||||
"pipeline_version": row["pipeline_version"],
|
||||
"attempt": 1,
|
||||
"created_at": iso(now()),
|
||||
}
|
||||
message_id = self.redis.xadd(STREAM, fields, maxlen=10000, approximate=True)
|
||||
self.store.mark_outbox_published(row["id"], iso(now()))
|
||||
LOG.info("published job=%s message=%s", fields["job_id"], message_id)
|
||||
|
||||
def process(self, message_id, fields):
|
||||
job_id = fields.get("job_id")
|
||||
attachment_id = fields.get("attachment_id")
|
||||
span = None
|
||||
token = None
|
||||
started = time.perf_counter()
|
||||
if tracer is not None:
|
||||
span = tracer.start_span("attachment process")
|
||||
span.set_attribute("messaging.operation", "process")
|
||||
span.set_attribute("messaging.destination.name", STREAM)
|
||||
token = context.attach(trace.set_span_in_context(span))
|
||||
if not job_id or not attachment_id:
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
return
|
||||
claim = self.store.claim_job(
|
||||
job_id, iso(now() + timedelta(seconds=LEASE_SECONDS)), iso(now())
|
||||
)
|
||||
if claim is None:
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
return
|
||||
inject_crash("before-side-effect")
|
||||
attachment = self.store.get_attachment_for_job(attachment_id)
|
||||
if not attachment:
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
return
|
||||
if int(fields.get("pipeline_version", 1)) < int(attachment.get("pipeline_version", 1)):
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
return
|
||||
try:
|
||||
staged = self.objects.get_object(attachment["staging_key"])
|
||||
if staged is None:
|
||||
raise ValueError("staging_missing")
|
||||
raw = staged["Body"].read()
|
||||
staged["Body"].close()
|
||||
self.objects.put_file(
|
||||
attachment["object_key"], io.BytesIO(raw), len(raw), attachment["content_type"]
|
||||
)
|
||||
inject_crash("after-original")
|
||||
rendered = prepare_variants(raw, attachment_id)
|
||||
for item in rendered:
|
||||
self.objects.put_file(
|
||||
item["_key"], io.BytesIO(item["_data"]), item["size_bytes"], item["content_type"]
|
||||
)
|
||||
inject_crash("after-side-effect")
|
||||
self.store.set_attachment_variants(attachment_id, public_variants(rendered), iso(now()))
|
||||
try:
|
||||
self.objects.delete_object(attachment["staging_key"])
|
||||
except S3Unavailable:
|
||||
LOG.warning("staging cleanup deferred attachment=%s", attachment_id)
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "success", started, job_id)
|
||||
except (S3Unavailable, OSError, ValueError, RuntimeError) as error:
|
||||
attempt = claim["attempts"]
|
||||
code = "processing_failed" if not isinstance(error, ValueError) else str(error)
|
||||
if attempt >= MAX_ATTEMPTS:
|
||||
self.store.fail_job(job_id, attachment_id, "retry_exhausted", iso(now()))
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
else:
|
||||
delay = min(300, 2 ** (attempt - 1)) + (hash(job_id) % 1000) / 1000
|
||||
self.store.retry_job(
|
||||
job_id, attachment_id, iso(now() + timedelta(seconds=delay)), code, iso(now())
|
||||
)
|
||||
self.redis.xack(STREAM, GROUP, message_id)
|
||||
time.sleep(delay)
|
||||
self.redis.xadd(STREAM, fields, maxlen=10000, approximate=True)
|
||||
finish_job(span, token, "failed", started, job_id)
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
self.publish_outbox()
|
||||
pending = self.redis.xautoclaim(
|
||||
STREAM, GROUP, self.consumer, min_idle_time=LEASE_SECONDS * 1000,
|
||||
start_id="0-0", count=PREFETCH
|
||||
)
|
||||
for message_id, fields in pending[1]:
|
||||
self.process(message_id, fields)
|
||||
result = self.redis.xreadgroup(
|
||||
GROUP, self.consumer, {STREAM: ">"}, count=PREFETCH, block=1000
|
||||
)
|
||||
for _, messages in result:
|
||||
for message_id, fields in messages:
|
||||
self.process(message_id, fields)
|
||||
|
||||
|
||||
def main():
|
||||
global tracer, jobs_processed, job_duration
|
||||
tracer, meter = configure("messenger-worker")
|
||||
jobs_processed = meter.create_counter(
|
||||
"messenger_jobs_processed_total",
|
||||
description="Attachment jobs processed",
|
||||
unit="{job}",
|
||||
)
|
||||
job_duration = meter.create_histogram(
|
||||
"messenger_job_processing_duration_seconds",
|
||||
description="Attachment job processing duration",
|
||||
unit="s",
|
||||
)
|
||||
worker = Worker()
|
||||
try:
|
||||
worker.run()
|
||||
finally:
|
||||
worker.store.close()
|
||||
worker.objects.client.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user