fix(api): wire research credentials into server security (RP20)
This commit is contained in:
parent
fd90d17214
commit
7de497e9b0
5 changed files with 274 additions and 26 deletions
|
|
@ -3,7 +3,6 @@ import { readFileSync } from 'node:fs';
|
|||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { Pool } from 'pg';
|
||||
import { createHttpSecurity } from '@stock-bot/di';
|
||||
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
|
||||
import { PostgreSQLClient } from '@stock-bot/postgres';
|
||||
import type {
|
||||
|
|
@ -13,9 +12,9 @@ import type {
|
|||
import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/BacktestRunner';
|
||||
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
|
||||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createWebApiSecurity } from '../../apps/stock/web-api/src/http-security';
|
||||
import { createRoutes } from '../../apps/stock/web-api/src/routes/create-routes';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { createResearchAgentRoutes } from '../../apps/stock/web-api/src/routes/research-agents.routes';
|
||||
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { BacktestDispatcher } from '../../apps/stock/web-api/src/services/BacktestDispatcher';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
|
|
@ -206,18 +205,14 @@ async function totals(f: Fixture) {
|
|||
)
|
||||
).rows[0];
|
||||
}
|
||||
const operatorToken = 'operator-test-secret-12345678901234567890';
|
||||
function http(f: Fixture) {
|
||||
const app = new Hono();
|
||||
const security = createHttpSecurity({
|
||||
adminToken: 'operator-test-secret-12345678901234567890',
|
||||
const security = createWebApiSecurity(f.container, {
|
||||
adminToken: operatorToken,
|
||||
serviceToken: 'service-test-secret-123456789012345678901',
|
||||
environment: 'test',
|
||||
resolveResearchCredential: request => f.agents.authenticate(request),
|
||||
});
|
||||
security.mount(app);
|
||||
app.route('/', createResearchAgentRoutes(f.container, security.researchEnabled));
|
||||
app.route('/', createExperimentRoutes(f.container));
|
||||
app.route('/', createResearchRoutes(f.container));
|
||||
const app = createRoutes(f.container, security);
|
||||
return (path: string, token: string, body?: unknown, key?: string) =>
|
||||
app.request(path, {
|
||||
method: body === undefined ? 'GET' : 'POST',
|
||||
|
|
@ -308,10 +303,20 @@ describe.skipIf(!url)('authoritative research principals and budgets', () => {
|
|||
await expect(update('description=description')).rejects.toThrow('disabled');
|
||||
expect(await f.research.getProject(p.project.id)).toEqual(after);
|
||||
});
|
||||
test('operator provisioning stores only a hash; real mounted authentication, AI authorship and scoped writes', async () => {
|
||||
const f = await fixture(1),
|
||||
p = await provision(f);
|
||||
test('production security issues research credentials with scoped identity, durable budgets and revocation', async () => {
|
||||
const f = await fixture(1);
|
||||
await f.approve();
|
||||
const project = await f.research.createProject({ title: 'Agent research' }, 'operator');
|
||||
const request = http(f);
|
||||
const issued = await request('/api/v2/research-agents', operatorToken, {
|
||||
name: 'Bounded researcher',
|
||||
allowedProjectIds: [project.id],
|
||||
allowedBacktestIds: [f.draft.id],
|
||||
limits,
|
||||
});
|
||||
expect(issued.status).toBe(201);
|
||||
const created = (await issued.json()) as Awaited<ReturnType<ResearchAgentsService['create']>>;
|
||||
const p = { ...created, project };
|
||||
expect(p.token.startsWith('sb-research.')).toBe(true);
|
||||
const stored = (await f.pool.query('SELECT * FROM research_agents WHERE id=$1', [p.agent.id]))
|
||||
.rows[0];
|
||||
|
|
@ -372,6 +377,29 @@ describe.skipIf(!url)('authoritative research principals and budgets', () => {
|
|||
f.agents.update(p.agent.id, { expectedRevision: 1, allowedBacktestIds: [candidate.id] })
|
||||
).rejects.toThrow('approved canonical templates');
|
||||
expect((await totals(f)).runs).toBe(0);
|
||||
const admitted = await request(
|
||||
'/api/v2/experiments',
|
||||
p.token,
|
||||
await preparation(f),
|
||||
randomUUID()
|
||||
);
|
||||
expect(admitted.status).toBe(202);
|
||||
const beforeDenial = await totals(f);
|
||||
expect(beforeDenial.runs).toBe(limits.maxActiveTrials);
|
||||
const owners = await f.pool.query('SELECT DISTINCT research_agent_id FROM runs');
|
||||
expect(owners.rows).toEqual([{ research_agent_id: p.agent.id }]);
|
||||
const submitters = await f.pool.query(
|
||||
'SELECT DISTINCT submission_principal FROM research_experiments'
|
||||
);
|
||||
expect(submitters.rows).toEqual([{ submission_principal: p.agent.id }]);
|
||||
const exhausted = await request(
|
||||
'/api/v2/experiments',
|
||||
p.token,
|
||||
await preparation(f, 'Capacity exceeded', f.draft.id, { fastPeriod: [1] }),
|
||||
randomUUID()
|
||||
);
|
||||
expect(exhausted.status).toBe(429);
|
||||
expect(await totals(f)).toEqual(beforeDenial);
|
||||
await f.agents.update(p.agent.id, { expectedRevision: 1, enabled: false });
|
||||
expect((await request('/api/v2/research-agent/budget', p.token)).status).toBe(401);
|
||||
expect((await f.agents.budget(p.agent.id, true)).agent.enabled).toBe(false);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue