feat(orchestration): integrate read-only v4 surfaces with mutation lockdown

This commit is contained in:
devops committed 2026-10-22 19:04:00 +00:00
1 parent 70d8128bfb
commit ac065fa911
8 files changed
+357 -17

No files matched your search

@@ -0,0 +1,151 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('integration read boundary', () => {
let app: INestApplication;
const task = {
id: 't1',
key: 'k',
title: 'Review',
department: 'SEO',
state: 'GATE2_APPROVED',
version: 1,
operationMode: 'PREPARE',
budgetLimitCents: 0,
metadata: { token: 'secret' },
runs: [
{
id: 'r',
status: 'VERIFIED',
commitSha: 'abc',
environment: 'stage',
artifacts: [
{
id: 'a',
sha256: 'f'.repeat(64),
sizeBytes: 1n,
uri: 'secret-uri',
metadata: { token: 'secret' },
},
],
},
],
events: [
{
id: 'e',
eventType: 'CREATED',
sequence: 1,
payload: { token: 'secret' },
},
],
};
const service = {
findAll: jest.fn(() => Promise.resolve([task])),
findOne: jest.fn(() => Promise.resolve(task)),
createTask: jest.fn(),
transition: jest.fn(),
decideGate: jest.fn(),
recordBudget: jest.fn(),
setKillSwitch: jest.fn(),
};
beforeAll(async () => {
const m = await Test.createTestingModule({
controllers: [OrchestrationController],
providers: [
{ provide: OrchestrationService, useValue: service },
RolesGuard,
],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(c: any) {
const req = c.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = { id: 'verified-user', role: req.headers.authorization };
return true;
},
})
.compile();
app = m.createNestApplication();
await app.init();
});
afterAll(async () => app.close());
it('denies every mutation before legacy core is invoked', async () => {
for (const [method, path] of [
['post', 'tasks'],
['put', 'tasks/t1/transition'],
['post', 'tasks/t1/gates/2/decide'],
['post', 'tasks/t1/budget'],
['put', 'kill-switch'],
['post', 'chat/messages'],
] as const) {
await request(app.getHttpServer())
[method]('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.send({})
.expect(403);
}
for (const name of [
'createTask',
'transition',
'decideGate',
'recordBudget',
'setKillSwitch',
] as const)
expect(service[name]).not.toHaveBeenCalled();
});
it('projects list and detail with no actions, arbitrary secrets or BigInt transport failures', async () => {
for (const path of ['tasks', 'tasks/t1']) {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(200);
const t = Array.isArray(r.body) ? r.body[0] : r.body;
expect(t.availableActions).toEqual([]);
expect(t.budgetLimitCents).toBe(0);
expect(JSON.stringify(t)).not.toMatch(/secret|sizeBytes|payload|token/);
}
});
it('returns actual inventory overview and explicit unavailable runtime resources', async () => {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.set('Authorization', 'Admin')
.expect(200);
expect(r.body.items).toEqual([
expect.objectContaining({ id: 'tasks', count: 1 }),
]);
expect(r.body.capabilities).toMatchObject({
mutations: false,
chat: false,
runtime: false,
});
for (const path of ['agents', 'schedules', 'model-policy'])
await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(503);
const releases = await request(app.getHttpServer())
.get('/admin/orchestration/releases')
.set('Authorization', 'Admin')
.expect(200);
expect(releases.body.items[0]).toMatchObject({
id: 'r',
commitSha: 'abc',
environment: 'stage',
});
});
it('protects read surfaces from anonymous and customer access', async () => {
await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.expect(403);
await request(app.getHttpServer())
.get('/admin/orchestration/tasks')
.set('Authorization', 'CUSTOMER')
.expect(403);
});
});
@@ -1,13 +1,147 @@
import { Body, Controller, Get, Param, Post, Put, Req, UseGuards } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { JwtAuthGuard } from "../auth/jwt-auth.guard"; import { RolesGuard } from "../common/guards/roles.guard"; import { Roles } from "../common/decorators/roles.decorator";
import { CreateOrchestrationTaskDto, GateDecisionDto, RecordBudgetDto, SetKillSwitchDto, TransitionTaskDto } from "./dto/orchestration.dto"; import { OrchestrationService } from "./orchestration.service";
@ApiTags("Admin - Orchestration v4") @ApiBearerAuth() @UseGuards(JwtAuthGuard,RolesGuard) @Roles("Admin","SUPER_ADMIN") @Controller("admin/orchestration")
export class OrchestrationController { constructor(private readonly service:OrchestrationService){} private actor(req:any){return String(req.user?.id??req.user?.sub??"SYSTEM")}
@Get("tasks") all(){return this.service.findAll()} @Get("tasks/:id") one(@Param("id")id:string){return this.service.findOne(id)}
@Post("tasks") create(@Body()dto:CreateOrchestrationTaskDto,@Req()req:any){return this.service.createTask(dto,this.actor(req))}
@Put("tasks/:id/transition") transition(@Param("id")id:string,@Body()dto:TransitionTaskDto,@Req()req:any){return this.service.transition(id,dto,this.actor(req))}
@Post("tasks/:id/gates/:gate/decide") decide(@Param("id")id:string,@Param("gate")gate:string,@Body()dto:GateDecisionDto,@Req()req:any){if(gate!=="1"&&gate!=="2")throw new Error("gate must be 1 or 2");return this.service.decideGate(id,Number(gate) as 1|2,dto,this.actor(req))}
@Post("tasks/:id/budget") budget(@Param("id")id:string,@Body()dto:RecordBudgetDto,@Req()req:any){return this.service.recordBudget(id,dto,this.actor(req))}
@Put("kill-switch") kill(@Body()dto:SetKillSwitchDto,@Req()req:any){return this.service.setKillSwitch(dto.level,dto.reason,this.actor(req))}
import {
Controller,
Get,
Param,
Post,
Put,
UseGuards,
ForbiddenException,
ServiceUnavailableException,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { OrchestrationService } from './orchestration.service';
// Explicit response allowlists: never serialize arbitrary JSON, signatures, URIs or BigInt.
function pick(row: any, keys: string[]) {
return Object.fromEntries(
keys.filter((k) => row[k] !== undefined).map((k) => [k, row[k]]),
);
}
function project(row: any) {
return {
...pick(row, [
'id',
'key',
'title',
'department',
'state',
'version',
'operationMode',
'budgetLimitCents',
]),
availableActions: [],
runs: (row.runs ?? []).map((run: any) => ({
...pick(run, ['id', 'commitSha', 'environment', 'status']),
artifacts: (run.artifacts ?? []).map((a: any) =>
pick(a, ['id', 'kind', 'sha256']),
),
})),
decisions: (row.decisions ?? []).map((d: any) =>
pick(d, [
'id',
'gate',
'decision',
'expiresAt',
'runId',
'artifactHash',
'commitSha',
'environment',
'createdAt',
]),
),
events: (row.events ?? []).map((e: any) =>
pick(e, [
'id',
'eventType',
'fromState',
'toState',
'sequence',
'createdAt',
]),
),
budgetLedger: (row.budgetLedger ?? []).map((b: any) =>
pick(b, ['id', 'amountCents', 'kind', 'createdAt']),
),
};
}
@ApiTags('Admin - Orchestration v4')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin', 'SUPER_ADMIN')
@Controller('admin/orchestration')
export class OrchestrationController {
constructor(private readonly service: OrchestrationService) {}
@Get('tasks') async all() {
return (await this.service.findAll()).map(project);
}
@Get('tasks/:id') async one(@Param('id') id: string) {
return project(await this.service.findOne(id));
}
@Get('overview') async overview() {
const tasks = await this.service.findAll();
return {
items: [
{ id: 'tasks', title: 'Persisted task inventory', count: tasks.length },
],
measuredAt: new Date().toISOString(),
capabilities: { mutations: false, chat: false, runtime: false },
reason: 'security_hardening_pending',
};
}
@Get('releases') async releases() {
const tasks = await this.service.findAll();
const details = await Promise.all(
tasks.map((t: any) => this.service.findOne(t.id)),
);
return {
items: details.flatMap((t: any) =>
(t.runs ?? []).map((run: any) =>
pick(run, ['id', 'status', 'commitSha', 'environment']),
),
),
measuredAt: new Date().toISOString(),
};
}
@Get('agents') agents() {
throw new ServiceUnavailableException(
'Hermes runtime unavailable: trusted identity and runtime authentication not configured',
);
}
@Get('schedules') schedules() {
throw new ServiceUnavailableException(
'Hermes schedules unavailable; runtime remains disabled',
);
}
@Get('model-policy') models() {
throw new ServiceUnavailableException(
'No persisted model policy read contract available',
);
}
// The committed legacy core is not safe to expose. No env switch or role may bypass this boundary.
@Post('tasks') create() {
return this.deny();
}
@Put('tasks/:id/transition') transition() {
return this.deny();
}
@Post('tasks/:id/gates/:gate/decide') decide() {
return this.deny();
}
@Post('tasks/:id/budget') budget() {
return this.deny();
}
@Put('kill-switch') kill() {
return this.deny();
}
@Post('chat/messages') chat() {
return this.deny();
}
private deny(): never {
throw new ForbiddenException(
'Orchestration mutations and chat disabled pending hardened core and trusted runtime integration',
);
}
}