"""Inspect trusted host-captured docker inspect JSON. Does not certify external tools."""
import argparse,json
from pathlib import Path

def verify(d,public_path,workspace_path):
    if isinstance(d,list):
        if len(d)!=1:raise ValueError('Expected exactly one container')
        d=d[0]
    h=d['HostConfig'];errors=[]
    required={'NetworkMode':'none','Privileged':False,'ReadonlyRootfs':True,'IpcMode':'private'}
    for k,v in required.items():
        if h.get(k)!=v:errors.append(f'HostConfig.{k} must be {v!r}')
    if 'ALL' not in [x.upper() for x in h.get('CapDrop',[])]:errors.append('All capabilities must be dropped')
    if not any(x in ['no-new-privileges','no-new-privileges:true'] for x in h.get('SecurityOpt',[])):errors.append('no-new-privileges required')
    if h.get('PidMode') not in ['',None,'private']:errors.append('Shared PID namespace forbidden')
    if h.get('CapAdd') or h.get('Devices') or h.get('DeviceRequests'):errors.append('Extra capabilities/devices forbidden')
    if d.get('Config',{}).get('User')!='1000:1000':errors.append('Expected unprivileged UID/GID 1000:1000')
    expected={'/input':(str(Path(public_path).resolve()),False),'/workspace':(str(Path(workspace_path).resolve()),True)}
    mounts=d.get('Mounts',[])
    if len(mounts)!=2:errors.append('Exactly two mounts required')
    seen=set()
    for m in mounts:
        dst=m.get('Destination');seen.add(dst)
        if dst not in expected:errors.append(f'Unexpected mount: {dst}');continue
        src,rw=expected[dst]
        if m.get('Type')!='bind' or m.get('Source')!=src or m.get('RW') is not rw:errors.append(f'Unexpected source/access for {dst}')
    if seen!=set(expected):errors.append('Required input/workspace mounts missing')
    return {'container_configuration_valid':not errors,'errors':errors,'outside_controller_review_required':True,'certifies_complete_evaluation_isolation':False}

if __name__=='__main__':
    p=argparse.ArgumentParser();p.add_argument('--inspect',type=Path,required=True);p.add_argument('--public',required=True);p.add_argument('--workspace',required=True);p.add_argument('--out',type=Path,required=True);a=p.parse_args()
    result=verify(json.loads(a.inspect.read_text()),a.public,a.workspace);a.out.write_text(json.dumps(result,indent=2));print(json.dumps(result,indent=2));raise SystemExit(0 if result['container_configuration_valid'] else 2)
