ЛР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;
+14
View File
@@ -0,0 +1,14 @@
-- name: CreateAttachmentReplay :exec
INSERT INTO attachment_replays (
attachment_id,
attempt,
previous_error_code,
operator,
reason
) VALUES (
sqlc.arg(attachment_id),
sqlc.arg(attempt),
sqlc.narg(previous_error_code),
sqlc.arg(operator),
sqlc.arg(reason)
);
+28
View File
@@ -0,0 +1,28 @@
-- name: CreateAttachmentVariant :exec
INSERT INTO attachment_variants (
attachment_id,
pipeline_version,
variant,
s3_key,
content_type,
size_bytes,
width,
height,
sha256
) VALUES (
sqlc.arg(attachment_id),
sqlc.arg(pipeline_version),
sqlc.arg(variant),
sqlc.arg(s3_key),
sqlc.arg(content_type),
sqlc.arg(size_bytes),
sqlc.arg(width),
sqlc.arg(height),
sqlc.arg(sha256)
);
-- name: ListAttachmentVariants :many
SELECT * FROM attachment_variants
WHERE attachment_id = sqlc.arg(attachment_id)::uuid
AND pipeline_version = sqlc.arg(pipeline_version)::int
ORDER BY variant;
+227
View File
@@ -0,0 +1,227 @@
-- name: CreateAttachment :one
INSERT INTO attachments (
id,
chat_id,
owner_id,
s3_key,
staging_key,
job_id,
filename,
content_type,
size_bytes,
sha256,
width,
height,
error_code
)
SELECT
sqlc.arg(id)::uuid,
sqlc.arg(chat_id)::uuid,
sqlc.arg(owner_id)::uuid,
sqlc.arg(s3_key)::text,
sqlc.arg(staging_key)::text,
sqlc.arg(job_id)::uuid,
sqlc.arg(filename)::text,
sqlc.arg(content_type)::attachment_content_type,
sqlc.arg(size_bytes)::int,
sqlc.arg(sha256)::text,
sqlc.arg(width)::int,
sqlc.arg(height)::int,
NULLIF(sqlc.arg(error_code)::text, '')
WHERE EXISTS (
SELECT 1 FROM chat_members
WHERE chat_id = sqlc.arg(chat_id)
AND user_id = sqlc.arg(owner_id)
)
RETURNING *;
-- name: CreateMessageAttachment :exec
INSERT INTO message_attachments (
message_id,
attachment_id,
position
) VALUES (
sqlc.arg(message_id),
sqlc.arg(attachment_id),
sqlc.arg(position)
);
-- name: GetAttachmentByID :one
SELECT * FROM attachments
WHERE id = $1::uuid;
-- name: LockAttachmentByStagingKey :one
SELECT * FROM attachments
WHERE staging_key = sqlc.arg(staging_key)::text
FOR UPDATE;
-- name: ListAttachmentsByIDs :many
SELECT status
FROM attachments
WHERE chat_id = sqlc.arg(chat_id)::uuid
AND id = ANY(sqlc.arg(ids)::uuid[]);
-- name: ListMessageAttachmentIDs :many
SELECT attachment_id
FROM message_attachments
WHERE message_id = sqlc.arg(message_id)::uuid
ORDER BY position;
-- name: ListMessageAttachmentsByMessageIDs :many
SELECT message_id, attachment_id
FROM message_attachments
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
ORDER BY message_id, position;
-- name: ListAttachmentsForReconcile :many
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
FROM attachments;
-- name: ListFailedAttachments :many
SELECT id, job_id, error_code, attempts, updated_at
FROM attachments
WHERE status = 'failed'
ORDER BY updated_at, id
LIMIT sqlc.arg(batch_size)::int;
-- name: LockAttachmentJob :one
SELECT * FROM attachments
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND job_id = sqlc.arg(job_id)::uuid
AND pipeline_version = sqlc.arg(pipeline_version)::int
AND attempts = sqlc.arg(attempt)::int
AND lease_until > now()
FOR UPDATE;
-- name: LockExpiredAttachmentJobs :many
SELECT * FROM attachments
WHERE status = 'processing'
AND lease_until <= now()
ORDER BY lease_until, id
LIMIT sqlc.arg(batch_size)::int
FOR UPDATE SKIP LOCKED;
-- name: LockFailedAttachmentForReplay :one
SELECT * FROM attachments
WHERE id = sqlc.arg(id)::uuid
AND status = 'failed'
AND staging_key IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_variants
WHERE attachment_id = attachments.id
)
FOR UPDATE;
-- name: LockAttachmentForUpgrade :one
SELECT * FROM attachments
WHERE id = sqlc.arg(id)::uuid
AND pipeline_version = 1
AND status IN ('queued', 'processing')
AND staging_key IS NOT NULL
FOR UPDATE;
-- name: ClearAttachmentStagingKey :exec
UPDATE attachments
SET staging_key = NULL
WHERE id = sqlc.arg(id)::uuid;
-- name: MarkAttachmentFailedIfReady :execrows
UPDATE attachments
SET status = 'failed',
error_code = sqlc.arg(error_code)::text,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid AND status = 'ready';
-- name: ClaimAttachmentJob :one
UPDATE attachments
SET status = 'processing',
attempts = attempts + 1,
lease_until = sqlc.arg(lease_until)::timestamptz,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND job_id = sqlc.arg(job_id)::uuid
AND pipeline_version = sqlc.arg(pipeline_version)::int
AND status = 'queued'
AND attempts + 1 = sqlc.arg(attempt)::int
RETURNING *;
-- name: CompleteAttachmentJob :execrows
UPDATE attachments
SET status = 'ready',
error_code = NULL,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND attempts = sqlc.arg(attempt)::int
AND lease_until > now();
-- name: RetryAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
error_code = sqlc.arg(error_code)::text,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND attempts = sqlc.arg(attempt)::int
AND lease_until > now();
-- name: UpgradeAttachmentPipeline :execrows
UPDATE attachments
SET pipeline_version = 2,
status = 'queued',
retry_base = attempts,
error_code = NULL,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND pipeline_version = 1
AND status IN ('queued', 'processing')
AND staging_key IS NOT NULL;
-- name: ReplayAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
retry_base = attempts,
error_code = NULL,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'failed'
AND staging_key IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_variants
WHERE attachment_id = attachments.id
);
-- name: FailAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = sqlc.arg(error_code)::text,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND attempts = sqlc.arg(attempt)::int
AND lease_until > now();
-- name: RecoverExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND lease_until <= now();
-- name: FailExpiredAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',
error_code = 'retry_exhausted',
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'processing'
AND lease_until <= now();
+73
View File
@@ -0,0 +1,73 @@
-- name: CreateChat :one
INSERT INTO chats (
id,
title
) VALUES (
sqlc.arg(id),
sqlc.arg(title)
)
RETURNING *;
-- name: AddChatMembers :exec
INSERT INTO chat_members (
chat_id,
user_id
)
SELECT
sqlc.arg(chat_id)::uuid,
unnest(sqlc.arg(user_ids)::uuid[]);
-- name: IsChatMember :one
SELECT EXISTS (
SELECT 1 FROM chat_members
WHERE chat_id = sqlc.arg(chat_id)
AND user_id = sqlc.arg(user_id)
);
-- name: GetChat :one
SELECT
c.*,
ARRAY(
SELECT cm.user_id FROM chat_members cm
WHERE cm.chat_id = c.id
ORDER BY cm.user_id
)::uuid[] AS member_ids
FROM chats c
WHERE c.id = sqlc.arg(id)
AND EXISTS (
SELECT 1 FROM chat_members m
WHERE m.chat_id = c.id
AND m.user_id = sqlc.arg(user_id)
);
-- name: ListChatsFirstPage :many
SELECT
c.*,
ARRAY(
SELECT cm.user_id FROM chat_members cm
WHERE cm.chat_id = c.id
ORDER BY cm.user_id
)::uuid[] AS member_ids
FROM chats c
JOIN chat_members m ON m.chat_id = c.id
WHERE m.user_id = sqlc.arg(user_id)
ORDER BY c.created_at, c.id
LIMIT sqlc.arg(lim);
-- name: ListChatsAfter :many
SELECT
c.*,
ARRAY(
SELECT cm.user_id FROM chat_members cm
WHERE cm.chat_id = c.id
ORDER BY cm.user_id
)::uuid[] AS member_ids
FROM chats c
JOIN chat_members m ON m.chat_id = c.id
WHERE m.user_id = sqlc.arg(user_id)
AND (c.created_at, c.id) > (
sqlc.arg(after_created_at)::timestamptz,
sqlc.arg(after_id)::uuid
)
ORDER BY c.created_at, c.id
LIMIT sqlc.arg(lim);
+36
View File
@@ -0,0 +1,36 @@
-- name: CreateIdempotencyKey :execrows
INSERT INTO idempotency_keys (
user_id,
method,
path,
key,
request_hash,
message_id
) VALUES (
sqlc.arg(user_id),
sqlc.arg(method),
sqlc.arg(path),
sqlc.arg(key),
sqlc.arg(request_hash),
sqlc.arg(message_id)
)
ON CONFLICT DO NOTHING;
-- name: GetIdempotentMessage :one
SELECT
k.request_hash,
m.id,
m.chat_id,
m.sender_id,
m.text,
m.created_at
FROM idempotency_keys AS k
JOIN messages AS m ON m.id = k.message_id
WHERE k.user_id = sqlc.arg(user_id)
AND k.method = sqlc.arg(method)
AND k.path = sqlc.arg(path)
AND k.key = sqlc.arg(key);
-- name: DeleteExpiredIdempotencyKeys :execrows
DELETE FROM idempotency_keys
WHERE created_at < sqlc.arg(cutoff);
+34
View File
@@ -0,0 +1,34 @@
-- name: CreateMessage :one
INSERT INTO messages (
id,
chat_id,
sender_id,
text
)
SELECT
sqlc.arg(id)::uuid,
sqlc.arg(chat_id)::uuid,
sqlc.arg(sender_id)::uuid,
sqlc.arg(text)::text
WHERE EXISTS (
SELECT 1 FROM chat_members
WHERE chat_id = sqlc.arg(chat_id)
AND user_id = sqlc.arg(sender_id)
)
RETURNING *;
-- name: ListMessagesFirstPage :many
SELECT * FROM messages
WHERE chat_id = sqlc.arg(chat_id)
ORDER BY created_at, id
LIMIT sqlc.arg(lim);
-- name: ListMessagesAfter :many
SELECT * FROM messages
WHERE chat_id = sqlc.arg(chat_id)
AND (created_at, id) > (
sqlc.arg(after_created_at)::timestamptz,
sqlc.arg(after_id)::uuid
)
ORDER BY created_at, id
LIMIT sqlc.arg(lim);
+36
View File
@@ -0,0 +1,36 @@
-- name: CreateOutboxEntry :one
INSERT INTO outbox (
job_id,
attachment_id,
pipeline_version,
attempt,
available_at,
traceparent,
tracestate
)
SELECT
job_id,
id,
pipeline_version,
sqlc.arg(attempt)::int,
sqlc.arg(available_at)::timestamptz,
sqlc.arg(traceparent)::text,
sqlc.arg(tracestate)::text
FROM attachments
WHERE id = sqlc.arg(attachment_id)::uuid
AND job_id IS NOT NULL
RETURNING *;
-- name: LockPendingOutboxEntries :many
SELECT * FROM outbox
WHERE published_at IS NULL
AND available_at <= now()
ORDER BY available_at, id
LIMIT sqlc.arg(batch_size)::int
FOR UPDATE SKIP LOCKED;
-- name: MarkOutboxPublished :execrows
UPDATE outbox
SET published_at = now()
WHERE id = sqlc.arg(id)::bigint
AND published_at IS NULL;
+24
View File
@@ -0,0 +1,24 @@
-- name: CreateSession :exec
INSERT INTO sessions (
token_hash,
user_id,
expires_at
) VALUES (
sqlc.arg(token_hash),
sqlc.arg(user_id),
sqlc.arg(expires_at)
);
-- name: GetActiveSession :one
SELECT user_id, expires_at FROM sessions
WHERE token_hash = sqlc.arg(token_hash)
AND expires_at > now();
-- name: DeleteSession :execrows
DELETE FROM sessions
WHERE token_hash = sqlc.arg(token_hash)
AND expires_at > now();
-- name: DeleteExpiredSessions :execrows
DELETE FROM sessions
WHERE expires_at <= now();
+24
View File
@@ -0,0 +1,24 @@
-- name: CreateUser :one
INSERT INTO users (
id,
username,
display_name,
password_hash
) VALUES (
sqlc.arg(id),
sqlc.arg(username),
sqlc.arg(display_name),
sqlc.arg(password_hash)
)
RETURNING id, username, display_name, created_at;
-- name: GetUser :one
SELECT id, username, display_name, created_at FROM users
WHERE id = sqlc.arg(id);
-- name: GetUserByUsername :one
SELECT * FROM users
WHERE username = sqlc.arg(username);
-- name: UpdatePasswordHash :exec
UPDATE users SET password_hash = sqlc.arg(password_hash) WHERE id = sqlc.arg(id);