Files
openbot/scripts/development-secrets.ts
T
NB 0086fc4eff Separate encrypted development settings from local defaults (#1580)
* Add shared development secrets with persistent local defaults

* Replace the shared Stripe development key

* Use encrypted development env and remove legacy state import

* Document the two account environment files

* Clarify shell overrides and production worktree setup
2026-10-08 14:16:15 +02:00

306 lines
10 KiB
TypeScript

import { generateKeyPairSync, randomBytes } from "node:crypto";
import {
chmodSync,
closeSync,
existsSync,
fsyncSync,
linkSync,
lstatSync,
mkdirSync,
openSync,
readFileSync,
renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
// Every value the local Auth API needs is derivable on the machine that runs it, so local defaults
// live in private state instead of a committed file. Nothing here is shared with production:
// `.env.production` stays encrypted and its key stays in `.env.keys`. This is what lets a fork run
// `bun run dev` without the maintainer's dotenvx keys.
//
// `prepare-dev-environment.ts` calls this before `bun install`, so it uses node builtins only —
// `@openbot/logging` is not resolvable yet on a fresh clone, which is why this writes to stdout.
// `wrangler.jsonc` pins the active ticket key ID in its top-level `vars`, and `parseJwks` rejects a
// JWKS that does not carry that `kid`, so the generated pair has to claim the same one.
const TICKET_KEY_ID = "openbot-remote-1";
const DEVELOPMENT_STATE_VERSION = 1 as const;
const DEVELOPMENT_STATE_DIRECTORY = ".openbot";
const DEVELOPMENT_STATE_FILE = "dev-state.json";
const DEVELOPMENT_STATE_LOCK = "dev-state.json.lock";
/** The persisted local values used by development services. */
export interface DevelopmentState {
version: typeof DEVELOPMENT_STATE_VERSION;
defaults: Record<string, string>;
overrides: Record<string, string>;
}
const REQUIRED_DEFAULT_NAMES = ["REMOTE_TICKET_PRIVATE_JWK", "REMOTE_TICKET_PUBLIC_JWKS"] as const;
export interface DevelopmentTicketKeyPair {
privateJwk: string;
publicJwks: string;
}
export function createDevelopmentTicketKeyPair(keyId: string = TICKET_KEY_ID): DevelopmentTicketKeyPair {
const { privateKey, publicKey } = generateKeyPairSync("ec", { namedCurve: "P-256" });
const privateJwk = { ...privateKey.export({ format: "jwk" }), kid: keyId, alg: "ES256", use: "sig" };
const publicJwk = { ...publicKey.export({ format: "jwk" }), kid: keyId, alg: "ES256", use: "sig" };
return {
privateJwk: JSON.stringify(privateJwk),
publicJwks: JSON.stringify({ keys: [publicJwk] }),
};
}
// `strongSecret` in `remote/api/src/config.ts` rejects anything under 32 bytes, and the same
// `REMOTE_AUTH_WEBHOOK_SECRET` is read from this file by both the Worker and the Signal service, so
// one generated value pairs the two halves of local development.
function developmentSecret(): string {
return randomBytes(32).toString("hex");
}
export function createDevelopmentDefaults(): Record<string, string> {
const tickets = createDevelopmentTicketKeyPair();
return {
AUTH_EXPOSE_DEVELOPMENT_CODE: "true",
// Empty SMTP settings disable delivery, including values set in Wrangler's vars.
EMAIL_SMTP_HOST: "",
EMAIL_SMTP_PORT: "",
EMAIL_SMTP_USERNAME: "",
EMAIL_FROM: "",
EMAIL_SMTP_PASSWORD: "",
SKILLS_ADMIN_TOKEN: developmentSecret(),
SITE_REPORT_HASH_SECRET: developmentSecret(),
REMOTE_AUTH_WEBHOOK_SECRET: developmentSecret(),
REMOTE_TICKET_PRIVATE_JWK: tickets.privateJwk,
REMOTE_TICKET_PUBLIC_JWKS: tickets.publicJwks,
};
}
export type DevelopmentStateOutcome = "created" | "kept";
export function ensureDevelopmentState(projectRoot: string): DevelopmentStateOutcome {
const paths = developmentStatePaths(projectRoot);
const existed = existsSync(paths.state);
readDevelopmentState(projectRoot);
return existed ? "kept" : "created";
}
/** Reads or creates local state before dependency installation, using Node built-ins only. */
export function readDevelopmentState(projectRoot: string): DevelopmentState {
const paths = developmentStatePaths(projectRoot);
ensurePrivateDirectory(paths.directory);
const current = readStateIfPresent(paths.state);
if (current) return current;
const lock = acquireStateLock(paths.lock);
try {
const afterLock = readStateIfPresent(paths.state);
if (afterLock) return afterLock;
const state = createInitialState();
publishInitialState(paths.state, state);
return readStateIfPresent(paths.state) ?? state;
} finally {
releaseStateLock(paths.lock, lock);
}
}
/**
* Applies explicit local development overrides and publishes them as one atomic state update.
* `undefined` and `null` remove an override. The writer takes an exclusive lock and therefore
* fails safely if another process is updating the state instead of losing that process's update.
*/
export function setDevelopmentOverrides(
projectRoot: string,
updates: Record<string, string | null | undefined>,
): DevelopmentState {
const paths = developmentStatePaths(projectRoot);
ensurePrivateDirectory(paths.directory);
readDevelopmentState(projectRoot);
const lock = acquireStateLock(paths.lock);
try {
const current = readStateIfPresent(paths.state);
if (!current) throw new Error("The development state disappeared during update. Retry the operation.");
const overrides = { ...current.overrides };
for (const [name, value] of Object.entries(updates)) {
if (value === undefined || value === null) delete overrides[name];
else overrides[name] = value;
}
const next: DevelopmentState = { ...current, overrides };
publishState(paths.state, next);
return next;
} finally {
releaseStateLock(paths.lock, lock);
}
}
interface DevelopmentStatePaths {
directory: string;
state: string;
lock: string;
}
interface ParsedDevelopmentState {
version?: unknown;
defaults?: unknown;
overrides?: unknown;
}
function developmentStatePaths(projectRoot: string): DevelopmentStatePaths {
const directory = join(projectRoot, DEVELOPMENT_STATE_DIRECTORY);
return {
directory,
state: join(directory, DEVELOPMENT_STATE_FILE),
lock: join(directory, DEVELOPMENT_STATE_LOCK),
};
}
function createInitialState(): DevelopmentState {
return {
version: DEVELOPMENT_STATE_VERSION,
defaults: createDevelopmentDefaults(),
overrides: {},
};
}
function readStateIfPresent(path: string): DevelopmentState | undefined {
if (!existsSync(path)) return undefined;
assertRegularPrivateFile(path, "development state");
let parsed: unknown;
try {
parsed = JSON.parse(readFileSync(path, "utf8"));
} catch {
throw new Error("The development state file is invalid or unreadable. Restore .openbot/dev-state.json.");
}
if (!isDevelopmentState(parsed)) {
throw new Error(
"The development state file has an invalid format. Restore .openbot/dev-state.json before continuing.",
);
}
chmodSync(path, 0o600);
return parsed;
}
function isDevelopmentState(value: unknown): value is DevelopmentState {
if (!value || typeof value !== "object") return false;
const state: ParsedDevelopmentState = value;
if (state.version !== DEVELOPMENT_STATE_VERSION) return false;
if (!isStringRecord(state.defaults) || !isStringRecord(state.overrides)) return false;
for (const name of REQUIRED_DEFAULT_NAMES) {
if (!state.defaults[name]) return false;
}
return true;
}
function isStringRecord(value: unknown): value is Record<string, string> {
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
return Object.values(value).every((entry) => typeof entry === "string");
}
function publishInitialState(path: string, state: DevelopmentState): void {
if (existsSync(path)) return;
const temporary = writeTemporary(path, state);
try {
// Hard-link publication is exclusive: a concurrent first run cannot replace a state that won
// the race and thereby rotate the ticket identity.
try {
linkSync(temporary, path);
} catch (error) {
if (!isAlreadyExistsError(error)) throw error;
}
} finally {
unlinkIfPresent(temporary);
}
}
function publishState(path: string, state: DevelopmentState): void {
assertRegularPrivateFileIfPresent(path, "development state");
const temporary = writeTemporary(path, state);
try {
renameSync(temporary, path);
chmodSync(path, 0o600);
} finally {
unlinkIfPresent(temporary);
}
}
function writeTemporary(path: string, state: DevelopmentState): string {
const temporary = `${path}.tmp-${process.pid}-${randomBytes(8).toString("hex")}`;
const descriptor = openSync(temporary, "wx", 0o600);
try {
const contents = `${JSON.stringify(state, null, 2)}\n`;
writeFileSync(descriptor, contents, "utf8");
fsyncSync(descriptor);
} finally {
closeSync(descriptor);
}
chmodSync(temporary, 0o600);
return temporary;
}
function acquireStateLock(path: string): string {
try {
mkdirSync(path, { mode: 0o700 });
} catch (error) {
if (isAlreadyExistsError(error)) {
throw new Error(
`Another process is updating ${path}. Retry after it finishes; do not delete the lock automatically.`,
);
}
throw error;
}
return path;
}
function releaseStateLock(path: string, lock: string): void {
if (lock !== path) return;
rmSync(path, { recursive: true, force: true });
}
function ensurePrivateDirectory(path: string): void {
if (existsSync(path)) {
if (!lstatSync(path).isDirectory()) throw new Error("The .openbot development state path is not a directory.");
} else {
mkdirSync(path, { recursive: true, mode: 0o700 });
}
chmodSync(path, 0o700);
}
function assertRegularPrivateFile(path: string, label: string): void {
const stat = lstatSync(path);
if (!stat.isFile()) throw new Error(`The ${label} path is not a regular file.`);
}
function assertRegularPrivateFileIfPresent(path: string, label: string): void {
if (existsSync(path)) assertRegularPrivateFile(path, label);
}
function unlinkIfPresent(path: string): void {
try {
rmSync(path, { force: true });
} catch {
// The state has already been published. A leftover private temporary can be removed on the
// next run, but must not hide the successful operation.
}
}
function isAlreadyExistsError(error: unknown): boolean {
return Boolean(error && typeof error === "object" && "code" in error && error.code === "EEXIST");
}
if (import.meta.main) {
const projectRoot = dirname(dirname(fileURLToPath(import.meta.url)));
const outcome = ensureDevelopmentState(projectRoot);
process.stdout.write(
outcome === "created"
? "Generated .openbot/dev-state.json for local development.\n"
: "Kept the existing .openbot/dev-state.json.\n",
);
}