From 01f56c6db7b213893a762a6e7d8ceea62f040f63 Mon Sep 17 00:00:00 2001 From: devops Date: Fri, 23 Oct 2026 12:32:56 +0000 Subject: [PATCH] Add admin-scoped Hermes runtime reads and isolated pinned HTTP inventory --- .../hermes-management/runtime-http.spec.ts | 83 +++++++++ .../runtime-read.controller.ts | 169 ++++++++++++++++++ .../hermes-management/runtime-read.module.ts | 48 +++++ .../hermes-management/runtime-read.spec.ts | 75 ++++++++ docs/hermes-runtime-read-slice.md | 93 ++++++++++ infra/hermes-runtime/ci-apply.sh | 17 ++ infra/hermes-runtime/ci-job.template.yml | 16 ++ infra/hermes-runtime/compose.readonly.yml | 36 ++++ infra/hermes-runtime/fixture_server.py | 8 + infra/hermes-runtime/serve.py | 42 +++++ infra/hermes-runtime/test_broker_http.cjs | 19 ++ infra/hermes-runtime/test_http_acceptance.py | 30 ++++ infra/hermes-runtime/test_serve.py | 31 ++++ infra/hermes-runtime/test_ws_denial.py | 18 ++ 14 files changed, 685 insertions(+) create mode 100644 backend/src/hermes-management/runtime-http.spec.ts create mode 100644 backend/src/hermes-management/runtime-read.controller.ts create mode 100644 backend/src/hermes-management/runtime-read.module.ts create mode 100644 backend/src/hermes-management/runtime-read.spec.ts create mode 100644 docs/hermes-runtime-read-slice.md create mode 100644 infra/hermes-runtime/ci-apply.sh create mode 100644 infra/hermes-runtime/ci-job.template.yml create mode 100644 infra/hermes-runtime/compose.readonly.yml create mode 100644 infra/hermes-runtime/fixture_server.py create mode 100644 infra/hermes-runtime/serve.py create mode 100644 infra/hermes-runtime/test_broker_http.cjs create mode 100644 infra/hermes-runtime/test_http_acceptance.py create mode 100644 infra/hermes-runtime/test_serve.py create mode 100644 infra/hermes-runtime/test_ws_denial.py diff --git a/backend/src/hermes-management/runtime-http.spec.ts b/backend/src/hermes-management/runtime-http.spec.ts new file mode 100644 index 00000000..493c8245 --- /dev/null +++ b/backend/src/hermes-management/runtime-http.spec.ts @@ -0,0 +1,83 @@ +import { Test } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import { PassportModule } from '@nestjs/passport'; +import { Strategy, ExtractJwt } from 'passport-jwt'; +import passport from 'passport'; +import { sign } from 'jsonwebtoken'; +import request from 'supertest'; +import { RuntimeReadModule } from './runtime-read.module'; +import { HermesIntegrationService } from './hermes-integration.service'; +describe('runtime HTTP authentication and read contract', () => { + let app: INestApplication; + const health = jest.fn().mockResolvedValue({ ok: true, auth_required: true }); + beforeAll(async () => { + passport.use( + 'jwt', + new Strategy( + { + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + secretOrKey: 'test-only-http-key', + }, + (payload, done) => done(null, { id: payload.sub, role: payload.role }), + ), + ); + const module = await Test.createTestingModule({ + imports: [ + PassportModule, + RuntimeReadModule.register({ 'admin-1': ['default'] }), + ], + }) + .overrideProvider(HermesIntegrationService) + .useValue({ health }) + .compile(); + app = module.createNestApplication(); + await app.init(); + }); + afterAll(async () => { + await app.close(); + passport.unuse('jwt'); + }); + it('rejects missing and forged credentials before upstream', async () => { + health.mockClear(); + await request(app.getHttpServer()) + .get('/admin/orchestration/runtime/health') + .expect(401); + await request(app.getHttpServer()) + .get('/admin/orchestration/runtime/health') + .set( + 'Authorization', + 'Bearer ' + sign({ sub: 'admin-1', role: 'Admin' }, 'forged'), + ) + .expect(401); + expect(health).not.toHaveBeenCalled(); + }); + it('accepts verified allowlisted admin but rejects signed ungranted subject', async () => { + await request(app.getHttpServer()) + .get('/admin/orchestration/runtime/health') + .set( + 'Authorization', + 'Bearer ' + sign({ sub: 'other', role: 'Admin' }, 'test-only-http-key'), + ) + .expect(403); + const r = await request(app.getHttpServer()) + .get('/admin/orchestration/runtime/health') + .set( + 'Authorization', + 'Bearer ' + + sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'), + ) + .expect(200); + expect(r.body).toEqual({ ok: true, auth_required: true }); + expect(r.headers['set-cookie']).toBeUndefined(); + }); + it('does not register mutation handlers', async () => { + await request(app.getHttpServer()) + .post('/admin/orchestration/runtime/schedules') + .set( + 'Authorization', + 'Bearer ' + + sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'), + ) + .expect(404); + }); +}); diff --git a/backend/src/hermes-management/runtime-read.controller.ts b/backend/src/hermes-management/runtime-read.controller.ts new file mode 100644 index 00000000..8c7c679c --- /dev/null +++ b/backend/src/hermes-management/runtime-read.controller.ts @@ -0,0 +1,169 @@ +import { + Controller, + Get, + Req, + Query, + UseGuards, + Injectable, + ForbiddenException, + ServiceUnavailableException, + HttpException, +} from '@nestjs/common'; +import { AuthGuard } from '@nestjs/passport'; +import { + HermesIntegrationService, + RuntimePrincipal, +} from './hermes-integration.service'; +import { HermesError } from './hermes.adapter'; +export interface VerifiedRuntimeRequest { + user?: { + id?: string; + role?: string; + isApiKey?: boolean; + profiles?: string[]; + }; +} +@Injectable() +export class RuntimeReadAccess { + private readonly grants: Readonly>; + constructor(grants: Readonly>) { + const copy: Record = Object.create(null); + for (const [subject, profiles] of Object.entries(grants)) { + if ( + !subject || + !Array.isArray(profiles) || + profiles.length === 0 || + profiles.some((p) => !/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(p)) + ) + throw new Error('Invalid runtime access configuration'); + copy[subject] = Object.freeze([...profiles]); + } + this.grants = Object.freeze(copy); + } + principal(req: VerifiedRuntimeRequest): RuntimePrincipal { + const u = req.user; + if ( + !u?.id || + u.isApiKey || + !['admin', 'superadmin'].includes( + (u.role ?? '').toLowerCase().replace(/[_-]/g, ''), + ) || + !Object.hasOwn(this.grants, u.id) + ) + throw new ForbiddenException('Runtime access denied'); + return { subject: u.id, profiles: this.grants[u.id] }; + } + authorize( + p: RuntimePrincipal, + permission: string, + profile?: string, + ): Promise { + return Promise.resolve( + permission === 'runtime.read' && + Object.hasOwn(this.grants, p.subject) && + (!profile || this.grants[p.subject].includes(profile)), + ); + } +} +@Controller('admin/orchestration/runtime') +@UseGuards(AuthGuard('jwt')) +export class RuntimeReadController { + constructor( + private readonly runtime: HermesIntegrationService, + private readonly access: RuntimeReadAccess, + ) {} + private async read( + req: VerifiedRuntimeRequest, + call: (p: RuntimePrincipal) => Promise, + ) { + const p = this.access.principal(req); + try { + return await call(p); + } catch (e) { + if (e instanceof HermesError) { + if (e.code === 'FORBIDDEN') + throw new ForbiddenException('Runtime access denied'); + if (e.code === 'LIMIT') + throw new HttpException('Runtime request limit', 429); + } + throw new ServiceUnavailableException('Runtime unavailable'); + } + } + private profile( + q: Record, + p: RuntimePrincipal, + extras: string[] = [], + ): string { + if ( + Object.keys(q).some((k) => !['profile', ...extras].includes(k)) || + typeof q.profile !== 'string' || + !p.profiles.includes(q.profile) + ) + throw new HermesError('FORBIDDEN'); + return q.profile; + } + private number( + q: Record, + key: string, + fallback: number, + min: number, + max: number, + ): number { + if (q[key] === undefined) return fallback; + if (typeof q[key] !== 'string' || !/^\d+$/.test(q[key])) + throw new HermesError('LIMIT'); + const n = Number(q[key]); + if (!Number.isSafeInteger(n) || n < min || n > max) + throw new HermesError('LIMIT'); + return n; + } + @Get('health') health(@Req() req: VerifiedRuntimeRequest) { + return this.read(req, (p) => this.runtime.health(p)); + } + @Get('profiles') profiles(@Req() req: VerifiedRuntimeRequest) { + return this.read(req, (p) => this.runtime.profiles(p)); + } + @Get('status') status( + @Req() req: VerifiedRuntimeRequest, + @Query() q: Record, + ) { + return this.read(req, (p) => this.runtime.status(p, this.profile(q, p))); + } + @Get('usage') usage( + @Req() req: VerifiedRuntimeRequest, + @Query() q: Record, + ) { + return this.read(req, (p) => + this.runtime.usage( + p, + this.profile(q, p, ['days']), + this.number(q, 'days', 30, 1, 365), + ), + ); + } + @Get('sessions') sessions( + @Req() req: VerifiedRuntimeRequest, + @Query() q: Record, + ) { + return this.read(req, (p) => + this.runtime.sessions( + p, + this.profile(q, p, ['limit', 'offset']), + this.number(q, 'limit', 20, 1, 100), + this.number(q, 'offset', 0, 0, 10000), + ), + ); + } + @Get('schedules') schedules( + @Req() req: VerifiedRuntimeRequest, + @Query() q: Record, + ) { + return this.read(req, (p) => this.runtime.crons(p, this.profile(q, p))); + } + @Get('model-info') modelInfo( + @Req() req: VerifiedRuntimeRequest, + @Query() q: Record, + ) { + return this.read(req, (p) => this.runtime.modelInfo(p, this.profile(q, p))); + } +} diff --git a/backend/src/hermes-management/runtime-read.module.ts b/backend/src/hermes-management/runtime-read.module.ts new file mode 100644 index 00000000..e4bc333b --- /dev/null +++ b/backend/src/hermes-management/runtime-read.module.ts @@ -0,0 +1,48 @@ +import { DynamicModule, Module } from '@nestjs/common'; +import { open } from 'node:fs/promises'; +import { HermesManagementModule } from './hermes-management.module'; +import { + RuntimeReadAccess, + RuntimeReadController, +} from './runtime-read.controller'; +import { HermesError } from './hermes.adapter'; +export async function runtimeSecret(path: string): Promise { + if (!/^\/run\/secrets\/[a-zA-Z0-9_-]+$/.test(path)) + throw new HermesError('CONFIGURATION'); + try { + const file = await open(path, 'r'); + try { + const stat = await file.stat(); + if (!stat.isFile() || stat.size > 4096) throw new Error(); + const value = (await file.readFile('utf8')).trim(); + if (!value || /[\r\n]/.test(value)) throw new Error(); + return value; + } finally { + await file.close(); + } + } catch { + throw new HermesError('AUTH_REQUIRED'); + } +} +@Module({}) +export class RuntimeReadModule { + static register( + grants: Readonly>, + ): DynamicModule { + const access = new RuntimeReadAccess(grants); + return { + module: RuntimeReadModule, + imports: [ + HermesManagementModule.registerAuthenticated( + async () => ({ + username: await runtimeSecret('/run/secrets/hermes_username'), + password: await runtimeSecret('/run/secrets/hermes_password'), + }), + access, + ), + ], + controllers: [RuntimeReadController], + providers: [{ provide: RuntimeReadAccess, useValue: access }], + }; + } +} diff --git a/backend/src/hermes-management/runtime-read.spec.ts b/backend/src/hermes-management/runtime-read.spec.ts new file mode 100644 index 00000000..a7e93884 --- /dev/null +++ b/backend/src/hermes-management/runtime-read.spec.ts @@ -0,0 +1,75 @@ +import { Test } from '@nestjs/testing'; +import { + RuntimeReadController, + RuntimeReadAccess, +} from './runtime-read.controller'; +import { HermesIntegrationService } from './hermes-integration.service'; +import { HermesError } from './hermes.adapter'; +import { + ForbiddenException, + ServiceUnavailableException, +} from '@nestjs/common'; +describe('runtime read boundary', () => { + const health = jest.fn(); + const profiles = jest.fn(); + const status = jest.fn(); + const service = { + health, + profiles, + status, + } as unknown as HermesIntegrationService; + const access = new RuntimeReadAccess({ 'admin-1': ['default'] }); + const controller = new RuntimeReadController(service, access); + beforeEach(() => jest.clearAllMocks()); + it('denies unconfigured identity without IO', async () => { + await expect( + controller.health({ user: { id: 'other', role: 'Admin' } }), + ).rejects.toBeInstanceOf(ForbiddenException); + expect(health).not.toHaveBeenCalled(); + }); + it('uses server scope not body or user supplied profiles', async () => { + status.mockResolvedValue({ overall: 'ok' }); + await expect( + controller.status( + { user: { id: 'admin-1', role: 'Admin', profiles: ['other'] } }, + { profile: 'other' }, + ), + ).rejects.toBeInstanceOf(ForbiddenException); + expect(status).not.toHaveBeenCalled(); + await controller.status( + { user: { id: 'admin-1', role: 'Admin' } }, + { profile: 'default' }, + ); + expect(status).toHaveBeenCalledWith( + { subject: 'admin-1', profiles: ['default'] }, + 'default', + ); + }); + it('rejects duplicate/missing/unknown query parameters before IO', async () => { + for (const q of [ + {}, + { profile: ['default', 'other'] }, + { profile: 'default', token: 'leak' }, + ]) + await expect( + controller.status({ user: { id: 'admin-1', role: 'Admin' } }, q), + ).rejects.toBeInstanceOf(ForbiddenException); + expect(status).not.toHaveBeenCalled(); + }); + it('maps credential failure without leaking upstream errors', async () => { + health.mockRejectedValue(new HermesError('AUTH_REQUIRED')); + await expect( + controller.health({ user: { id: 'admin-1', role: 'Admin' } }), + ).rejects.toThrow('Runtime unavailable'); + }); + it('rejects API keys and non-admins even when subject is configured', async () => { + for (const user of [ + { id: 'admin-1', role: 'User' }, + { id: 'admin-1', role: 'Admin', isApiKey: true }, + ]) + await expect(controller.health({ user })).rejects.toBeInstanceOf( + ForbiddenException, + ); + expect(health).not.toHaveBeenCalled(); + }); +}); diff --git a/docs/hermes-runtime-read-slice.md b/docs/hermes-runtime-read-slice.md new file mode 100644 index 00000000..613a458c --- /dev/null +++ b/docs/hermes-runtime-read-slice.md @@ -0,0 +1,93 @@ +# Runtime read vertical slice (not deployed) + +Branch feature/v4-runtime-completion, base 5507fbc. No AppModule, live gateway, +production database, cron, dispatch, or existing compose was changed. + +## Parent registration + +Import `RuntimeReadModule.register({ '': ['default', ''] })` +from `backend/src/hermes-management/runtime-read.module` into AppModule. +This module uses the existing Passport `jwt` strategy (DB-resolved user identity), +not the API-key fallback. Parent must retain AuthModule/JwtStrategy initialization. +Missing grants deny. Never construct grants from browser body, JWT profile claims, +or unreviewed environment input. Allowlist is copied/frozen. Admin/SUPER_ADMIN +role AND exact subject grant required; API-key identities forbidden. + +GET `/admin/orchestration/runtime/{health,profiles,status,usage,sessions,schedules,model-info}`. +All except health/profiles require exactly one allowed `profile` query string. +Usage accepts days 1..365 (30); sessions limit 1..100 (20), offset 0..10000 (0). +Unknown parameters on profile routes rejected. Profiles is filtered to server grants. +Responses use existing adapter projections, no prompts/transcripts/config/tokens. +Missing/forged JWT ->401; missing grant/role/profile ->403; runtime auth/network/ +contract failure ->503 `Runtime unavailable`; limits ->429. No browser Hermes +cookie, credential, socket ticket or mutation handler is provided. + +Mount backend `/run/secrets/hermes_username` and `hermes_password` as files. +Only these fixed secret paths are read, after authorization. Broker login/cache +stays server-side. Backend and isolated inventory runtime must receive matching +credentials. Runtime additionally mounts `hermes_signing_key` (>=32 random chars). +Do not put credentials in YAML/env, commands, logs or Git. Rotate key to invalidate +sessions; rotate password and restart inventory plus backend to clear cached access. +Stateless old sessions remain valid until TTL/key rotation. + +## Deliberately separate snapshot topology + +Installed Hermes `serve` lifespan starts reconciliation and hosted-room recovery; +GET sessions auto-archives. Sharing its writable home with existing gateway is +therefore NOT a safe read-only sidecar. `serve.py` runs the pinned installed app +with lifespan OFF and an outer exact GET allowlist, password-login only and all +WebSockets denied1008. It does NOT start scheduler/gateway/MCP/rooms/provider chat. +The existing gateway and external volume `canina-hermes_canina_hermes_data` remain +unchanged, solely owned by existing gateway. Inventory mounts an independently +provisioned snapshot volume `canina_hermes_inventory_snapshot`; it NEVER mounts +original home. Its incidental session auto-archive writes affect only snapshot. +This is snapshot inventory, NOT live runtime freshness/status acceptance. A copy +of gateway PID state is not evidence of live gateway health. UI must not claim live +readiness from this deployment. Parent should expose snapshot provenance/freshness +before enabling it for business use. + +Provisioning must use an approved consistent SQLite backup/checkpoint/export and +redacted profile config inventory, not raw `cp` of changing SQLite/WAL or credentials. +No automatic snapshot job is installed here. No secrets need be copied from live +home. Review which configs the model/status APIs need and omit auth.json/.env. +Missing store/schema -> unavailable, not fabricated empty healthy telemetry. +Dedicated internal network must contain only backend and inventory; do NOT connect +existing gateway with same `canina-hermes` DNS alias. Backend name resolves inventory. +Inventory network must have no external egress. Compose refers to external network; +operator must create it `--internal`. No host ports/socket are published. + +## CI ownership / apply + +`compose.readonly.yml` is deployable ONLY after external snapshot, internal network, +secret files and backend mounts/registration are provisioned. `ci-apply.sh` requires +CI=true, exact reviewed HEAD SHA, clean checkout, snapshot-reviewed assertion, +existing volume/network/files, then compose config/up. Never run alongside any +other inventory instance writing same snapshot. CI job fragment is tracked under +infra ownership; parent owns real workflow wiring and approval environment. No +active deployment trigger added. Existing review-only compose template is unchanged. + +## Executed evidence + +Remote `/opt/services/canina/validation/v4-runtime-completion/`: +- read-red.log missing controller regression; read-green.log 6 suites/27 tests. +- read-http-green.log 7 suites/30 tests: real Nest HTTP with signed JWT strategy, + bad signature/no auth401, ungranted admin403, allowed admin200, no cookie, + mutation404. This uses a mocked runtime service, not live Canina credentials. +- pinned-http.log exact installed image FastAPI login/protected inventory: + wrong password401, real login200/HttpOnly provider-verifiable token, profiles200, + tampered token401. No lifespan/live home. +- serve-http.log actual loopback TCP9119 in network-none disposable pinned image: + unauth inventory401, wrong password401, login200, profiles200, health auth true, + mutation/config/ws HTTP404. Generated test-only credentials. No live volume. +- ws-red.log existing socket rejection code differed; ws-green.log explicit + outer boundary denies authenticated sockets1008 independently of upstream tickets. +- build.log private Prisma generation and full backend tsc build exit0. + +Pinned local exact image ID sha256:373ddd5e6649f525b17993e1c885cdf1352127f8e98fd329140a73237125e52f; +tracked registry digest sha256:9774f4f39a9bb8c2f68ce728ed5e99ddbad282163be56764afacf88ed952b784. +Node22 disposable runner copied read-only source/dependency donor privately; +no DB migration. broker-pinned-e2e.log proves actual compiled Node broker -> TCP pinned Hermes login -> adapter -> policy-filtered inventory (and deny zero secret reads). Nest HTTP authentication tested separately; these tests do not establish full Canina application E2E, snapshot +refresh, CI deployment, live gateway truth or business workflow acceptance. +Those remain explicit integration/provisioning gates, not claimed completion. + +Final full non-DB regression: 106 suites / 850 tests pass; expected legacy negative SSL/payment logs. Scoped ESLint has zero errors, six type-safety warnings; full build passes. No RELEASE_AUTHORIZE integration suite run. diff --git a/infra/hermes-runtime/ci-apply.sh b/infra/hermes-runtime/ci-apply.sh new file mode 100644 index 00000000..5634460f --- /dev/null +++ b/infra/hermes-runtime/ci-apply.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail +# Invoked ONLY by an approved CI job on the target host; never by application startup. +[[ "${CI:-}" == true && "${RUNTIME_READ_APPROVED_SHA:-}" =~ ^[0-9a-f]{40}$ ]] || exit 64 +root=$(git rev-parse --show-toplevel) +[[ "$(git rev-parse HEAD)" == "$RUNTIME_READ_APPROVED_SHA" ]] || exit 65 +[[ -z "$(git status --porcelain)" ]] || exit 65 +[[ "${RUNTIME_SNAPSHOT_REVIEWED:-}" == true ]] || exit 66 +# Gateway remains untouched. Its original writable data volume is NEVER mounted. +# The external snapshot must already exist; do not silently initialize empty data. +docker volume inspect canina_hermes_inventory_snapshot >/dev/null +docker network inspect canina_hermes_backend >/dev/null +for name in hermes_username hermes_password hermes_signing_key; do + test -s "/opt/services/canina/secrets/$name" +done +docker compose -f "$root/infra/hermes-runtime/compose.readonly.yml" config --quiet +docker compose -f "$root/infra/hermes-runtime/compose.readonly.yml" up -d --no-build diff --git a/infra/hermes-runtime/ci-job.template.yml b/infra/hermes-runtime/ci-job.template.yml new file mode 100644 index 00000000..7545439a --- /dev/null +++ b/infra/hermes-runtime/ci-job.template.yml @@ -0,0 +1,16 @@ +# Merge this job into the owning Stage workflow only after provisioning review. +# No production trigger or secret material belongs in this file. +runtime-read-acceptance: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Pinned Hermes HTTP acceptance + run: | + set -euo pipefail + image=nousresearch/hermes-agent@sha256:9774f4f39a9bb8c2f68ce728ed5e99ddbad282163be56764afacf88ed952b784 + for test in test_http_acceptance.py test_serve.py test_ws_denial.py; do + docker run --rm --network none --entrypoint /opt/hermes/.venv/bin/python3 -v "$PWD/infra/hermes-runtime:/contract:ro" "$image" "/contract/$test" + done +# Apply job must require approved SHA, Stage environment approval, provisioned external +# snapshot/network/secrets, and call bash infra/hermes-runtime/ci-apply.sh on target. +# Not enabled here: parent owns actual workflow and Stage promotion. diff --git a/infra/hermes-runtime/compose.readonly.yml b/infra/hermes-runtime/compose.readonly.yml new file mode 100644 index 00000000..7fa0c643 --- /dev/null +++ b/infra/hermes-runtime/compose.readonly.yml @@ -0,0 +1,36 @@ +name: canina-hermes-inventory +services: + runtime-inventory: + image: nousresearch/hermes-agent@sha256:9774f4f39a9bb8c2f68ce728ed5e99ddbad282163be56764afacf88ed952b784 + entrypoint: [/opt/hermes/.venv/bin/python3, /runtime/serve.py] + restart: unless-stopped + environment: + HERMES_HOME: /inventory + HERMES_SERVE_HEADLESS: "1" + volumes: + - ./serve.py:/runtime/serve.py:ro + - inventory_snapshot:/inventory + secrets: [hermes_username, hermes_password, hermes_signing_key] + networks: + backend: + aliases: [canina-hermes] + security_opt: [no-new-privileges:true] + cap_drop: [ALL] + pids_limit: 128 + mem_limit: 1g + # No ports, host socket, original home, or egress network. +volumes: + inventory_snapshot: + external: true + name: canina_hermes_inventory_snapshot +networks: + backend: + external: true + name: canina_hermes_backend +secrets: + hermes_username: + file: /opt/services/canina/secrets/hermes_username + hermes_password: + file: /opt/services/canina/secrets/hermes_password + hermes_signing_key: + file: /opt/services/canina/secrets/hermes_signing_key diff --git a/infra/hermes-runtime/fixture_server.py b/infra/hermes-runtime/fixture_server.py new file mode 100644 index 00000000..62041688 --- /dev/null +++ b/infra/hermes-runtime/fixture_server.py @@ -0,0 +1,8 @@ +# Disposable acceptance fixture only; not production entrypoint. +import os,tempfile,importlib.util +os.environ['HERMES_HOME']=tempfile.mkdtemp() +os.environ['HERMES_SERVE_HEADLESS']='1' +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 +uvicorn.run(m.build_app(),host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning') diff --git a/infra/hermes-runtime/serve.py b/infra/hermes-runtime/serve.py new file mode 100644 index 00000000..39da62bb --- /dev/null +++ b/infra/hermes-runtime/serve.py @@ -0,0 +1,42 @@ +"""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') diff --git a/infra/hermes-runtime/test_broker_http.cjs b/infra/hermes-runtime/test_broker_http.cjs new file mode 100644 index 00000000..fb9d0f01 --- /dev/null +++ b/infra/hermes-runtime/test_broker_http.cjs @@ -0,0 +1,19 @@ +// 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 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 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'}); + 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;}); diff --git a/infra/hermes-runtime/test_http_acceptance.py b/infra/hermes-runtime/test_http_acceptance.py new file mode 100644 index 00000000..d07c0ea8 --- /dev/null +++ b/infra/hermes-runtime/test_http_acceptance.py @@ -0,0 +1,30 @@ +"""Pinned image HTTP contract, no live volumes/network or provider calls.""" +import os, secrets, tempfile +from pathlib import Path +os.environ['HERMES_HOME']=tempfile.mkdtemp(prefix='runtime-contract-') +os.environ['HERMES_SERVE_HEADLESS']='1' +from fastapi.testclient import TestClient +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 +password=secrets.token_urlsafe(32) +provider=BasicAuthProvider(username='contract',password_hash=hash_password(password),secret=secrets.token_bytes(32)) +register_global_provider(provider) +web_server.app.state.auth_required=True +web_server.app.state.bound_host='0.0.0.0' +# No lifespan: avoids hosted-room recovery, cron/MCP and schema reconciliation threads. +client=TestClient(web_server.app,base_url='http://localhost') +def test_protected_inventory(): + assert client.get('/api/profiles').status_code==401 + assert client.post('/auth/password-login',json={'provider':'basic','username':'contract','password':'wrong'}).status_code==401 + response=client.post('/auth/password-login',json={'provider':'basic','username':'contract','password':password}) + assert response.status_code==200 and response.json()['ok'] is True + token=client.cookies.get('hermes_session_at') + assert token and provider.verify_session(access_token=token) + assert 'HttpOnly' in response.headers['set-cookie'] + response=client.get('/api/profiles',headers={'Authorization':'Bearer '+token}) + assert response.status_code==200 and any(p['name']=='default' for p in response.json()['profiles']) + client.cookies.clear() + assert client.get('/api/profiles',headers={'Authorization':'Bearer tampered'}).status_code==401 + print('PASS exact-image HTTP wrong-login=401 login=200 HttpOnly verified-token protected-profiles=200 tampered=401') +if __name__=='__main__':test_protected_inventory() diff --git a/infra/hermes-runtime/test_serve.py b/infra/hermes-runtime/test_serve.py new file mode 100644 index 00000000..b0674eb1 --- /dev/null +++ b/infra/hermes-runtime/test_serve.py @@ -0,0 +1,31 @@ +import importlib.util, os, secrets, tempfile, threading, socket, time +from pathlib import Path +from fastapi.testclient import TestClient +os.environ['HERMES_HOME']=tempfile.mkdtemp(prefix='inventory-snapshot-') +os.environ['HERMES_SERVE_HEADLESS']='1' +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] +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')) +thread=threading.Thread(target=server.run,daemon=True);thread.start() +for _ in range(200): + if server.started:break + time.sleep(.05) +assert server.started +import httpx +with httpx.Client(base_url='http://127.0.0.1:9119') as c: + assert c.get('/api/profiles').status_code==401 + assert c.post('/auth/password-login',json={'provider':'basic','username':values['hermes_username'],'password':'wrong'}).status_code==401 + 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 + 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 + print('PASS real TCP pinned serve: unauth401 wrong-password401 login200 inventory200 health.auth_required=true mutations/config/ws404') +server.should_exit=True;thread.join(10) +assert not thread.is_alive() diff --git a/infra/hermes-runtime/test_ws_denial.py b/infra/hermes-runtime/test_ws_denial.py new file mode 100644 index 00000000..78a13601 --- /dev/null +++ b/infra/hermes-runtime/test_ws_denial.py @@ -0,0 +1,18 @@ +import os,tempfile,importlib.util +os.environ['HERMES_HOME']=tempfile.mkdtemp() +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) +m.secret=lambda name:'test-only-'+'x'*32 +app=m.build_app() +# A permitted session must NOT unlock powerful WebSocket routes. +client=TestClient(app) +r=client.post('/auth/password-login',json={'provider':'basic','username':m.secret('hermes_username'),'password':m.secret('hermes_password')}) +assert r.status_code==200 +for path in ['/api/ws','/api/pty','/api/events']: + try: + with client.websocket_connect(path): + raise AssertionError('Read-only boundary admitted socket '+path) + except WebSocketDisconnect as e: + assert e.code==1008, (path,e.code) +print('PASS authenticated sockets denied1008')