Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6db634a35 | ||
|
|
01f56c6db7 |
No files matched your search
@@ -12,7 +12,7 @@ export class HermesManagementModule {
|
||||
policy: RuntimeAuthorizer = {authorize: async () => false},
|
||||
): DynamicModule {
|
||||
const broker = new HermesSessionBroker(credentials);
|
||||
return this.register({origin:'http://canina-hermes:9119',token:()=>broker.token()},policy);
|
||||
return this.register({origin:'http://canina-hermes-inventory:9119',token:()=>broker.token()},policy);
|
||||
}
|
||||
|
||||
static register(
|
||||
|
||||
@@ -34,7 +34,7 @@ describe('server-only Hermes session broker',()=>{
|
||||
expect(await broker.token()).toBe('verified-test-token');
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
const [url,options]=fetcher.mock.calls[0];
|
||||
expect(url).toBe('http://canina-hermes:9119/auth/password-login');
|
||||
expect(url).toBe('http://canina-hermes-inventory:9119/auth/password-login');
|
||||
expect(JSON.parse(options.body)).toEqual({provider:'basic',username:'backend',password:'test-only'});
|
||||
expect(options.redirect).toBe('error');
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@ export class HermesSessionBroker {
|
||||
try{
|
||||
const creds=await Promise.race([this.credentials(),new Promise<never>((_,reject)=>controller.signal.addEventListener('abort',()=>reject(new HermesError('UNAVAILABLE')),{once:true}))]);
|
||||
if(!creds.username || !creds.password) throw new HermesError('AUTH_REQUIRED');
|
||||
const response=await this.fetcher('http://canina-hermes:9119/auth/password-login',{method:'POST',redirect:'error',signal:controller.signal,headers:{'Content-Type':'application/json'},body:JSON.stringify({provider:'basic',...creds})});
|
||||
const response=await this.fetcher('http://canina-hermes-inventory:9119/auth/password-login',{method:'POST',redirect:'error',signal:controller.signal,headers:{'Content-Type':'application/json'},body:JSON.stringify({provider:'basic',...creds})});
|
||||
if(!response.ok) throw new HermesError('AUTH_REQUIRED');
|
||||
const reader=response.body?.getReader();
|
||||
if(!reader) throw new HermesError('AUTH_REQUIRED');
|
||||
|
||||
@@ -28,6 +28,7 @@ export interface Health {
|
||||
version: string;
|
||||
commit: string;
|
||||
auth_required: boolean;
|
||||
provenance?: Row;
|
||||
}
|
||||
function object(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value))
|
||||
@@ -57,7 +58,12 @@ export class HermesAdapter {
|
||||
private readonly config: HermesConfig,
|
||||
private readonly fetcher: typeof fetch = fetch,
|
||||
) {
|
||||
if (config.origin !== 'http://canina-hermes:9119')
|
||||
if (
|
||||
![
|
||||
'http://canina-hermes:9119',
|
||||
'http://canina-hermes-inventory:9119',
|
||||
].includes(config.origin)
|
||||
)
|
||||
throw new HermesError('CONFIGURATION');
|
||||
this.origin = config.origin;
|
||||
}
|
||||
@@ -122,7 +128,12 @@ export class HermesAdapter {
|
||||
} finally {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
}
|
||||
return JSON.parse(Buffer.concat(chunks).toString('utf8')) as unknown;
|
||||
const data: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
||||
if (this.origin === 'http://canina-hermes-inventory:9119') {
|
||||
const rows = Array.isArray(data) ? data : [data];
|
||||
for (const row of rows) provenance(row, true);
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
if (error instanceof HermesError) throw error;
|
||||
throw new HermesError('UNAVAILABLE');
|
||||
@@ -132,12 +143,16 @@ export class HermesAdapter {
|
||||
}
|
||||
}
|
||||
async health(): Promise<Health> {
|
||||
const health = fields(await this.request('/api/health'), {
|
||||
ok: 'boolean',
|
||||
version: 'string',
|
||||
commit: 'string',
|
||||
auth_required: 'boolean',
|
||||
}) as unknown as Health;
|
||||
const raw = await this.request('/api/health');
|
||||
const health = {
|
||||
...fields(raw, {
|
||||
ok: 'boolean',
|
||||
version: 'string',
|
||||
commit: 'string',
|
||||
auth_required: 'boolean',
|
||||
}),
|
||||
...provenance(raw),
|
||||
} as unknown as Health;
|
||||
if (!health.auth_required) throw new HermesError('AUTH_REQUIRED');
|
||||
return health;
|
||||
}
|
||||
@@ -151,23 +166,47 @@ export class HermesAdapter {
|
||||
});
|
||||
if (raw.gateway_state !== null && typeof raw.gateway_state !== 'string')
|
||||
throw new HermesError('CONTRACT');
|
||||
return { ...row, gateway_state: raw.gateway_state as Json };
|
||||
return {
|
||||
...row,
|
||||
gateway_state: raw.gateway_state as Json,
|
||||
...provenance(raw),
|
||||
...(raw.provenance
|
||||
? {
|
||||
gateway_running: false,
|
||||
active_agents: 0,
|
||||
gateway_state: null,
|
||||
overall: 'unknown',
|
||||
liveGatewayState: 'unknown',
|
||||
liveActiveAgents: null,
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
async profiles(): Promise<Row[]> {
|
||||
return list(object(await this.request('/api/profiles')).profiles).map((r) =>
|
||||
fields(r, {
|
||||
name: 'string',
|
||||
is_default: 'boolean',
|
||||
gateway_running: 'boolean',
|
||||
return list(object(await this.request('/api/profiles')).profiles).map(
|
||||
(r) => ({
|
||||
...fields(r, {
|
||||
name: 'string',
|
||||
is_default: 'boolean',
|
||||
gateway_running: 'boolean',
|
||||
}),
|
||||
...provenance(r),
|
||||
...(object(r).provenance
|
||||
? { gateway_running: false, liveGatewayState: 'unknown' }
|
||||
: {}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
async modelInfo(profile: string): Promise<Row> {
|
||||
return fields(await this.request('/api/model/info', profile), {
|
||||
model: 'string',
|
||||
provider: 'string',
|
||||
effective_context_length: 'number',
|
||||
});
|
||||
const raw = await this.request('/api/model/info', profile);
|
||||
return {
|
||||
...fields(raw, {
|
||||
model: 'string',
|
||||
provider: 'string',
|
||||
effective_context_length: 'number',
|
||||
}),
|
||||
...provenance(raw),
|
||||
};
|
||||
}
|
||||
async modelOptions(profile: string): Promise<Row[]> {
|
||||
return list(
|
||||
@@ -189,6 +228,7 @@ export class HermesAdapter {
|
||||
);
|
||||
return {
|
||||
...fields(raw, { period_days: 'number' }),
|
||||
...provenance(raw),
|
||||
totals: fields(raw.totals, {
|
||||
total_input: 'number',
|
||||
total_output: 'number',
|
||||
@@ -218,7 +258,11 @@ export class HermesAdapter {
|
||||
is_active: 'boolean',
|
||||
}),
|
||||
);
|
||||
return { ...fields(raw, { total: 'number' }), sessions: rows };
|
||||
return {
|
||||
...fields(raw, { total: 'number' }),
|
||||
sessions: rows,
|
||||
...provenance(raw),
|
||||
};
|
||||
}
|
||||
async crons(profile: string): Promise<Row[]> {
|
||||
return list(await this.request('/api/cron/jobs', profile)).map((r) =>
|
||||
@@ -265,5 +309,34 @@ function scoped(
|
||||
): Row {
|
||||
const row = fields(value, schema);
|
||||
if (row.profile !== profile) throw new HermesError('FORBIDDEN');
|
||||
return row;
|
||||
return { ...row, ...provenance(value) };
|
||||
}
|
||||
|
||||
/** Snapshot provenance is mandatory on inventory origin; never synthesize a capture time. */
|
||||
function provenance(value: unknown, required = false): Row {
|
||||
const raw = object(value);
|
||||
if (!raw.provenance && !required) return {};
|
||||
const p = object(raw.provenance);
|
||||
if (
|
||||
p.source !== 'snapshot' ||
|
||||
p.liveProcess !== 'unknown' ||
|
||||
typeof p.capturedAt !== 'string' ||
|
||||
!p.capturedAt.endsWith('Z') ||
|
||||
!Number.isFinite(Date.parse(p.capturedAt)) ||
|
||||
Date.parse(p.capturedAt) > Date.now() ||
|
||||
typeof p.ageSeconds !== 'number' ||
|
||||
!Number.isInteger(p.ageSeconds) ||
|
||||
p.ageSeconds < 0 ||
|
||||
!['fresh', 'stale'].includes(String(p.freshness))
|
||||
)
|
||||
throw new HermesError('CONTRACT');
|
||||
return {
|
||||
provenance: {
|
||||
source: 'snapshot',
|
||||
capturedAt: p.capturedAt,
|
||||
ageSeconds: p.ageSeconds,
|
||||
freshness: p.freshness as string,
|
||||
liveProcess: 'unknown',
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { loadRuntimeGrants } from './runtime-read.module';
|
||||
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
describe('reviewed runtime grants file', () => {
|
||||
it('loads an exact reviewed file and fails closed for malformed grants', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'runtime-grants-'));
|
||||
const file = join(dir, 'grants.json');
|
||||
try {
|
||||
await writeFile(file, JSON.stringify({ 'subject-1': ['default'] }));
|
||||
expect(await loadRuntimeGrants(file, dir)).toEqual({
|
||||
'subject-1': ['default'],
|
||||
});
|
||||
for (const invalid of [
|
||||
null,
|
||||
[],
|
||||
{ 'subject-1': [] },
|
||||
{ 'subject-1': ['default', 'default'] },
|
||||
{ 'subject-1': ['../x'] },
|
||||
{ 'subject-1': [3] },
|
||||
{ 'subject-1': ['default'], extra: true },
|
||||
]) {
|
||||
await writeFile(file, JSON.stringify(invalid));
|
||||
await expect(loadRuntimeGrants(file, dir)).rejects.toThrow();
|
||||
}
|
||||
await expect(loadRuntimeGrants('/etc/passwd')).rejects.toThrow();
|
||||
} finally {
|
||||
await rm(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
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<Record<string, readonly string[]>>;
|
||||
constructor(grants: Readonly<Record<string, readonly string[]>>) {
|
||||
if (
|
||||
!grants ||
|
||||
typeof grants !== 'object' ||
|
||||
Array.isArray(grants) ||
|
||||
Object.keys(grants).length > 1000
|
||||
)
|
||||
throw new Error('Invalid runtime access configuration');
|
||||
const copy: Record<string, readonly string[]> = Object.create(null);
|
||||
for (const [subject, profiles] of Object.entries(grants)) {
|
||||
if (
|
||||
!/^[a-zA-Z0-9][a-zA-Z0-9@._:-]{0,127}$/.test(subject) ||
|
||||
['__proto__', 'constructor', 'prototype'].includes(subject) ||
|
||||
!Array.isArray(profiles) ||
|
||||
profiles.length === 0 ||
|
||||
profiles.length > 100 ||
|
||||
new Set(profiles).size !== profiles.length ||
|
||||
profiles.some(
|
||||
(p) =>
|
||||
typeof p !== 'string' ||
|
||||
!/^[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<boolean> {
|
||||
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<unknown>,
|
||||
) {
|
||||
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<string, unknown>,
|
||||
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<string, unknown>,
|
||||
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<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.status(p, this.profile(q, p)));
|
||||
}
|
||||
@Get('usage') usage(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
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<string, unknown>,
|
||||
) {
|
||||
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<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.crons(p, this.profile(q, p)));
|
||||
}
|
||||
@Get('model-info') modelInfo(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.modelInfo(p, this.profile(q, p)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { open, lstat } from 'node:fs/promises';
|
||||
import { constants } from 'node:fs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
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<string> {
|
||||
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');
|
||||
}
|
||||
}
|
||||
/** Exact fixed mount roots only; startup fails closed, never browser/JWT grants. */
|
||||
export async function loadRuntimeGrants(
|
||||
path = '/run/secrets/hermes_runtime_grants',
|
||||
reviewedRoot?: string,
|
||||
): Promise<Readonly<Record<string, readonly string[]>>> {
|
||||
const roots = reviewedRoot
|
||||
? [resolve(reviewedRoot)]
|
||||
: ['/run/secrets', '/etc/canina'];
|
||||
if (!roots.includes(dirname(path)) || resolve(path) !== path)
|
||||
throw new HermesError('CONFIGURATION');
|
||||
try {
|
||||
const stat = await lstat(path);
|
||||
if (
|
||||
!stat.isFile() ||
|
||||
stat.isSymbolicLink() ||
|
||||
stat.size > 65536 ||
|
||||
stat.mode & 0o022
|
||||
)
|
||||
throw new Error();
|
||||
const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
||||
try {
|
||||
const data: unknown = JSON.parse(await file.readFile('utf8'));
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data))
|
||||
throw new Error();
|
||||
new RuntimeReadAccess(data as Record<string, readonly string[]>);
|
||||
return data as Record<string, readonly string[]>;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
} catch {
|
||||
throw new HermesError('CONFIGURATION');
|
||||
}
|
||||
}
|
||||
@Module({})
|
||||
export class RuntimeReadModule {
|
||||
static async registerFromFile(
|
||||
path = '/run/secrets/hermes_runtime_grants',
|
||||
): Promise<DynamicModule> {
|
||||
return this.register(await loadRuntimeGrants(path));
|
||||
}
|
||||
static register(
|
||||
grants: Readonly<Record<string, readonly string[]>>,
|
||||
): 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 }],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
import { HermesAdapter } from './hermes.adapter';
|
||||
const provenance = {
|
||||
source: 'snapshot',
|
||||
capturedAt: '2020-01-01T00:00:00Z',
|
||||
ageSeconds: 100,
|
||||
freshness: 'stale',
|
||||
liveProcess: 'unknown',
|
||||
};
|
||||
function adapter(data: unknown) {
|
||||
return new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes-inventory:9119',
|
||||
token: () => Promise.resolve('test'),
|
||||
},
|
||||
jest.fn(() =>
|
||||
Promise.resolve(new Response(JSON.stringify(data), { status: 200 })),
|
||||
) as typeof fetch,
|
||||
);
|
||||
}
|
||||
describe('snapshot provenance boundary', () => {
|
||||
it('rejects missing provenance from dedicated inventory origin', async () => {
|
||||
await expect(
|
||||
adapter({
|
||||
ok: true,
|
||||
version: '1',
|
||||
commit: 'x',
|
||||
auth_required: true,
|
||||
}).health(),
|
||||
).rejects.toMatchObject({ code: 'CONTRACT' });
|
||||
});
|
||||
it('cannot present copied gateway scalars as live telemetry', async () => {
|
||||
const row = await adapter({
|
||||
version: '1',
|
||||
gateway_running: true,
|
||||
active_agents: 12,
|
||||
gateway_state: 'running',
|
||||
overall: 'ok',
|
||||
provenance,
|
||||
}).status('default');
|
||||
expect(row).toMatchObject({
|
||||
gateway_running: false,
|
||||
active_agents: 0,
|
||||
gateway_state: null,
|
||||
overall: 'unknown',
|
||||
liveGatewayState: 'unknown',
|
||||
liveActiveAgents: null,
|
||||
provenance,
|
||||
});
|
||||
});
|
||||
it('profile readiness cannot be inferred from copied PID state', async () => {
|
||||
const rows = await adapter({
|
||||
profiles: [
|
||||
{
|
||||
name: 'default',
|
||||
is_default: true,
|
||||
gateway_running: true,
|
||||
provenance,
|
||||
},
|
||||
],
|
||||
provenance,
|
||||
}).profiles();
|
||||
expect(rows[0]).toMatchObject({
|
||||
gateway_running: false,
|
||||
liveGatewayState: 'unknown',
|
||||
provenance,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
**Superseded wiring/provenance: see [snapshot repair](hermes-snapshot-provenance.md). Do not use literal grants or the old DNS alias below.**
|
||||
|
||||
# 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({ '<verified-user-id>': ['default', '<department>'] })`
|
||||
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.
|
||||
@@ -0,0 +1,86 @@
|
||||
# Snapshot provenance repair — integration contract
|
||||
|
||||
This is independent snapshot inventory, **not a live Hermes executor**. No Production,
|
||||
deployment, live gateway edits, or RELEASE_AUTHORIZE execution is included.
|
||||
|
||||
## Parent-owned application wiring
|
||||
|
||||
Keep AuthModule/JwtStrategy. Replace literal user grants in parent-owned app.module.ts with:
|
||||
|
||||
```ts
|
||||
RuntimeReadModule.registerFromFile('/run/secrets/hermes_runtime_grants')
|
||||
```
|
||||
|
||||
Nest supports the resulting Promise<DynamicModule> in imports. Alternatively pass the
|
||||
reviewed nonsecret file `/etc/canina/runtime-grants.json`. No env/JWT/browser grant
|
||||
input is accepted by this registration. File is JSON object mapping exact verified DB
|
||||
user IDs to nonempty unique profile arrays; `{}` explicitly denies everyone. Missing,
|
||||
malformed, symlinked, group/world-writable, oversized files fail startup closed. Mount
|
||||
read-only and provision via reviewed configuration/secrets management; do not commit
|
||||
real user grants. Existing explicit register(grants) remains for tests/legacy consumers.
|
||||
Backend also mounts `/run/secrets/hermes_username` and `hermes_password` read-only.
|
||||
No app.module.ts modification belongs to this commit.
|
||||
|
||||
## Required coordinated origin change
|
||||
|
||||
Authenticated management module and broker now exclusively use
|
||||
`http://canina-hermes-inventory:9119`. compose.readonly.yml uses the distinct alias
|
||||
`canina-hermes-inventory`, NEVER `canina-hermes`. Generic adapter allowlist retains
|
||||
both reviewed exact origins for compatibility with separate existing consumers.
|
||||
No arbitrary origin/env URL accepted. Parent must cherry-pick broker/module/adapter
|
||||
and inventory compose together; do not add an alias to the existing gateway.
|
||||
No deployment performed. External network should still be internal and only contain
|
||||
backend/inventory; the distinct alias cannot shadow the live canina-hermes hostname.
|
||||
|
||||
## Snapshot exporter/operator gate
|
||||
|
||||
Every independent inventory root must include `snapshot-manifest.json`:
|
||||
|
||||
```json
|
||||
{"schemaVersion":1,"source":"snapshot","capturedAt":"2026-10-23T12:00:00Z"}
|
||||
```
|
||||
|
||||
The example is a schema example, NOT an actual capture attestation. Exporter must
|
||||
write the real capture instant from the approved consistent export. Never copy the
|
||||
example, use startup time, filesystem mtime, or claim this change captures/refreshes
|
||||
snapshots. Missing/invalid/future manifest prevents startup. It is revalidated per
|
||||
successful GET; invalid after startup returns503. Manifest is exporter metadata,
|
||||
not a cryptographic proof of consistency. Immutable reviewed export provisioning
|
||||
remains required. Fresh means age <=3600 seconds, otherwise stale; even fresh is
|
||||
NOT live readiness. No snapshot scheduler installed.
|
||||
|
||||
## Additive public response fields / panel handoff
|
||||
|
||||
All object read DTOs (health/status/usage/sessions/model-info) add:
|
||||
`provenance:{source:'snapshot',capturedAt:string,ageSeconds:number,freshness:'fresh'|'stale',liveProcess:'unknown'}`.
|
||||
Profile and schedule array shapes are preserved: provenance is on each row. Empty
|
||||
arrays have no rows to annotate; use the health response to display snapshot metadata.
|
||||
Profile rows add `liveGatewayState:'unknown'`; status adds the same plus
|
||||
`liveActiveAgents:null`. Existing required scalars remain typed but are conservative
|
||||
placeholders: `gateway_running:false`, `active_agents:0`, `gateway_state:null`,
|
||||
`overall:'unknown'`. False/zero MUST NOT render as measured offline/idle: when
|
||||
provenance.source is snapshot use the explicit unknown fields, label snapshot and
|
||||
show capturedAt/freshness. Health.ok only means inventory HTTP app responsiveness,
|
||||
never live gateway readiness. Profile existence is inventory availability, NOT
|
||||
process readiness. Adapter enforces provenance on dedicated snapshot origin and
|
||||
conservatively overwrites copied PID-derived status/profile scalars again.
|
||||
|
||||
Installed status handler was unsafe here: it probes gateway HTTP/PIDs/platforms and
|
||||
can stall despite this being an independent snapshot. Snapshot server replaces that
|
||||
route with measured package version and explicit unknown process fields; no status
|
||||
probe is run. Password auth/mutation denial/WebSocket denial remain intact.
|
||||
|
||||
## Executed acceptance
|
||||
|
||||
Remote disposable network-none exact pinned Hermes image, no original volumes:
|
||||
manifest-missing regression failed first; TCP login/profile/health/status provenance
|
||||
and stale capture assertions now pass; authenticated sockets denied1008. Disposable
|
||||
Node22 copy of source/dependencies: 9 hermes-management suites /34 tests pass and
|
||||
full backend tsc build passes. Adapter copied-live-state regression failed first;
|
||||
grants-loader missing-function regression failed first. Compiled actual Node broker
|
||||
-> TCP pinned server -> adapter -> policy-filtered profiles/status passes, deny reads
|
||||
zero credentials. Test timestamps/credentials are fixtures, not live telemetry.
|
||||
The first HTTP retry raced fixture startup and returned UNAVAILABLE; retry after
|
||||
server startup passed. No claim of full app/user-flow or live runtime integration.
|
||||
|
||||
Full non-DB Jest regression: 108 suites/854 tests passed; 1 suite/18 tests skipped. No RELEASE_AUTHORIZE suite was enabled.
|
||||
@@ -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
|
||||
@@ -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.
|
||||
@@ -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-inventory]
|
||||
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
|
||||
@@ -0,0 +1,11 @@
|
||||
# Disposable acceptance fixture only; not production entrypoint.
|
||||
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
|
||||
uvicorn.run(m.build_app(),host='0.0.0.0',port=9119,lifespan='off',access_log=False,log_level='warning')
|
||||
@@ -0,0 +1,94 @@
|
||||
"""Dedicated read-inventory process: never starts cron, gateway, rooms, MCP or PTY."""
|
||||
from pathlib import Path
|
||||
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
|
||||
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 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'
|
||||
# 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)
|
||||
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':
|
||||
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')
|
||||
@@ -0,0 +1,21 @@
|
||||
// Execute inside disposable compiled backend runner sharing fixture network namespace.
|
||||
const assert=require('node:assert/strict');
|
||||
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-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;});
|
||||
@@ -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()
|
||||
@@ -0,0 +1,65 @@
|
||||
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]
|
||||
# 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'))
|
||||
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
|
||||
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
|
||||
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()
|
||||
@@ -0,0 +1,21 @@
|
||||
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)
|
||||
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')
|
||||
Reference in new issue
Block a user