реализован уровень 4

This commit is contained in:
2026-10-15 20:12:22 +03:00
parent 158fed100c
commit 09b4b1d49d
25 changed files with 1794 additions and 91 deletions
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
"""Verify that Redis outage degrades to PostgreSQL without bypassing auth."""
import os
from pathlib import Path
import subprocess
import uuid
from client import Client, Failure, arguments, check, run
def execute_hook(script, action):
env = dict(os.environ)
env['CACHE_ACTION'] = action
try:
result = subprocess.run(
[str(script)],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=90,
check=False,
)
except subprocess.TimeoutExpired:
raise Failure('Redis outage hook exceeded 90 seconds') from None
check(result.returncode == 0, 'Redis outage hook failed for action ' + action)
def main():
def configure(parser):
parser.add_argument(
'--action-script',
required=True,
help='Executable student hook controlled by CACHE_ACTION=stop|start',
)
args = arguments(
'Redis failure scenario; creates only synthetic API data',
configure,
)
script = Path(args.action_script).expanduser().resolve()
check(
script.is_file() and os.access(script, os.X_OK),
'action script must exist and be executable (chmod +x)',
)
client = Client(args.base_url, args.ca_file, timeout=3)
alice, bob, _, original_chat = client.bootstrap()
path = '/api/v1/chats/' + original_chat['id']
warmed = client.call(
'GET',
path,
actor=bob,
schema='Chat',
)['json']
check(warmed == original_chat, 'warm read changed chat data')
print('Stopping only Redis; hook output is suppressed.')
execute_hook(script, 'stop')
created_during_outage = None
try:
client.call('GET', '/health/ready', schema='Health')
fallback = client.call(
'GET',
path,
actor=bob,
schema='Chat',
)['json']
check(fallback == original_chat, 'PostgreSQL fallback changed chat data')
created_during_outage = client.call(
'POST',
'/api/v1/chats',
201,
actor=alice,
body={
'title': 'Redis outage ' + uuid.uuid4().hex[:8],
'member_ids': [alice['id'], bob['id']],
},
schema='Chat',
)['json']
client.call('GET', path, 401, actor={'id': 'unused', 'token': 'invalid'})
finally:
execute_hook(script, 'start')
check(created_during_outage is not None, 'chat was not created during outage')
recovered = client.call(
'GET',
'/api/v1/chats/' + created_during_outage['id'],
actor=bob,
schema='Chat',
)['json']
check(recovered == created_during_outage, 'cache recovery changed chat data')
print(
'PASS: Redis outage used PostgreSQL fallback; readiness, session, '
'authorization and writes remained correct.'
)
if __name__ == '__main__':
run(main)
+120
View File
@@ -0,0 +1,120 @@
#!/usr/bin/env python3
"""Verify bounded behavior while mandatory PostgreSQL is unavailable."""
import os
from pathlib import Path
import subprocess
import time
import uuid
from client import Client, Failure, arguments, check, run
def execute_hook(script, action):
env = dict(os.environ)
env['DATABASE_ACTION'] = action
try:
result = subprocess.run(
[str(script)],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=90,
check=False,
)
except subprocess.TimeoutExpired:
raise Failure('PostgreSQL outage hook exceeded 90 seconds') from None
check(
result.returncode == 0,
'PostgreSQL outage hook failed for action ' + action,
)
def main():
def configure(parser):
parser.add_argument(
'--action-script',
required=True,
help='Executable hook controlled by DATABASE_ACTION=stop|start',
)
args = arguments(
'PostgreSQL failure scenario; creates only synthetic API data',
configure,
)
script = Path(args.action_script).expanduser().resolve()
check(
script.is_file() and os.access(script, os.X_OK),
'action script must exist and be executable (chmod +x)',
)
client = Client(args.base_url, args.ca_file, timeout=8)
alice, _, _, _ = client.bootstrap()
print('Stopping only PostgreSQL; hook output is suppressed.')
execute_hook(script, 'stop')
try:
client.call('GET', '/health/live', schema='Health')
readiness = client.call('GET', '/health/ready', 503)
check(
readiness['json']['error']['code'] == 'unavailable',
'readiness did not return a bounded unavailable error',
)
client.call('GET', '/api/v1/users/me', 401)
authenticated = client.call(
'GET',
'/api/v1/users/me',
503,
actor=alice,
)
check(
authenticated['json']['error']['code'] == 'unavailable',
'database auth failure did not return unavailable',
)
registration = client.call(
'POST',
'/api/v1/users',
503,
body={
'username': 'db_down_' + uuid.uuid4().hex[:16],
'display_name': 'Database outage',
'password': 'T3st-' + uuid.uuid4().hex,
},
)
check(
registration['json']['error']['code'] == 'unavailable',
'write during database outage did not return unavailable',
)
finally:
execute_hook(script, 'start')
deadline = time.monotonic() + 20
while True:
try:
recovered = client.call(
'GET',
'/api/v1/users/me',
actor=alice,
schema='User',
)['json']
break
except Failure:
check(
time.monotonic() < deadline,
'PostgreSQL pool did not recover within 20 seconds',
)
time.sleep(0.5)
check(
recovered['id'] == alice['id'],
'existing session changed after PostgreSQL recovery',
)
print(
'PASS: PostgreSQL outage returned bounded 503 responses, '
'anonymous access stayed rejected, and the existing session recovered.'
)
if __name__ == '__main__':
run(main)
+114
View File
@@ -0,0 +1,114 @@
\set ON_ERROR_STOP on
\pset pager off
BEGIN;
INSERT INTO users (
id,
username,
display_name,
password_hash
)
VALUES (
'00000000-0000-4000-8000-000000000041',
'level4_explain_user',
'Level 4 EXPLAIN user',
'synthetic-not-a-login-hash'
);
INSERT INTO chats (
id,
title
)
VALUES (
'00000000-0000-4000-8000-000000000042',
'Level 4 EXPLAIN chat'
);
INSERT INTO chat_members (
chat_id,
user_id,
position
)
VALUES (
'00000000-0000-4000-8000-000000000042',
'00000000-0000-4000-8000-000000000041',
0
);
WITH generated AS (
SELECT
number,
md5('level4-message-' || number::text) AS digest
FROM generate_series(1, 10000) AS series(number)
)
INSERT INTO messages (
id,
chat_id,
sender_id,
text,
created_at
)
SELECT
(
substr(digest, 1, 8) || '-' ||
substr(digest, 9, 4) || '-4' ||
substr(digest, 14, 3) || '-a' ||
substr(digest, 18, 3) || '-' ||
substr(digest, 21, 12)
)::uuid,
'00000000-0000-4000-8000-000000000042',
'00000000-0000-4000-8000-000000000041',
'Synthetic history message ' || number,
TIMESTAMPTZ '2026-01-01 00:00:00+00'
+ number * INTERVAL '1 microsecond'
FROM generated;
ANALYZE messages;
\echo 'Synthetic message count (must be 10000):'
SELECT count(*) AS synthetic_message_count
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000042';
\echo 'First page plan:'
EXPLAIN (ANALYZE, BUFFERS)
SELECT
id,
chat_id,
sender_id,
text,
created_at
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
ORDER BY created_at, id
LIMIT 101;
SELECT
id AS cursor_id,
created_at AS cursor_created_at
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
ORDER BY created_at, id
OFFSET 4999
LIMIT 1
\gset
\echo 'Page after cursor plan:'
EXPLAIN (ANALYZE, BUFFERS)
SELECT
id,
chat_id,
sender_id,
text,
created_at
FROM messages
WHERE chat_id = '00000000-0000-4000-8000-000000000042'
AND (created_at, id) > (
:'cursor_created_at'::timestamptz,
:'cursor_id'::uuid
)
ORDER BY created_at, id
LIMIT 101;
ROLLBACK;
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env python3
"""Verify PostgreSQL uniqueness and atomic chat creation through the public API."""
import base64
from concurrent.futures import ThreadPoolExecutor
import uuid
from client import Client, arguments, check, run
def register_same_user(base_url, ca_file, body):
client = Client(base_url, ca_file, timeout=15)
return client.call(
'POST',
'/api/v1/users',
[201, 409],
body=body,
schema='User',
)
def login(client, registration):
credential = base64.b64encode(
(
registration['username']
+ ':'
+ registration['password']
).encode()
).decode()
session = client.call(
'POST',
'/api/v1/auth/sessions',
201,
headers={'Authorization': 'Basic ' + credential},
schema='Session',
)['json']
return {
'id': session['user']['id'],
'user': session['user'],
'registration': registration,
'token': session['token'],
}
def main():
args = arguments(
'Level 4 PostgreSQL race and transaction checks; creates synthetic data'
)
client = Client(args.base_url, args.ca_file, timeout=15)
client.call('GET', '/health/ready', schema='Health')
registration = {
'username': 'race_' + uuid.uuid4().hex[:16],
'display_name': 'Concurrent registration',
'password': 'T3st-' + uuid.uuid4().hex,
}
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [
executor.submit(
register_same_user,
args.base_url,
args.ca_file,
registration,
)
for _ in range(4)
]
results = [future.result() for future in futures]
statuses = [result['status'] for result in results]
check(
statuses.count(201) == 1 and statuses.count(409) == 3,
'concurrent duplicate registration must yield one 201 and three 409 responses',
)
actor = login(client, registration)
before = client.call(
'GET',
'/api/v1/chats?limit=100',
actor=actor,
schema='ChatPage',
)['json']
check(not before['items'], 'new race-test user unexpectedly has chats')
failed_title = 'Rollback proof ' + uuid.uuid4().hex[:8]
client.call(
'POST',
'/api/v1/chats',
400,
actor=actor,
body={
'title': failed_title,
'member_ids': [actor['id'], str(uuid.uuid4())],
},
)
after = client.call(
'GET',
'/api/v1/chats?limit=100',
actor=actor,
schema='ChatPage',
)['json']
check(
not after['items'],
'failed composite chat creation left visible partial data',
)
successful = client.call(
'POST',
'/api/v1/chats',
201,
actor=actor,
body={
'title': 'After rollback ' + uuid.uuid4().hex[:8],
'member_ids': [actor['id']],
},
schema='Chat',
)['json']
check(
successful['member_ids'] == [actor['id']],
'connection did not recover after rolled-back transaction',
)
print(
'PASS: concurrent username race produced one winner; '
'failed composite chat creation rolled back and the connection remained usable.'
)
if __name__ == '__main__':
run(main)
+115
View File
@@ -0,0 +1,115 @@
#!/usr/bin/env python3
"""Verify deployed short session TTL, wrong password and logout."""
import base64
import os
from pathlib import Path
import subprocess
import time
from client import Client, Failure, arguments, check, run
def execute_hook(script, action, ttl_seconds):
env = dict(os.environ)
env['SESSION_TTL_ACTION'] = action
env['SHORT_SESSION_TTL_SECONDS'] = str(ttl_seconds)
try:
result = subprocess.run(
[str(script)],
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=90,
check=False,
)
except subprocess.TimeoutExpired:
raise Failure('session TTL hook exceeded 90 seconds') from None
check(
result.returncode == 0,
'session TTL hook failed for action ' + action,
)
def basic(username, password):
encoded = base64.b64encode((username + ':' + password).encode()).decode()
return {'Authorization': 'Basic ' + encoded}
def main():
def configure(parser):
parser.add_argument(
'--action-script',
required=True,
help='Executable hook controlled by SESSION_TTL_ACTION=short|restore',
)
parser.add_argument('--ttl-seconds', type=int, default=2)
args = arguments(
'Short deployed session TTL, wrong-password and logout checks',
configure,
)
check(
1 <= args.ttl_seconds <= 10,
'ttl-seconds must be between 1 and 10 for this test',
)
script = Path(args.action_script).expanduser().resolve()
check(
script.is_file() and os.access(script, os.X_OK),
'action script must exist and be executable (chmod +x)',
)
execute_hook(script, 'short', args.ttl_seconds)
try:
client = Client(args.base_url, args.ca_file, timeout=10)
actor = client.user('short_ttl')
registration = actor['registration']
client.call(
'POST',
'/api/v1/auth/sessions',
401,
headers=basic(registration['username'], 'Wrong-password-123'),
)
client.call(
'GET',
'/api/v1/users/me',
actor=actor,
schema='User',
)
time.sleep(args.ttl_seconds + 1.25)
client.call('GET', '/api/v1/users/me', 401, actor=actor)
session = client.call(
'POST',
'/api/v1/auth/sessions',
201,
headers=basic(
registration['username'],
registration['password'],
),
schema='Session',
)['json']
fresh_actor = {
'id': actor['id'],
'token': session['token'],
}
client.call(
'DELETE',
'/api/v1/auth/sessions/current',
204,
actor=fresh_actor,
)
client.call('GET', '/api/v1/users/me', 401, actor=fresh_actor)
finally:
execute_hook(script, 'restore', args.ttl_seconds)
print(
'PASS: deployed short SESSION_TTL_SECONDS expired the token; '
'wrong password and logout remained enforced.'
)
if __name__ == '__main__':
run(main)