Files
openbot/scripts/development-secrets.test.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

93 lines
3.4 KiB
TypeScript

import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { importJWK, jwtVerify, SignJWT } from "jose";
import { afterEach, describe, expect, it } from "vitest";
import {
createDevelopmentEnvFile,
createDevelopmentTicketKeyPair,
ensureDevelopmentEnvFile,
} from "./development-secrets";
const temporaryRoots: string[] = [];
afterEach(() => {
for (const root of temporaryRoots.splice(0)) rmSync(root, { recursive: true, force: true });
});
describe("generated development secrets", () => {
it("issues a ticket the published JWKS can verify", async () => {
const { privateJwk, publicJwks } = createDevelopmentTicketKeyPair();
const signingKey = await importJWK(JSON.parse(privateJwk), "ES256");
const [published] = JSON.parse(publicJwks).keys;
const ticket = await new SignJWT({ sessionId: "session-1" })
.setProtectedHeader({ alg: "ES256", kid: published.kid })
.sign(signingKey);
const verified = await jwtVerify(ticket, await importJWK(published, "ES256"));
expect(verified.payload.sessionId).toBe("session-1");
});
it("claims the ticket key ID that wrangler.jsonc pins", () => {
const { publicJwks } = createDevelopmentTicketKeyPair();
expect(JSON.parse(publicJwks).keys[0].kid).toBe("openbot-remote-1");
});
it("writes secrets the Signal service accepts, and a different set each time", () => {
const [first, second] = [readGeneratedValues(), readGeneratedValues()];
for (const name of ["SKILLS_ADMIN_TOKEN", "SITE_REPORT_HASH_SECRET", "REMOTE_AUTH_WEBHOOK_SECRET"]) {
expect(new TextEncoder().encode(first[name]).byteLength).toBeGreaterThanOrEqual(32);
expect(first[name]).not.toBe(second[name]);
}
});
it("turns email delivery off and exposes the development sign-in code", () => {
const values = readGeneratedValues();
expect(values.AUTH_EXPOSE_DEVELOPMENT_CODE).toBe("true");
expect([
values.EMAIL_SMTP_HOST,
values.EMAIL_SMTP_PORT,
values.EMAIL_SMTP_USERNAME,
values.EMAIL_FROM,
values.EMAIL_SMTP_PASSWORD,
]).toEqual(["", "", "", "", ""]);
});
it("keeps an env file that already exists", () => {
const root = createTemporaryRoot();
const path = join(root, "apps", "auth-api", ".env.dev");
writeFileSync(path, "SKILLS_ADMIN_TOKEN=the-developer-own-value\n");
expect(ensureDevelopmentEnvFile(root)).toBe("kept");
expect(readFileSync(path, "utf8")).toBe("SKILLS_ADMIN_TOKEN=the-developer-own-value\n");
});
it("generates an env file when the checkout has none", () => {
const root = createTemporaryRoot();
expect(ensureDevelopmentEnvFile(root)).toBe("created");
expect(readFileSync(join(root, "apps", "auth-api", ".env.dev"), "utf8")).toContain("REMOTE_TICKET_PRIVATE_JWK=");
});
});
function readGeneratedValues(): Record<string, string> {
const values: Record<string, string> = {};
for (const line of createDevelopmentEnvFile().split("\n")) {
if (line.startsWith("#") || !line.includes("=")) continue;
const separator = line.indexOf("=");
values[line.slice(0, separator)] = line.slice(separator + 1);
}
return values;
}
function createTemporaryRoot(): string {
const root = mkdtempSync(join(tmpdir(), "openbot-dev-secrets-"));
temporaryRoots.push(root);
mkdirSync(join(root, "apps", "auth-api"), { recursive: true });
return root;
}