Реализовать защиту Lab7 и проверку отзыва worker
Skeleton contracts / contracts (pull_request) Canceled after 0s
Lab7 HTTPS integration / compose (pull_request) Canceled after 0s

This commit is contained in:
R0flyan
2026-11-27 21:53:26 +03:00
parent 20afc2c790
commit 2fa513f56c
68 changed files with 6767 additions and 64 deletions
+28
View File
@@ -0,0 +1,28 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats(id),
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx ON chat_members(user_id, chat_id);
CREATE INDEX chats_page_idx ON chats(created_at, id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
sender_id uuid NOT NULL,
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
+12
View File
@@ -0,0 +1,12 @@
-- PostgreSQL text cannot contain U+0000, although it is a valid JSON string
-- code point. Keep exact UTF-8 bytes for free-form API strings. The API checks
-- Unicode length/whitespace; these checks also bound stored byte lengths.
ALTER TABLE users DROP CONSTRAINT users_display_name_check;
ALTER TABLE users ALTER COLUMN display_name TYPE bytea USING convert_to(display_name,'UTF8');
ALTER TABLE users ADD CHECK (octet_length(display_name) BETWEEN 1 AND 400);
ALTER TABLE chats DROP CONSTRAINT chats_title_check;
ALTER TABLE chats ALTER COLUMN title TYPE bytea USING convert_to(title,'UTF8');
ALTER TABLE chats ADD CHECK (octet_length(title) BETWEEN 1 AND 400);
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ALTER COLUMN text TYPE bytea USING convert_to(text,'UTF8');
ALTER TABLE messages ADD CHECK (octet_length(text) BETWEEN 1 AND 16000);
+8
View File
@@ -0,0 +1,8 @@
CREATE TABLE message_keys (
user_id uuid NOT NULL REFERENCES users(id),
chat_id uuid NOT NULL REFERENCES chats(id),
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
message_id uuid NOT NULL REFERENCES messages(id),
expires_at timestamptz NOT NULL,
PRIMARY KEY (user_id, chat_id, key)
);
+27
View File
@@ -0,0 +1,27 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL,
object_key text NOT NULL UNIQUE CHECK (object_key = 'original/' || id::text),
filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255),
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 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
missing_since timestamptz,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (chat_id,id),
FOREIGN KEY (chat_id,owner_id) REFERENCES chat_members(chat_id,user_id)
);
CREATE INDEX attachments_page_idx ON attachments(id);
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD CHECK (octet_length(text) BETWEEN 0 AND 16000);
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE messages ADD CHECK (cardinality(attachment_ids) <= 10);
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages(id),
chat_id uuid NOT NULL,
attachment_id uuid NOT NULL,
PRIMARY KEY (message_id,attachment_id),
FOREIGN KEY (chat_id,attachment_id) REFERENCES attachments(chat_id,id)
);
+37
View File
@@ -0,0 +1,37 @@
ALTER TABLE attachments
ADD COLUMN status text NOT NULL DEFAULT 'ready' CHECK (status IN ('queued','processing','ready','failed')),
ADD COLUMN error_code text,
ADD COLUMN active_version integer NOT NULL DEFAULT 0,
ADD COLUMN result_version integer NOT NULL DEFAULT 0,
ADD COLUMN variants jsonb NOT NULL DEFAULT '[]',
ADD CHECK ((status='failed') = (error_code IS NOT NULL)),
ADD CHECK (active_version >= result_version AND result_version >= 0);
-- A queued row is also the transactional outbox entry. Keep completed rows as
-- the deduplication ledger and failed rows as the operator's dead-letter registry.
CREATE TABLE image_jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id),
version integer NOT NULL CHECK (version IN (1,2)),
attempt integer NOT NULL DEFAULT 1,
cycle_start integer NOT NULL DEFAULT 1 CHECK (cycle_start BETWEEN 1 AND attempt),
state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed')),
published boolean NOT NULL DEFAULT false,
next_at timestamptz NOT NULL DEFAULT clock_timestamp(),
lease_until timestamptz,
error_code text,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (attachment_id,version),
CHECK ((state='processing') = (lease_until IS NOT NULL))
);
CREATE INDEX image_jobs_outbox ON image_jobs(next_at,id) WHERE state='queued' AND NOT published;
CREATE INDEX image_jobs_lease ON image_jobs(lease_until,id) WHERE state='processing';
CREATE TABLE image_job_replays (
job_id uuid NOT NULL REFERENCES image_jobs(id),
attempt integer NOT NULL,
previous_error text NOT NULL,
replayed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (job_id,attempt)
);
+7
View File
@@ -0,0 +1,7 @@
ALTER TABLE image_jobs
ADD COLUMN traceparent text NOT NULL DEFAULT '',
ADD COLUMN tracestate text NOT NULL DEFAULT '',
ADD COLUMN ready_at timestamptz;
-- Use the historical ready timestamp for lab5 rows that already completed.
UPDATE image_jobs SET ready_at=updated_at WHERE state='ready';
+24
View File
@@ -0,0 +1,24 @@
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
REVOKE CONNECT, TEMPORARY ON DATABASE messenger FROM PUBLIC;
GRANT CONNECT ON DATABASE messenger TO lab_api,lab_dispatcher,lab_worker,lab_jobs;
GRANT USAGE ON SCHEMA public TO lab_api,lab_dispatcher,lab_worker,lab_jobs;
GRANT SELECT(version) ON schema_migrations TO lab_api,lab_dispatcher,lab_worker,lab_jobs;
GRANT SELECT, INSERT ON users,chats,chat_members,messages,attachments,message_attachments TO lab_api;
GRANT UPDATE(password_hash) ON users TO lab_api;
GRANT SELECT, INSERT, DELETE ON message_keys TO lab_api;
GRANT INSERT(id,attachment_id,version,traceparent,tracestate) ON image_jobs TO lab_api;
GRANT SELECT(attachment_id,version) ON image_jobs TO lab_api;
GRANT SELECT ON image_jobs TO lab_dispatcher,lab_worker,lab_jobs;
GRANT SELECT(id,result_version,status,updated_at) ON attachments TO lab_dispatcher;
GRANT UPDATE(published) ON image_jobs TO lab_dispatcher;
GRANT SELECT(id,active_version,result_version,content_type,size_bytes,sha256) ON attachments TO lab_worker;
GRANT UPDATE(status,updated_at,error_code,result_version,variants) ON attachments TO lab_worker;
GRANT UPDATE(state,lease_until,updated_at,error_code,ready_at,attempt,published,next_at) ON image_jobs TO lab_worker;
GRANT SELECT(id,active_version,result_version,status,updated_at) ON attachments TO lab_jobs;
GRANT UPDATE(active_version,status,error_code,updated_at) ON attachments TO lab_jobs;
GRANT INSERT(id,attachment_id,version,traceparent,tracestate) ON image_jobs TO lab_jobs;
GRANT UPDATE(state,attempt,cycle_start,error_code,published,next_at,updated_at) ON image_jobs TO lab_jobs;
GRANT INSERT ON image_job_replays TO lab_jobs;
+4
View File
@@ -0,0 +1,4 @@
-- An expired worker lease is recovered by the dispatcher before publishing.
GRANT SELECT(active_version) ON attachments TO lab_dispatcher;
GRANT UPDATE(status,error_code,updated_at) ON attachments TO lab_dispatcher;
GRANT UPDATE(state,lease_until,error_code,attempt,next_at,updated_at) ON image_jobs TO lab_dispatcher;