Files
openbot/scripts/development-secrets.ts
T
NBandClaude Opus 5 5359ed2642 Generate the development env file per checkout (#282)
* Generate the development env file per checkout

`apps/auth-api/.env.dev` was committed encrypted, so running the app needed the
maintainer's `DOTENV_PRIVATE_KEY_DEV` and a fork could not run `bun run dev` at
all. Every value in it is derivable on the machine that runs it, so
`scripts/development-secrets.ts` now writes one on first run: a fresh ES256
ticket key pair claiming the `openbot-remote-1` key ID that `wrangler.jsonc`
pins, three 32-byte secrets, the development sign-in flag, and the blank SMTP
set. The file is untracked and never overwritten, so an existing one keeps its
secrets, and a maintainer's encrypted copy still works unchanged.

`prepare-dev-environment.ts` generates before `bun install`, because the script
needs node builtins only and both dev services load the file. Its `.env.keys`
assertion is gone: only `.env.production` is still encrypted, and nothing in
local development reads it. `dotenvx run` accepts a plaintext `-f` file with a
missing `-fk` path, verified under `--redact --strict` too, so the launch
commands need no change.

`env:encrypt:dev` and `env:validate:dev` are removed — both operated on a file
that is now plaintext and untracked.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* Say when the development env file was generated

`prepareDevelopmentEnvironment` created the file silently, so a first `bun run
dev` gave no sign that secrets had been written or where they live. It now
returns the outcome and both entry points report a fresh file: `dev-services.ts`
through its logger, the standalone script through stdout, since that one runs
before `bun install` on a clean clone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 14:03:08 +02:00

84 lines
3.7 KiB
TypeScript

import { generateKeyPairSync, randomBytes } from "node:crypto";
import { existsSync, 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 the development
// env file is generated per checkout instead of being committed. 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";
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 createDevelopmentEnvFile(): string {
const tickets = createDevelopmentTicketKeyPair();
return `# Generated by scripts/development-secrets.ts on first run. Not committed: every value is
# local to this checkout, so no two machines share one and nothing here reaches production.
# Delete this file and rerun \`bun run dev\` to get a fresh set.
AUTH_EXPOSE_DEVELOPMENT_CODE=true
# Local development sends no email. Blanking all five turns SMTP delivery off, so
# \`readSmtpConfig\` sees nothing configured instead of the partial set that
# \`wrangler.jsonc\` leaves in the top-level \`vars\`. The team-invitation endpoint answers
# \`503 email_delivery_not_configured\`, and login already returns its code through
# \`AUTH_EXPOSE_DEVELOPMENT_CODE\` without reaching SMTP at all.
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 DevelopmentEnvOutcome = "created" | "kept";
export function ensureDevelopmentEnvFile(projectRoot: string): DevelopmentEnvOutcome {
const path = join(projectRoot, "apps", "auth-api", ".env.dev");
if (existsSync(path)) return "kept";
writeFileSync(path, createDevelopmentEnvFile(), { encoding: "utf8", mode: 0o600 });
return "created";
}
if (import.meta.main) {
const projectRoot = dirname(dirname(fileURLToPath(import.meta.url)));
const outcome = ensureDevelopmentEnvFile(projectRoot);
process.stdout.write(
outcome === "created"
? "Generated apps/auth-api/.env.dev for local development.\n"
: "Kept the existing apps/auth-api/.env.dev.\n",
);
}