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>
265 lines
12 KiB
TypeScript
265 lines
12 KiB
TypeScript
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import { inspect } from 'node:util';
|
|
import { DeploymentSecretBroker, type DeploymentSecretScope } from '../packages/runtime/src/deployment-secrets.js';
|
|
|
|
const dirs: string[] = [];
|
|
afterEach(async () => {
|
|
await Promise.all(dirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })));
|
|
});
|
|
const fakeValue = 'fixture-private-db-password-$-with-newline\nlast-line';
|
|
const scope: DeploymentSecretScope = {
|
|
tenantId: 'tenant-a',
|
|
environmentId: 'environment-a',
|
|
releaseId: 'release-a',
|
|
secretVersionRefs: ['database@version-7'],
|
|
publicConfigKeys: ['PORT'],
|
|
};
|
|
async function fixture(
|
|
fetcher: typeof fetch = async () =>
|
|
Response.json({
|
|
data: {
|
|
data: { url: 'postgresql://fixture.invalid/database', password: fakeValue, unselected: 'not-exported' },
|
|
metadata: { version: 7, destroyed: false, deletion_time: '' },
|
|
},
|
|
}),
|
|
) {
|
|
const dir = await mkdtemp(join(tmpdir(), 'wishforge-deployment-secret-'));
|
|
dirs.push(dir);
|
|
const map = join(dir, 'map.json'),
|
|
token = join(dir, 'token');
|
|
const binding = {
|
|
tenantId: 'tenant-a',
|
|
environmentId: 'environment-a',
|
|
ref: 'database@version-7',
|
|
mount: 'applications',
|
|
path: 'tenant-a/environment-a/database',
|
|
version: 7,
|
|
fields: { DATABASE_URL: 'url', DATABASE_PASSWORD: 'password' },
|
|
};
|
|
await writeFile(map, JSON.stringify({ schemaVersion: 1, bindings: [binding] }));
|
|
await writeFile(token, 'fixture-deployment-vault-token-only', { mode: 0o600 });
|
|
const env = {
|
|
NODE_ENV: 'production',
|
|
WF_DEPLOYMENT_SECRET_MAP_FILE: map,
|
|
WF_DEPLOYMENT_VAULT_ORIGIN: 'https://vault.deployment.invalid',
|
|
WF_DEPLOYMENT_VAULT_TOKEN_FILE: token,
|
|
};
|
|
const broker = new DeploymentSecretBroker(env, fetcher),
|
|
plan = await broker.plan(scope);
|
|
return { broker, plan, scope: { ...scope, expectedPolicyDigest: plan.policyDigest }, binding, map, token, env };
|
|
}
|
|
describe('isolated versioned application SecretBroker', () => {
|
|
it('reads the exact authorized KV v2 version and exports only selected fields privately', async () => {
|
|
const calls: Array<{ url: string; init: RequestInit | undefined }> = [];
|
|
const f = await fixture(async (url, init) => {
|
|
calls.push({ url: String(url), init });
|
|
return Response.json({
|
|
data: {
|
|
data: { url: 'postgresql://fixture.invalid/database', password: fakeValue, unselected: 'do-not-export' },
|
|
metadata: { version: 7, destroyed: false, deletion_time: '' },
|
|
},
|
|
});
|
|
});
|
|
const prepared = await f.broker.prepare(f.scope);
|
|
try {
|
|
expect(calls).toHaveLength(1);
|
|
expect(calls[0].url).toBe(
|
|
'https://vault.deployment.invalid/v1/applications/data/tenant-a/environment-a/database?version=7',
|
|
);
|
|
expect(calls[0].init).toMatchObject({ method: 'GET', redirect: 'error' });
|
|
expect(new Headers(calls[0].init?.headers).get('X-Vault-Token')).toBe('fixture-deployment-vault-token-only');
|
|
expect(JSON.stringify(prepared)).not.toContain(fakeValue);
|
|
expect(inspect(prepared)).not.toContain(fakeValue);
|
|
expect(JSON.stringify(prepared.public)).not.toContain('tenant-a/environment-a/database');
|
|
expect(Object.isFrozen(prepared.public)).toBe(true);
|
|
await prepared.material.withValues(values => {
|
|
expect(values.DATABASE_PASSWORD).toBe(fakeValue);
|
|
expect(Object.keys(values)).toEqual(['DATABASE_URL', 'DATABASE_PASSWORD']);
|
|
expect(values).not.toHaveProperty('unselected');
|
|
});
|
|
} finally {
|
|
prepared.material.dispose();
|
|
}
|
|
await expect(prepared.material.withValues(() => undefined)).rejects.toMatchObject({
|
|
code: 'DEPLOYMENT_SECRET_MATERIAL_DISPOSED',
|
|
});
|
|
});
|
|
it('prepares public Kubernetes/Compose references without persisting material in rendered output', async () => {
|
|
const f = await fixture(),
|
|
prepared = await f.broker.prepare(f.scope);
|
|
try {
|
|
expect(prepared.public.kubernetesEnv).toContainEqual({
|
|
name: 'DATABASE_PASSWORD',
|
|
valueFrom: { secretKeyRef: { name: prepared.public.secretName, key: 'DATABASE_PASSWORD' } },
|
|
});
|
|
expect(prepared.public.composeEnvironment.DATABASE_PASSWORD).toMatch(/^\$\{WF_DEPLOY_APP_SECRET_\d:\?/);
|
|
let retained: Record<string, string> | undefined;
|
|
await prepared.material.withKubernetesSecret('tenant-a', secret => {
|
|
expect(secret.immutable).toBe(true);
|
|
expect(secret.metadata.name).toBe(prepared.public.secretName);
|
|
expect(Buffer.from(secret.data.DATABASE_PASSWORD, 'base64').toString()).toBe(fakeValue);
|
|
retained = secret.data;
|
|
});
|
|
expect(retained).toEqual({});
|
|
await prepared.material.withComposeEnvironment(environment => {
|
|
const placeholder = prepared.public.composeEnvironment.DATABASE_PASSWORD.match(/^\$\{([^:]+)/)![1];
|
|
expect(environment[placeholder]).toBe(fakeValue);
|
|
expect(environment).not.toHaveProperty('DATABASE_PASSWORD');
|
|
expect(environment).not.toHaveProperty('WF_DEPLOYMENT_VAULT_TOKEN_FILE');
|
|
});
|
|
} finally {
|
|
prepared.material.dispose();
|
|
}
|
|
});
|
|
it('requires approved policy before fetching and rejects mapping drift without a latest-version fallback', async () => {
|
|
let calls = 0;
|
|
const f = await fixture(async () => {
|
|
calls++;
|
|
throw new Error('Should not fetch');
|
|
});
|
|
await expect(f.broker.prepare(scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_POLICY_REQUIRED' });
|
|
await writeFile(
|
|
f.map,
|
|
JSON.stringify({ schemaVersion: 1, bindings: [{ ...f.binding, path: 'tenant-a/environment-a/replaced' }] }),
|
|
);
|
|
await expect(f.broker.prepare(f.scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_POLICY_CHANGED' });
|
|
expect(calls).toBe(0);
|
|
});
|
|
it('rejects cross-tenant/environment references, key collisions, privilege keys and caller URLs', async () => {
|
|
const f = await fixture();
|
|
for (const changed of [
|
|
{ tenantId: 'tenant-b' },
|
|
{ environmentId: 'environment-b' },
|
|
{ secretVersionRefs: ['other@version-7'] },
|
|
])
|
|
await expect(f.broker.plan({ ...scope, ...changed })).rejects.toMatchObject({
|
|
code: 'DEPLOYMENT_SECRET_SCOPE_DENIED',
|
|
});
|
|
await expect(f.broker.plan({ ...scope, publicConfigKeys: ['DATABASE_URL'] })).rejects.toMatchObject({
|
|
code: 'DEPLOYMENT_SECRET_KEY_CONFLICT',
|
|
});
|
|
await expect(
|
|
f.broker.plan({ ...scope, secretVersionRefs: ['database@version-7', 'database@version-7'] }),
|
|
).rejects.toMatchObject({ code: 'INVALID_SECRET_REFERENCE' });
|
|
await expect(
|
|
f.broker.plan({ ...scope, url: 'https://attacker.invalid' } as DeploymentSecretScope),
|
|
).rejects.toMatchObject({ code: 'INVALID_DEPLOYMENT_SECRET_SCOPE' });
|
|
await writeFile(
|
|
f.map,
|
|
JSON.stringify({ schemaVersion: 1, bindings: [{ ...f.binding, fields: { NODE_OPTIONS: 'password' } }] }),
|
|
);
|
|
await expect(f.broker.plan(scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_CONFIGURATION_INVALID' });
|
|
});
|
|
it('never loads BYOK transit credentials or permits production HTTP or traversal', async () => {
|
|
const f = await fixture();
|
|
await expect(
|
|
new DeploymentSecretBroker({
|
|
...f.env,
|
|
WF_DEPLOYMENT_VAULT_TOKEN_FILE: undefined,
|
|
VAULT_TOKEN: 'provider-key-must-not-be-used',
|
|
VAULT_ADDR: 'https://provider-vault.invalid',
|
|
}).prepare(f.scope),
|
|
).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRETS_UNCONFIGURED' });
|
|
await expect(
|
|
new DeploymentSecretBroker({
|
|
...f.env,
|
|
WF_DEPLOYMENT_VAULT_ORIGIN: 'http://127.0.0.1:8200',
|
|
WF_DEPLOYMENT_VAULT_ALLOW_LOOPBACK_HTTP: 'true',
|
|
}).plan(scope),
|
|
).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_CONFIGURATION_INVALID' });
|
|
await writeFile(
|
|
f.map,
|
|
JSON.stringify({ schemaVersion: 1, bindings: [{ ...f.binding, path: 'tenant-a/../other-tenant' }] }),
|
|
);
|
|
await expect(f.broker.plan(scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_CONFIGURATION_INVALID' });
|
|
});
|
|
it('rejects deleted, destroyed, missing, mismatched and oversized versions with redacted errors', async () => {
|
|
for (const metadata of [
|
|
{ version: 8, destroyed: false, deletion_time: '' },
|
|
{ version: 7, destroyed: true, deletion_time: '' },
|
|
{ version: 7, destroyed: false, deletion_time: '2026-01-01T00:00:00Z' },
|
|
]) {
|
|
const f = await fixture(async () =>
|
|
Response.json({ data: { data: { password: fakeValue, url: 'fixture' }, metadata } }),
|
|
);
|
|
await expect(f.broker.prepare(f.scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_VERSION_MISMATCH' });
|
|
}
|
|
const missing = await fixture(async () => new Response(fakeValue, { status: 404 }));
|
|
await expect(missing.broker.prepare(missing.scope)).rejects.toMatchObject({
|
|
code: 'DEPLOYMENT_SECRET_VERSION_UNAVAILABLE',
|
|
});
|
|
const huge = await fixture(async () => new Response(fakeValue + 'x'.repeat(262145)));
|
|
await expect(huge.broker.prepare(huge.scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_RESPONSE_LIMIT' });
|
|
const invalid = await fixture(async () =>
|
|
Response.json({
|
|
data: {
|
|
data: { password: 'x'.repeat(16385), url: 'fixture' },
|
|
metadata: { version: 7, destroyed: false, deletion_time: '' },
|
|
},
|
|
}),
|
|
);
|
|
await expect(invalid.broker.prepare(invalid.scope)).rejects.toMatchObject({
|
|
code: 'DEPLOYMENT_SECRET_VALUE_INVALID',
|
|
});
|
|
});
|
|
it('suppresses secret-bearing transport and consumer errors', async () => {
|
|
const unavailable = await fixture(async () => {
|
|
throw new Error(fakeValue);
|
|
});
|
|
try {
|
|
await unavailable.broker.prepare(unavailable.scope);
|
|
throw new Error('Expected failure');
|
|
} catch (error) {
|
|
expect(String(error)).not.toContain(fakeValue);
|
|
}
|
|
const f = await fixture(),
|
|
prepared = await f.broker.prepare(f.scope);
|
|
try {
|
|
for (const operation of [
|
|
() =>
|
|
prepared.material.withValues(values => {
|
|
throw new Error(values.DATABASE_PASSWORD);
|
|
}),
|
|
() =>
|
|
prepared.material.withKubernetesSecret('tenant-a', secret => {
|
|
throw new Error(JSON.stringify(secret));
|
|
}),
|
|
]) {
|
|
await expect(operation()).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_USE_FAILED' });
|
|
try {
|
|
await operation();
|
|
} catch (error) {
|
|
expect(String(error)).not.toContain(fakeValue);
|
|
expect(String(error)).not.toContain(Buffer.from(fakeValue).toString('base64'));
|
|
}
|
|
}
|
|
} finally {
|
|
prepared.material.dispose();
|
|
}
|
|
});
|
|
it('rechecks the mapping after Vault I/O and does not fetch for releases without secrets', async () => {
|
|
let map = '',
|
|
binding: unknown;
|
|
const f = await fixture(async () => {
|
|
await writeFile(map, JSON.stringify({ schemaVersion: 1, bindings: [{ ...(binding as object), version: 8 }] }));
|
|
return Response.json({
|
|
data: {
|
|
data: { password: fakeValue, url: 'fixture' },
|
|
metadata: { version: 7, destroyed: false, deletion_time: '' },
|
|
},
|
|
});
|
|
});
|
|
map = f.map;
|
|
binding = f.binding;
|
|
await expect(f.broker.prepare(f.scope)).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_POLICY_CHANGED' });
|
|
const empty = await new DeploymentSecretBroker({}, async () => {
|
|
throw new Error('No network permitted');
|
|
}).prepare({ ...scope, secretVersionRefs: [] });
|
|
expect(empty.public.environmentKeys).toEqual([]);
|
|
empty.material.dispose();
|
|
});
|
|
});
|