diff --git a/Makefile b/Makefile index 9ecb223..a6a1382 100644 --- a/Makefile +++ b/Makefile @@ -24,3 +24,19 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +TMP_TTL ?= 2 + +my_tests_expiry: + COMPOSE_ENV_FILE="$(or $(COMPOSE_ENV_FILE),.env)" \ + COMPOSE_FILE="$(or $(COMPOSE_FILE),compose.yaml)" \ + $(PYTHON) student_tests/lab2_expiry.py \ + --base-url "$(BASE_URL)" --ttl "$(TMP_TTL)" $(TLS_ARGS) + +my_tests_extra: + COMPOSE_ENV_FILE="$(or $(COMPOSE_ENV_FILE),.env)" \ + COMPOSE_FILE="$(or $(COMPOSE_FILE),compose.yaml)" \ + $(PYTHON) student_tests/lab2_extra.py \ + --base-url "$(BASE_URL)" $(TLS_ARGS) + +my_tests: my_tests_expiry my_tests_extra \ No newline at end of file diff --git a/evidence/lab2_db_schema.txt b/evidence/lab2_db_schema.txt new file mode 100644 index 0000000..1ca4f64 --- /dev/null +++ b/evidence/lab2_db_schema.txt @@ -0,0 +1,77 @@ + Table "public.users" + Column | Type | Collation | Nullable | Default +---------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + username | text | | not null | + display_name | text | | not null | + password_hash | text | | not null | + created_at | timestamp with time zone | | not null | now() +Indexes: + "users_pkey" PRIMARY KEY, btree (id) + "users_username_key" UNIQUE CONSTRAINT, btree (username) +Referenced by: + TABLE "chat_members" CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + TABLE "messages" CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) + TABLE "sessions" CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + + Table "public.chats" + Column | Type | Collation | Nullable | Default +------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + title | text | | not null | + created_at | timestamp with time zone | | not null | now() +Indexes: + "chats_pkey" PRIMARY KEY, btree (id) +Referenced by: + TABLE "chat_members" CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + TABLE "messages" CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + + Table "public.chat_members" + Column | Type | Collation | Nullable | Default +---------+------+-----------+----------+--------- + chat_id | uuid | | not null | + user_id | uuid | | not null | +Indexes: + "chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id) +Foreign-key constraints: + "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + + Table "public.messages" + Column | Type | Collation | Nullable | Default +------------+--------------------------+-----------+----------+--------- + id | uuid | | not null | + chat_id | uuid | | not null | + sender_id | uuid | | not null | + text | text | | not null | + created_at | timestamp with time zone | | not null | now() +Indexes: + "messages_pkey" PRIMARY KEY, btree (id) + "idx_messages_chat_covering" btree (chat_id, created_at, id) INCLUDE (sender_id, text) + "idx_messages_chat_created_id" btree (chat_id, created_at, id) +Foreign-key constraints: + "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE + "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id) + + Table "public.sessions" + Column | Type | Collation | Nullable | Default +------------+--------------------------+-----------+----------+--------- + token | text | | not null | + user_id | uuid | | not null | + expires_at | timestamp with time zone | | not null | + created_at | timestamp with time zone | | not null | now() +Indexes: + "sessions_pkey" PRIMARY KEY, btree (token) + "idx_sessions_expires" btree (expires_at) + "idx_sessions_user" btree (user_id) +Foreign-key constraints: + "sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + + Index "public.idx_messages_chat_created_id" + Column | Type | Key? | Definition | Storage | Stats target +------------+--------------------------+------+------------+---------+-------------- + chat_id | uuid | yes | chat_id | plain | + created_at | timestamp with time zone | yes | created_at | plain | + id | uuid | yes | id | plain | +btree, for table "public.messages" + diff --git a/go.mod b/go.mod index 54fbcab..2c998d2 100644 --- a/go.mod +++ b/go.mod @@ -16,5 +16,6 @@ require ( github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 8806fc4..71fcfab 100644 --- a/go.sum +++ b/go.sum @@ -19,13 +19,10 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -33,23 +30,13 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/migrations/0003_messages_alt_index.sql b/migrations/0003_messages_alt_index.sql new file mode 100644 index 0000000..12d58f1 --- /dev/null +++ b/migrations/0003_messages_alt_index.sql @@ -0,0 +1,5 @@ +-- Индекс B для «стоимости записи»: +-- покрывающий индекс, включающий text — растёт быстрее, помогает только +-- покрывающим запросам. +CREATE INDEX IF NOT EXISTS idx_messages_chat_covering + ON messages (chat_id, created_at, id) INCLUDE (sender_id, text); \ No newline at end of file diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100644 index 0000000..7cb4138 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu +# Логический backup БД messenger в файл на хосте. +# Формат custom (-Fc) позволяет pg_restore с параллелизмом. +OUT_DIR="${1:-evidence}" +mkdir -p "$OUT_DIR" +STAMP=$(date -u +%Y%m%dT%H%M%SZ) +FILE="$OUT_DIR/lab2_track2_backup_${STAMP}.dump" + +docker compose --env-file .env exec -T postgres \ + pg_dump -U messenger -d messenger -Fc \ + > "$FILE" + +echo "$FILE" \ No newline at end of file diff --git a/scripts/bench_insert.sql b/scripts/bench_insert.sql new file mode 100644 index 0000000..4e257b7 --- /dev/null +++ b/scripts/bench_insert.sql @@ -0,0 +1,10 @@ +\timing on +-- 10 000 вставок в один чат. +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT + gen_random_uuid(), + (SELECT id FROM chats ORDER BY created_at LIMIT 1), + (SELECT id FROM users ORDER BY created_at LIMIT 1), + 'bench ' || g, + now() - (g || ' milliseconds')::interval +FROM generate_series(1, 10000) g; \ No newline at end of file diff --git a/scripts/explain_A.sql b/scripts/explain_A.sql new file mode 100644 index 0000000..43b8e25 --- /dev/null +++ b/scripts/explain_A.sql @@ -0,0 +1,8 @@ +-- План чтения по «узкому» индексу (baseline). text достаётся через heap. +EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) +SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = (SELECT id FROM chats ORDER BY created_at LIMIT 1) + AND (created_at, id) > ('2000-01-01T00:00:00Z', '00000000-0000-0000-0000-000000000000') + ORDER BY created_at, id + LIMIT 51; \ No newline at end of file diff --git a/scripts/explain_B.sql b/scripts/explain_B.sql new file mode 100644 index 0000000..b9c1f72 --- /dev/null +++ b/scripts/explain_B.sql @@ -0,0 +1,9 @@ +-- Тот же запрос, но planner имеет выбор: узкий vs покрывающий индекс. +-- После VACUUM ANALYZE PostgreSQL может выбрать Index Only Scan. +EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) +SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = (SELECT id FROM chats ORDER BY created_at LIMIT 1) + AND (created_at, id) > ('2000-01-01T00:00:00Z', '00000000-0000-0000-0000-000000000000') + ORDER BY created_at, id + LIMIT 51; \ No newline at end of file diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100644 index 0000000..9583e81 --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu +# Восстанавливает дамп в НОВУЮ БД messenger_restore на том же инстансе +# (учебный компромисс: физически это тот же PostgreSQL-контейнер, +# но логически — другое хранилище). +DUMP="$1" +[ -f "$DUMP" ] || { echo "no dump: $DUMP" >&2; exit 2; } + +docker compose --env-file .env exec -T postgres \ + psql -U messenger -d postgres -c "DROP DATABASE IF EXISTS messenger_restore;" +docker compose --env-file .env exec -T postgres \ + psql -U messenger -d postgres -c "CREATE DATABASE messenger_restore OWNER messenger;" + +docker compose --env-file .env exec -T postgres \ + pg_restore -U messenger -d messenger_restore --no-owner --no-privileges < "$DUMP" +echo "restored into messenger_restore" \ No newline at end of file diff --git a/scripts/run_my_tests.sh b/scripts/run_my_tests.sh new file mode 100644 index 0000000..a7483c6 --- /dev/null +++ b/scripts/run_my_tests.sh @@ -0,0 +1,23 @@ +#!/bin/sh +# Запускает собственные проверки lab2: +# 1) student_tests/lab2_expiry.py — короткий TTL, .env правится самим тестом; +# 2) student_tests/lab2_extra.py — все остальные сценарии. +# Первый шаг возвращает .env в исходное состояние до запуска второго. +set -eu + +PYTHON="${PYTHON:-python3}" +BASE_URL="${BASE_URL:-http://localhost:8080}" +TMP_TTL="${TMP_TTL:-2}" +COMPOSE_ENV_FILE="${COMPOSE_ENV_FILE:-.env}" +COMPOSE_FILE="${COMPOSE_FILE:-compose.yaml}" + +export COMPOSE_ENV_FILE COMPOSE_FILE +export PYTHON + +echo "=== student_tests/lab2_expiry.py (SESSION_TTL_SECONDS=$TMP_TTL) ===" +"$PYTHON" student_tests/lab2_expiry.py \ + --base-url "$BASE_URL" --ttl "$TMP_TTL" + +echo "=== student_tests/lab2_extra.py ===" +"$PYTHON" student_tests/lab2_extra.py \ + --base-url "$BASE_URL" \ No newline at end of file diff --git a/scripts/seed_messages.sql b/scripts/seed_messages.sql new file mode 100644 index 0000000..48d88fa --- /dev/null +++ b/scripts/seed_messages.sql @@ -0,0 +1,63 @@ +-- Синтетический датасет: 150 000 сообщений, неравномерное распределение по чатам. +-- Требует уже существующих пользователей и хотя бы одного чата; +-- создаёт дополнительные чаты и membership по необходимости. + +DO $$ +DECLARE + n_users CONSTANT int := 50; + n_chats CONSTANT int := 200; + n_messages CONSTANT int := 150000; + hot_chats CONSTANT int := 5; + i int; + uid uuid; + cid uuid; + chosen_chat uuid; + chosen_sender uuid; + r double precision; + uname text; + users_arr uuid[]; + chats_arr uuid[]; +BEGIN + -- Пользователи + FOR i IN 1..n_users LOOP + uid := gen_random_uuid(); + uname := 'seed_u_' || i; + INSERT INTO users (id, username, display_name, password_hash) + VALUES (uid, uname, 'Seed ' || i, '$2a$10$invalidinvalidinvalidinvalidinvalidinvali') + ON CONFLICT (username) DO NOTHING; + users_arr := array_append(users_arr, uid); + END LOOP; + + -- Чаты (равномерно по пользователям, все участники — выборка из users_arr) + FOR i IN 1..n_chats LOOP + cid := gen_random_uuid(); + INSERT INTO chats (id, title) VALUES (cid, 'seed chat ' || i); + INSERT INTO chat_members (chat_id, user_id) + SELECT cid, unnest(users_arr[1 + (i % n_users) : 1 + ((i + 4) % n_users)]) + ON CONFLICT DO NOTHING; + chats_arr := array_append(chats_arr, cid); + END LOOP; + + -- Сообщения: 80% идут в 5 «горячих» чатов, 20% — равномерно по остальным. + FOR i IN 1..n_messages LOOP + r := random(); + IF r < 0.80 THEN + chosen_chat := chats_arr[1 + (i % hot_chats)]; + ELSE + chosen_chat := chats_arr[1 + (i % n_chats)]; + END IF; + chosen_sender := users_arr[1 + (i % n_users)]; + + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ( + gen_random_uuid(), + chosen_chat, + chosen_sender, + 'seed message ' || i || ' ' || repeat('x', 1 + (i % 200)), + -- Разные timestamps: сдвигаем на i миллисекунд назад от now(). + now() - (i || ' milliseconds')::interval + ); + END LOOP; + + RAISE NOTICE 'seeded % messages across % chats', n_messages, n_chats; +END $$; \ No newline at end of file diff --git a/scripts/verify_restore.sql b/scripts/verify_restore.sql new file mode 100644 index 0000000..f4bbed1 --- /dev/null +++ b/scripts/verify_restore.sql @@ -0,0 +1,13 @@ +-- Сверка: count + контрольные сообщения. +SELECT 'src' AS side, count(*) AS messages FROM messages +UNION ALL +SELECT 'restore', count(*) FROM dblink( + 'dbname=messenger_restore user=messenger', + 'SELECT count(*) FROM messages' +) AS t(n bigint); + +-- Контрольные UUID: 5 самых старых сообщений. +SELECT id, chat_id, sender_id, text, created_at + FROM messages + ORDER BY created_at, id + LIMIT 5; \ No newline at end of file diff --git a/src/domain/cache.go b/src/domain/cache.go index d4b399b..965c7ba 100644 --- a/src/domain/cache.go +++ b/src/domain/cache.go @@ -19,10 +19,20 @@ func NewSessionCache(ctx context.Context, url string) (*SessionCache, error) { if err != nil { return nil, err } + // Жёсткие короткие таймауты: Redis — кэш, а не критический путь. + opt.DialTimeout = 1 * time.Second + opt.ReadTimeout = 500 * time.Millisecond + opt.WriteTimeout = 500 * time.Millisecond + opt.MaxRetries = 0 // без автоматических ретраев: одна попытка — и уходим в БД + opt.PoolTimeout = 1 * time.Second + rdb := redis.NewClient(opt) - c, cancel := context.WithTimeout(ctx, 3*time.Second) + + // Проверка связи при старте — короткая. + c, cancel := context.WithTimeout(ctx, 2*time.Second) defer cancel() if err := rdb.Ping(c).Err(); err != nil { + _ = rdb.Close() return nil, err } return &SessionCache{rdb: rdb}, nil @@ -30,6 +40,12 @@ func NewSessionCache(ctx context.Context, url string) (*SessionCache, error) { func (c *SessionCache) key(token string) string { return "session:" + token } +// opCtx возвращает контекст с коротким таймаутом для каждой операции. +// Не наследуем HTTP-контекст напрямую, чтобы не блокировать ответ клиенту. +func (c *SessionCache) opCtx() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.Background(), 500*time.Millisecond) +} + func (c *SessionCache) Set(ctx context.Context, token string, u User, expires time.Time) { ttl := time.Until(expires) if ttl <= 0 { @@ -39,11 +55,16 @@ func (c *SessionCache) Set(ctx context.Context, token string, u User, expires ti if err != nil { return } - c.rdb.Set(ctx, c.key(token), b, ttl) + opCtx, cancel := c.opCtx() + defer cancel() + // Ошибку глотаем: кэш не критичен. + _ = c.rdb.Set(opCtx, c.key(token), b, ttl).Err() } func (c *SessionCache) Get(ctx context.Context, token string) (User, bool) { - b, err := c.rdb.Get(ctx, c.key(token)).Bytes() + opCtx, cancel := c.opCtx() + defer cancel() + b, err := c.rdb.Get(opCtx, c.key(token)).Bytes() if err != nil { return User{}, false } @@ -55,11 +76,13 @@ func (c *SessionCache) Get(ctx context.Context, token string) (User, bool) { } func (c *SessionCache) Del(ctx context.Context, token string) { - c.rdb.Del(ctx, c.key(token)) + opCtx, cancel := c.opCtx() + defer cancel() + _ = c.rdb.Del(opCtx, c.key(token)).Err() } func (c *SessionCache) Ping(ctx context.Context) error { - c2, cancel := context.WithTimeout(ctx, 2*time.Second) + c2, cancel := context.WithTimeout(ctx, 1*time.Second) defer cancel() return c.rdb.Ping(c2).Err() } diff --git a/src/domain/postgres.go b/src/domain/postgres.go index 52846fe..3dac9a8 100644 --- a/src/domain/postgres.go +++ b/src/domain/postgres.go @@ -3,13 +3,19 @@ package domain import ( "context" "errors" + "fmt" + "strings" "time" + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgxpool" - "golang.org/x/crypto/bcrypt" + "golang.org/x/crypto/argon2" ) type Store struct { @@ -48,10 +54,56 @@ func (s *Store) Ping(ctx context.Context) error { return s.pool.Ping(c) } +// Argon2id параметры — OWASP baseline: m=19456 KiB (19 MiB), t=2, p=1. +// Хеш кодируется в формате PHC: $argon2id$v=19$m=19456,t=2,p=1$$ +const ( + argonMemory uint32 = 19456 + argonTime uint32 = 2 + argonThreads uint8 = 1 + argonKeyLen uint32 = 32 + argonSaltLen = 16 +) + +func hashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLen) + if _, err := rand.Read(salt); err != nil { + return "", err + } + key := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + b64Salt := base64.RawStdEncoding.EncodeToString(salt) + b64Key := base64.RawStdEncoding.EncodeToString(key) + // PHC string; параметры можно будет менять в будущем, старые хеши останутся валидными. + return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s", + argonMemory, argonTime, argonThreads, b64Salt, b64Key), nil +} + +func verifyPassword(password, encoded string) bool { + // Минимальный парсер PHC-строки. + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false + } + var mem, time uint32 + var threads uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &mem, &time, &threads); err != nil { + return false + } + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false + } + wantKey, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false + } + gotKey := argon2.IDKey([]byte(password), salt, time, mem, threads, uint32(len(wantKey))) + return subtle.ConstantTimeCompare(gotKey, wantKey) == 1 +} + // --- Users --- func (s *Store) CreateUser(ctx context.Context, username, displayName, password string) (*User, error) { - hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + hash, err := hashPassword(password) if err != nil { return nil, err } @@ -96,14 +148,15 @@ func (s *Store) VerifyPassword(ctx context.Context, username, password string) ( username, ).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt, &hash) if errors.Is(err, pgx.ErrNoRows) { - // Всё равно тратим время на bcrypt, чтобы не различать "нет юзера"/"неверный пароль". - _ = bcrypt.CompareHashAndPassword([]byte("$2a$10$0000000000000000000000000000000000000000000000000000"), []byte(password)) + // Тратим сопоставимое время на проверку фиктивного хеша, + // чтобы не различать "нет пользователя" и "неверный пароль" по таймингу. + _ = verifyPassword(password, "$argon2id$v=19$m=19456,t=2,p=1$AAAAAAAAAAAAAAAAAAAAAA$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") return nil, ErrUnauthorized } if err != nil { return nil, err } - if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil { + if !verifyPassword(password, hash) { return nil, ErrUnauthorized } return &u, nil diff --git a/student_tests/lab2_expiry.py b/student_tests/lab2_expiry.py new file mode 100644 index 0000000..260d399 --- /dev/null +++ b/student_tests/lab2_expiry.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +""" +Проверка истечения сессии при коротком SESSION_TTL_SECONDS. + +Сам: + 1. Запоминает текущее значение SESSION_TTL_SECONDS в .env. + 2. Ставит временное (по умолчанию 2) и перезапускает контейнер api. + 3. Ждёт /health/ready. + 4. Регистрирует пользователя, создаёт сессию, убеждается, что токен живой, + ждёт TTL+2 с и убеждается, что тот же токен даёт 401. + 5. Восстанавливает .env и перезапускает api (в finally). + +Запуск: + python3 student_tests/lab2_expiry.py --base-url http://localhost:8080 --ttl 2 + +.env остаётся в исходном состоянии даже при ошибке теста. +""" +import base64 +import os +import subprocess +import sys +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client, Failure, LAB, arguments, check, run # noqa: E402 + + +COMPOSE_ENV = os.environ.get('COMPOSE_ENV_FILE', '.env') +COMPOSE_FILE = os.environ.get('COMPOSE_FILE', 'compose.yaml') + + +def compose(*args, check_rc=True): + cmd = ['docker', 'compose', '--env-file', COMPOSE_ENV, '-f', COMPOSE_FILE, *args] + proc = subprocess.run(cmd, capture_output=True, text=True) + if check_rc and proc.returncode != 0: + raise Failure('compose %s failed: %s' % (' '.join(args), proc.stderr.strip())) + return proc + + +def read_env_ttl(): + """Возвращает (value, exists): значение SESSION_TTL_SECONDS из .env, если есть.""" + try: + with open(COMPOSE_ENV, 'r', encoding='utf-8') as f: + for line in f: + if line.startswith('SESSION_TTL_SECONDS='): + return line.split('=', 1)[1].strip(), True + except FileNotFoundError: + raise Failure('no %s' % COMPOSE_ENV) + return '', False + + +def write_env_ttl(value, existed): + """Перезаписывает SESSION_TTL_SECONDS, сохраняя остальные строки.""" + with open(COMPOSE_ENV, 'r', encoding='utf-8') as f: + lines = f.readlines() + lines = [ln for ln in lines if not ln.startswith('SESSION_TTL_SECONDS=')] + if existed: + lines.append('SESSION_TTL_SECONDS=%s\n' % value) + with open(COMPOSE_ENV, 'w', encoding='utf-8') as f: + f.writelines(lines) + + +def restart_api(): + compose('up', '-d', '--force-recreate', 'api') + + +def wait_ready(base_url, timeout=90): + # curl без внешних зависимостей: используем http.client через urllib. + import urllib.request + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + with urllib.request.urlopen(base_url + '/health/ready', timeout=2) as r: + if r.status == 200: + return + except Exception: + pass + time.sleep(0.5) + raise Failure('API did not become ready within %d s' % timeout) + + +def test_short_ttl_expiry(c, ttl_seconds): + username = 'ttl_' + str(int(time.time())) + password = 'T3st-' + username + body = {'username': username, 'display_name': 'TTL', 'password': password} + user = c.call('POST', '/api/v1/users', 201, body=body, schema='User')['json'] + cred = base64.b64encode(('%s:%s' % (username, password)).encode()).decode() + session = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, + schema='Session')['json'] + actor = {'id': user['id'], 'user': user, 'token': session['token']} + + c.call('GET', '/api/v1/users/me', 200, actor=actor, schema='User') + time.sleep(ttl_seconds + 2) + c.call('GET', '/api/v1/users/me', 401, actor=actor) + print('PASS: session expires after SESSION_TTL_SECONDS=%d' % ttl_seconds) + + +def main(): + def configure(p): + p.add_argument('--ttl', type=int, default=2, + help='короткий SESSION_TTL_SECONDS на время теста') + + args = arguments('Lab2: истечение сессии при коротком TTL', configure) + check(LAB == 2, 'this test targets lab2 only') + check(1 <= args.ttl <= 5, '--ttl must be 1..5 (короткий TTL только для этого теста)') + + original, existed = read_env_ttl() + print('original SESSION_TTL_SECONDS=%s' % (original if existed else '')) + + def restore(): + print('restoring SESSION_TTL_SECONDS=%s' % (original if existed else '')) + write_env_ttl(original, existed) + restart_api() + + try: + write_env_ttl(str(args.ttl), True) + print('starting API with SESSION_TTL_SECONDS=%d' % args.ttl) + restart_api() + wait_ready(args.base_url) + + c = Client(args.base_url, args.ca_file) + test_short_ttl_expiry(c, args.ttl) + finally: + restore() + + print('PASS: lab2 short-TTL expiry (own test)') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student_tests/lab2_extra.py b/student_tests/lab2_extra.py new file mode 100644 index 0000000..c2a358b --- /dev/null +++ b/student_tests/lab2_extra.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python3 +""" +Собственные проверки критериев уровня 4 lab2, не связанные с коротким TTL: + - гонка регистрации одного username (201 + 409); + - откат CreateChat при неизвестном участнике; + - logout удаляет сессию из источника истины; + - недоступность Redis/Valkey: API работает через БД; + - недоступность PostgreSQL: /health/ready → 503. + +Истечение сессии при коротком SESSION_TTL_SECONDS — в lab2_expiry.py. + +Запуск: + python3 student_tests/lab2_extra.py --base-url http://localhost:8080 +""" +import base64 +import os +import subprocess +import sys +import time +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) +from client import Client, Failure, LAB, arguments, check, run # noqa: E402 + + +COMPOSE_ENV = os.environ.get('COMPOSE_ENV_FILE', '.env') +COMPOSE_FILE = os.environ.get('COMPOSE_FILE', 'compose.yaml') + + +def compose(*args, check_rc=True): + cmd = ['docker', 'compose', '--env-file', COMPOSE_ENV, '-f', COMPOSE_FILE, *args] + proc = subprocess.run(cmd, capture_output=True, text=True) + if check_rc and proc.returncode != 0: + raise Failure('compose %s failed: %s' % (' '.join(args), proc.stderr.strip())) + return proc + + + +def test_redis_down_api_still_works(c): + """Остановить Redis → API продолжает обслуживать через БД (источник истины).""" + alice, bob, eve, chat = c.bootstrap() + c.call('GET', '/api/v1/users/me', 200, actor=alice, schema='User') + + compose('stop', '-t', '1', 'redis') + try: + c.call('GET', '/health/ready', 200, schema='Health') + c.call('GET', '/api/v1/users/me', 200, actor=alice, schema='User') + cred = base64.b64encode( + (alice['registration']['username'] + ':' + alice['registration']['password']).encode() + ).decode() + c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session') + c.message(bob, chat, 'redis down, but DB is up') + finally: + compose('start', 'redis') + for _ in range(30): + if compose('ps', '--format', 'json', 'redis', check_rc=False).stdout.find('"Health":"healthy"') >= 0: + break + time.sleep(0.5) + print('PASS: API works through DB when Redis is down') + + +def test_postgres_down_readiness_503(c): + """Остановить PostgreSQL → /health/ready должен вернуть 503.""" + c.call('GET', '/health/ready', 200, schema='Health') + compose('stop', '-t', '1', 'postgres') + try: + c.call('GET', '/health/ready', 503) + finally: + compose('start', 'postgres') + for _ in range(60): + if compose('ps', '--format', 'json', 'postgres', check_rc=False).stdout.find('"Health":"healthy"') >= 0: + break + time.sleep(0.5) + print('PASS: /health/ready returns 503 when PostgreSQL is down') + + +def test_username_race(c): + """Два параллельных POST с одинаковым username: ровно один 201, другой 409.""" + username = 'race_' + str(int(time.time() * 1000))[-8:] + body = {'username': username, 'display_name': 'Race', + 'password': 'T3st-Race-password-123'} + + def post(): + try: + r = c.call('POST', '/api/v1/users', (201, 409), body=body) + return r['status'] + except Failure: + return -1 + + with ThreadPoolExecutor(max_workers=2) as pool: + statuses = list(pool.map(lambda _: post(), range(2))) + check(sorted(statuses) == [201, 409], + 'username race produced statuses %r, expected [201,409]' % statuses) + print('PASS: username race yields exactly one 201 and one 409') + + +def test_create_chat_rollback_unknown_member(c): + """CreateChat с неизвестным UUID: 400, и в БД нет ни чата, ни membership.""" + alice, _, _, _ = c.bootstrap() + unknown = '99999999-9999-4999-8999-999999999999' + body = {'title': 'rollback ' + str(int(time.time())), + 'member_ids': [alice['id'], unknown]} + c.call('POST', '/api/v1/chats', 400, actor=alice, body=body) + + listed = c.call('GET', '/api/v1/chats?limit=100', actor=alice, schema='ChatPage')['json'] + check(all(ch['title'] != body['title'] for ch in listed['items']), + 'rollback failed: partial chat persisted') + print('PASS: create chat is atomic; unknown member aborts the whole operation') + + +def test_logout_removes_from_source_of_truth(c): + """Logout должен отзывать сессию так, чтобы она не воскресла из кэша/БД.""" + alice, _, _, _ = c.bootstrap() + c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice) + c.call('GET', '/api/v1/users/me', 401, actor=alice) + c.call('GET', '/api/v1/users/me', 401, actor=alice) + print('PASS: logout revokes session in source of truth') + + + +def main(): + def configure(p): + p.add_argument('--skip-docker', action='store_true', + help='пропустить сценарии с остановкой redis/postgres') + + args = arguments('Lab2 собственные проверки уровня 4 (без короткого TTL)', configure) + check(LAB == 2, 'this test targets lab2 only') + + c = Client(args.base_url, args.ca_file) + + test_username_race(c) + test_create_chat_rollback_unknown_member(c) + test_logout_removes_from_source_of_truth(c) + + if args.skip_docker: + print('SKIP: --skip-docker set; skipped redis/postgres down tests') + else: + test_redis_down_api_still_works(c) + test_postgres_down_readiness_503(c) + + print('PASS: lab2 level-4 criteria (own tests, no short TTL)') + + +if __name__ == '__main__': + run(main) \ No newline at end of file