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>) { if ( !grants || typeof grants !== 'object' || Array.isArray(grants) || Object.keys(grants).length > 1000 ) throw new Error('Invalid runtime access configuration'); const copy: Record = 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 { 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))); } }