wishforge-ide/tests/integration/deployment-secrets.test.ts
eust-w 0ef99b1e6d style: add Prettier and EditorConfig, format the codebase
Adds prettier 3.9.8 (exact) with a shared config (single quotes,
120 columns, trailing commas), .prettierignore, .editorconfig and the
`format` / `format:check` scripts, then formats all TypeScript,
JavaScript, CSS, JSON and YAML sources.

Purely mechanical: no behaviour change. Typecheck passes, the vitest
suite and the 74 IDE node tests produce the same results as before
(socket-binding tests still cannot run inside this sandbox).
Previously the longest source line was ~3000 characters; reviewing the
following semantic changes now produces readable diffs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:32:43 +08:00

109 lines
3.9 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import http from 'node:http';
import { once } from 'node:events';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { DeploymentSecretBroker } from '../../packages/runtime/src/deployment-secrets.js';
describe.skipIf(process.env.WF_INTEGRATION !== '1')('loopback Vault KV v2 protocol fixture, no real Vault', () => {
let directory: string,
server: http.Server,
origin: string,
redirect = false,
requests = 0,
unexpected = 0;
const token = 'fixture-local-deployment-vault-token',
value = 'fixture-only-application-password';
const scope = {
tenantId: 'fixture-tenant',
environmentId: 'fixture-env',
releaseId: 'fixture-release',
secretVersionRefs: ['db@version-3'],
};
beforeAll(async () => {
directory = await mkdtemp(join(tmpdir(), 'wishforge-kv-fixture-'));
await writeFile(join(directory, 'token'), token, { mode: 0o600 });
await writeFile(
join(directory, 'map.json'),
JSON.stringify({
schemaVersion: 1,
bindings: [
{
tenantId: scope.tenantId,
environmentId: scope.environmentId,
ref: 'db@version-3',
mount: 'apps',
path: 'fixture-tenant/fixture-env/database',
version: 3,
fields: { DATABASE_PASSWORD: 'password' },
},
],
}),
);
server = http.createServer((req, res) => {
requests++;
if (
req.url !== '/v1/apps/data/fixture-tenant/fixture-env/database?version=3' ||
req.method !== 'GET' ||
req.headers['x-vault-token'] !== token
) {
unexpected++;
res.writeHead(403);
res.end();
return;
}
if (redirect) {
res.writeHead(302, { location: '/must-never-follow' });
res.end();
return;
}
res.setHeader('content-type', 'application/json');
res.end(
JSON.stringify({
data: { data: { password: value }, metadata: { version: 3, destroyed: false, deletion_time: '' } },
}),
);
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
origin = `http://127.0.0.1:${(server.address() as { port: number }).port}`;
});
afterAll(async () => {
server?.closeAllConnections();
if (server?.listening) await new Promise<void>(resolve => server.close(() => resolve()));
if (directory) await rm(directory, { recursive: true, force: true });
});
const broker = () =>
new DeploymentSecretBroker({
NODE_ENV: 'test',
WF_DEPLOYMENT_SECRET_MAP_FILE: join(directory, 'map.json'),
WF_DEPLOYMENT_VAULT_ORIGIN: origin,
WF_DEPLOYMENT_VAULT_TOKEN_FILE: join(directory, 'token'),
WF_DEPLOYMENT_VAULT_ALLOW_LOOPBACK_HTTP: 'true',
});
it('sends a fixed version request over real HTTP and keeps material out of public serialization', async () => {
const current = broker(),
plan = await current.plan(scope);
const prepared = await current.prepare({ ...scope, expectedPolicyDigest: plan.policyDigest });
try {
await prepared.material.withValues(values => expect(values.DATABASE_PASSWORD).toBe(value));
expect(JSON.stringify(prepared)).not.toContain(value);
expect(JSON.stringify(prepared)).not.toContain(token);
expect(requests).toBe(1);
expect(unexpected).toBe(0);
} finally {
prepared.material.dispose();
}
});
it('does not follow an upstream redirect or leak the Vault token to its new path', async () => {
redirect = true;
const current = broker(),
plan = await current.plan(scope);
await expect(current.prepare({ ...scope, expectedPolicyDigest: plan.policyDigest })).rejects.toMatchObject({
code: 'DEPLOYMENT_VAULT_UNAVAILABLE',
});
expect(unexpected).toBe(0);
});
});