Files
canina/backend/src/orchestration-worker/worker-registration.spec.ts
T

85 lines
5.7 KiB
TypeScript

import { Test } from '@nestjs/testing';
import { writeFileSync, chmodSync, symlinkSync, mkdirSync, rmSync } from 'fs';
import { execFileSync } from 'child_process';
import { registerPrepareWorker } from './worker-registration';
import { loadPrepareWorkerConfig } from './worker-config';
import { linkSync } from 'fs';
describe('reviewed prepare-worker startup registration', () => {
const path = '/run/secrets/prepare_worker_grants';
const valid = () => ({ grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' });
it('loader itself rejects malformed SHA grants', () => {
mkdirSync('/run/secrets', { recursive: true });
const c = valid(); c.grants[0].credentialSha256 = 'secret-not-a-sha';
writeFileSync(path, JSON.stringify(c), { mode: 0o400 });
try { expect(() => loadPrepareWorkerConfig()).toThrow('Prepare worker startup configuration rejected'); }
finally { rmSync(path, { force: true }); }
});
it.each(['1', 'TRUE', 'yes', ' true', 'false', ''])('does not opt in for %s', value => {
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: value })).toEqual([]);
});
it.each(['missing', 'symlink', 'directory', 'fifo', 'oversize', 'group-write', 'world-write', 'hardlink'])('fails startup on unsafe file: %s', kind => {
mkdirSync('/run/secrets', { recursive: true });
const other = '/run/secrets/test-other';
try {
if (kind === 'fifo') execFileSync('mkfifo', [path]);
else if (kind === 'directory') mkdirSync(path);
else if (kind === 'symlink') symlinkSync(other, path);
else if (kind !== 'missing') {
writeFileSync(path, kind === 'oversize' ? ' '.repeat(16385) : JSON.stringify(valid()), { mode: 0o400 });
if (kind === 'group-write') chmodSync(path, 0o420);
if (kind === 'world-write') chmodSync(path, 0o402);
if (kind === 'hardlink') linkSync(path, other);
}
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow('Prepare worker startup configuration rejected');
} finally { rmSync(path, { recursive: true, force: true }); rmSync(other, { force: true }); }
});
it.each(['extra', 'raw-token', 'wrong-role', 'overlap', 'duplicate-sha', 'missing-grant', 'http', 'credentials', 'localhost', 'query', 'bad-json', 'null'])('rejects unreviewed JSON: %s without leaking contents or logging', kind => {
mkdirSync('/run/secrets', { recursive: true });
const c: any = valid();
if (kind === 'extra') c.artifactDirectory = '/user-selected';
if (kind === 'raw-token') c.grants[0].token = 'DO_NOT_LOG_THIS_SECRET';
if (kind === 'wrong-role') c.grants[0].role = 'Admin';
if (kind === 'overlap') c.stageCiIds = ['worker'];
if (kind === 'duplicate-sha') c.grants[1].credentialSha256 = c.grants[0].credentialSha256;
if (kind === 'missing-grant') c.workerIds.push('unbound');
if (kind === 'http') c.artifactBaseUrl = 'http://stage.example.com/internal/prepare-worker/artifacts';
if (kind === 'credentials') c.artifactBaseUrl = 'https://DO_NOT_LOG_THIS_SECRET@stage.example.com/internal/prepare-worker/artifacts';
if (kind === 'localhost') c.artifactBaseUrl = 'https://127.0.0.1/internal/prepare-worker/artifacts';
if (kind === 'query') c.artifactBaseUrl += '?secret=DO_NOT_LOG_THIS_SECRET';
writeFileSync(path, kind === 'bad-json' ? 'DO_NOT_LOG_THIS_SECRET' : kind === 'null' ? 'null' : JSON.stringify(c), { mode: 0o400 });
const logs = [jest.spyOn(console, 'log'), jest.spyOn(console, 'warn'), jest.spyOn(console, 'error')];
try {
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow(/^Prepare worker startup configuration rejected$/);
logs.forEach(log => expect(log).not.toHaveBeenCalled());
} finally { logs.forEach(log => log.mockRestore()); rmSync(path, { force: true }); }
});
it('defaults to no module/no config IO and exposes no worker routes', async () => {
expect(registerPrepareWorker({})).toEqual([]);
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'false' })).toEqual([]);
const module = await Test.createTestingModule({ imports: registerPrepareWorker({}) }).compile();
const app = module.createNestApplication();
await app.listen(0, '127.0.0.1');
try {
const response = await fetch((await app.getUrl()) + '/internal/prepare-worker/claim', { method: 'POST' });
expect(response.status).toBe(404);
} finally { await app.close(); }
});
it('loads immutable reviewed JSON into the real transport registration', async () => {
mkdirSync('/run/secrets', { recursive: true });
const file = '/run/secrets/prepare_worker_grants';
const config = { grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' };
writeFileSync(file, JSON.stringify(config), { mode: 0o400 });
try {
const [registered] = registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' });
expect(registered.module.name).toBe('WorkerTransportModule');
expect(registered.controllers?.length).toBe(1);
const snapshot = (registered.providers as any[]).find(p => p.useValue?.artifactDirectory)?.useValue;
expect(snapshot).toEqual({ ...config, enabled: true, artifactDirectory: '/var/lib/canina/prepare-artifacts' });
expect(Object.isFrozen(snapshot)).toBe(true);
expect(Object.isFrozen(snapshot.grants[0])).toBe(true);
} finally { rmSync(file, { force: true }); }
});
});