Files
canina/infra/hermes-runtime/serve.py
T

43 lines
2.2 KiB
Python

"""Dedicated read-inventory process: never starts cron, gateway, rooms, MCP or PTY."""
from pathlib import Path
import os
from fastapi import Request
from fastapi.responses import JSONResponse
from plugins.dashboard_auth.basic import BasicAuthProvider, hash_password
from hermes_cli.dashboard_auth.registry import register_global_provider
from hermes_cli import web_server
import uvicorn
def secret(name):
p=Path('/run/secrets')/name
if not p.is_file() or p.stat().st_size>4096: raise RuntimeError('Runtime credential unavailable')
value=p.read_text().strip()
if not value or '\n' in value or '\r' in value: raise RuntimeError('Runtime credential unavailable')
return value
def build_app():
register_global_provider(BasicAuthProvider(username=secret('hermes_username'),password_hash=hash_password(secret('hermes_password')),secret=secret('hermes_signing_key').encode(),ttl_seconds=300))
web_server.app.state.auth_required=True
web_server.app.state.bound_host='0.0.0.0'
# Enforce an outer exact route/method allowlist. Installed public health/status stay
# internal; protected inventory still uses installed provider middleware.
allowed={'/api/health','/api/profiles','/api/status','/api/analytics/usage','/api/sessions','/api/cron/jobs','/api/model/info'}
@web_server.app.middleware('http')
async def readonly_gate(request:Request,call_next):
path=request.url.path
if not ((request.method=='GET' and path in allowed) or (request.method=='POST' and path=='/auth/password-login')):
return JSONResponse({'detail':'Unavailable'},status_code=404)
return await call_next(request)
class NoSockets:
async def __call__(self, scope, receive, send):
if scope['type']=='websocket':
await send({'type':'websocket.close','code':1008})
return
await web_server.app(scope,receive,send)
return NoSockets()
if __name__=='__main__':
# No upstream lifespan: no eager schema reconciliation or hosted-room recovery.
# Data is an operator-prepared independent snapshot, NEVER the gateway's volume.
uvicorn.run(build_app(),host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning')