67 lines
3.3 KiB
Python
67 lines
3.3 KiB
Python
"""Run inside the API container: private bucket and cleanup failure recovery."""
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import uuid
|
|
import urllib.request
|
|
import urllib.error
|
|
sys.path.insert(0, '/srv/app')
|
|
import psycopg
|
|
import storage
|
|
from cleanup import sweep
|
|
|
|
prefix = uuid.uuid4().hex
|
|
ids = [str(uuid.uuid4()) for _ in range(3)]
|
|
keys = ['originals/'+i for i in ids]
|
|
user,chat = str(uuid.uuid4()),str(uuid.uuid4())
|
|
dsn=storage.secret('DATABASE_URL')
|
|
# Model a process stopping after S3 PUT but before final DB COMMIT.
|
|
# Also create a fresh intent and an old ready file; neither may be removed.
|
|
try:
|
|
with psycopg.connect(dsn) as db:
|
|
db.execute('INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s)',
|
|
(user,'cleanup_'+prefix[:12],'Synthetic cleanup','not-a-login-hash'))
|
|
db.execute('INSERT INTO chats(id,title) VALUES (%s,%s)', (chat,'Synthetic cleanup'))
|
|
for i,(aid,key) in enumerate(zip(ids,keys)):
|
|
db.execute("""INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,status,created_at)
|
|
VALUES (%s,%s,%s,'test.png',%s,'image/png',3,%s,%s,clock_timestamp()-%s*interval '1 hour')""",
|
|
(aid,chat,user,key,'0'*64,'ready' if i==2 else 'pending',2 if i!=1 else 0))
|
|
for key in keys:
|
|
storage.client.put_object(Bucket=storage.BUCKET,Key=key,Body=b'abc')
|
|
try:
|
|
urllib.request.urlopen('http://s3:9000/'+storage.BUCKET+'/'+keys[2],timeout=5)
|
|
raise AssertionError('Anonymous object access allowed')
|
|
except urllib.error.HTTPError as error:
|
|
assert error.code==403, error.code
|
|
# Roll back a simulated failed finalization; durable pending intent remains.
|
|
with psycopg.connect(dsn) as db:
|
|
db.execute("UPDATE attachments SET status='ready' WHERE id=%s", (ids[0],))
|
|
db.rollback()
|
|
# Even an old intent must survive while a live finalization holds its lock.
|
|
with psycopg.connect(dsn) as active:
|
|
active.execute('SELECT id FROM attachments WHERE id=%s FOR UPDATE', (ids[0],))
|
|
sweep()
|
|
storage.client.head_object(Bucket=storage.BUCKET, Key=keys[0])
|
|
assert active.execute('SELECT status FROM attachments WHERE id=%s', (ids[0],)).fetchone()[0] == 'pending'
|
|
sweep()
|
|
sweep() # Idempotent after completion.
|
|
with psycopg.connect(dsn) as db:
|
|
remaining={str(r[0]) for r in db.execute('SELECT id FROM attachments WHERE id=ANY(%s::uuid[])',(ids,))}
|
|
assert remaining==set(ids[1:]), remaining
|
|
try:
|
|
storage.client.head_object(Bucket=storage.BUCKET,Key=keys[0])
|
|
raise AssertionError('Abandoned object still exists')
|
|
except storage.ClientError as error:
|
|
assert error.response['ResponseMetadata']['HTTPStatusCode']==404
|
|
for key in keys[1:]:
|
|
storage.client.head_object(Bucket=storage.BUCKET,Key=key)
|
|
print('PASS: private bucket, failed finalization recovery, active lock protection, grace period, ready protection, repeated cleanup')
|
|
finally:
|
|
for key in keys:
|
|
storage.client.delete_object(Bucket=storage.BUCKET,Key=key)
|
|
with psycopg.connect(dsn) as db:
|
|
db.execute('DELETE FROM attachments WHERE id=ANY(%s::uuid[])',(ids,))
|
|
db.execute('DELETE FROM chats WHERE id=%s',(chat,))
|
|
db.execute('DELETE FROM users WHERE id=%s',(user,))
|