wishforge-ide/infra/scripts/compose-acceptance.mjs
eust-w ee2fbe9f6a fix: match the Kubernetes baseline to the provisioner and drop stale settings
The base manifests described a design the code does not have: a Role that only
allowed Jobs, an unused sandbox account, a tool gateway that does not exist and
a deployer bound into the sandbox namespace. They now grant exactly what the
provisioner applies and deletes for runs and IDE workspaces, let sandbox pods
reach the API, Temporal and Git through the control namespace's default deny,
and let the IDE gateway reach workspace pods.

- The API reads the runtime image from WF_RUN_WORKER_IMAGE only, the same name
  the provisioner uses; the WF_RUN_IMAGE alias is gone.
- The provisioner takes the public gateway origin from WF_IDE_PUBLIC_URL, which
  is what the gateway and API call it, and refuses to start without an explicit
  WF_DEPLOYMENT_MODE.
- Environments no longer accept a runnerId that was always rejected.
- The Compose acceptance script uses the current Docker context unless
  WF_DOCKER_CONTEXT names one, instead of a hardcoded colima.
- Tests cover waiting reasons and their exits, and the production rejection of
  unsandboxed or unstated verification isolation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:01:28 +08:00

319 lines
13 KiB
JavaScript

/** Explicit local acceptance only: node --import tsx infra/scripts/compose-acceptance.mjs */
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { mkdtemp, mkdir, writeFile, readFile, rm, symlink } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { createHash } from 'node:crypto';
import http from 'node:http';
import { once } from 'node:events';
import { ComposeDeploymentAdapter, DeploymentSecretBroker, executeCommand } from '../../packages/runtime/src/index.ts';
if (process.env.WF_COMPOSE_ACCEPTANCE !== 'true' || !process.env.WF_CADDY_BINARY)
throw new Error('Set WF_COMPOSE_ACCEPTANCE=true and WF_CADDY_BINARY to a verified local Caddy binary');
// Uses the current Docker context unless WF_DOCKER_CONTEXT names another one.
const dockerContext = process.env.WF_DOCKER_CONTEXT || undefined;
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-compose-acceptance-'));
const suffix = path.basename(directory).toLowerCase();
const dockerConfig = path.join(directory, 'docker');
await mkdir(dockerConfig);
await writeFile(path.join(dockerConfig, 'config.json'), '{}');
await symlink(path.join(os.homedir(), '.docker', 'contexts'), path.join(dockerConfig, 'contexts'));
await symlink(path.join(os.homedir(), '.docker', 'cli-plugins'), path.join(dockerConfig, 'cli-plugins'));
const command = async (args, environment = {}) => {
const result = await executeCommand({
executable: 'docker',
args: [...(dockerContext ? ['--context', dockerContext] : []), ...args],
env: { PATH: process.env.PATH, HOME: os.homedir(), ...environment, DOCKER_CONFIG: dockerConfig },
timeoutMs: 180000,
});
if (result.exitCode !== 0)
throw new Error(
`Local fixture Docker command exited ${result.exitCode}; secret-bearing diagnostics are suppressed`,
);
return result.stdout.trim();
};
const executeFixtureCommand = async input => {
const result = await executeCommand({
...input,
env: { PATH: process.env.PATH, HOME: os.homedir(), ...input.env, DOCKER_CONFIG: dockerConfig },
});
if (result.exitCode !== 0)
console.error('fixture command failed', { executable: input.executable, exitCode: result.exitCode });
return result;
};
// Deliberately public test values: never read a real Vault token or customer secret.
const fixtureSecret = 'wishforge-compose-fixture-only-not-a-real-secret';
const fixtureToken = 'wishforge-compose-fake-vault-token-not-real';
const fixtureSecretHash = createHash('sha256').update(fixtureSecret).digest('hex');
const image = `127.0.0.1:45000/wishforge/${suffix}:fixture`;
const config = {
admin: { listen: '127.0.0.1:45219' },
apps: {
http: {
servers: {
app: { listen: ['127.0.0.1:45280'], routes: [{ handle: [{ handler: 'reverse_proxy', upstreams: [] }] }] },
},
},
},
};
await writeFile(path.join(directory, 'caddy.json'), JSON.stringify(config));
await writeFile(
path.join(directory, 'Dockerfile'),
'FROM node:22.14.0-bookworm-slim\nWORKDIR /app\nCOPY app.cjs ./\nUSER node\nCMD ["node","app.cjs"]\n',
);
await writeFile(
path.join(directory, 'app.cjs'),
`require('node:http').createServer((q,s)=>{if(q.url==='/secret-ready'){s.setHeader('content-type','application/json');s.end(JSON.stringify({secretConfigured:require('node:crypto').createHash('sha256').update(process.env.APPLICATION_SECRET??'').digest('hex')==='${fixtureSecretHash}'}));return;}s.setHeader('content-type','text/plain');s.end(q.url==='/health'?'ok':process.env.FAIL_STABLE==='true' && q.headers.host.endsWith(':45280')?'post-cutover-failure':process.env.VERSION)}).listen(3000,'0.0.0.0');\n`,
);
const ids = ['first', 'second', 'bad', 'rollback', 'cutover-failure', 'secret'];
if (
await fetch('http://127.0.0.1:45219/config/', { signal: AbortSignal.timeout(1000) }).then(
() => true,
() => false,
)
)
throw new Error('Acceptance admin port is occupied; will not modify an existing proxy');
const caddy = spawn(process.env.WF_CADDY_BINARY, ['run', '--config', path.join(directory, 'caddy.json')], {
env: { ...process.env, XDG_CONFIG_HOME: directory, XDG_DATA_HOME: directory },
stdio: ['ignore', 'ignore', 'pipe'],
});
let caddyLogs = '';
caddy.stderr.on('data', chunk => {
caddyLogs += chunk.toString();
});
let vault;
try {
for (let count = 0; count < 100; count++) {
if (
await fetch('http://127.0.0.1:45219/config/').then(
r => r.ok,
() => false,
)
)
break;
await new Promise(resolve => setTimeout(resolve, 100));
}
await command(['build', '--pull=false', '-t', image, directory]);
await command(['push', image]);
const inspection = JSON.parse(await command(['image', 'inspect', image]));
const digest = inspection[0].RepoDigests.find(value => value.startsWith(image.split(':fixture')[0] + '@'));
assert.ok(digest, 'Image must have actual registry digest');
const architecture = `linux/${inspection[0].Architecture}`;
const target = {
directory,
project: suffix,
dockerContext,
dockerConfig,
healthUrl: 'http://127.0.0.1:45280',
candidateHealthUrlTemplate: 'http://127.0.0.1:{port}',
proxyAdminUrl: 'http://127.0.0.1:45219',
proxyConfigPath: '/config/apps/http/servers/app/routes/0/handle/0',
proxyDialHost: '127.0.0.1',
};
const adapter = new ComposeDeploymentAdapter(target, executeFixtureCommand);
const release = (id, previous, version, expect = version) => ({
schemaVersion: 1,
id,
tenantId: 'local-acceptance',
environmentId: suffix,
sourceCommit: 'a'.repeat(40),
image: digest,
architecture,
configVersion: '1',
containerPort: 3000,
healthPath: '/health',
config: { VERSION: version },
smokeChecks: [{ path: '/', expectedStatus: 200, bodyIncludes: expect }],
expectedPreviousReleaseId: previous,
});
const first = release('first', null, 'version-one');
const second = release('second', 'first', 'version-two');
const observations = [];
for (const current of [first, second]) {
const result = await adapter.deploy(current, { execute: true });
observations.push(result);
assert.equal(result.status, 'verified', JSON.stringify(result));
assert.equal(await fetch('http://127.0.0.1:45280').then(r => r.text()), current.config.VERSION);
}
const failed = await adapter.deploy(release('bad', 'second', 'broken', 'expected-content'), { execute: true });
observations.push(failed);
assert.equal(failed.evidence.trafficSwitched, false);
assert.equal(await fetch('http://127.0.0.1:45280').then(r => r.text()), 'version-two');
const cutover = await adapter.deploy(
{
...release('cutover-failure', 'second', 'version-three'),
config: { VERSION: 'version-three', FAIL_STABLE: 'true' },
},
{ execute: true },
);
observations.push(cutover);
assert.equal(cutover.status, 'failed');
assert.equal(cutover.evidence.rolledBack, true);
assert.equal(await fetch('http://127.0.0.1:45280').then(r => r.text()), 'version-two');
const stale = await adapter.deploy(release('rollback', 'first', 'version-one'), { execute: true });
assert.equal(stale.status, 'conflict');
observations.push(stale);
const rebound = await adapter.deploy({ ...second, config: { VERSION: 'mutated' } }, { execute: true });
assert.equal(rebound.status, 'conflict');
observations.push(rebound);
const rollback = await adapter.deploy({ ...first, expectedPreviousReleaseId: 'second' }, { execute: true });
assert.equal(rollback.status, 'verified', JSON.stringify(rollback));
observations.push(rollback);
assert.equal(await fetch('http://127.0.0.1:45280').then(r => r.text()), 'version-one');
const readback = await adapter.reconcile(first);
assert.equal(readback.status, 'verified');
// Create the fake Vault and its files only after the image build: neither the
// plaintext fixture value nor the fake token enters the image build context.
let vaultReads = 0,
vaultUnexpected = 0;
const secretRef = 'fixture-password@version-3',
vaultPath = `local-acceptance/${suffix}/application`;
vault = http.createServer((req, res) => {
if (
req.method !== 'GET' ||
req.url !== `/v1/fixture-apps/data/${vaultPath}?version=3` ||
req.headers['x-vault-token'] !== fixtureToken
) {
vaultUnexpected++;
res.writeHead(403);
res.end('{}');
return;
}
vaultReads++;
res.setHeader('content-type', 'application/json');
res.end(
JSON.stringify({
data: { data: { password: fixtureSecret }, metadata: { version: 3, destroyed: false, deletion_time: '' } },
}),
);
});
vault.listen(0, '127.0.0.1');
await once(vault, 'listening');
const mapFile = path.join(directory, 'application-secret-map.json'),
tokenFile = path.join(directory, 'fake-vault-token');
await writeFile(
mapFile,
JSON.stringify({
schemaVersion: 1,
bindings: [
{
tenantId: 'local-acceptance',
environmentId: suffix,
ref: secretRef,
mount: 'fixture-apps',
path: vaultPath,
version: 3,
fields: { APPLICATION_SECRET: 'password' },
},
],
}),
{ mode: 0o600 },
);
await writeFile(tokenFile, fixtureToken, { mode: 0o600 });
const broker = new DeploymentSecretBroker({
NODE_ENV: 'test',
WF_DEPLOYMENT_SECRET_MAP_FILE: mapFile,
WF_DEPLOYMENT_VAULT_TOKEN_FILE: tokenFile,
WF_DEPLOYMENT_VAULT_ORIGIN: `http://127.0.0.1:${vault.address().port}`,
WF_DEPLOYMENT_VAULT_ALLOW_LOOPBACK_HTTP: 'true',
});
const secretRelease = {
...release('secret', 'first', 'version-with-secret'),
secretVersionRefs: [secretRef],
smokeChecks: [
{ path: '/', expectedStatus: 200, bodyIncludes: 'version-with-secret' },
{ path: '/secret-ready', expectedStatus: 200, bodyIncludes: '"secretConfigured":true' },
],
};
const secretPlan = await broker.plan({
tenantId: secretRelease.tenantId,
environmentId: secretRelease.environmentId,
releaseId: secretRelease.id,
secretVersionRefs: secretRelease.secretVersionRefs,
publicConfigKeys: Object.keys(secretRelease.config),
});
secretRelease.deploymentSecretPolicyDigest = secretPlan.policyDigest;
const secretAdapter = new ComposeDeploymentAdapter({ ...target, secretBroker: broker }, executeFixtureCommand);
const secretResult = await secretAdapter.deploy(secretRelease, { execute: true });
assert.equal(
secretResult.status,
'verified',
'Fixed-version secret deployment must pass its boolean application check',
);
observations.push(secretResult);
const secretState = await fetch('http://127.0.0.1:45280/secret-ready').then(response => response.json());
assert.equal(secretState.secretConfigured, true);
assert.equal(
vaultReads > 0,
true,
'The application credential must have been fetched from the local versioned Vault fixture',
);
assert.equal(vaultUnexpected, 0);
const secretRendered = await readFile(path.join(directory, 'secret.compose.json'), 'utf8');
const renderedContainsValues = secretRendered.includes(fixtureSecret) || secretRendered.includes(fixtureToken),
receiptContainsValues =
JSON.stringify(observations).includes(fixtureSecret) || JSON.stringify(observations).includes(fixtureToken);
assert.equal(secretRendered.includes('WF_DEPLOY_APP_SECRET_0'), true);
assert.equal(renderedContainsValues, false, 'Rendered Compose must contain references only');
assert.equal(receiptContainsValues, false, 'Receipts must not expose application secret material');
assert.equal((await secretAdapter.reconcile(secretRelease)).status, 'verified');
console.log(
JSON.stringify(
{
status: 'passed',
dockerContext: dockerContext ?? 'current',
architecture,
image: digest,
checks: [
'real image build and registry digest',
'first release',
'blue-green candidate validation',
'ETag switch',
'failed candidate retains stable',
'post-cutover failure automatically rolls back',
'stale previous rejected',
'immutable release identity',
'rollback and readback',
'fixed Vault version to Compose application environment',
],
secretAcceptance: {
exactVersionRequested: vaultReads > 0,
unexpectedVaultRequests: vaultUnexpected !== 0,
applicationConfirmed: secretState.secretConfigured === true,
renderedContainsValues,
receiptContainsValues,
},
observations,
},
null,
2,
),
);
} catch (error) {
console.error(caddyLogs.slice(-2000));
throw error;
} finally {
if (vault) {
vault.closeAllConnections();
if (vault.listening) await new Promise(resolve => vault.close(resolve));
}
caddy.kill('SIGTERM');
for (const id of ids) {
const project = `${suffix}-${createHash('sha256').update(id).digest('hex').slice(0, 12)}`;
const file = path.join(directory, `${id}.compose.json`);
try {
const saved = await readFile(file, 'utf8');
const placeholders = Object.fromEntries(
[...saved.matchAll(/\$\{(WF_DEPLOY_APP_SECRET_\d+):\?/g)].map(match => [
match[1],
'fixture-cleanup-placeholder',
]),
);
await command(['compose', '-p', project, '-f', file, 'down', '--remove-orphans'], placeholders);
} catch {}
}
await command(['image', 'rm', image]).catch(() => {});
await rm(directory, { recursive: true, force: true });
}