import base64 import hashlib import io import json import os import time import re import secrets import socket import uuid import warnings import hmac import ssl from datetime import datetime, timedelta, timezone from app.secrets import load_secrets load_secrets() import boto3 import psycopg from psycopg.types.json import Jsonb from argon2 import PasswordHasher, exceptions as argon2_exceptions from fastapi import FastAPI, Request from fastapi.responses import JSONResponse, Response from jsonschema import Draft202012Validator, FormatChecker from PIL import Image, ImageOps import pika from cryptography import x509 from cryptography.x509.oid import ExtensionOID from starlette.concurrency import run_in_threadpool from opentelemetry import propagate from opentelemetry.trace import SpanKind, Status, StatusCode from app.telemetry import HTTP_DURATION, HTTP_REQUESTS, tracer app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) INSTANCE = os.getenv('APP_INSTANCE_ID', 'api') LAB = int(os.getenv('LAB_NUMBER', '4')) DSN = os.environ['DATABASE_URL'] BUCKET = os.getenv('S3_BUCKET', 'attachments') STAGING_BUCKET = os.getenv('S3_STAGING_BUCKET', BUCKET+'-staging') ORIGINAL_BUCKET = os.getenv('S3_ORIGINAL_BUCKET', BUCKET+'-original') VARIANTS_BUCKET = os.getenv('S3_VARIANTS_BUCKET', BUCKET+'-variants') ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1) schemas = json.load(open('contracts/openapi.json'))['components']['schemas'] Image.MAX_IMAGE_PIXELS = 40_000_000 warnings.simplefilter('error', Image.DecompressionBombWarning) def s3(): return boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT'], aws_access_key_id=os.environ['S3_ACCESS_KEY'], aws_secret_access_key=os.environ['S3_SECRET_KEY'], region_name='us-east-1', verify=os.getenv('S3_CA_CERT') or True, config=boto3.session.Config(connect_timeout=2, read_timeout=5, retries={'max_attempts': 0}, s3={'addressing_style':'path'})) def now(): return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z') def publish_job(payload): params = pika.URLParameters(os.environ['BROKER_URL']) params.heartbeat = 30 params.blocked_connection_timeout = 5 tls = ssl.create_default_context(cafile=os.environ['BROKER_CA_CERT']) tls.check_hostname = True params.ssl_options = pika.SSLOptions(tls, server_hostname='broker') connection = pika.BlockingConnection(params) try: channel = connection.channel() channel.queue_declare(queue='attachment.process.v1', durable=True) channel.confirm_delivery() with tracer.start_as_current_span('rabbitmq.publish', context=propagate.extract(payload), kind=SpanKind.PRODUCER, attributes={'messenger.job.type': payload['type']}): headers = {} propagate.inject(headers) channel.basic_publish( exchange='', routing_key='attachment.process.v1', body=json.dumps(payload).encode(), properties=pika.BasicProperties(content_type='application/json', delivery_mode=2, message_id=payload['job_id'], type=payload['type'], headers=headers)) finally: connection.close() def job_payload(job_id, attachment_id, attempt=1, trace_context=None): payload = {'schema_version':1,'job_id':str(job_id),'type':'attachment.process.v1', 'attachment_id':str(attachment_id),'pipeline_version':1,'attempt':attempt,'created_at':now()} if trace_context: payload.update({key:trace_context[key] for key in ('traceparent','tracestate') if key in trace_context}) return payload def uid(value): try: return str(uuid.UUID(value)) except (ValueError, TypeError, AttributeError): fail(400) def fail(status): raise APIError(status) class APIError(Exception): def __init__(self, status, retry_after=60): self.status = status self.retry_after = retry_after CODES = {400: 'invalid_request', 401: 'unauthorized', 403: 'forbidden', 404: 'not_found', 409: 'conflict', 413: 'payload_too_large', 415: 'unsupported_media_type', 429: 'rate_limited', 503: 'unavailable', 500: 'internal_error'} def error(status, request_id, path, retry_after=60): code = CODES[status] headers = {} if status == 401: headers['WWW-Authenticate'] = ('Basic realm="messenger", charset="UTF-8"' if path.endswith('/auth/sessions') else 'Bearer realm="messenger"') if status == 429: headers['Retry-After'] = str(max(1,int(retry_after))) return JSONResponse({'error': {'code': code, 'message': code, 'request_id': request_id}}, status_code=status, headers=headers) def allowed_load_balancer(request): chain=request.scope.get('extensions',{}).get('tls',{}).get('client_cert_chain',[]) if not chain: return False try: certificate=x509.load_pem_x509_certificate(chain[0].encode()) names=certificate.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_ALTERNATIVE_NAME).value return 'spiffe://mtusi2026/lab7/load-balancer' in names.get_values_for_type(x509.UniformResourceIdentifier) except Exception: return False @app.middleware('http') async def headers_and_errors(request, call_next): request_id = secrets.token_hex(16) parent = propagate.extract(dict(request.headers)) started = time.perf_counter() with tracer.start_as_current_span('http.server', context=parent, kind=SpanKind.SERVER, attributes={'http.request.method': request.method}) as span: try: if LAB >= 7 and not allowed_load_balancer(request): fail(403) response = await call_next(request) except APIError as exc: response = error(exc.status, request_id, request.url.path, getattr(exc,'retry_after',60)) except (psycopg.OperationalError, psycopg.InterfaceError): response = error(503, request_id, request.url.path) except Exception as exc: import logging logging.error('request.failed', extra={'event':'request.failed','error_type':type(exc).__name__}) response = error(500, request_id, request.url.path) route = getattr(request.scope.get('route'), 'path', 'unmatched') status_class = f'{response.status_code // 100}xx' span.set_attribute('http.route', route) span.set_attribute('http.response.status_code', response.status_code) attrs = {'method':request.method,'route':route,'status_class':status_class} elapsed = time.perf_counter()-started HTTP_REQUESTS.add(1, attrs) HTTP_DURATION.record(elapsed, attrs) import logging logging.info('http.request.completed', extra={'event':'http.request.completed','request_id':request_id, 'method':request.method,'route':route,'status_class':status_class}) if response.status_code >= 500: span.set_status(Status(StatusCode.ERROR)) response.headers['X-Request-Id'] = request_id response.headers['X-Instance-Id'] = INSTANCE return response def validate(body, name): if not Draft202012Validator(schemas[name], format_checker=FormatChecker()).is_valid(body): fail(400) return body def user(db, request): auth = request.headers.get('authorization', '') if not auth.startswith('Bearer '): fail(401) token = hashlib.sha256(auth[7:].encode()).hexdigest() row = db.execute('SELECT u.doc FROM sessions s JOIN users u ON u.id=s.user_id WHERE s.token=%s AND s.expires_at>now()', (token,)).fetchone() if not row: fail(401) return row[0] def chat(db, ident, actor): ident = uid(ident) row = db.execute('SELECT doc FROM chats WHERE id=%s', (ident,)).fetchone() if not row or actor['id'] not in row[0]['member_ids']: fail(404) return row[0] def attachment(db, ident, actor): row = db.execute('SELECT doc FROM attachments WHERE id=%s', (uid(ident),)).fetchone() if not row: fail(404) chat(db, row[0]['chat_id'], actor) return row[0] def page(db, request, table, scope, actor): raw = request.query_params.get('limit', '50') if not re.fullmatch('[0-9]+', raw) or not 1 <= int(raw) <= 100: fail(400) limit = int(raw) cursor = request.query_params.get('cursor') after = None if cursor: try: decoded = json.loads(base64.urlsafe_b64decode(cursor.encode())) if len(decoded) != 3 or decoded[0] != scope: fail(400) datetime.fromisoformat(decoded[1].replace('Z', '+00:00')) uid(decoded[2]) after = decoded[1:] except Exception: fail(400) if table == 'chats': where, args = "doc->'member_ids' ? %s", [actor['id']] else: where, args = "chat_id=%s", [scope] if after: where += " AND (doc->>'created_at',id)>(%s,%s::uuid)" args += after rows = db.execute(f"SELECT doc FROM {table} WHERE {where} ORDER BY doc->>'created_at',id LIMIT %s", args + [limit+1]).fetchall() items = [r[0] for r in rows[:limit]] next_cursor = None if len(rows) > limit: last = items[-1] next_cursor = base64.urlsafe_b64encode(json.dumps([scope,last['created_at'],last['id']]).encode()).decode() return {'items': items, 'next_cursor': next_cursor} @app.get('/health/live') def live(): return {'status': 'ok'} @app.get('/health/ready') def ready(): with psycopg.connect(DSN, connect_timeout=2) as db: db.execute('SELECT count(*) FROM sessions') if LAB >= 4: try: client=s3() for bucket_name in (STAGING_BUCKET,ORIGINAL_BUCKET,VARIANTS_BUCKET): client.list_objects_v2(Bucket=bucket_name,MaxKeys=0) except Exception: fail(503) return {'status': 'ok'} @app.api_route('/api/v1/{path:path}', methods=['GET', 'POST', 'DELETE']) async def api(request: Request, path: str): # Read bounded chunks before JSON parsing/image decoding. limit = 10*1024*1024 if path.endswith('/attachments') else 65536 data = bytearray() async for chunk in request.stream(): if len(data)+len(chunk) > limit: fail(413) data.extend(chunk) return await run_in_threadpool(handle, request, path, bytes(data)) def handle(request, path, raw): method = request.method parts = path.split('/') body = None if method == 'POST' and path != 'auth/sessions' and not path.endswith('/attachments'): try: body = json.loads(raw) except (ValueError, UnicodeError): fail(400) with psycopg.connect(DSN) as db: if path == 'users' and method == 'POST': validate(body, 'CreateUser') if not body['display_name'].strip(): fail(400) doc = {'id': str(uuid.uuid4()), 'username': body['username'], 'display_name': body['display_name'], 'created_at': now()} hashed = ph.hash(body['password']) if not db.execute('INSERT INTO users(id,username,password,doc) VALUES (%s,%s,%s,%s) ON CONFLICT(username) DO NOTHING RETURNING id', (doc['id'],doc['username'],hashed,Jsonb(doc))).fetchone(): fail(409) return JSONResponse(doc, 201) if path == 'auth/sessions' and method == 'POST': auth = request.headers.get('authorization', '') if not auth.startswith('Basic ') or raw: fail(401) try: username, password = base64.b64decode(auth[6:], validate=True).decode().split(':',1) except Exception: fail(401) rate_key=hmac.new(os.environ['LOGIN_RATE_HMAC_KEY'].encode(), (str(request.client.host if request.client else 'unknown')+'\0'+username).encode(), hashlib.sha256).hexdigest() existing=db.execute("SELECT failures,window_started FROM login_rate_limits WHERE key_hash=%s",(rate_key,)).fetchone() if existing and existing[0]>=5 and existing[1] > datetime.now(timezone.utc)-timedelta(seconds=60): remaining=max(1,int((existing[1]+timedelta(seconds=60)-datetime.now(timezone.utc)).total_seconds())) raise APIError(429,remaining) row = db.execute('SELECT password,doc FROM users WHERE username=%s',(username,)).fetchone() valid=False if row: try: valid=ph.verify(row[0],password) except (argon2_exceptions.VerifyMismatchError,argon2_exceptions.VerificationError,argon2_exceptions.InvalidHashError): valid=False if not valid: db.execute("""INSERT INTO login_rate_limits(key_hash,window_started,failures) VALUES(%s,now(),1) ON CONFLICT(key_hash) DO UPDATE SET window_started=CASE WHEN login_rate_limits.window_started<=now()-interval '60 seconds' THEN now() ELSE login_rate_limits.window_started END, failures=CASE WHEN login_rate_limits.window_started<=now()-interval '60 seconds' THEN 1 ELSE login_rate_limits.failures+1 END""",(rate_key,)) db.commit() fail(401) if ph.check_needs_rehash(row[0]): db.execute('UPDATE users SET password=%s WHERE username=%s',(ph.hash(password),username)) db.execute('DELETE FROM login_rate_limits WHERE key_hash=%s',(rate_key,)) token = secrets.token_urlsafe(32) expiry = datetime.now(timezone.utc)+timedelta(seconds=int(os.getenv('SESSION_TTL_SECONDS','3600'))) db.execute('INSERT INTO sessions VALUES(%s,%s,%s)', (hashlib.sha256(token.encode()).hexdigest(),row[1]['id'],expiry)) return JSONResponse({'token':token,'token_type':'Bearer','expires_at':expiry.isoformat().replace('+00:00','Z'),'user':row[1]},201) actor = user(db, request) if path == 'users/me' and method == 'GET': return actor if path == 'auth/sessions/current' and method == 'DELETE': db.execute('DELETE FROM sessions WHERE token=%s',(hashlib.sha256(request.headers['authorization'][7:].encode()).hexdigest(),)) return Response(status_code=204) if path == 'chats': if method == 'GET': return page(db,request,'chats','chats:'+actor['id'],actor) if method == 'POST': validate(body,'CreateChat') if not body['title'].strip() or actor['id'] not in body['member_ids']: fail(400) for ident in body['member_ids']: if not db.execute('SELECT id FROM users WHERE id=%s',(uid(ident),)).fetchone(): fail(400) doc = dict(body,id=str(uuid.uuid4()),created_at=now()) db.execute('INSERT INTO chats VALUES(%s,%s)',(doc['id'],Jsonb(doc))) return JSONResponse(doc,201) if parts[0] == 'chats' and len(parts) >= 2: current = chat(db, parts[1], actor) if len(parts) == 2 and method == 'GET': return current if len(parts) == 3 and parts[2] == 'messages': if method == 'GET': return page(db,request,'messages',current['id'],actor) if method == 'POST': validate(body,'CreateMessage') attachments = body.get('attachment_ids',[]) if LAB >= 4 else [] if not body['text'].strip() and not attachments: fail(400) key = request.headers.get('idempotency-key','') if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}',key): fail(400) for ident in attachments: att = attachment(db,ident,actor) if att['chat_id'] != current['id']: fail(404) if att['status'] == 'failed': fail(409) normalized = {'text':body['text'],'attachment_ids':attachments} # A transaction-scoped shared lock serializes the entire lookup+insert. scope = actor['id']+':'+current['id']+':'+key db.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s,0))',(scope,)) old = db.execute('SELECT body,doc FROM messages WHERE sender_id=%s AND chat_id=%s AND idem_key=%s AND saved_at>now()-interval \'24 hours\'',(actor['id'],current['id'],key)).fetchone() if old: if old[0] != normalized: fail(409) return JSONResponse(old[1],201) doc = {'id':str(uuid.uuid4()),'chat_id':current['id'],'sender_id':actor['id'],'text':body['text'],'created_at':now()} if LAB >= 4: doc['attachment_ids'] = attachments db.execute('INSERT INTO messages(id,chat_id,sender_id,idem_key,body,doc) VALUES(%s,%s,%s,%s,%s,%s)',(doc['id'],current['id'],actor['id'],key,Jsonb(normalized),Jsonb(doc))) return JSONResponse(doc,201) if LAB >= 4 and len(parts) == 3 and parts[2] == 'attachments' and method == 'POST': filename = request.query_params.get('filename','') if not 1 <= len(filename) <= 255 or any(c in '/\\' or ord(c)<32 or 127<=ord(c)<=159 for c in filename): fail(400) mime = request.headers.get('content-type','') if mime not in ('image/png','image/jpeg'): fail(415) try: with Image.open(io.BytesIO(raw)) as image: if image.width*image.height > 40_000_000: fail(413) if image.format not in ('PNG','JPEG') or Image.MIME[image.format] != mime: fail(415) image.verify() with Image.open(io.BytesIO(raw)) as image: image.load() except APIError: raise except (Image.DecompressionBombError, Image.DecompressionBombWarning): fail(413) except Exception: fail(415) ident, job_id, stamp = str(uuid.uuid4()), str(uuid.uuid4()), now() doc = {'id':ident,'chat_id':current['id'],'owner_id':actor['id'],'filename':filename, 'content_type':mime,'size_bytes':len(raw),'sha256':hashlib.sha256(raw).hexdigest(), 'status':'queued','variants':[],'error_code':None,'created_at':stamp,'updated_at':stamp} stage_key = 'staging/'+ident try: s3().put_object(Bucket=STAGING_BUCKET,Key=stage_key,Body=raw,ContentType=mime) except Exception: fail(503) trace_context = {} propagate.inject(trace_context) payload = job_payload(job_id,ident,trace_context=trace_context) db.execute('INSERT INTO attachments(id,chat_id,doc,stage_key) VALUES(%s,%s,%s,%s)', (ident,current['id'],Jsonb(doc),stage_key)) db.execute('INSERT INTO attachment_jobs(job_id,attachment_id,payload) VALUES(%s,%s,%s)', (job_id,ident,Jsonb(payload))) db.commit() # Publisher confirms durable broker handoff. The PostgreSQL outbox remains # recoverable if the broker is unavailable or the process exits here. try: publish_job(payload) db.execute('UPDATE attachment_jobs SET published_at=now() WHERE job_id=%s AND published_at IS NULL',(job_id,)) except Exception as exc: import logging logging.error('Durable job publish failed (%s)',type(exc).__name__) fail(503) return JSONResponse(doc,202) if LAB >= 4 and parts[0] == 'attachments' and len(parts) in (2,3) and method == 'GET': doc = attachment(db,parts[1],actor) if len(parts) == 2: return doc if parts[2] == 'content': if doc['status'] in ('queued','processing','failed'): fail(409) variant = request.query_params.get('variant','original') if variant == 'original': bucket_name,key,content_type = ORIGINAL_BUCKET,'original/'+doc['id'],doc['content_type'] else: selected = next((v for v in doc['variants'] if v['name']==variant),None) if not selected: fail(404) bucket_name,key,content_type = VARIANTS_BUCKET,'variants/'+doc['id']+'/'+variant+'.jpg',selected['content_type'] try: obj = s3().get_object(Bucket=bucket_name,Key=key) try: content = obj['Body'].read(10*1024*1024+1) finally: obj['Body'].close() except Exception: fail(503) return Response(content,media_type=content_type) fail(404)