"""Real HTTP negative/transaction tests against the isolated lab4 stack.""" import base64 from concurrent.futures import ThreadPoolExecutor import hashlib import http.client import io import json from pathlib import Path import socket import sys import time from urllib.parse import quote import uuid sys.path.insert(0,str(Path(__file__).resolve().parents[1])) sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) from client import Client, check from PIL import Image from botocore import UNSIGNED from botocore.config import Config from botocore.exceptions import ClientError import boto3 from src.storage import storage from src.objects import client,BUCKET def main(): c=Client('http://lb:8080',timeout=40) alice,bob,eve,chat=c.bootstrap(); path='/api/v1/chats/'+chat['id'] png=(Path('tests/fixtures/sample.png')).read_bytes() def upload(data=png,mime='image/png',filename='sample.png',expected=201): return c.call('POST',path+'/attachments?filename='+quote(filename,safe=''),expected, actor=alice,raw=data,headers={'Content-Type':mime},schema='Attachment' if expected==201 else None) att=upload()['json'] for name in ('../x.png','a\\b.png','a/b.png','a\x00.png','a\n.png',''): upload(filename=name,expected=400) upload(data=b'not an image',expected=415) upload(data=png[:100],expected=415) upload(mime='image/jpeg',expected=415) upload(mime='application/octet-stream',expected=415) upload(data=b'x'*(10*1024*1024+1),expected=413) # Chunked body exercises the streaming limit with no Content-Length. connection=http.client.HTTPConnection('lb',8080,timeout=30) connection.request('POST',path+'/attachments?filename=chunked.png',body=iter([b'x'*1048576]*11), headers={'Authorization':'Bearer '+alice['token'],'Content-Type':'image/png'},encode_chunked=True) response=connection.getresponse(); payload=json.loads(response.read()); connection.close() check(response.status==413 and payload['error']['code']=='payload_too_large','chunked byte limit failed') # Pixel limit checked before decompression. No huge fixture stored in Git. image=Image.new('L',(8001,5000)); buff=io.BytesIO(); image.save(buff,format='PNG') upload(data=buff.getvalue(),expected=413) del image,buff image=Image.new('RGB',(8000,5000),(1,2,3)); buff=io.BytesIO(); image.save(buff,format='PNG') upload(data=buff.getvalue()); del image,buff buff=io.BytesIO(); Image.new('RGB',(48,32),(90,20,10)).save(buff,format='JPEG') jpeg=buff.getvalue(); j=upload(data=jpeg,mime='image/jpeg',filename='фото.jpg')['json'] check(c.call('GET','/api/v1/attachments/'+j['id']+'/content',actor=bob)['bytes']==jpeg,'JPEG changed') upload(data=jpeg[:-20],mime='image/jpeg',expected=415) c.call('GET','/api/v1/attachments/'+att['id']+'/content?variant=thumbnail',404,actor=bob) c.call('GET','/api/v1/attachments/'+att['id']+'/content?variant=unknown',400,actor=bob) c.call('POST',path+'/messages',404,actor=alice,body={'text':'','attachment_ids':[str(uuid.uuid4())]},headers={'Idempotency-Key':uuid.uuid4().hex}) other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'other','member_ids':[alice['id'],bob['id']]},schema='Chat')['json'] c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=alice,body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex}) c.call('POST',path+'/messages',400,actor=alice,body={'text':'','attachment_ids':[att['id'],att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex}) key=uuid.uuid4().hex with ThreadPoolExecutor(max_workers=16) as pool: messages=list(pool.map(lambda _:Client('http://lb:8080',timeout=40).message(alice,chat,'',key=key,attachments=[att['id']])['json'],range(16))) check(len({m['id'] for m in messages})==1,'concurrent attachment retry duplicated') c.call('POST',path+'/messages',409,actor=alice,body={'text':'','attachment_ids':[j['id']]},headers={'Idempotency-Key':key}) text_key=uuid.uuid4().hex m=c.message(alice,chat,'semantic equality',key=text_key)['json'] check(c.message(alice,chat,'semantic equality',key=text_key,attachments=[])['json']==m,'default [] differs') # Direct unauthenticated bucket read must fail. No public S3 host port needed. storage.pool.open(wait=True) s3=client() with storage.pool.connection() as conn: row=conn.execute('SELECT object_key FROM attachments WHERE id=%s',(att['id'],)).fetchone() unsigned=boto3.client('s3',endpoint_url='http://s3:9000',config=Config(signature_version=UNSIGNED)) try: unsigned.get_object(Bucket=BUCKET,Key=row['object_key']) except ClientError as exc: check(exc.response['ResponseMetadata']['HTTPStatusCode']==403,'unsigned wrong error') else: raise AssertionError('public object') for operation in (lambda:s3.list_buckets(),lambda:s3.put_object(Bucket=BUCKET,Key='outside-prefix',Body=b'x'),lambda:s3.delete_bucket(Bucket=BUCKET)): try: operation() except ClientError as exc: check(exc.response['ResponseMetadata']['HTTPStatusCode']==403,'policy wrong error') else: raise AssertionError('excessive app S3 privileges') # Real TCP disconnect before declared length: no S3 PUT or metadata commit. before=sum(1 for page in s3.get_paginator('list_objects_v2').paginate(Bucket=BUCKET,Prefix='originals/') for _ in page.get('Contents',[])) sock=socket.create_connection(('lb',8080)) header=('POST '+path+'/attachments?filename=abort.png HTTP/1.1\r\nHost: lb\r\nAuthorization: Bearer '+alice['token']+'\r\nContent-Type: image/png\r\nContent-Length: 1000000\r\n\r\n').encode() sock.sendall(header+png[:100]); sock.close(); time.sleep(1) after=sum(1 for page in s3.get_paginator('list_objects_v2').paginate(Bucket=BUCKET,Prefix='originals/') for _ in page.get('Contents',[])) check(before==after,'interrupted upload created object') # Inject DB failure AFTER a real S3 PUT, restricted to this new synthetic chat. with storage.pool.connection() as conn: conn.execute("CREATE OR REPLACE FUNCTION lab4_test_failure() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.chat_id='"+chat['id']+"'::uuid THEN RAISE EXCEPTION 'synthetic failure'; END IF; RETURN NEW; END $$") conn.execute('CREATE TRIGGER lab4_test_failure BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION lab4_test_failure()') try: upload(expected=500) finally: with storage.pool.connection() as conn: conn.execute('DROP TRIGGER IF EXISTS lab4_test_failure ON attachments') conn.execute('DROP FUNCTION IF EXISTS lab4_test_failure()') after_failure=sum(1 for page in s3.get_paginator('list_objects_v2').paginate(Bucket=BUCKET,Prefix='originals/') for _ in page.get('Contents',[])) check(after_failure==after+1,'did not exercise S3/DB gap') upload() storage.pool.close() print(json.dumps({'result':'PASS','concurrent_retries':16,'failure_after_s3_put':True,'interrupted_upload_no_object':True,'private_bucket':True,'restricted_credentials':True})) if __name__=='__main__': main()