ЛР 7: Безопасность, mTLS и OpenBao
Skeleton contracts / contracts (pull_request) Blocked by required conditions

This commit is contained in:
2026-12-05 18:23:13 +03:00
parent 20afc2c790
commit e7d7bbbe77
66 changed files with 4865 additions and 81 deletions
+34
View File
@@ -0,0 +1,34 @@
# PostgreSQL schema and transactions
## Tables and invariants
- `users`: UUID primary key, unique constrained username, display name, Argon2 password hash, server timestamp.
- `chats`: UUID primary key and title.
- `chat_members`: composite `(chat_id, user_id)` primary key, foreign keys to both parent tables, unique stable member position per chat. The `(user_id, chat_id)` index supports listing a user's chats.
- `messages`: UUID primary key and foreign keys to chat and sender. `(chat_id, created_at, id)` supports the API's ascending keyset history query.
- `sessions`: SHA-256 digest of a random opaque token as primary key, user foreign key, creation and expiry timestamps. Raw tokens are returned only once and are never stored.
- `idempotency_records`: unique per user, method, request path, and key; stores the request hash and committed response for 24 hours. Expired records are removed when a message is submitted.
- `attachments`: PostgreSQL metadata for a chat-scoped image; `object_key` points to the object in the private MinIO bucket and is never exposed by the API.
- `message_attachments`: ordered message-to-attachment links with composite foreign keys that prevent linking a file from another chat.
- `image_jobs`: durable job/outbox state keyed by a stable `job_id`, including pipeline version, managed attempt, retry limit, publication confirmation, and terminal state.
- `schema_migrations`: migration filename and application timestamp; created by the migration runner.
All timestamps are `TIMESTAMPTZ`. PostgreSQL owns generated UUIDs and creation timestamps. Named migrations run once in lexicographic order; an advisory transaction lock serializes startup migration attempts.
## Transaction boundaries
Each HTTP request uses one pooled PostgreSQL connection and one transaction. A successful response is written only after that transaction commits. Creating a chat and inserting every member is one transaction, so a failed member insert rolls back both the chat and any earlier member rows. Registration relies on the unique username constraint as the final arbiter of concurrent registrations; the losing request returns `409`.
Creating a message, linking its attachments, and recording its idempotency result share one transaction. PostgreSQL's composite primary key serializes concurrent requests across API replicas. Matching bodies replay the original status and message; a different body returns `409`. Authentication, chat membership, and attachment scope are checked before replay. Upload writes bytes to S3 first, then inserts metadata in PostgreSQL; a failed DB transaction triggers a best-effort object delete compensation.
The original file is authoritative in MinIO; PostgreSQL stores its chat/owner ACL, final object key, durable staging key, checksum, type, size, state, and generated variant metadata. Upload first writes the validated original under `staging/<attachment-id>/original`; only after that succeeds does PostgreSQL commit the attachment and job. After commit, the API publishes the job to RabbitMQ and waits for publisher confirmation before returning `202`. An API outbox loop republishes queued attempts not yet confirmed by the broker.
The worker reads trusted object keys from PostgreSQL, writes deterministic final keys, and only marks the attachment ready after both original and thumbnail objects are stored and metadata commits. It acknowledges the broker message after that commit. The job row and RabbitMQ message are both durable; duplicate deliveries are serialized by a PostgreSQL advisory lock and use deterministic S3 keys. Staging is removed only after the ready transaction commits. The API and worker currently use the local MinIO root credentials, acceptable only for this grade-3 isolated lab topology; use a scoped service account for a stronger deployment.
Migration `012_attachment_jobs.sql` expands the attachment status constraint to `queued`, `processing`, `ready`, and `failed`, adds the staging key and creates the job/outbox table. Existing Lab4-ready attachments remain valid; their staging key and job row may remain null/absent.
Message history uses `WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4`, matching `messages_chat_history_idx`. Cursors include their resource scope and the last row's timestamp and UUID.
## Durability
Compose mounts PostgreSQL, MinIO, and RabbitMQ data directories on named `postgres_data`, `minio_data`, and `rabbitmq_data` volumes. The persistence hook restarts the API, worker, database, MinIO, and broker without removing those volumes. Backups and destructive volume resets are deliberately separate operator actions.