fix(orchestration): provision explicit Stage owner Gate1 review access
Deploy Canina / deploy (push) Successful in 4m40s
Deploy Canina / deploy (push) Successful in 4m40s
Preserve independent proposer checks, canonical Admin UUID allowlist and deny ordinary admins. Revalidate pinned owner in read-only CI preflight; expose server-advertised card decisions and distinguish reservation from unknown real cost. Verified backend 42 HTTP/service/config tests and build; admin 18 relevant tests, app tsc and Vite build; 3 CI config tests and bash syntax.
This commit is contained in:
1 parent
bfb1841370
commit
3e78b89cc1
11 files changed
+172
-11
No files matched your search
@@ -2,6 +2,7 @@ import {
|
||||
PrepareWorkflowService,
|
||||
PREPARE_WORKFLOW_CONFIG,
|
||||
} from './prepare-workflow.service';
|
||||
import { workflowConfig } from './workflow-config';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { KillSwitchService } from './kill-switch.service';
|
||||
@@ -18,7 +19,7 @@ import { OrchestrationService } from './orchestration.service';
|
||||
PrepareWorkflowService,
|
||||
{
|
||||
provide: PREPARE_WORKFLOW_CONFIG,
|
||||
useValue: { workerIds: [], stageCiIds: [] },
|
||||
useFactory: () => workflowConfig(process.env),
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
|
||||
@@ -6,7 +6,26 @@ describe('owner Gate1 hold operation', () => {
|
||||
beforeEach(()=>{
|
||||
task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'proposer'}};
|
||||
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>task),update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
|
||||
service=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)}, {workerIds:[],stageCiIds:[]});
|
||||
service=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)}, {workerIds:[],stageCiIds:[],ownerReviewerIds:['12345678-1234-1234-1234-123456789012']} as any);
|
||||
});
|
||||
it('advertises and permits the pinned canonical Admin independent reviewer only',async()=>{
|
||||
const owner={id:'12345678-1234-1234-1234-123456789012',role:'Admin'};
|
||||
expect(service.project(task,owner).availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
|
||||
await expect(service.transition('t',hold,owner)).resolves.toMatchObject({state:'GATE1_HELD'});
|
||||
await expect(service.decide('t',1,{decision:'APPROVED',expectedVersion:3,idempotencyKey:'owner-approve',expiresAt:new Date(Date.now()+60000).toISOString()},owner)).resolves.toMatchObject({state:'GATE1_APPROVED'});
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner.id})});
|
||||
});
|
||||
it.each(['Admin','SUPER_ADMIN'])('never permits proposer self-review even if allowlisted: %s',async role=>{
|
||||
const actor={id:'12345678-1234-1234-1234-123456789012',role}; task.metadata.createdBy=actor.id;
|
||||
expect(service.project(task,actor).availableActions).toEqual([]);
|
||||
await expect(service.transition('t',hold,actor)).rejects.toMatchObject({status:403});
|
||||
await expect(service.decide('t',1,{decision:'REJECTED',expectedVersion:2,idempotencyKey:'self',expiresAt:new Date(Date.now()+60000).toISOString()},actor)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each([{id:'other',role:'Admin'},{id:'12345678-1234-1234-1234-123456789012',role:'User'},{id:'12345678-1234-1234-1234-123456789012',role:'ADMIN'}])('denies noncanonical or ungranted reviewer %j',async actor=>{
|
||||
expect(service.project(task,actor).availableActions).toEqual([]);
|
||||
await expect(service.decide('t',1,{decision:'REJECTED',expectedVersion:2,idempotencyKey:'deny',expiresAt:new Date(Date.now()+60000).toISOString()},actor)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
const hold={toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold-t',reason:'Need revised budget'};
|
||||
it('advertises hold only to independent owner reviewer',()=>{
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
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 {PrepareWorkflowService} from './prepare-workflow.service';
|
||||
import {JwtAuthGuard} from '../auth/jwt-auth.guard';
|
||||
import {RolesGuard} from '../common/guards/roles.guard';
|
||||
describe('configured owner real workflow HTTP authorization',()=>{
|
||||
const owner='12345678-1234-1234-1234-123456789012';
|
||||
let app:INestApplication,task:any,tx:any;
|
||||
beforeAll(async()=>{
|
||||
tx={$queryRaw:jest.fn(async()=>[]),orchestrationTask:{findUnique:async()=>task,update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
|
||||
const workflow=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)},{workerIds:[],stageCiIds:[],ownerReviewerIds:[owner]});
|
||||
const m=await Test.createTestingModule({controllers:[OrchestrationController],providers:[{provide:OrchestrationService,useValue:{findAll:async()=>[task],findOne:async()=>task}},{provide:PrepareWorkflowService,useValue:workflow},RolesGuard]})
|
||||
.overrideGuard(JwtAuthGuard).useValue({canActivate(c:any){const r=c.switchToHttp().getRequest();if(!r.headers.authorization)return false;r.user={sub:r.headers.authorization,role:'ADMIN'};return true;}}).compile();
|
||||
app=m.createNestApplication();await app.init();
|
||||
});
|
||||
beforeEach(()=>{jest.clearAllMocks();task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo'}};});
|
||||
afterAll(async()=>{await app.close();});
|
||||
it.each(['APPROVED','REJECTED'])('advertises and executes %s only for verified granted JWT subject',async decision=>{
|
||||
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',owner).expect(200);
|
||||
expect(r.body[0].availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString(),notes:'Delegated system testing by independent owner'}).expect(201);
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,decision})});
|
||||
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,payload:expect.objectContaining({fingerprint:expect.stringContaining('Delegated system testing')})})});
|
||||
});
|
||||
it('holds via canonicalized ADMIN without granting proposer powers',async()=>{
|
||||
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',owner).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Independent owner needs evidence'}).expect(200);
|
||||
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each(['ordinary-admin','pmo'])('denies ungranted subject %s and body impersonation',async subject=>{
|
||||
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',subject).expect(200);
|
||||
expect(r.body[0].availableActions).toEqual([]);
|
||||
const body={decision:'APPROVED',expectedVersion:2,idempotencyKey:'deny',expiresAt:new Date(Date.now()+60000).toISOString()};
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send(body).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send({...body,actorId:owner}).expect(400);
|
||||
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',subject).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'hold'}).expect(403);
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it('denies grant holder self review, Gate2 and release',async()=>{
|
||||
task.metadata.createdBy=owner;
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision:'APPROVED',expectedVersion:2,idempotencyKey:'self',expiresAt:new Date(Date.now()+60000).toISOString()}).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/2/decide').set('Authorization',owner).send({}).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/commands/RELEASE_AUTHORIZE').set('Authorization',owner).send({}).expect(403);
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -14,6 +14,7 @@ export interface WorkflowIdentity {
|
||||
role: string;
|
||||
}
|
||||
export interface WorkflowConfig {
|
||||
ownerReviewerIds?: string[];
|
||||
workerIds: string[];
|
||||
stageCiIds: string[];
|
||||
}
|
||||
@@ -34,6 +35,11 @@ export class PrepareWorkflowService {
|
||||
)
|
||||
throw new ForbiddenException('Authenticated workflow identity required');
|
||||
}
|
||||
private reviewer(a: WorkflowIdentity) {
|
||||
return typeof a?.id === 'string' && !!a.id && a.id.length <= 100 &&
|
||||
(a.role === 'SUPER_ADMIN' ||
|
||||
(a.role === 'Admin' && (this.config.ownerReviewerIds ?? []).includes(a.id)));
|
||||
}
|
||||
private worker(a: WorkflowIdentity) {
|
||||
this.identity(a, ['WORKFLOW_WORKER']);
|
||||
if (!this.config.workerIds.includes(a.id))
|
||||
@@ -199,7 +205,7 @@ export class PrepareWorkflowService {
|
||||
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
|
||||
this.version(t, dto);
|
||||
if (dto.toState === 'GATE1_HELD') {
|
||||
if (a.role !== 'SUPER_ADMIN' || t.metadata.createdBy === a.id || t.state !== 'GATE1_PENDING')
|
||||
if (!this.reviewer(a) || t.metadata.createdBy === a.id || t.state !== 'GATE1_PENDING')
|
||||
throw new ForbiddenException('Independent Gate1 reviewer and pending proposal required');
|
||||
if (typeof dto.reason !== 'string' || !dto.reason.trim() || dto.reason.length > 2000)
|
||||
throw new BadRequestException('Hold requires reason of 1..2000 characters');
|
||||
@@ -237,7 +243,8 @@ export class PrepareWorkflowService {
|
||||
});
|
||||
}
|
||||
async decide(id: string, gate: number, dto: any, a: WorkflowIdentity) {
|
||||
this.identity(a, ['SUPER_ADMIN']);
|
||||
if (!this.reviewer(a))
|
||||
throw new ForbiddenException('Provisioned independent Gate1 reviewer required');
|
||||
if (gate !== 1)
|
||||
throw new ForbiddenException(
|
||||
'Gate2 decisions and release remain disabled',
|
||||
@@ -543,7 +550,7 @@ export class PrepareWorkflowService {
|
||||
actions.push('QUEUE_PREPARE');
|
||||
if (
|
||||
safe &&
|
||||
a?.role === 'SUPER_ADMIN' &&
|
||||
this.reviewer(a) &&
|
||||
t.metadata?.createdBy !== a.id &&
|
||||
['GATE1_PENDING', 'GATE1_HELD'].includes(t.state)
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { workflowConfig } from './workflow-config';
|
||||
describe('startup owner reviewer configuration',()=>{
|
||||
it('defaults denied, accepts explicit UUID only in staging',()=>{
|
||||
expect(workflowConfig({}).ownerReviewerIds).toEqual([]);
|
||||
expect(workflowConfig({NODE_ENV:'production',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:'12345678-1234-1234-1234-123456789012'})).toMatchObject({ownerReviewerIds:[]});
|
||||
expect(workflowConfig({CANINA_ENV:'staging',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:'12345678-1234-1234-1234-123456789012'}).ownerReviewerIds).toEqual(['12345678-1234-1234-1234-123456789012']);
|
||||
});
|
||||
it.each(['*','admin','12345678-1234-1234-1234-123456789012,'])('fails startup on malformed explicit grant %s',ids=>{
|
||||
expect(()=>workflowConfig({CANINA_ENV:'staging',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:ids})).toThrow('Invalid owner reviewer UUID allowlist');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { WorkflowConfig } from './prepare-workflow.service';
|
||||
/** Startup-only configuration; HTTP payloads can never supply reviewer grants. */
|
||||
export function workflowConfig(env: NodeJS.ProcessEnv): WorkflowConfig {
|
||||
const raw = env.CANINA_ENV === 'staging' ? env.CANINA_WORKFLOW_OWNER_REVIEWER_IDS ?? '' : '';
|
||||
const ids = raw ? raw.split(',') : [];
|
||||
if (ids.length > 20 || ids.some(id => !/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(id)))
|
||||
throw new Error('Invalid owner reviewer UUID allowlist');
|
||||
return {workerIds:[],stageCiIds:[],ownerReviewerIds:[...new Set(ids)]};
|
||||
}
|
||||
Reference in new issue
Block a user