Make snapshot inventory provenance explicit and deny false live readiness

This commit is contained in:
devops committed 2026-10-23 13:22:21 +00:00
1 parent 0bed6e0313
commit 2023dd4e36
16 files changed
+443 -35

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@ services:
secrets: [hermes_username, hermes_password, hermes_signing_key]
networks:
backend:
aliases: [canina-hermes]
aliases: [canina-hermes-inventory]
security_opt: [no-new-privileges:true]
cap_drop: [ALL]
pids_limit: 128
+3
View File
@@ -2,6 +2,9 @@
import os,tempfile,importlib.util
os.environ['HERMES_HOME']=tempfile.mkdtemp()
os.environ['HERMES_SERVE_HEADLESS']='1'
from pathlib import Path
import json
(Path(os.environ["HERMES_HOME"])/"snapshot-manifest.json").write_text(json.dumps({"schemaVersion":1,"source":"snapshot","capturedAt":"2020-01-01T00:00:00Z"}))
spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
m.secret=lambda name:'isolated-test-only-'+name+'x'*32
import uvicorn
+54 -2
View File
@@ -1,6 +1,7 @@
"""Dedicated read-inventory process: never starts cron, gateway, rooms, MCP or PTY."""
from pathlib import Path
import os
import os, json
from datetime import datetime, timezone
from fastapi import Request
from fastapi.responses import JSONResponse
from plugins.dashboard_auth.basic import BasicAuthProvider, hash_password
@@ -15,7 +16,38 @@ def secret(name):
if not value or '\n' in value or '\r' in value: raise RuntimeError('Runtime credential unavailable')
return value
def snapshot_provenance():
try:
path=Path(os.environ['HERMES_HOME'])/'snapshot-manifest.json'
if path.is_symlink() or not path.is_file() or path.stat().st_size>4096:
raise ValueError()
data=json.loads(path.read_text())
if set(data)!={'schemaVersion','source','capturedAt'} or data['schemaVersion']!=1 or data['source']!='snapshot':
raise ValueError()
captured=data['capturedAt']
if not isinstance(captured,str) or not captured.endswith('Z'):
raise ValueError()
at=datetime.fromisoformat(captured.replace('Z','+00:00'))
age=(datetime.now(timezone.utc)-at).total_seconds()
if age<0: raise ValueError()
return {'source':'snapshot','capturedAt':captured,'ageSeconds':int(age),
'freshness':'fresh' if age<=3600 else 'stale','liveProcess':'unknown'}
except (OSError, ValueError, TypeError, KeyError):
raise RuntimeError('Valid snapshot manifest required') from None
def build_app():
snapshot_provenance()
# Never execute installed status: it probes PIDs, gateway HTTP and platform health.
web_server.app.router.routes[:] = [route for route in web_server.app.router.routes
if getattr(route,'path',None)!='/api/status']
@web_server.app.get('/api/status')
async def snapshot_status():
from hermes_cli.web_server import get_version_info
return {'version':get_version_info().base_version,'gateway_running':False,
'gateway_state':None,'active_agents':0,'overall':'unknown',
'liveGatewayState':'unknown','liveActiveAgents':None}
# Place before installed catch-all UI route.
web_server.app.router.routes.insert(0, web_server.app.router.routes.pop())
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'
@@ -27,7 +59,27 @@ def build_app():
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)
response=await call_next(request)
if request.method!='GET' or response.status_code!=200: return response
try:
provenance=snapshot_provenance()
except RuntimeError:
return JSONResponse({'detail':'Snapshot unavailable'},status_code=503)
body=b''.join([chunk async for chunk in response.body_iterator])
data=json.loads(body)
def annotate(row):
return {**row,'provenance':provenance}
if isinstance(data,list):
data=[annotate(row) for row in data]
else:
data=annotate(data)
if path=='/api/profiles':
data['profiles']=[{**annotate(row),'gateway_running':False,'liveGatewayState':'unknown'} for row in data['profiles']]
if path=='/api/status':
# Legacy required scalars are conservative placeholders, NOT measurements.
data.update(gateway_running=False,active_agents=0,overall='unknown',gateway_state=None,
liveGatewayState='unknown',liveActiveAgents=None)
return JSONResponse(data,headers={'Cache-Control':'no-store'})
class NoSockets:
async def __call__(self, scope, receive, send):
if scope['type']=='websocket':
+6 -4
View File
@@ -1,19 +1,21 @@
// Execute inside disposable compiled backend runner sharing fixture network namespace.
const assert=require('node:assert/strict');
const {HermesSessionBroker}=require('/work/dist/hermes-management/hermes-session-broker');
const {HermesAdapter}=require('/work/dist/hermes-management/hermes.adapter');
const {HermesIntegrationService}=require('/work/dist/hermes-management/hermes-integration.service');
const {HermesSessionBroker}=require('/opt/backend/dist/hermes-management/hermes-session-broker');
const {HermesAdapter}=require('/opt/backend/dist/hermes-management/hermes.adapter');
const {HermesIntegrationService}=require('/opt/backend/dist/hermes-management/hermes-integration.service');
const fetcher=(input,options)=>{const url=new URL(String(input));url.hostname='127.0.0.1';return fetch(url,options);};
(async()=>{
let reads=0;
const broker=new HermesSessionBroker(async()=>{reads++;return {username:'isolated-test-only-hermes_username'+'x'.repeat(32),password:'isolated-test-only-hermes_password'+'x'.repeat(32)};},fetcher);
const adapter=new HermesAdapter({origin:'http://canina-hermes:9119',token:()=>broker.token()},fetcher);
const adapter=new HermesAdapter({origin:'http://canina-hermes-inventory:9119',token:()=>broker.token()},fetcher);
const runtime=new HermesIntegrationService(adapter,{authorize:async(p,action,profile)=>p.subject==='test-admin'&&action==='runtime.read'&&(!profile||profile==='default')});
await assert.rejects(()=>runtime.health({subject:'denied',profiles:[]}),{code:'FORBIDDEN'});assert.equal(reads,0);
const principal={subject:'test-admin',profiles:['default']};
assert.equal((await runtime.health(principal)).auth_required,true);
const profiles=await runtime.profiles(principal);assert(profiles.some(p=>p.name==='default'));assert(profiles.every(p=>p.name==='default'));
await assert.rejects(()=>runtime.status(principal,'outside'),{code:'FORBIDDEN'});
const status=await runtime.status(principal,'default');assert.equal(status.overall,'unknown');assert.equal(status.liveActiveAgents,null);assert.equal(status.provenance.source,'snapshot');
assert.equal(profiles[0].provenance.source,'snapshot');assert.equal(profiles[0].liveGatewayState,'unknown');
assert.equal(reads,1);
console.log('PASS real Node broker -> pinned TCP Hermes login -> adapter -> policy-filtered profiles; deny performs zero secret reads; cache coalesced');
})().catch(e=>{console.error(e.code||'contract failure');process.exitCode=1;});
+35 -1
View File
@@ -7,6 +7,21 @@ spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py'
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
values={'hermes_username':'contract','hermes_password':secrets.token_urlsafe(32),'hermes_signing_key':secrets.token_urlsafe(32)}
module.secret=lambda name:values[name]
# A capture timestamp is supplied by a reviewed exporter, never inferred at boot.
import json
manifest=Path(os.environ['HERMES_HOME'])/'snapshot-manifest.json'
for invalid in [None, {}, {'schemaVersion':1,'source':'snapshot','capturedAt':'not-a-date'}, {'schemaVersion':1,'source':'snapshot','capturedAt':'2999-01-01T00:00:00Z'}]:
if invalid is None:
manifest.unlink(missing_ok=True)
else:
manifest.write_text(json.dumps(invalid))
try:
module.build_app()
except (RuntimeError, ValueError):
pass
else:
raise AssertionError('invalid or missing manifest accepted')
manifest.write_text(json.dumps({'schemaVersion':1,'source':'snapshot','capturedAt':'2020-01-01T00:00:00Z'}))
app=module.build_app()
import uvicorn
server=uvicorn.Server(uvicorn.Config(app,host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning'))
@@ -22,7 +37,26 @@ with httpx.Client(base_url='http://127.0.0.1:9119') as c:
r=c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':values['hermes_password']})
assert r.status_code==200 and r.json()['ok'] is True
assert c.get('/api/profiles').status_code==200
assert c.get('/api/health').json()['auth_required'] is True
health=c.get('/api/health').json()
assert health['auth_required'] is True
assert health['provenance']['source']=='snapshot'
assert health['provenance']['capturedAt']=='2020-01-01T00:00:00Z'
assert health['provenance']['freshness']=='stale'
assert health['provenance']['liveProcess']=='unknown'
profiles=c.get('/api/profiles').json()['profiles']
assert profiles and all(p['liveGatewayState']=='unknown' and p['gateway_running'] is False and p['provenance']['source']=='snapshot' for p in profiles)
status=c.get('/api/status',params={'profile':'default'}).json()
assert status['overall']=='unknown' and status['gateway_state'] is None
assert status['gateway_running'] is False and status['active_agents']==0
assert status['liveActiveAgents'] is None and status['liveGatewayState']=='unknown'
for path in ['/api/model/info','/api/analytics/usage','/api/sessions','/api/cron/jobs']:
response=c.get(path,params={'profile':'default'})
if response.status_code==200:
data=response.json()
if isinstance(data,list):
assert all(r['provenance']['source']=='snapshot' for r in data)
else:
assert data['provenance']['source']=='snapshot'
assert c.post('/api/cron/jobs/example/resume').status_code==404
assert c.get('/api/config').status_code==404
assert c.get('/api/ws').status_code==404
+3
View File
@@ -1,5 +1,8 @@
import os,tempfile,importlib.util
os.environ['HERMES_HOME']=tempfile.mkdtemp()
from pathlib import Path
import json
(Path(os.environ['HERMES_HOME'])/'snapshot-manifest.json').write_text(json.dumps({'schemaVersion':1,'source':'snapshot','capturedAt':'2020-01-01T00:00:00Z'}))
from fastapi.testclient import TestClient
from starlette.websockets import WebSocketDisconnect
spec=importlib.util.spec_from_file_location('runtime_serve','/contract/serve.py');m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)