wishforge-ide/apps/worker/test/temporal.test.ts
eust-w 15e137ff7f feat: isolate tenants in Temporal with per-tenant namespaces and mutual TLS
Sandboxes can reach Temporal, which used to accept any client in one shared
namespace. Now:

- Every Temporal client (API dispatcher, coordinator, deployer, sandboxes) can
  present a client certificate (TEMPORAL_TLS_*); production refuses to start
  without one.
- TEMPORAL_NAMESPACE_PREFIX gives each tenant its own namespace: the dispatcher
  starts and signals a tenant's workflows there, the shared coordinator and
  deployer poll every served tenant's namespace with one workflow bundle, and a
  sandbox polls only its tenant's. Production refuses to serve several tenants
  through one namespace.
- The Kubernetes provisioner mounts the tenant's Temporal client certificate
  into executor and verifier pods (WF_RUN_TEMPORAL_TLS_SECRET, required in
  production); the local provisioner passes the TLS settings through.

Authorizing certificates per namespace remains Temporal server configuration;
the operations guide describes it.

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

49 lines
2.8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { temporalTls, workerNamespaces } from '../src/temporal.js';
describe('Temporal connections', () => {
it('keeps each tenant in its own namespace when a prefix is configured', () => {
const shared = { WF_DISPATCH_TENANTS: 'alpha,beta,alpha', TEMPORAL_NAMESPACE: 'wishforge' };
expect(workerNamespaces(shared, 'coordinator')).toEqual(['wishforge']);
const perTenant = { ...shared, TEMPORAL_NAMESPACE_PREFIX: 'wf-' };
expect(workerNamespaces(perTenant, 'coordinator')).toEqual(['wf-alpha', 'wf-beta']);
expect(workerNamespaces(perTenant, 'deployer')).toEqual(['wf-alpha', 'wf-beta']);
expect(workerNamespaces({ ...perTenant, WF_SANDBOX_TENANT_ID: 'beta' }, 'executor')).toEqual(['wf-beta']);
// A sandbox started by a tenant's provisioner uses the namespace it was given.
expect(workerNamespaces({ TEMPORAL_NAMESPACE: 'wf-beta', WF_SANDBOX_TENANT_ID: 'beta' }, 'verifier')).toEqual([
'wf-beta',
]);
expect(() => workerNamespaces({ TEMPORAL_NAMESPACE_PREFIX: 'wf-' }, 'coordinator')).toThrow('WF_DISPATCH_TENANTS');
});
it('refuses a shared namespace for several tenants, and plaintext, in production', async () => {
const production = { WF_DEPLOYMENT_MODE: 'production', WF_DISPATCH_TENANTS: 'alpha,beta' };
expect(() => workerNamespaces(production, 'coordinator')).toThrow('per-tenant Temporal namespaces');
expect(workerNamespaces({ ...production, WF_DISPATCH_TENANTS: 'alpha' }, 'coordinator')).toEqual(['default']);
await expect(temporalTls(production)).rejects.toThrow('mutual TLS');
expect(await temporalTls({ WF_DEPLOYMENT_MODE: 'development' })).toBeUndefined();
});
it('reads the client certificate, key and CA for mutual TLS', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-temporal-tls-'));
try {
for (const name of ['tls.crt', 'tls.key', 'ca.crt']) await writeFile(path.join(directory, name), name);
const tls = await temporalTls({
WF_DEPLOYMENT_MODE: 'production',
TEMPORAL_TLS_CERT: path.join(directory, 'tls.crt'),
TEMPORAL_TLS_KEY: path.join(directory, 'tls.key'),
TEMPORAL_TLS_CA: path.join(directory, 'ca.crt'),
TEMPORAL_TLS_SERVER_NAME: 'temporal.internal',
});
expect(String(tls?.clientCertPair.crt)).toBe('tls.crt');
expect(String(tls?.clientCertPair.key)).toBe('tls.key');
expect(String(tls?.serverRootCACertificate)).toBe('ca.crt');
expect(tls?.serverNameOverride).toBe('temporal.internal');
await expect(temporalTls({ TEMPORAL_TLS_CERT: path.join(directory, 'tls.crt') })).rejects.toThrow('together');
} finally {
await rm(directory, { recursive: true, force: true });
}
});
});