Выполнить лабораторную 7 по безопасности мессенджера
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name varchar(100) NOT NULL CHECK (length(btrim(display_name)) > 0),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_by_user ON chat_members(user_id, chat_id);
|
||||
|
||||
CREATE TABLE 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 RESTRICT,
|
||||
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX messages_history_keyset ON messages(chat_id, created_at, id);
|
||||
|
||||
-- PostgreSQL is authoritative for session durability and revocation. Valkey only
|
||||
-- keeps a short-lived lookup hint; a cache hit is still checked against this row.
|
||||
CREATE TABLE sessions (
|
||||
token_hash char(64) PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at timestamptz NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX sessions_expiry ON sessions(expires_at);
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method varchar(8) NOT NULL CHECK (method = 'POST'),
|
||||
resource_path text NOT NULL,
|
||||
key varchar(128) NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
request_hash char(64) NOT NULL,
|
||||
response_body jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
PRIMARY KEY (user_id, method, resource_path, key)
|
||||
);
|
||||
CREATE INDEX idempotency_expiry ON idempotency_keys(created_at);
|
||||
@@ -0,0 +1,30 @@
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_length_check CHECK (length(text) <= 4000);
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
|
||||
|
||||
CREATE TABLE 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 RESTRICT,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
filename varchar(255) NOT NULL CHECK (length(filename) BETWEEN 1 AND 255),
|
||||
content_type varchar(20) 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 CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
||||
status varchar(16) NOT NULL CHECK (status IN ('ready','failed')),
|
||||
error_code varchar(64),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
UNIQUE (id, chat_id)
|
||||
);
|
||||
CREATE INDEX attachments_chat_created ON attachments(chat_id, created_at);
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL,
|
||||
attachment_id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE RESTRICT
|
||||
);
|
||||
CREATE INDEX message_attachments_by_attachment ON message_attachments(attachment_id);
|
||||
@@ -0,0 +1,38 @@
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
|
||||
ALTER TABLE attachments ALTER COLUMN object_key DROP NOT NULL;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key text;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS job_id uuid;
|
||||
UPDATE attachments SET job_id=gen_random_uuid() WHERE job_id IS NULL;
|
||||
ALTER TABLE attachments ALTER COLUMN job_id SET NOT NULL;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0);
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS attempts integer NOT NULL DEFAULT 0 CHECK (attempts >= 0);
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS lease_until timestamptz;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS next_attempt_at timestamptz;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variants jsonb NOT NULL DEFAULT '[]'::jsonb;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed'));
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachment_stage_for_pending CHECK (status IN ('ready','failed') OR staging_key IS NOT NULL);
|
||||
CREATE INDEX attachments_recovery ON attachments(status, lease_until, next_attempt_at);
|
||||
CREATE INDEX attachments_job_id ON attachments(job_id);
|
||||
|
||||
CREATE TABLE job_outbox (
|
||||
job_id uuid NOT NULL,
|
||||
attempt integer NOT NULL CHECK (attempt >= 1),
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
available_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
published_at timestamptz,
|
||||
PRIMARY KEY(job_id, attempt)
|
||||
);
|
||||
CREATE INDEX job_outbox_pending ON job_outbox(available_at, created_at) WHERE published_at IS NULL;
|
||||
|
||||
CREATE TABLE pipeline_audit (
|
||||
id bigserial PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL,
|
||||
job_id uuid NOT NULL,
|
||||
pipeline_version integer NOT NULL,
|
||||
event varchar(40) NOT NULL,
|
||||
detail jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX pipeline_audit_attachment ON pipeline_audit(attachment_id, created_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
ALTER TABLE job_outbox ADD COLUMN traceparent text;
|
||||
ALTER TABLE job_outbox ADD COLUMN tracestate text;
|
||||
CREATE INDEX attachments_pending_created ON attachments(created_at)
|
||||
WHERE status IN ('queued', 'processing');
|
||||
|
||||
-- Only uploads accepted under this migration belong to the ready SLO.
|
||||
ALTER TABLE attachments ADD COLUMN slo_accepted_at timestamptz;
|
||||
ALTER TABLE attachments ADD COLUMN slo_first_ready_at timestamptz;
|
||||
CREATE INDEX attachments_slo_acceptance ON attachments(slo_accepted_at)
|
||||
INCLUDE (slo_first_ready_at) WHERE slo_accepted_at IS NOT NULL;
|
||||
@@ -0,0 +1,23 @@
|
||||
CREATE TABLE login_limits (
|
||||
scope text NOT NULL,
|
||||
bucket bigint NOT NULL,
|
||||
attempts integer NOT NULL,
|
||||
PRIMARY KEY (scope, bucket)
|
||||
);
|
||||
|
||||
REVOKE ALL ON SCHEMA public FROM PUBLIC;
|
||||
REVOKE TEMPORARY ON DATABASE messenger FROM PUBLIC;
|
||||
GRANT USAGE ON SCHEMA public TO messenger_api, messenger_worker;
|
||||
GRANT SELECT, INSERT ON users, chats, chat_members, messages, message_attachments, idempotency_keys TO messenger_api;
|
||||
GRANT UPDATE (password_hash) ON users TO messenger_api;
|
||||
GRANT SELECT, INSERT, DELETE ON sessions, login_limits TO messenger_api;
|
||||
GRANT UPDATE (attempts) ON login_limits TO messenger_api;
|
||||
GRANT SELECT, INSERT ON attachments, job_outbox, pipeline_audit TO messenger_api;
|
||||
GRANT UPDATE (status, error_code, lease_until, updated_at, next_attempt_at) ON attachments TO messenger_api;
|
||||
GRANT UPDATE (published_at) ON job_outbox TO messenger_api;
|
||||
GRANT SELECT, INSERT ON job_outbox, pipeline_audit TO messenger_worker;
|
||||
GRANT SELECT ON attachments TO messenger_worker;
|
||||
GRANT UPDATE (
|
||||
status, attempts, lease_until, next_attempt_at, error_code, updated_at,
|
||||
object_key, variants, slo_first_ready_at
|
||||
) ON attachments TO messenger_worker;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- The API replaces expired idempotency records under a transaction-scoped lock.
|
||||
GRANT DELETE ON idempotency_keys TO messenger_api;
|
||||
-- The outbox path and worker append records using the audit table's sequence.
|
||||
GRANT USAGE ON SEQUENCE pipeline_audit_id_seq TO messenger_api, messenger_worker;
|
||||
Reference in New Issue
Block a user