#!/usr/bin/env python3 """Utah security workbench client. State, execution and evidence stay hosted.""" import argparse,json,os,sys,uuid,urllib.request,urllib.error,urllib.parse p=argparse.ArgumentParser(description=__doc__) p.add_argument('operation',choices=['index','create','read','export','search','evidence','assets','step','plan','run','observe','probe','adopt','capture_start','capture_stop','capture_ingest','finding','coverage','retest','retest_run','resume','archive','purge']) p.add_argument('mission',nargs='?');p.add_argument('--input',default='{}',help='JSON or @filename');p.add_argument('--query',default='');p.add_argument('--id',default='');p.add_argument('--request-id',default=str(uuid.uuid4()));p.add_argument('--out') a=p.parse_args();key=os.environ.get('UTAH_API_KEY','') if not key: p.error('Set UTAH_API_KEY to your agent execution key. Never put it in a command argument.') base='https://aithatsee.com/utah/api/security/workbench' read=a.operation in ['index','read','export','search','evidence'] if a.operation not in ['index','create'] and not a.mission: p.error('This operation requires a saved mission ID.') path='' if a.operation in ['index','create'] else '/'+a.mission if a.operation not in ['index','create','read']: path+='/'+a.operation if a.operation=='search': path+='?q='+urllib.parse.quote(a.query) if a.operation=='evidence': path+='/'+a.id try: raw=open(a.input[1:],encoding='utf-8').read() if a.input.startswith('@') else a.input data=None if read else json.dumps({'request_id':a.request_id,'input':json.loads(raw)}).encode() req=urllib.request.Request(base+path,data=data,headers={'Authorization':'Bearer '+key,'Content-Type':'application/json','Accept':'application/json','User-Agent':'Utah-Security-CLI/1.0'}) with urllib.request.urlopen(req,timeout=45) as r: result=r.read().decode() if a.out: with open(a.out,'w',encoding='utf-8') as f: f.write(result+'\n') print('Saved '+a.out) else: print(json.dumps(json.loads(result),indent=2,ensure_ascii=False)) except urllib.error.HTTPError as e: print('HTTP '+str(e.code)+': '+e.read(2000).decode(errors='replace'),file=sys.stderr) print('Request ID: '+a.request_id+'. Preserve this ID when reconciling the same operation.',file=sys.stderr);sys.exit(1) except (ValueError,OSError) as e: print(type(e).__name__+': operation not confirmed. Request ID: '+a.request_id,file=sys.stderr);sys.exit(1)