Защита стенда и управление секретами через OpenBao

This commit is contained in:
EgorAnti
2026-11-25 19:36:12 +03:00
parent 20afc2c790
commit d23847f953
159 changed files with 9135 additions and 82 deletions
@@ -0,0 +1,60 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username varchar(32) NOT NULL,
display_name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT users_username_uk UNIQUE (username),
CONSTRAINT users_username_format_chk
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_length_chk
CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT chats_title_length_chk
CHECK (char_length(title) BETWEEN 1 AND 100)
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL,
user_id uuid NOT NULL,
joined_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT chat_members_pk PRIMARY KEY (chat_id, user_id),
CONSTRAINT chat_members_chat_fk
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE,
CONSTRAINT chat_members_user_fk
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE RESTRICT
);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL,
sender_id uuid NOT NULL,
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT messages_text_length_chk
CHECK (char_length(text) BETWEEN 1 AND 4000),
CONSTRAINT messages_sender_membership_fk
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members(chat_id, user_id)
ON DELETE RESTRICT
);
CREATE INDEX chat_members_user_chat_idx
ON chat_members (user_id, chat_id);
CREATE INDEX chats_created_id_idx
ON chats (created_at, id);
CREATE INDEX messages_chat_created_id_idx
ON messages (chat_id, created_at, id);
CREATE INDEX messages_sender_created_idx
ON messages (sender_id, created_at);
@@ -0,0 +1,28 @@
ALTER TABLE users
ADD COLUMN password_hash text;
ALTER TABLE users
ALTER COLUMN password_hash SET NOT NULL;
CREATE TABLE sessions (
id uuid PRIMARY KEY,
user_id uuid NOT NULL,
token_hash bytea NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at timestamptz NOT NULL,
revoked_at timestamptz,
CONSTRAINT sessions_user_fk
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
CONSTRAINT sessions_token_hash_uk UNIQUE (token_hash),
CONSTRAINT sessions_token_hash_length_chk
CHECK (octet_length(token_hash) = 32),
CONSTRAINT sessions_expiry_chk
CHECK (expires_at > created_at),
CONSTRAINT sessions_revocation_chk
CHECK (revoked_at IS NULL OR revoked_at >= created_at)
);
CREATE INDEX sessions_active_expiry_idx
ON sessions (expires_at)
WHERE revoked_at IS NULL;
@@ -0,0 +1,25 @@
CREATE TABLE message_idempotency (
user_id uuid NOT NULL,
method varchar(8) NOT NULL,
request_path text NOT NULL,
idempotency_key varchar(128) NOT NULL,
request_hash bytea NOT NULL,
message_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT message_idempotency_pk
PRIMARY KEY (user_id, method, request_path, idempotency_key),
CONSTRAINT message_idempotency_user_fk
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
CONSTRAINT message_idempotency_message_fk
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE,
CONSTRAINT message_idempotency_method_chk
CHECK (method = 'POST'),
CONSTRAINT message_idempotency_key_format_chk
CHECK (idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
CONSTRAINT message_idempotency_hash_length_chk
CHECK (octet_length(request_hash) = 32)
);
CREATE INDEX message_idempotency_created_idx
ON message_idempotency (created_at);
@@ -0,0 +1,53 @@
ALTER TABLE messages DROP CONSTRAINT messages_text_length_chk;
ALTER TABLE messages ADD CONSTRAINT messages_text_length_chk
CHECK (char_length(text) BETWEEN 0 AND 4000);
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_uk UNIQUE (id, chat_id);
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL,
owner_id uuid NOT NULL,
filename varchar(255) NOT NULL,
content_type varchar(32) NOT NULL,
size_bytes bigint NOT NULL,
sha256 char(64) NOT NULL,
object_key text NOT NULL,
status varchar(16) NOT NULL DEFAULT 'ready',
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT attachments_id_chat_uk UNIQUE (id, chat_id),
CONSTRAINT attachments_object_key_uk UNIQUE (object_key),
CONSTRAINT attachments_owner_membership_fk
FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id) ON DELETE RESTRICT,
CONSTRAINT attachments_content_type_chk
CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size_chk
CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_chk
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_status_chk CHECK (status = 'ready'),
CONSTRAINT attachments_filename_chk
CHECK (char_length(filename) BETWEEN 1 AND 255)
);
CREATE TABLE message_attachments (
message_id uuid NOT NULL,
chat_id uuid NOT NULL,
attachment_id uuid NOT NULL,
position smallint NOT NULL,
CONSTRAINT message_attachments_pk PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position_uk UNIQUE (message_id, position),
CONSTRAINT message_attachments_message_fk
FOREIGN KEY (message_id, chat_id)
REFERENCES messages(id, chat_id) ON DELETE CASCADE,
CONSTRAINT message_attachments_attachment_fk
FOREIGN KEY (attachment_id, chat_id)
REFERENCES attachments(id, chat_id) ON DELETE RESTRICT,
CONSTRAINT message_attachments_position_chk CHECK (position BETWEEN 0 AND 9)
);
CREATE INDEX attachments_chat_created_idx ON attachments (chat_id, created_at, id);
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id);
@@ -0,0 +1,57 @@
ALTER TABLE attachments DROP CONSTRAINT attachments_status_chk;
ALTER TABLE attachments
ADD COLUMN staging_key text,
ADD COLUMN error_code varchar(64),
ADD CONSTRAINT attachments_status_chk
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
ADD CONSTRAINT attachments_error_state_chk
CHECK ((status = 'failed' AND error_code IS NOT NULL)
OR (status <> 'failed' AND error_code IS NULL));
CREATE UNIQUE INDEX attachments_staging_key_uk
ON attachments (staging_key) WHERE staging_key IS NOT NULL;
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
name varchar(32) NOT NULL,
content_type varchar(32) NOT NULL,
size_bytes bigint NOT NULL,
width integer NOT NULL,
height integer NOT NULL,
sha256 char(64) NOT NULL,
object_key text NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT attachment_variants_pk
PRIMARY KEY (attachment_id, pipeline_version, name),
CONSTRAINT attachment_variants_object_key_uk UNIQUE (object_key),
CONSTRAINT attachment_variants_name_chk
CHECK (name IN ('thumbnail', 'crop', 'optimized')),
CONSTRAINT attachment_variants_content_type_chk
CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0),
CONSTRAINT attachment_variants_dimensions_chk CHECK (width > 0 AND height > 0),
CONSTRAINT attachment_variants_sha256_chk CHECK (sha256 ~ '^[a-f0-9]{64}$')
);
CREATE TABLE attachment_jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL DEFAULT 1,
attempt integer NOT NULL DEFAULT 1,
status varchar(16) NOT NULL DEFAULT 'queued',
published boolean NOT NULL DEFAULT false,
last_error_code varchar(64),
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT attachment_jobs_pipeline_chk CHECK (pipeline_version > 0),
CONSTRAINT attachment_jobs_attempt_chk CHECK (attempt > 0),
CONSTRAINT attachment_jobs_status_chk
CHECK (status IN ('queued', 'processing', 'ready', 'failed'))
);
CREATE INDEX attachment_jobs_publish_idx
ON attachment_jobs (published, status, created_at)
WHERE published = false AND status = 'queued';
@@ -0,0 +1,41 @@
ALTER TABLE attachment_jobs
ADD COLUMN next_attempt_at timestamptz,
ADD COLUMN lease_token uuid,
ADD COLUMN lease_until timestamptz,
ADD COLUMN publish_fault_injected boolean NOT NULL DEFAULT false,
ADD COLUMN result_fault_injected boolean NOT NULL DEFAULT false;
UPDATE attachment_jobs SET next_attempt_at = created_at WHERE next_attempt_at IS NULL;
UPDATE attachment_jobs
SET status = 'queued', published = false, last_error_code = 'lease_migration_recovery'
WHERE status = 'processing';
UPDATE attachments SET status = 'queued', error_code = NULL WHERE status = 'processing';
ALTER TABLE attachment_jobs ALTER COLUMN next_attempt_at SET NOT NULL;
ALTER TABLE attachment_jobs ALTER COLUMN next_attempt_at SET DEFAULT clock_timestamp();
ALTER TABLE attachment_jobs
ADD CONSTRAINT attachment_jobs_lease_chk CHECK (
(status = 'processing' AND lease_token IS NOT NULL AND lease_until IS NOT NULL)
OR (status <> 'processing' AND lease_token IS NULL AND lease_until IS NULL)
);
DROP INDEX attachment_jobs_publish_idx;
CREATE INDEX attachment_jobs_publish_idx
ON attachment_jobs (next_attempt_at, created_at, id)
WHERE published = false AND status = 'queued';
CREATE INDEX attachment_jobs_expired_lease_idx
ON attachment_jobs (lease_until)
WHERE status = 'processing';
CREATE TABLE attachment_job_replays (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs(id) ON DELETE RESTRICT,
requested_by varchar(128) NOT NULL,
previous_attempt integer NOT NULL,
requested_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT attachment_job_replays_actor_chk
CHECK (char_length(requested_by) BETWEEN 1 AND 128)
);
CREATE INDEX attachment_job_replays_job_idx
ON attachment_job_replays (job_id, requested_at);
@@ -0,0 +1,33 @@
ALTER TABLE attachments
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1,
ADD CONSTRAINT attachments_pipeline_version_chk CHECK (pipeline_version > 0);
ALTER TABLE attachment_jobs
DROP CONSTRAINT attachment_jobs_attachment_id_key,
DROP CONSTRAINT attachment_jobs_status_chk,
ADD COLUMN before_effect_fault_injected boolean NOT NULL DEFAULT false,
ADD COLUMN after_effect_fault_injected boolean NOT NULL DEFAULT false,
ADD CONSTRAINT attachment_jobs_status_chk
CHECK (status IN ('queued', 'processing', 'ready', 'failed', 'superseded'));
CREATE UNIQUE INDEX attachment_jobs_attachment_pipeline_uk
ON attachment_jobs (attachment_id, pipeline_version);
CREATE TABLE attachment_pipeline_promotions (
id bigserial PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
from_version integer NOT NULL,
to_version integer NOT NULL,
job_id uuid NOT NULL REFERENCES attachment_jobs(id) ON DELETE RESTRICT,
requested_by varchar(128) NOT NULL,
requested_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT attachment_pipeline_promotions_versions_chk
CHECK (from_version > 0 AND to_version > from_version),
CONSTRAINT attachment_pipeline_promotions_actor_chk
CHECK (char_length(requested_by) BETWEEN 1 AND 128),
CONSTRAINT attachment_pipeline_promotions_target_uk
UNIQUE (attachment_id, to_version)
);
CREATE INDEX attachment_pipeline_promotions_attachment_idx
ON attachment_pipeline_promotions (attachment_id, requested_at);
@@ -0,0 +1,12 @@
ALTER TABLE attachment_jobs
ADD COLUMN trace_parent varchar(55),
ADD COLUMN trace_state text,
ADD CONSTRAINT attachment_jobs_trace_parent_chk CHECK (
trace_parent IS NULL
OR trace_parent ~ '^[0-9a-f]{2}-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$'
);
COMMENT ON COLUMN attachment_jobs.trace_parent IS
'W3C traceparent used only to continue a durable retry trace';
COMMENT ON COLUMN attachment_jobs.trace_state IS
'W3C tracestate used only to continue a durable retry trace';
@@ -0,0 +1,9 @@
GRANT USAGE ON SCHEMA public TO messenger_api, messenger_worker;
GRANT SELECT, INSERT, UPDATE, DELETE ON users, sessions, chats, chat_members,
messages, message_idempotency, attachments, message_attachments,
attachment_variants, attachment_jobs, attachment_job_replays,
attachment_pipeline_promotions TO messenger_api;
GRANT SELECT, INSERT, UPDATE, DELETE ON attachments, attachment_variants,
attachment_jobs, attachment_job_replays, attachment_pipeline_promotions TO messenger_worker;
REVOKE ALL ON users, sessions, chats, chat_members, messages, message_idempotency
FROM messenger_worker;