forked from longtao.wu/droboticflow
29 lines
1.6 KiB
Python
29 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Bounded HTTP client for external agents; credentials never enter CLI arguments."""
|
|
import argparse,json,os,ssl,urllib.request,urllib.error
|
|
from pathlib import Path
|
|
p=argparse.ArgumentParser()
|
|
p.add_argument('tool',choices=['capabilities','nodes','pods','node-usage','pod-usage','tasks','logs','egress'])
|
|
p.add_argument('--id', help='Task ID for tasks or 32-character request ID for logs')
|
|
a=p.parse_args()
|
|
import re
|
|
route=a.tool
|
|
if a.tool in ('tasks','logs'):
|
|
pattern=r'annotation-task-[A-Za-z0-9_-]{1,100}' if a.tool=='tasks' else r'[a-f0-9]{32}'
|
|
if not a.id or not re.fullmatch(pattern,a.id):p.error('A valid --id is required')
|
|
route=('tasks/' if a.tool=='tasks' else 'logs/api/')+a.id
|
|
elif a.id:p.error('--id is only valid for tasks/logs')
|
|
key=Path(os.environ['DIAGNOSTIC_KEY_FILE']).read_text().strip()
|
|
ctx=ssl.create_default_context(cafile=os.environ['DIAGNOSTIC_CA_FILE'])
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self,*args,**kwargs):return None
|
|
opener=urllib.request.build_opener(NoRedirect,urllib.request.HTTPSHandler(context=ctx))
|
|
req=urllib.request.Request('https://120.48.180.80/agent/diagnostics/v1/'+route,headers={'Authorization':'Bearer '+key})
|
|
try:
|
|
with opener.open(req,timeout=15) as r:
|
|
data=r.read(1024*1024+1)
|
|
if len(data)>1024*1024:raise SystemExit('Response too large; stop and contact operator')
|
|
print(json.dumps(json.loads(data),ensure_ascii=False,indent=2))
|
|
except urllib.error.HTTPError as e:
|
|
raise SystemExit(f'HTTP {e.code}; do not retry authentication failures or bypass TLS') from None
|