ЛР7 на 4: HTTPS, mTLS, OpenBao AppRole и ротация leaf.
Skeleton contracts / contracts (pull_request) Blocked by required conditions

Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
This commit is contained in:
2026-12-02 23:08:27 +03:00
parent 20afc2c790
commit 2f2047909f
182 changed files with 15169 additions and 90 deletions
+51
View File
@@ -0,0 +1,51 @@
-- +goose Up
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL
CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '\S'),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE sessions (
token_hash text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL
);
CREATE INDEX idx_sessions_expires_at ON sessions (expires_at);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL
CONSTRAINT chats_title_not_blank CHECK (title ~ '\S'),
created_at timestamptz NOT NULL DEFAULT now()
);
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 CASCADE,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX idx_chat_members_user_id ON chat_members (user_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 text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id);
-- +goose Down
DROP TABLE messages;
DROP TABLE chat_members;
DROP TABLE chats;
DROP TABLE sessions;
DROP TABLE users;
@@ -0,0 +1,17 @@
-- +goose Up
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
method text NOT NULL,
path text NOT NULL,
key text NOT NULL,
request_hash text NOT NULL,
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, method, path, key)
);
CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at);
-- +goose Down
DROP TABLE idempotency_keys;
+38
View File
@@ -0,0 +1,38 @@
-- +goose Up
CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg');
CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed');
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,
s3_key text NOT NULL UNIQUE,
filename text NOT NULL,
content_type attachment_content_type NOT NULL,
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status attachment_status NOT NULL DEFAULT 'queued',
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_attachments_chat_id ON attachments (chat_id, created_at);
CREATE INDEX idx_attachments_owner_id ON attachments (owner_id);
CREATE INDEX idx_attachments_queued_created_at ON attachments (created_at) WHERE status = 'queued';
CREATE TABLE 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 AND position < 10),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position)
);
CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attachment_id);
-- +goose Down
DROP TABLE message_attachments;
DROP TABLE attachments;
DROP TYPE attachment_status;
DROP TYPE attachment_content_type;
@@ -0,0 +1,67 @@
-- +goose Up
CREATE TYPE attachment_variant_name AS ENUM ('thumbnail', 'crop', 'optimized');
ALTER TABLE attachments
ADD COLUMN staging_key text UNIQUE,
ADD COLUMN job_id uuid UNIQUE,
ADD COLUMN pipeline_version int NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
ADD COLUMN attempts int NOT NULL DEFAULT 0 CHECK (attempts >= 0),
ADD COLUMN retry_base int NOT NULL DEFAULT 0 CHECK (retry_base >= 0 AND retry_base <= attempts),
ADD COLUMN width int CHECK (width > 0),
ADD COLUMN height int CHECK (height > 0),
ADD COLUMN lease_until timestamptz;
CREATE INDEX idx_attachments_expired_lease ON attachments (lease_until) WHERE status = 'processing';
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
variant attachment_variant_name NOT NULL,
s3_key text NOT NULL UNIQUE,
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg', 'image/webp')),
size_bytes int NOT NULL CHECK (size_bytes > 0),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
width int NOT NULL CHECK (width > 0),
height int NOT NULL CHECK (height > 0),
PRIMARY KEY (attachment_id, pipeline_version, variant)
);
CREATE TABLE outbox (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachments (job_id) ON DELETE CASCADE,
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
attempt int NOT NULL CHECK (attempt > 0),
created_at timestamptz NOT NULL DEFAULT now(),
available_at timestamptz NOT NULL DEFAULT now(),
published_at timestamptz,
UNIQUE (job_id, pipeline_version, attempt)
);
CREATE INDEX idx_outbox_pending ON outbox (available_at, id) WHERE published_at IS NULL;
CREATE TABLE attachment_replays (
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
attempt int NOT NULL CHECK (attempt > 0),
previous_error_code text,
operator text NOT NULL,
reason text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, attempt)
);
-- +goose Down
DROP TABLE attachment_replays;
DROP TABLE outbox;
DROP TABLE attachment_variants;
DROP TYPE attachment_variant_name;
DROP INDEX idx_attachments_expired_lease;
ALTER TABLE attachments
DROP COLUMN height,
DROP COLUMN width,
DROP COLUMN lease_until,
DROP COLUMN retry_base,
DROP COLUMN attempts,
DROP COLUMN pipeline_version,
DROP COLUMN job_id,
DROP COLUMN staging_key;
+9
View File
@@ -0,0 +1,9 @@
-- +goose Up
ALTER TABLE outbox
ADD COLUMN traceparent text NOT NULL DEFAULT '',
ADD COLUMN tracestate text NOT NULL DEFAULT '';
-- +goose Down
ALTER TABLE outbox
DROP COLUMN tracestate,
DROP COLUMN traceparent;