diff --git a/scripts/env-file.mjs b/scripts/env-file.mjs new file mode 100644 index 0000000..4ecb9e6 --- /dev/null +++ b/scripts/env-file.mjs @@ -0,0 +1,48 @@ +import { randomUUID } from 'node:crypto'; +import { lstat, open, readFile, rename, rm } from 'node:fs/promises'; +import path from 'node:path'; + +/** + * Sets values in the text of a .env file: an existing assignment, with or without `export`, is + * replaced where it stands and its duplicates dropped; a new one is appended. + */ +export function setEnvValues(source, values) { + const lines = source.split('\n'); + if (lines.at(-1) === '') lines.pop(); + for (const [key, value] of Object.entries(values)) { + if (!/^[A-Z_][A-Z0-9_]*$/.test(key) || /[\r\n]/.test(String(value))) + throw new Error(`Cannot write ${key} as a single-line setting`); + const assigns = new RegExp(`^\\s*(?:export\\s+)?${key}\\s*=`); + const first = lines.findIndex(line => assigns.test(line)); + if (first < 0) lines.push(`${key}=${value}`); + else { + lines[first] = `${key}=${value}`; + for (let index = lines.length - 1; index > first; index--) if (assigns.test(lines[index])) lines.splice(index, 1); + } + } + return `${lines.join('\n')}\n`; +} + +/** + * Replaces a private .env file whole or not at all: the new text goes to a temporary file beside + * it (named like `.env.*.local`, which Git ignores) that then takes its place, unless the file + * changed meanwhile. A crash leaves either the old file or the new one, never half of either. + */ +export async function replaceEnvFile(file, expected, next) { + const temporary = path.join(path.dirname(file), `.env.update-${randomUUID()}.local`); + const handle = await open(temporary, 'wx', 0o600); + try { + await handle.writeFile(next); + await handle.sync(); + } finally { + await handle.close(); + } + try { + const latest = await lstat(file); + if (!latest.isFile() || (await readFile(file, 'utf8')) !== expected) + throw new Error(`${file} changed while it was being updated; it was left as it was.`); + await rename(temporary, file); + } finally { + await rm(temporary, { force: true }); + } +} diff --git a/scripts/env-file.test.mjs b/scripts/env-file.test.mjs new file mode 100644 index 0000000..ad87429 --- /dev/null +++ b/scripts/env-file.test.mjs @@ -0,0 +1,28 @@ +import assert from 'node:assert/strict'; +import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import test from 'node:test'; +import { replaceEnvFile, setEnvValues } from './env-file.mjs'; + +test('sets values where they stand, exported or not, without duplicates, and appends new ones', () => { + const source = '# Local settings\nexport WF_WEB_HOST=127.0.0.1\nDATABASE_URL=postgres://keep\nWF_WEB_HOST=::1\n'; + assert.equal( + setEnvValues(source, { WF_WEB_HOST: '0.0.0.0', WF_WEB_PORT: '3000' }), + '# Local settings\nWF_WEB_HOST=0.0.0.0\nDATABASE_URL=postgres://keep\nWF_WEB_PORT=3000\n', + ); + assert.throws(() => setEnvValues('', { WF_TOKEN: 'one\nWF_DEV_AUTH_TOKEN=two' }), /single-line/); +}); + +test('replaces the file whole, or leaves it as it was when it changed meanwhile', async t => { + const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-env-')); + t.after(() => rm(directory, { recursive: true, force: true })); + const file = path.join(directory, '.env.local'); + await writeFile(file, 'A=1\n', { mode: 0o600 }); + await replaceEnvFile(file, 'A=1\n', 'A=2\n'); + assert.equal(await readFile(file, 'utf8'), 'A=2\n'); + await assert.rejects(replaceEnvFile(file, 'A=1\n', 'A=3\n'), /changed while it was being updated/); + assert.equal(await readFile(file, 'utf8'), 'A=2\n'); + // No temporary copy of the secrets is left behind. + assert.deepEqual(await readdir(directory), ['.env.local']); +}); diff --git a/scripts/setup-local-poc.ts b/scripts/setup-local-poc.ts index c249987..395ed0c 100644 --- a/scripts/setup-local-poc.ts +++ b/scripts/setup-local-poc.ts @@ -1,7 +1,10 @@ /** Explicit local operator bootstrap; no wish, Git push, build or POC deployment is started here. */ import { mkdir, readFile, writeFile, chmod } from 'node:fs/promises'; -import { homedir, networkInterfaces } from 'node:os'; +import { homedir } from 'node:os'; import path from 'node:path'; +import { parseEnv } from 'node:util'; +import { lanAddresses } from './configure-local-network.mjs'; +import { replaceEnvFile, setEnvValues } from './env-file.mjs'; import { PgStore } from '../apps/api/src/db/store.js'; import { ControlPlane, touch } from '../apps/api/src/domain/control-plane.js'; import { Releases } from '../apps/api/src/domain/releases.js'; @@ -33,9 +36,14 @@ try { throw Error('Run db:seed before this bootstrap'); const control = new ControlPlane(store, env), releases = new Releases(store, env); - const existing = await readFile(bindingFile, 'utf8') - .then(text => localPocBindingSchema.parse(JSON.parse(text))) - .catch(() => undefined); + // Only a missing binding means a first setup; an unreadable one must not create a second project. + const existing = await readFile(bindingFile, 'utf8').then( + text => localPocBindingSchema.parse(JSON.parse(text)), + (error: NodeJS.ErrnoException) => { + if (error.code === 'ENOENT') return undefined; + throw error; + }, + ); let project = existing ? await control.projects(actor).then(items => items.find(p => p.id === existing.projectId)) : undefined; @@ -97,9 +105,14 @@ try { }); const runnerFile = path.join(directory, 'runner.env'), keysFile = path.join(directory, 'runner-public.json'); - const configuredRunner = await readFile(runnerFile, 'utf8') - .then(() => true) - .catch(() => false); + // A runner registered for an environment that was since replaced is registered again. + const configuredRunner = await readFile(runnerFile, 'utf8').then( + text => parseEnv(text).WF_ENVIRONMENT_ID === target.id, + (error: NodeJS.ErrnoException) => { + if (error.code === 'ENOENT') return false; + throw error; + }, + ); if (!configuredRunner) { const registration = await new RunnerBroker(store, new Releases(store, env), env).register(actor, target.id); await writeFile(keysFile, JSON.stringify(registration.publicKeys, null, 2) + '\n', { mode: 0o600 }); @@ -123,9 +136,8 @@ try { { mode: 0o600 }, ); } - const addresses = Object.entries(networkInterfaces()) - .filter(([name]) => /^en\d+$/.test(name)) - .flatMap(([, items]) => (items ?? []).filter(i => i.family === 'IPv4' && !i.internal).map(i => i.address)); + // Private LAN addresses only, like npm run local:network: no public address becomes an entry. + const addresses = lanAddresses().map(entry => entry.address); const origins = [ 'http://localhost:3000', 'http://127.0.0.1:3000', @@ -148,13 +160,7 @@ try { HOST: '127.0.0.1', }; const old = await readFile('.env.local', 'utf8'); - let lines = old.split('\n'); - for (const [k, v] of Object.entries(settings)) { - const i = lines.findIndex(line => line.startsWith(k + '=')); - if (i < 0) lines.push(`${k}=${v}`); - else lines[i] = `${k}=${v}`; - } - await writeFile('.env.local', lines.join('\n') + '\n', { mode: 0o600 }); + await replaceEnvFile(path.join(root, '.env.local'), old, setEnvValues(old, settings)); await chmod('.env.local', 0o600); console.log( JSON.stringify(