Майорова Софья, БВТ2301, на 5. Трек1. HTTPS/TLS/ACL,18 API tests,13 pipeline groups,3 compromise runs,clean clone,persistence. Original tests/API contract preserved; residual CVE triage in REPORT/security.
75 lines
4.1 KiB
Python
75 lines
4.1 KiB
Python
"""Track1: actual worker scope, identity revoke/secret rotation and durable job recovery."""
|
|
import concurrent.futures
|
|
import json
|
|
import math
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
import uuid
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path[:0]=[str(ROOT/'tests'),str(ROOT/'scripts')]
|
|
from client import Client
|
|
from rotate_worker import run as rotate
|
|
from bao_operator import call,unseal
|
|
|
|
def dc(*a):return subprocess.check_output(['docker','compose',*a],cwd=ROOT,text=True)
|
|
def audit(script):return json.loads(dc('--profile','tools','run','--rm','--no-deps','audit','/app/student-tests/'+script))
|
|
def resource_snapshot():
|
|
ids=dc('ps','-q','api','worker','dispatcher').split()
|
|
raw=subprocess.check_output(['docker','stats','--no-stream','--format','{{json .}}',*ids],text=True)
|
|
return [{key:row[key] for key in ['Name','CPUPerc','MemUsage','MemPerc','PIDs']} for row in map(json.loads,raw.splitlines())]
|
|
|
|
c=Client();a,b,e,chat=c.bootstrap()
|
|
result={'hypothesis':'Service-specific least privilege reduces stolen worker credential scope. Revocation and rollover stop old access while already accepted jobs remain durable.','baseline_model':audit('baseline_model.py')}
|
|
# Run the privilege probe IN the existing worker UID/namespace/mounts, not only an operator test client.
|
|
code=(ROOT/'student-tests/worker_access.py').read_text()
|
|
result['actual_worker_before']=json.loads(dc('exec','-T','worker','python','-c',code))
|
|
result['resource_snapshots_before']=resource_snapshot()
|
|
dc('stop','worker')
|
|
attachments=[c.attachment(a,chat)[0] for _ in range(6)]
|
|
fixture=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
|
stop=threading.Event();messages=[];latencies=[];errors=[];started=time.monotonic()
|
|
def load():
|
|
index=0
|
|
while not stop.is_set():
|
|
try:
|
|
r=c.message(a,chat,'revoke-load-'+str(index));messages.append(r['json']['id']);latencies.append(r['elapsed'])
|
|
except Exception as error:errors.append(type(error).__name__)
|
|
index+=1;stop.wait(.1)
|
|
thread=threading.Thread(target=load);thread.start()
|
|
try:
|
|
image_before=dc('images','-q','worker').strip()
|
|
result['rotation']=rotate()
|
|
time.sleep(6) # Bound old persistent proxy connections by hard-stop-after5s, do not assume immediate invalidation.
|
|
result['old_access']=audit('old_worker_denied.py')
|
|
ready=[]
|
|
for att in attachments:
|
|
value=c.wait_ready(a,att);content=c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=a)['bytes']
|
|
assert content==fixture;ready.append(value['id'])
|
|
assert dc('images','-q','worker').strip()==image_before
|
|
result['new_worker_after']=json.loads(dc('exec','-T','worker','python','-c',code))
|
|
finally:
|
|
stop.set();thread.join(timeout=5)
|
|
dc('up','-d','--no-deps','worker')
|
|
items=[];cursor=None
|
|
while True:
|
|
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages'+('?cursor='+cursor if cursor else ''),actor=a,schema='MessagePage')['json']
|
|
items+=page['items'];cursor=page['next_cursor']
|
|
if not cursor:break
|
|
assert set(messages)<={m['id'] for m in items}
|
|
elapsed=time.monotonic()-started
|
|
percentile=lambda p:sorted(latencies)[max(0,math.ceil(len(latencies)*p)-1)] if latencies else None
|
|
result['workload']={'messages_confirmed':len(messages),'errors':errors,'all_confirmed_messages_found':True,
|
|
'queued_images_before_revoke':len(attachments),'same_images_ready_after_rollover':len(ready),'original_bytes_match':True,
|
|
'elapsed_seconds':round(elapsed,3),'success_rps':round(len(messages)/elapsed,3),
|
|
'p50_success_seconds':percentile(.50),'p95_success_seconds':percentile(.95),'p99_success_seconds':percentile(.99),
|
|
'load_profile':'One HTTP writer, 100ms wait after completion (closed loop, nominal ceiling10/s); six queued PNGs, no saturation search.',
|
|
'image_unchanged':True,'no_plaintext_or_skip_verify_fallback':True}
|
|
result['resource_snapshots_after']=resource_snapshot()
|
|
(ROOT/'evidence/worker-compromise.json').write_text(json.dumps(result,indent=2)+'\n')
|
|
print('PASS: actual non-root worker scope, old identity/secret denial, same queued jobs ready and confirmed messages preserved.')
|
|
if not messages:raise AssertionError('No workload')
|