Files
mtusi2026lab7/scripts/prove-auth.py
T
Grbisba de4d471d80
Application HTTPS smoke / application (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
Реализация на 5
2026-11-18 11:36:21 +03:00

34 lines
3.2 KiB
Python

#!/usr/bin/env python3
import base64,json,subprocess,sys,time,concurrent.futures,statistics,uuid
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'scripts'));import security
def main():
name,password,token,user=security.actor()
args=['run','--rm','-T','--no-deps','--entrypoint','securityprobe','-e','BAO_ADDR=','-e','PROBE_MODE=hash','-e','ARGON2_PARALLELISM=4','-e','PROBE_PASSWORD='+password,'worker']
old=security.compose(*args).stdout.strip();assert old.startswith('$argon2id$v=19$m=65536,t=3,p=4$')
sql="UPDATE users SET password_hash='"+old+"' WHERE id='"+user+"';"
subprocess.run([str(ROOT/'scripts/compose.sh'),'exec','-T','postgres','psql','-U','messenger','-d','messenger','-v','ON_ERROR_STOP=1'],input=sql,text=True,check=True,stdout=subprocess.DEVNULL,stderr=subprocess.PIPE)
status,_,_=security.request('/api/v1/auth/sessions','POST',headers={'Authorization':'Basic '+base64.b64encode((name+':'+password).encode()).decode()});assert status==201
query="SELECT password_hash LIKE '$argon2id$v=19$m=65536,t=3,p=1$%' FROM users WHERE id='"+user+"';"
assert security.compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',query).stdout.strip()=='t'
kdf=security.compose('run','--rm','-T','--no-deps','--entrypoint','securityprobe','-e','BAO_ADDR=','-e','PROBE_MODE=kdf','worker').stdout
result={'rehash_lab6_profile_on_login':True,'kdf':json.loads(kdf)}
def login_attempt(i):
credentials=base64.b64encode(('absent_'+uuid.uuid4().hex[:12]+':synthetic-wrong-password').encode()).decode();start=time.monotonic()
status,_,_=security.request('/api/v1/auth/sessions','POST',headers={'Authorization':'Basic '+credentials});return status,(time.monotonic()-start)*1000
started=time.monotonic()
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:attempts=list(executor.map(login_attempt,range(24)))
result['login_dos_budget']={'requests':24,'concurrency':8,'duration_seconds':time.monotonic()-started,'statuses':{str(s):sum(x[0]==s for x in attempts) for s in set(x[0] for x in attempts)},'p95_all_ms':sorted(x[1] for x in attempts)[22],'api_memory_limit_mib':512,'max_kdf_memory_mib_per_process':128,'max_kdf_parallel_per_process':2}
override=ROOT/'secrets/lab7/operator/expiry.json';override.write_text(json.dumps({'services':{'api':{'environment':{'SESSION_TTL_SECONDS':'2'}}}}))
base=[str(ROOT/'scripts/compose.sh')]
try:
subprocess.run(['docker','compose','-p','mtusi2026lab7','-f',str(ROOT/'compose.yaml'),'-f',str(override),'up','-d','api'],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.PIPE)
subprocess.run([sys.executable,str(ROOT/'scripts/wait-ready.py')],check=True,stdout=subprocess.DEVNULL)
_,_,short,_=security.actor();time.sleep(3);assert security.request('/api/v1/users/me',headers={'Authorization':'Bearer '+short})[0]==401;result['session_ttl_2_seconds_expires']=True
finally:
security.compose('up','-d','api');subprocess.run([sys.executable,str(ROOT/'scripts/wait-ready.py')],check=True,stdout=subprocess.DEVNULL)
(ROOT/'evidence/auth.json').write_text(json.dumps(result,indent=2)+'\n');print('PASS: legacy rehash, KDF measurements and session expiry')
if __name__=='__main__':main()