Events 3.4.0, Accounts 1.4.0, contracts 1.12.0, agent 1.3.0: jobs from the dashboard, styled Accounts emails, .env from the Hub
- Events and Accounts: Background jobs are switched on or off and rescheduled from the admin; the choice is kept in the database (job_overrides), wins over the .env and applies at once. Accounts gets an Admin -> Background jobs page and per-job env flags, as Events. - Accounts: every email styled as Cross Events' (church colour, logo, button, footer), same plain text (src/lib/emails.ts). - Contracts 1.12.0: env-read / env-set agent jobs, envReadResultSchema, isSecretEnvName. - crosscode script: `crosscode env <church> <product>` reads the .env (secrets hidden) and, with --set/--unset/--restart, changes one file, rebuilds the frontend when its file changed, reloads, and puts the file back if the site doesn't answer (lib/envedit.cjs, tests/envedit.bats). - Agent 1.3.0: runs env-read and env-set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@crosscode/contracts",
|
||||
"version": "1.11.0",
|
||||
"version": "1.12.0",
|
||||
"description": "The shared JSON formats used by every Cross Code product: zod schemas, JSON Schema, TypeScript types and sample payloads",
|
||||
"license": "UNLICENSED",
|
||||
"type": "commonjs",
|
||||
|
||||
@@ -854,6 +854,250 @@
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"const": "crosscode.agent-job/1"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"pattern": "^job_[A-Za-z0-9_-]{8,64}$"
|
||||
},
|
||||
"serverId": {
|
||||
"type": "string",
|
||||
"pattern": "^srv_[A-Za-z0-9_-]{8,64}$"
|
||||
},
|
||||
"config": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"alg": {
|
||||
"type": "string",
|
||||
"const": "x25519-hkdf-sha256-aes-256-gcm"
|
||||
},
|
||||
"epk": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{43}$"
|
||||
},
|
||||
"iv": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{16}$"
|
||||
},
|
||||
"ciphertext": {
|
||||
"type": "string",
|
||||
"maxLength": 200000,
|
||||
"pattern": "^[A-Za-z0-9_-]+$"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"alg",
|
||||
"epk",
|
||||
"iv",
|
||||
"ciphertext"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"notBefore": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
|
||||
},
|
||||
"expiresAt": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
|
||||
},
|
||||
"signature": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{86}$"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": "env-read"
|
||||
},
|
||||
"args": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"church": {
|
||||
"type": "string",
|
||||
"pattern": "^[a-z][a-z0-9-]{1,30}$"
|
||||
},
|
||||
"product": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"events",
|
||||
"website",
|
||||
"app",
|
||||
"accounts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"church",
|
||||
"product"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"schema",
|
||||
"id",
|
||||
"serverId",
|
||||
"notBefore",
|
||||
"expiresAt",
|
||||
"signature",
|
||||
"type",
|
||||
"args"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"const": "crosscode.agent-job/1"
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"pattern": "^job_[A-Za-z0-9_-]{8,64}$"
|
||||
},
|
||||
"serverId": {
|
||||
"type": "string",
|
||||
"pattern": "^srv_[A-Za-z0-9_-]{8,64}$"
|
||||
},
|
||||
"config": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"alg": {
|
||||
"type": "string",
|
||||
"const": "x25519-hkdf-sha256-aes-256-gcm"
|
||||
},
|
||||
"epk": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{43}$"
|
||||
},
|
||||
"iv": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{16}$"
|
||||
},
|
||||
"ciphertext": {
|
||||
"type": "string",
|
||||
"maxLength": 200000,
|
||||
"pattern": "^[A-Za-z0-9_-]+$"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"alg",
|
||||
"epk",
|
||||
"iv",
|
||||
"ciphertext"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"notBefore": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
|
||||
},
|
||||
"expiresAt": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
"pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
|
||||
},
|
||||
"signature": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-]{86}$"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"const": "env-set"
|
||||
},
|
||||
"args": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"church": {
|
||||
"type": "string",
|
||||
"pattern": "^[a-z][a-z0-9-]{1,30}$"
|
||||
},
|
||||
"product": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"events",
|
||||
"website",
|
||||
"app",
|
||||
"accounts"
|
||||
]
|
||||
},
|
||||
"file": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"pattern": "^(backend|frontend)\\/\\.env(\\.production)?$"
|
||||
},
|
||||
"set": {
|
||||
"type": "object",
|
||||
"propertyNames": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"pattern": "^[A-Z][A-Z0-9_]*$"
|
||||
},
|
||||
"additionalProperties": {
|
||||
"type": "string",
|
||||
"maxLength": 4000,
|
||||
"pattern": "^[^\\r\\n]*$"
|
||||
}
|
||||
},
|
||||
"unset": {
|
||||
"maxItems": 50,
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"maxLength": 100,
|
||||
"pattern": "^[A-Z][A-Z0-9_]*$"
|
||||
}
|
||||
},
|
||||
"restart": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"church",
|
||||
"product",
|
||||
"file",
|
||||
"set",
|
||||
"unset",
|
||||
"restart"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"schema",
|
||||
"id",
|
||||
"serverId",
|
||||
"notBefore",
|
||||
"expiresAt",
|
||||
"signature",
|
||||
"type",
|
||||
"args"
|
||||
],
|
||||
"additionalProperties": false
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
"adopt-undo",
|
||||
"restart",
|
||||
"seed-demo",
|
||||
"env-read",
|
||||
"env-set",
|
||||
"export",
|
||||
"uninstall",
|
||||
"purge",
|
||||
|
||||
@@ -62,6 +62,25 @@ export type DemoProfile = z.infer<typeof demoProfileSchema>;
|
||||
/** The first agent that runs `seed-demo` jobs (an older one refuses the unknown type). */
|
||||
export const SEED_DEMO_MIN_AGENT = "1.2.0";
|
||||
|
||||
/** The first agent that runs `env-read` and `env-set` jobs (an older one refuses the unknown types). */
|
||||
export const ENV_JOBS_MIN_AGENT = "1.3.0";
|
||||
|
||||
/** A .env variable's name. */
|
||||
export const envNameSchema = z.string().max(100).regex(/^[A-Z][A-Z0-9_]*$/, "Expected an env variable name (A-Z, 0-9, _)");
|
||||
|
||||
/** A product's .env file, relative to `apps/<product>`: `backend/.env`, `frontend/.env.production`. */
|
||||
export const envFileSchema = z.string().max(100).regex(/^(backend|frontend)\/\.env(\.production)?$/, "Expected backend/.env or frontend/.env.production");
|
||||
|
||||
/**
|
||||
* Names whose values are secrets: never shown on the Hub (only whether they're set) and never
|
||||
* changed from it. Keep in step with SECRET_NAME in scripts/lib/envedit.cjs.
|
||||
*/
|
||||
export const SECRET_ENV_NAME = /(SECRET|PASS|TOKEN|PRIVATE|CREDENTIAL|_KEY$|_DSN$|DATABASE_URL|ADMIN_URL)/;
|
||||
export const isSecretEnvName = (name: string): boolean => SECRET_ENV_NAME.test(name);
|
||||
|
||||
/** At most this many changes in one `env-set` job. */
|
||||
export const ENV_SET_MAX = 50;
|
||||
|
||||
/** Each job type and the arguments it takes. Unknown keys are refused, not stripped. */
|
||||
export const jobArgsSchemas = {
|
||||
/** `crosscode install <church> --products … --frp --json` */
|
||||
@@ -95,6 +114,35 @@ export const jobArgsSchemas = {
|
||||
* marked as demo sites. `adminEmail`: the account kept (default: the release's own choice).
|
||||
*/
|
||||
"seed-demo": z.strictObject({ church: shortNameSchema, profile: demoProfileSchema, adminEmail: z.email().max(254).optional() }),
|
||||
/**
|
||||
* `crosscode env <church> <product> --json`: the product's .env values, secrets hidden (only
|
||||
* whether they're set). The report's `result` is an {@link envReadResultSchema}.
|
||||
*/
|
||||
"env-read": z.strictObject({ church: shortNameSchema, product: productSchema }),
|
||||
/**
|
||||
* `crosscode env <church> <product> --file <file> --set NAME=value … --unset NAME … [--restart]
|
||||
* --json`: changes values in one .env file (a copy is kept first), rebuilds the frontend when its
|
||||
* file changed (Next.js builds the values in), then reloads; on a failure the file is put back.
|
||||
* Never secrets, never what the server generates (DATABASE_URL, JWT_SECRET …): the script
|
||||
* refuses those too.
|
||||
*/
|
||||
"env-set": z.strictObject({
|
||||
church: shortNameSchema,
|
||||
product: productSchema,
|
||||
file: envFileSchema,
|
||||
set: z.record(envNameSchema, z.string().max(4000).regex(/^[^\r\n]*$/, "Values can't contain newlines")),
|
||||
unset: z.array(envNameSchema).max(ENV_SET_MAX),
|
||||
restart: z.boolean(),
|
||||
}).check((ctx) => {
|
||||
const a = ctx.value;
|
||||
const names = [...Object.keys(a.set), ...a.unset];
|
||||
const issue = (message: string) => ctx.issues.push({ code: "custom", message, input: a });
|
||||
if (names.length === 0) issue("Nothing to change");
|
||||
if (names.length > ENV_SET_MAX) issue(`At most ${ENV_SET_MAX} changes at a time`);
|
||||
const secret = names.find(isSecretEnvName);
|
||||
if (secret) issue(`${secret} is a secret: it can't be changed from the Hub`);
|
||||
if (a.unset.some((n) => Object.prototype.hasOwnProperty.call(a.set, n))) issue("A name can't be both set and removed");
|
||||
}),
|
||||
/** `crosscode export <church> --out <zip> --json`; the agent encrypts the ZIP and uploads it under `exportId`. */
|
||||
export: z.strictObject({ church: shortNameSchema, exportId: z.string().regex(/^exp_[A-Za-z0-9_-]{8,64}$/) }),
|
||||
/** `crosscode uninstall <church> --json` */
|
||||
@@ -119,7 +167,7 @@ export const CENTRAL_JOB_TYPES: readonly JobType[] = ["check-ports", "status", "
|
||||
|
||||
/** Time limits per job type, in minutes (§3). A job past it is stopped and reported as failed. */
|
||||
export const JOB_TIME_LIMIT_MINUTES: Record<JobType, number> = {
|
||||
install: 30, "add-product": 30, deploy: 20, adopt: 30, "adopt-undo": 15, restart: 5, "seed-demo": 15, export: 20,
|
||||
install: 30, "add-product": 30, deploy: 20, adopt: 30, "adopt-undo": 15, restart: 5, "seed-demo": 15, "env-read": 2, "env-set": 20, export: 20,
|
||||
uninstall: 20, purge: 10, "update-agent": 15, "check-ports": 2, status: 3,
|
||||
};
|
||||
|
||||
@@ -164,7 +212,7 @@ function jobOf<T extends JobType>(type: T) {
|
||||
|
||||
export const agentJobSchema = z.discriminatedUnion("type", [
|
||||
jobOf("install"), jobOf("add-product"), jobOf("deploy"), jobOf("adopt"), jobOf("adopt-undo"), jobOf("restart"), jobOf("seed-demo"),
|
||||
jobOf("export"), jobOf("uninstall"), jobOf("purge"), jobOf("update-agent"), jobOf("check-ports"), jobOf("status"),
|
||||
jobOf("env-read"), jobOf("env-set"), jobOf("export"), jobOf("uninstall"), jobOf("purge"), jobOf("update-agent"), jobOf("check-ports"), jobOf("status"),
|
||||
]);
|
||||
export type AgentJob = z.infer<typeof agentJobSchema>;
|
||||
export type UnsignedAgentJob = Omit<AgentJob, "signature">;
|
||||
@@ -321,6 +369,32 @@ export const envTemplateSchema = z.object({
|
||||
});
|
||||
export type EnvTemplate = z.infer<typeof envTemplateSchema>;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Reading an install's .env (the env-read job, contracts 1.12.0)
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
||||
/** One variable in an `env-read` result. `value` is null for a secret (`set` says whether it has one). */
|
||||
export const envReadVariableSchema = z.object({
|
||||
name: envNameSchema,
|
||||
value: z.string().max(4000).nullable(),
|
||||
secret: z.boolean(),
|
||||
set: z.boolean(),
|
||||
/** From the release's env template (null when the template doesn't list it). */
|
||||
layer: envLayerSchema.nullable(),
|
||||
description: z.string().max(400).nullable(),
|
||||
/** Whether the Hub may change it: not secret, not generated on the server. */
|
||||
editable: z.boolean(),
|
||||
});
|
||||
export type EnvReadVariable = z.infer<typeof envReadVariableSchema>;
|
||||
|
||||
/** The `result` of an `env-read` report: each of the product's .env files, variables in file order. */
|
||||
export const envReadResultSchema = z.object({
|
||||
product: productSchema,
|
||||
files: z.array(z.object({ file: envFileSchema, exists: z.boolean(), variables: z.array(envReadVariableSchema).max(300) })).max(10),
|
||||
readAt: dateTimeSchema,
|
||||
});
|
||||
export type EnvReadResult = z.infer<typeof envReadResultSchema>;
|
||||
|
||||
/** The variables a layer must provide for a template, that it doesn't (the Hub's check before a job). */
|
||||
export function missingRequired(template: EnvTemplate, layer: "server" | "defaults", values: Record<string, string | undefined>): string[] {
|
||||
const missing: string[] = [];
|
||||
|
||||
@@ -167,6 +167,47 @@ describe("seed-demo jobs", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("env-read and env-set jobs (1.12.0)", () => {
|
||||
const set = (a: Record<string, unknown>) => agent.jobArgsSchemas["env-set"].safeParse({ church: "hope", product: "events", file: "backend/.env", set: {}, unset: [], restart: true, ...a });
|
||||
it("read one product", () => {
|
||||
expect(agent.jobArgsSchemas["env-read"].safeParse({ church: "hope", product: "accounts" }).success).toBe(true);
|
||||
expect(agent.jobArgsSchemas["env-read"].safeParse({ church: "hope", product: "nope" }).success).toBe(false);
|
||||
expect(agent.JOB_TYPES).toEqual(expect.arrayContaining(["env-read", "env-set"]));
|
||||
expect(agent.CENTRAL_JOB_TYPES).not.toContain("env-set");
|
||||
});
|
||||
it("set plain values in a product's .env file", () => {
|
||||
expect(set({ set: { FRONTEND_URL: "https://events.hope.test" } }).success).toBe(true);
|
||||
expect(set({ file: "frontend/.env.production", set: { NEXT_PUBLIC_API_URL: "https://events.hope.test" }, unset: ["OLD_THING"] }).success).toBe(true);
|
||||
});
|
||||
it("refuse secrets, nothing to do, newlines, other files and bad names", () => {
|
||||
expect(set({}).success).toBe(false);
|
||||
for (const name of ["JWT_SECRET", "DATABASE_URL", "SMTP_PASS", "HUB_LICENCE_KEY", "SENTRY_DSN", "DB_ADMIN_URL"]) {
|
||||
expect(set({ set: { [name]: "x" } }).success).toBe(false);
|
||||
expect(set({ unset: [name] }).success).toBe(false);
|
||||
}
|
||||
expect(set({ set: { A_URL: "x\nEVIL=1" } }).success).toBe(false);
|
||||
expect(set({ file: "../.env", set: { A: "1" } }).success).toBe(false);
|
||||
expect(set({ set: { "lower": "1" } }).success).toBe(false);
|
||||
expect(set({ set: { A: "1" }, unset: ["A"] }).success).toBe(false);
|
||||
expect(set({ set: { A: "1" }, extra: true }).success).toBe(false);
|
||||
});
|
||||
it("tell secrets by name", () => {
|
||||
expect(agent.isSecretEnvName("ACCOUNTS_SECRET")).toBe(true);
|
||||
expect(agent.isSecretEnvName("FRONTEND_URL")).toBe(false);
|
||||
expect(agent.isSecretEnvName("JOBS_ENABLED")).toBe(false);
|
||||
});
|
||||
it("read results", () => {
|
||||
const ok = agent.envReadResultSchema.safeParse({
|
||||
product: "events", readAt: "2026-11-20T10:00:00.000Z",
|
||||
files: [{ file: "backend/.env", exists: true, variables: [
|
||||
{ name: "FRONTEND_URL", value: "https://events.hope.test", secret: false, set: true, layer: "install", description: null, editable: true },
|
||||
{ name: "JWT_SECRET", value: null, secret: true, set: true, layer: "generated", description: "never change it", editable: false },
|
||||
] }],
|
||||
});
|
||||
expect(ok.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("env templates", () => {
|
||||
it("lists the required server and defaults values that are missing", () => {
|
||||
const template = agent.envTemplateSchema.parse({
|
||||
|
||||
Reference in New Issue
Block a user