@caelo-cms/provisioning 0.10.52 → 0.10.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/dist/agent/command.js +1 -0
  2. package/dist/agent/command.js.map +1 -1
  3. package/dist/agent-db.js +198 -102
  4. package/dist/audit-job-setup.d.ts +69 -0
  5. package/dist/audit-job-setup.d.ts.map +1 -0
  6. package/dist/audit-job-setup.js +317 -0
  7. package/dist/audit-job-setup.js.map +1 -0
  8. package/dist/aws-task-roles.d.ts +1 -1
  9. package/dist/aws-task-roles.d.ts.map +1 -1
  10. package/dist/aws-task-roles.js +0 -1
  11. package/dist/aws-task-roles.js.map +1 -1
  12. package/dist/caddy.d.ts +9 -0
  13. package/dist/caddy.d.ts.map +1 -1
  14. package/dist/caddy.js +38 -0
  15. package/dist/caddy.js.map +1 -1
  16. package/dist/cli-routing.d.ts +20 -0
  17. package/dist/cli-routing.d.ts.map +1 -1
  18. package/dist/cli-routing.js +106 -0
  19. package/dist/cli-routing.js.map +1 -1
  20. package/dist/cli.js +37 -14
  21. package/dist/cli.js.map +1 -1
  22. package/dist/compose.d.ts +4 -3
  23. package/dist/compose.d.ts.map +1 -1
  24. package/dist/compose.js +3 -2
  25. package/dist/compose.js.map +1 -1
  26. package/dist/gateway-credentials.d.ts +3 -5
  27. package/dist/gateway-credentials.d.ts.map +1 -1
  28. package/dist/gateway-credentials.js +8 -18
  29. package/dist/gateway-credentials.js.map +1 -1
  30. package/dist/gcp-names.d.ts +16 -1
  31. package/dist/gcp-names.d.ts.map +1 -1
  32. package/dist/gcp-names.js +23 -0
  33. package/dist/gcp-names.js.map +1 -1
  34. package/dist/infra-contract.d.ts +1 -1
  35. package/dist/infra-contract.d.ts.map +1 -1
  36. package/dist/infra-contract.js +14 -1
  37. package/dist/infra-contract.js.map +1 -1
  38. package/dist/private-file.d.ts +26 -0
  39. package/dist/private-file.d.ts.map +1 -0
  40. package/dist/private-file.js +49 -0
  41. package/dist/private-file.js.map +1 -0
  42. package/dist/release-contract.d.ts.map +1 -1
  43. package/dist/release-contract.js +3 -0
  44. package/dist/release-contract.js.map +1 -1
  45. package/dist/runtime-secrets.d.ts +1 -3
  46. package/dist/runtime-secrets.d.ts.map +1 -1
  47. package/dist/runtime-secrets.js +2 -6
  48. package/dist/runtime-secrets.js.map +1 -1
  49. package/dist/self-hosted-roles.d.ts +9 -8
  50. package/dist/self-hosted-roles.d.ts.map +1 -1
  51. package/dist/self-hosted-roles.js +9 -9
  52. package/dist/self-hosted-roles.js.map +1 -1
  53. package/dist/self-hosted-upgrade.d.ts +7 -1
  54. package/dist/self-hosted-upgrade.d.ts.map +1 -1
  55. package/dist/self-hosted-upgrade.js +17 -4
  56. package/dist/self-hosted-upgrade.js.map +1 -1
  57. package/dist/stack-contract.d.ts +60 -3
  58. package/dist/stack-contract.d.ts.map +1 -1
  59. package/dist/stack-contract.js +49 -37
  60. package/dist/stack-contract.js.map +1 -1
  61. package/dist/tar-entries.d.ts.map +1 -1
  62. package/dist/tar-entries.js +84 -9
  63. package/dist/tar-entries.js.map +1 -1
  64. package/dist/upgrade/app-roll.d.ts.map +1 -1
  65. package/dist/upgrade/app-roll.js +35 -0
  66. package/dist/upgrade/app-roll.js.map +1 -1
  67. package/dist/upgrade/audit-job-roll.d.ts +38 -0
  68. package/dist/upgrade/audit-job-roll.d.ts.map +1 -0
  69. package/dist/upgrade/audit-job-roll.js +43 -0
  70. package/dist/upgrade/audit-job-roll.js.map +1 -0
  71. package/dist/upgrade/converge.d.ts.map +1 -1
  72. package/dist/upgrade/converge.js +38 -15
  73. package/dist/upgrade/converge.js.map +1 -1
  74. package/dist/upgrader-setup.d.ts.map +1 -1
  75. package/dist/upgrader-setup.js +9 -3
  76. package/dist/upgrader-setup.js.map +1 -1
  77. package/dist/wizards/gcp-cost.js +3 -3
  78. package/dist/wizards/gcp-cost.js.map +1 -1
  79. package/dist/wizards/gcp-pulumi.d.ts +6 -0
  80. package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
  81. package/dist/wizards/gcp-pulumi.js +7 -0
  82. package/dist/wizards/gcp-pulumi.js.map +1 -1
  83. package/dist/wizards/gcp.d.ts.map +1 -1
  84. package/dist/wizards/gcp.js +32 -30
  85. package/dist/wizards/gcp.js.map +1 -1
  86. package/package.json +4 -4
  87. package/src/admin-memory.test.ts +1 -1
  88. package/src/agent/command.ts +1 -0
  89. package/src/agent/run.test.ts +1 -0
  90. package/src/audit-job-setup.test.ts +234 -0
  91. package/src/audit-job-setup.ts +360 -0
  92. package/src/aws-task-roles.ts +0 -2
  93. package/src/caddy-redirects.docker.test.ts +19 -6
  94. package/src/caddy-redirects.test.ts +1 -1
  95. package/src/caddy.test.ts +30 -0
  96. package/src/caddy.ts +43 -0
  97. package/src/checkpoint-secrets.test.ts +0 -2
  98. package/src/cli-init-validation.test.ts +44 -0
  99. package/src/cli-routing.test.ts +27 -1
  100. package/src/cli-routing.ts +131 -0
  101. package/src/cli.ts +41 -15
  102. package/src/compose.ts +13 -5
  103. package/src/gateway-credentials.test.ts +0 -29
  104. package/src/gateway-credentials.ts +11 -25
  105. package/src/gateway-env-isolation.test.ts +7 -2
  106. package/src/gcp-names.ts +28 -1
  107. package/src/index.test.ts +7 -6
  108. package/src/infra-contract.test.ts +4 -3
  109. package/src/infra-contract.ts +14 -1
  110. package/src/private-file.test.ts +80 -0
  111. package/src/private-file.ts +50 -0
  112. package/src/release-contract.ts +3 -0
  113. package/src/runtime-secrets.test.ts +9 -81
  114. package/src/runtime-secrets.ts +2 -6
  115. package/src/self-hosted-compose.test.ts +1 -1
  116. package/src/self-hosted-roles.integration.test.ts +23 -38
  117. package/src/self-hosted-roles.test.ts +11 -7
  118. package/src/self-hosted-roles.ts +17 -16
  119. package/src/self-hosted-upgrade.test.ts +28 -1
  120. package/src/self-hosted-upgrade.ts +29 -4
  121. package/src/stack-contract.test.ts +19 -11
  122. package/src/stack-contract.ts +54 -5
  123. package/src/stack-converge-secrets.test.ts +2 -0
  124. package/src/stack-converge.test.ts +3 -1
  125. package/src/tar-entries.test.ts +149 -7
  126. package/src/tar-entries.ts +90 -9
  127. package/src/upgrade/app-roll.ts +39 -0
  128. package/src/upgrade/audit-job-roll.ts +68 -0
  129. package/src/upgrade/converge.ts +47 -20
  130. package/src/upgrade/upgrade.test.ts +76 -2
  131. package/src/upgrader-assess.test.ts +1 -0
  132. package/src/upgrader-job.test.ts +1 -0
  133. package/src/upgrader-setup.test.ts +1 -0
  134. package/src/upgrader-setup.ts +9 -2
  135. package/src/wizards/gcp-cost.ts +3 -3
  136. package/src/wizards/gcp-pulumi.ts +7 -0
  137. package/src/wizards/gcp.ts +38 -31
  138. package/stacks/aws/README.md +1 -1
  139. package/stacks/aws/index.ts +9 -6
  140. package/stacks/azure/README.md +1 -1
  141. package/stacks/azure/index.ts +18 -6
  142. package/stacks/gcp/README.md +1 -1
  143. package/stacks/gcp/index.ts +14 -4
  144. package/stacks/gcp-firebase/index.ts +14 -4
  145. package/stacks/self-hosted/index.ts +4 -11
@@ -0,0 +1,360 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ /**
4
+ * #875 — set up (and converge) the parallel quality-audit job on a Google
5
+ * Cloud install (`gcp`, `gcp-firebase`). Run by the wizard after `pulumi up`
6
+ * and by `cms-provision upgrade`'s converge, both as the operator's own
7
+ * gcloud identity. App-roll moves the job's image with the admin's
8
+ * (upgrade/app-roll.ts), because the job runs the admin image.
9
+ *
10
+ * Why a separate job at all (CLAUDE.md §11.B Tier 5 exception): Lighthouse
11
+ * is CPU-bound and simulates the CPU slowdown from observed task durations,
12
+ * so a run that shares a CPU with another run or with the editor's chat
13
+ * measures that interference too. One task per run measures the page alone,
14
+ * and all runs finish in about the time of one.
15
+ *
16
+ * Why the CLI owns it and not the stacks: `upgrade` never runs Pulumi, so
17
+ * existing installs get the job only from here, and a bucket or job both
18
+ * Pulumi and `upgrade` create would collide on the next wizard re-run (the
19
+ * same reasoning as operator-access.ts and upgrader-setup.ts).
20
+ *
21
+ * Trust boundary. The job's service account, `caelo-<env>-audit`, holds:
22
+ *
23
+ * - `roles/storage.objectCreator` on the audit results bucket — create
24
+ * only: it can neither read results back nor overwrite one;
25
+ * - on `gcp` only, `roles/storage.objectViewer` on the staging and static
26
+ * buckets: it serves the staged build to its own browser, as the admin's
27
+ * audit does (admin-core quality/staged-origin.ts). gcp-firebase audits
28
+ * the public preview channel and needs no bucket;
29
+ * - `roles/logging.logWriter`.
30
+ *
31
+ * Nothing else: no Secret Manager, no Cloud SQL, and no VPC egress, so the
32
+ * database's private IP is not even routable from a task. Plugin data on a
33
+ * page comes the way any visitor gets it: the task's browser calls the
34
+ * site's public `/api/*` (the gateway, behind Cloud Armor) over the
35
+ * internet. The admin's run
36
+ * SA may run the job WITH overrides (`roles/run.jobsExecutorWithOverrides`:
37
+ * it passes each audit's batch of orders), read its executions
38
+ * (`roles/run.viewer`) and read the results bucket. Unlike the
39
+ * operator-access and upgrader jobs, overrides are acceptable here: a
40
+ * compromised admin can make the job load other URLs, but the job holds
41
+ * nothing worth taking. {@link AUDIT_FORBIDDEN_ROLES} names that line; the
42
+ * tests assert no grant crosses it.
43
+ */
44
+
45
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
46
+ import { tmpdir } from "node:os";
47
+ import { join } from "node:path";
48
+ import { gcloud as defaultGcloud, type GcloudResult } from "./gcloud.js";
49
+ import { type GcloudRunner, realSleep, runWithRetry, type Sleep } from "./gcloud-retry.js";
50
+ import {
51
+ auditJobName,
52
+ auditServiceAccountEmail,
53
+ auditServiceAccountId,
54
+ gcpBucketName,
55
+ runServiceAccountEmail,
56
+ } from "./gcp-names.js";
57
+ import { readAdminConfig } from "./migration-runner.js";
58
+ import type { GcpProvider } from "./stack-contract.js";
59
+ import { describeAuditJobImage } from "./upgrade/audit-job-roll.js";
60
+
61
+ /** Where the admin image ships the task's entry point. */
62
+ export const AUDIT_TASK_SCRIPT = "/app/packages/admin-core/src/quality/audit-task.ts";
63
+
64
+ /** How many audit tasks run at once by default: 5 pages x 3 runs. */
65
+ export const AUDIT_PARALLELISM_DEFAULT = 15;
66
+
67
+ /**
68
+ * Per task: container start, browser start, CPU benchmark and one
69
+ * Lighthouse run. Matches admin-core `parallelAuditTimeoutMs`, which the
70
+ * admin also passes per execution.
71
+ */
72
+ const TASK_TIMEOUT = "5m";
73
+
74
+ /**
75
+ * Roles the audit job's identity must never hold anywhere. Prefix matches
76
+ * (`roles/secretmanager.` = every Secret Manager role).
77
+ */
78
+ export const AUDIT_FORBIDDEN_ROLES = [
79
+ "roles/owner",
80
+ "roles/editor",
81
+ "roles/run.",
82
+ "roles/iam.",
83
+ "roles/secretmanager.",
84
+ "roles/cloudsql.",
85
+ "roles/compute.",
86
+ "roles/resourcemanager.",
87
+ "roles/storage.admin",
88
+ "roles/storage.objectAdmin",
89
+ "roles/storage.objectUser",
90
+ ] as const;
91
+
92
+ /** Delete results after a day: the admin reads them within minutes. */
93
+ export const AUDIT_RESULTS_LIFECYCLE = {
94
+ rule: [{ action: { type: "Delete" }, condition: { age: 1 } }],
95
+ };
96
+
97
+ export interface AuditJobTarget {
98
+ readonly provider: GcpProvider;
99
+ readonly projectId: string;
100
+ readonly region: string;
101
+ /** Pulumi stack name (`production`). */
102
+ readonly env: string;
103
+ /** The admin image the job runs, digest-pinned (`…/admin@sha256:…`). */
104
+ readonly adminImageRef: string;
105
+ /**
106
+ * Tasks at once (`auditParallelism`, 1–15). Absent on converge: an
107
+ * existing job keeps the value it has, so an operator's choice survives
108
+ * `upgrade`; a job the converge creates gets
109
+ * {@link AUDIT_PARALLELISM_DEFAULT}, as a fresh install's does.
110
+ */
111
+ readonly parallelism?: number;
112
+ }
113
+
114
+ export type AuditJobResult =
115
+ | { readonly ok: true; readonly done: string[] }
116
+ | { readonly ok: false; readonly done: string[]; readonly error: string };
117
+
118
+ /** Ensure the audit job and everything it needs. Never throws for a gcloud failure. */
119
+ export async function ensureAuditJob(
120
+ target: AuditJobTarget,
121
+ deps: { run?: GcloudRunner; sleep?: Sleep } = {},
122
+ ): Promise<AuditJobResult> {
123
+ const run = deps.run ?? defaultGcloud;
124
+ const sleep = deps.sleep ?? realSleep;
125
+ const retry = (args: string[]) => runWithRetry(run, sleep, args);
126
+ const { projectId, region, env, provider } = target;
127
+ const project = `--project=${projectId}`;
128
+ const sa = auditServiceAccountEmail(projectId, env);
129
+ const member = `serviceAccount:${sa}`;
130
+ const runSaMember = `serviceAccount:${runServiceAccountEmail(projectId, env)}`;
131
+ const job = auditJobName(env);
132
+ const results = gcpBucketName(projectId, env, "audit-results");
133
+ const resultsUrl = `gs://${results}`;
134
+ const done: string[] = [];
135
+ const fail = (step: string, r: GcloudResult): AuditJobResult => ({
136
+ ok: false,
137
+ done,
138
+ error: `${step}: ${r.stderr.trim()}`,
139
+ });
140
+
141
+ if (!/@sha256:[0-9a-f]{64}$/.test(target.adminImageRef)) {
142
+ return {
143
+ ok: false,
144
+ done,
145
+ error: `the audit job's image must be pinned by digest, got ${target.adminImageRef}`,
146
+ };
147
+ }
148
+ if (
149
+ target.parallelism !== undefined &&
150
+ (!Number.isInteger(target.parallelism) || target.parallelism < 1 || target.parallelism > 15)
151
+ ) {
152
+ return {
153
+ ok: false,
154
+ done,
155
+ error: `auditParallelism must be a whole number from 1 to 15, got ${target.parallelism}`,
156
+ };
157
+ }
158
+
159
+ // 1. The job's own service account.
160
+ const describe = await run([
161
+ "iam",
162
+ "service-accounts",
163
+ "describe",
164
+ sa,
165
+ project,
166
+ "--format=value(email)",
167
+ ]);
168
+ if (!describe.ok) {
169
+ const create = await run([
170
+ "iam",
171
+ "service-accounts",
172
+ "create",
173
+ auditServiceAccountId(env),
174
+ "--display-name=Caelo quality audit",
175
+ "--description=Runs the quality audit's Lighthouse runs in parallel; reads the staged build, writes audit results, nothing else.",
176
+ project,
177
+ ]);
178
+ if (!create.ok && !/already exists/i.test(create.stderr)) return fail(`create ${sa}`, create);
179
+ }
180
+ done.push(`service account ${sa}`);
181
+
182
+ // 2. The results bucket: private, results deleted after a day.
183
+ const owned = await run(["storage", "buckets", "list", project, "--format=value(name)"]);
184
+ if (!owned.ok) return fail(`list the buckets of project ${projectId}`, owned);
185
+ const names = owned.stdout.split("\n").map((l) =>
186
+ l
187
+ .trim()
188
+ .replace(/^gs:\/\//, "")
189
+ .replace(/\/$/, ""),
190
+ );
191
+ if (!names.includes(results)) {
192
+ const created = await run([
193
+ "storage",
194
+ "buckets",
195
+ "create",
196
+ resultsUrl,
197
+ project,
198
+ `--location=${region}`,
199
+ "--default-storage-class=STANDARD",
200
+ "--uniform-bucket-level-access",
201
+ "--public-access-prevention",
202
+ "--soft-delete-duration=0",
203
+ ]);
204
+ if (!created.ok) {
205
+ return {
206
+ ok: false,
207
+ done,
208
+ error: /already exists|409/i.test(created.stderr)
209
+ ? `${resultsUrl} exists but does not belong to project ${projectId}; delete or rename that bucket, this install needs the name`
210
+ : `create ${resultsUrl}: ${created.stderr.trim()}`,
211
+ };
212
+ }
213
+ }
214
+ // A private directory (mkdtemp, mode 700): no other local user can swap
215
+ // the file between write and read.
216
+ const dir = mkdtempSync(join(tmpdir(), "caelo-audit-"));
217
+ try {
218
+ const file = join(dir, "lifecycle.json");
219
+ writeFileSync(file, JSON.stringify(AUDIT_RESULTS_LIFECYCLE), { mode: 0o600 });
220
+ const lifecycle = await run([
221
+ "storage",
222
+ "buckets",
223
+ "update",
224
+ resultsUrl,
225
+ project,
226
+ `--lifecycle-file=${file}`,
227
+ ]);
228
+ if (!lifecycle.ok) return fail(`set the lifecycle of ${resultsUrl}`, lifecycle);
229
+ } finally {
230
+ rmSync(dir, { recursive: true, force: true });
231
+ }
232
+ done.push(`bucket ${resultsUrl} (results deleted after 1 day)`);
233
+
234
+ // 3. Bucket grants: the job creates results and (gcp) reads the build;
235
+ // the admin reads results.
236
+ const bucketGrants: { bucket: string; member: string; role: string; why: string }[] = [
237
+ { bucket: results, member, role: "roles/storage.objectCreator", why: "tasks write results" },
238
+ ...(provider === "gcp"
239
+ ? (["staging", "static"] as const).map((b) => ({
240
+ bucket: gcpBucketName(projectId, env, b),
241
+ member,
242
+ role: "roles/storage.objectViewer",
243
+ why: "tasks serve the staged build to their browser",
244
+ }))
245
+ : []),
246
+ {
247
+ bucket: results,
248
+ member: runSaMember,
249
+ role: "roles/storage.objectViewer",
250
+ why: "the admin reads the results",
251
+ },
252
+ ];
253
+ for (const g of bucketGrants) {
254
+ const r = await retry([
255
+ "storage",
256
+ "buckets",
257
+ "add-iam-policy-binding",
258
+ `gs://${g.bucket}`,
259
+ `--member=${g.member}`,
260
+ `--role=${g.role}`,
261
+ project,
262
+ "--quiet",
263
+ "--format=none",
264
+ ]);
265
+ if (!r.ok) return fail(`grant ${g.role} on gs://${g.bucket} (${g.why})`, r);
266
+ done.push(`${g.member === member ? "audit job" : "admin"}: ${g.role} on gs://${g.bucket}`);
267
+ }
268
+
269
+ // 4. Logs.
270
+ const logs = await retry([
271
+ "projects",
272
+ "add-iam-policy-binding",
273
+ projectId,
274
+ `--member=${member}`,
275
+ "--role=roles/logging.logWriter",
276
+ "--condition=None",
277
+ "--quiet",
278
+ "--format=none",
279
+ ]);
280
+ if (!logs.ok) return fail("grant the audit job roles/logging.logWriter", logs);
281
+ done.push("audit job: roles/logging.logWriter on the project");
282
+
283
+ // 5. The job: the admin image, its own SA, no secrets, no VPC. A job
284
+ // created without a parallelism would run every task at once with no
285
+ // cap; an existing job keeps its own.
286
+ let parallelism = target.parallelism;
287
+ if (parallelism === undefined) {
288
+ const existing = await describeAuditJobImage(run, { projectId, region, env });
289
+ if (!existing.ok) return { ok: false, done, error: `read job ${job}: ${existing.error}` };
290
+ if (existing.image === null) parallelism = AUDIT_PARALLELISM_DEFAULT;
291
+ }
292
+ const envVars = [
293
+ `CAELO_PROVIDER=${provider}`,
294
+ `CAELO_AUDIT_RESULTS_BUCKET=${results}`,
295
+ ...(provider === "gcp"
296
+ ? [
297
+ `CAELO_STAGING_BUCKET=${gcpBucketName(projectId, env, "staging")}`,
298
+ `CAELO_STATIC_BUCKET=${gcpBucketName(projectId, env, "static")}`,
299
+ ]
300
+ : []),
301
+ ];
302
+ const deploy = await run([
303
+ "run",
304
+ "jobs",
305
+ "deploy",
306
+ job,
307
+ `--image=${target.adminImageRef}`,
308
+ `--region=${region}`,
309
+ project,
310
+ `--service-account=${sa}`,
311
+ "--command=bun",
312
+ `--args=--bun,${AUDIT_TASK_SCRIPT}`,
313
+ `--set-env-vars=${envVars.join(",")}`,
314
+ "--max-retries=0",
315
+ `--task-timeout=${TASK_TIMEOUT}`,
316
+ // Lighthouse wants two cores: the browser and the trace processing.
317
+ "--cpu=2",
318
+ "--memory=2Gi",
319
+ ...(parallelism !== undefined ? [`--parallelism=${parallelism}`] : []),
320
+ "--quiet",
321
+ ]);
322
+ if (!deploy.ok) return fail(`deploy job ${job}`, deploy);
323
+ done.push(`job ${job} (${target.adminImageRef.split("@")[1]?.slice(0, 19)}...)`);
324
+
325
+ // 6. The admin may run it with its batch and read its executions.
326
+ for (const role of ["roles/run.jobsExecutorWithOverrides", "roles/run.viewer"]) {
327
+ const r = await retry([
328
+ "run",
329
+ "jobs",
330
+ "add-iam-policy-binding",
331
+ job,
332
+ `--region=${region}`,
333
+ project,
334
+ `--member=${runSaMember}`,
335
+ `--role=${role}`,
336
+ "--quiet",
337
+ "--format=none",
338
+ ]);
339
+ if (!r.ok) return fail(`grant the admin ${role} on ${job}`, r);
340
+ done.push(`admin: ${role} on ${job}`);
341
+ }
342
+ return { ok: true, done };
343
+ }
344
+
345
+ /**
346
+ * {@link ensureAuditJob}'s image: the deployed admin's, so the job runs the
347
+ * code the admin runs.
348
+ */
349
+ export async function resolveAdminImage(
350
+ opts: { readonly projectId: string; readonly region: string; readonly env: string },
351
+ deps: { run?: GcloudRunner } = {},
352
+ ): Promise<{ ok: true; imageRef: string } | { ok: false; error: string }> {
353
+ const cfg = await readAdminConfig(opts, deps.run ?? defaultGcloud);
354
+ if (!cfg)
355
+ return {
356
+ ok: false,
357
+ error: "could not read the admin service's image (is the admin deployed?)",
358
+ };
359
+ return { ok: true, imageRef: cfg.imageRef };
360
+ }
@@ -14,7 +14,6 @@
14
14
  export type AwsSecret =
15
15
  | "postgres-password"
16
16
  | "public-role-password"
17
- | "gateway-role-password"
18
17
  | "csrf-secret"
19
18
  | "cookie-secret"
20
19
  | "anthropic-api-key"
@@ -32,7 +31,6 @@ export const AWS_SERVICE_SECRETS: Readonly<Record<AwsTaskService, readonly AwsSe
32
31
  admin: [
33
32
  "postgres-password",
34
33
  "public-role-password",
35
- "gateway-role-password",
36
34
  "csrf-secret",
37
35
  "cookie-secret",
38
36
  "anthropic-api-key",
@@ -47,6 +47,8 @@ const LIVE = "example.test";
47
47
  const STAGING = "staging.example.test";
48
48
  const CADDY_DIR = resolve(import.meta.dir, "../caddy");
49
49
  const SERVED = ["index.html", "about/index.html", "blog/index.html"];
50
+ /** Consecutive matching answers that count as "the reload settled". */
51
+ const SETTLED_PROBES = 5;
50
52
 
51
53
  function docker(args: string[]): { ok: boolean; out: string } {
52
54
  const r = Bun.spawnSync(["docker", ...args], { stdout: "pipe", stderr: "pipe" });
@@ -100,11 +102,25 @@ describe.skipIf(!dockerUp)(`self-hosted redirects through ${CADDY_IMAGE}`, () =>
100
102
  const location = head.match(/^location: ([^\r\n]*)/im)?.[1]?.trim() ?? null;
101
103
  return { status, location };
102
104
  };
105
+ /**
106
+ * Waits until `path` answers with `location` on {@link SETTLED_PROBES}
107
+ * consecutive connections. One matching answer is not enough: Caddy's
108
+ * graceful reload keeps the old config's listener open next to the new
109
+ * one (SO_REUSEPORT) while it drains, so the connection after the first
110
+ * new answer can still reach the old config (CI runs 38867358899 and
111
+ * 39242522450). Only a streak of new answers shows the reload settled.
112
+ */
103
113
  const eventually = async (host: string, path: string, location: string | null) => {
104
114
  const deadline = Date.now() + 30_000;
115
+ let streak = 0;
105
116
  for (;;) {
106
117
  const got = probe(host, path).location;
107
- if (got === location) return;
118
+ streak = got === location ? streak + 1 : 0;
119
+ if (streak >= SETTLED_PROBES) return;
120
+ if (got === location) {
121
+ await Bun.sleep(200);
122
+ continue;
123
+ }
108
124
  if (Date.now() > deadline) {
109
125
  throw new Error(
110
126
  `${host}${path} → ${got}, expected ${location}: ${docker(["logs", name]).out}`,
@@ -241,11 +257,8 @@ describe.skipIf(!dockerUp)(`self-hosted redirects through ${CADDY_IMAGE}`, () =>
241
257
  it("the reload loop picks up a deploy's new snippet", async () => {
242
258
  write("production", snippet([["/brand-new", "/blog", 301]]));
243
259
  await eventually(LIVE, "/brand-new", "/blog/");
244
- // Caddy's graceful reload keeps the old config's listener open for a
245
- // moment next to the new one (SO_REUSEPORT), so a connection right
246
- // after the first new answer can still reach the old config: the old
247
- // redirect may answer once more (CI run 38867358899). What must hold is
248
- // that the new snippet REPLACES the old one once the reload settled.
260
+ // What must hold is that the new snippet REPLACES the old one once the
261
+ // reload settled (see `eventually` for why one answer is not enough).
249
262
  await eventually(LIVE, "/old", null);
250
263
  expect(probe(LIVE, "/old").status).toBe(404);
251
264
  expect(probe(LIVE, "/brand-new").location).toBe("/blog/");
@@ -61,7 +61,7 @@ describe("Compose — Caddy picks up new redirects (#587)", () => {
61
61
  const yaml = generateDockerCompose({
62
62
  domain: "example.com",
63
63
  postgresPassword: "pgpw",
64
- rolePasswords: { admin: "a", public: "p", gateway: "g" },
64
+ rolePasswords: { admin: "a", public: "p" },
65
65
  caeloSecretKek: "0".repeat(64),
66
66
  diskSize: "20Gi",
67
67
  ports: { http: 80, https: 443 },
package/src/caddy.test.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import { describe, expect, it } from "bun:test";
4
4
  import {
5
5
  type CaddyfileSpec,
6
+ caddyTokenProblem,
6
7
  generateCaddyfile,
7
8
  selfHostedAdminHost,
8
9
  selfHostedSeedDomains,
@@ -105,4 +106,33 @@ describe("Caddyfile TLS", () => {
105
106
  const out = generateCaddyfile({ ...spec, tls: "internal" });
106
107
  expect(out.startsWith("{\n email owner@example.com\n local_certs\n}\n")).toBe(true);
107
108
  });
109
+
110
+ it("rejects unsafe characters in ownerEmail and domain hostnames", () => {
111
+ expect(() => generateCaddyfile({ ...spec, ownerEmail: "owner@example.com\n debug" })).toThrow(
112
+ /invalid ownerEmail/,
113
+ );
114
+ expect(() =>
115
+ generateCaddyfile({
116
+ ...spec,
117
+ domains: [
118
+ { hostname: "evil.example.com {\n respond 200\n}", kind: "public", env: "production" },
119
+ ],
120
+ }),
121
+ ).toThrow(/invalid hostname/);
122
+ });
123
+
124
+ it("refuses a comma in a site address — it would add a second address to the block", () => {
125
+ // `admin.example.com,:80` would serve the admin on every host on port 80.
126
+ expect(() =>
127
+ generateCaddyfile({
128
+ ...spec,
129
+ domains: [{ hostname: "admin.example.com,:80", kind: "admin", env: "production" }],
130
+ }),
131
+ ).toThrow(/invalid hostname/);
132
+ // A comma is harmless inside a token like the email.
133
+ expect(caddyTokenProblem("a,b@example.com", "token")).toBeNull();
134
+ expect(caddyTokenProblem("a,b.example.com", "address")).toMatch(/','/);
135
+ expect(caddyTokenProblem("", "token")).toBe("it is empty");
136
+ expect(caddyTokenProblem("192.168.1.10", "address")).toBeNull();
137
+ });
108
138
  });
package/src/caddy.ts CHANGED
@@ -103,9 +103,52 @@ export function selfHostedSeedDomains(domain: string, stagingDomain: string): Ca
103
103
  ];
104
104
  }
105
105
 
106
+ /**
107
+ * Characters that would let a value end its Caddyfile token and start new
108
+ * directives or blocks: whitespace (incl. newlines), both quote styles Caddy
109
+ * tokenizes (`"` and backtick), braces (blocks and `{placeholders}`), the
110
+ * comment marker, escapes and semicolons. A site address additionally may
111
+ * not hold a comma, which would add further addresses to the same block.
112
+ */
113
+ const UNSAFE_CADDY_TOKEN_RE = /[\s"`{}#\\;]/;
114
+ const UNSAFE_CADDY_ADDRESS_RE = /[\s"`{}#\\;,]/;
115
+
116
+ /**
117
+ * Why `value` cannot go into the Caddyfile as one token, or null when it can.
118
+ * `init` checks the operator's domains and email with this before it writes
119
+ * anything, so a bad value never leaves a half-made install behind.
120
+ *
121
+ * @param kind `address` for a site address (hostname), `token` for any other value
122
+ */
123
+ export function caddyTokenProblem(value: string, kind: "address" | "token"): string | null {
124
+ if (value.length === 0) return "it is empty";
125
+ const unsafe = kind === "address" ? UNSAFE_CADDY_ADDRESS_RE : UNSAFE_CADDY_TOKEN_RE;
126
+ if (unsafe.test(value)) {
127
+ return `it contains whitespace, a quote, a brace, '#', '\\', ';'${kind === "address" ? " or ','" : ""}`;
128
+ }
129
+ return null;
130
+ }
131
+
132
+ function assertSafeCaddyToken(label: string, value: string, kind: "address" | "token"): void {
133
+ const problem = caddyTokenProblem(value, kind);
134
+ if (problem) {
135
+ throw new Error(
136
+ `Caddyfile: invalid ${label} ${JSON.stringify(value)} — ${problem}. Fix the value in .caelo/config.json (or the domain you added) and run the command again.`,
137
+ );
138
+ }
139
+ }
140
+
141
+ /** Every caller-supplied value is checked before any of it is emitted (#888). */
106
142
  export function generateCaddyfile(spec: CaddyfileSpec): string {
143
+ assertSafeCaddyToken("ownerEmail", spec.ownerEmail, "token");
144
+ assertSafeCaddyToken("publicSiteRoot", spec.publicSiteRoot, "token");
145
+ assertSafeCaddyToken("stagingSiteRoot", spec.stagingSiteRoot, "token");
146
+ assertSafeCaddyToken("adminUpstream", spec.adminUpstream, "token");
147
+ assertSafeCaddyToken("gatewayUpstream", spec.gatewayUpstream, "token");
148
+
107
149
  const hostnames = new Set<string>();
108
150
  for (const d of spec.domains) {
151
+ assertSafeCaddyToken("hostname", d.hostname, "address");
109
152
  if (hostnames.has(d.hostname)) {
110
153
  // Caddy refuses a config with two site blocks for one address, so a
111
154
  // duplicate would take every vhost down, not just this one.
@@ -294,7 +294,6 @@ describe("rotationHints", () => {
294
294
  [
295
295
  { path: "a", kind: "credential-url", user: "admin_role" },
296
296
  { path: "b", kind: "credential-url", user: "public_role" },
297
- { path: "c", kind: "credential-url", user: "gateway_role" },
298
297
  { path: "d", kind: "credential-url", user: "admin_role" },
299
298
  ],
300
299
  "gcp-acme",
@@ -302,7 +301,6 @@ describe("rotationHints", () => {
302
301
  expect(hints).toEqual([
303
302
  `admin_role's password was stored in progress.json. Rotate it: bunx @caelo-cms/provisioning@${CAELO_VERSION} rotate-secret postgres-password --install gcp-acme`,
304
303
  `public_role's password was stored in progress.json. Rotate it: bunx @caelo-cms/provisioning@${CAELO_VERSION} rotate-secret public-role-password --install gcp-acme`,
305
- `gateway_role's password was stored in progress.json. Rotate it: bunx @caelo-cms/provisioning@${CAELO_VERSION} rotate-secret gateway-role-password --install gcp-acme`,
306
304
  ]);
307
305
  });
308
306
 
@@ -0,0 +1,44 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
3
+ import { existsSync, mkdtempSync, rmSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join, resolve } from "node:path";
6
+
7
+ const CLI = resolve(import.meta.dir, "cli.ts");
8
+
9
+ let dir: string;
10
+ beforeEach(() => {
11
+ dir = mkdtempSync(join(tmpdir(), "caelo-init-validation-"));
12
+ });
13
+ afterEach(() => rmSync(dir, { recursive: true, force: true }));
14
+
15
+ function init(...args: string[]) {
16
+ return Bun.spawnSync([process.execPath, CLI, "init", ...args], {
17
+ env: { ...process.env, CAELO_SELF_HOSTED_DIR: join(dir, ".caelo") },
18
+ stdout: "pipe",
19
+ stderr: "pipe",
20
+ });
21
+ }
22
+
23
+ describe("cms-provision init refuses a value the Caddyfile cannot hold before writing anything", () => {
24
+ // Checked only when the Caddyfile was generated, a bad value left
25
+ // config.json behind, and the re-run stopped at "config already exists".
26
+ for (const [flag, value] of [
27
+ ["--domain", "example.com,:80"],
28
+ ["--domain", "example.com {\n respond 200\n}"],
29
+ ["--owner-email", "owner@example.com\n debug"],
30
+ ["--staging-domain", "staging.example.com #"],
31
+ ] as const) {
32
+ it(`${flag} ${JSON.stringify(value)}`, () => {
33
+ const base: Record<string, string> = {
34
+ "--domain": "example.com",
35
+ "--owner-email": "owner@example.com",
36
+ };
37
+ base[flag] = value;
38
+ const r = init(...Object.entries(base).flat());
39
+ expect(r.exitCode).toBe(2);
40
+ expect(r.stderr.toString()).toContain(flag);
41
+ expect(existsSync(join(dir, ".caelo"))).toBe(false);
42
+ });
43
+ }
44
+ });
@@ -1,7 +1,9 @@
1
1
  // SPDX-License-Identifier: MPL-2.0
2
2
 
3
3
  import { describe, expect, it } from "bun:test";
4
- import { initDelegatesToWizard, resolveCliRoute } from "./cli-routing.js";
4
+ import { readFileSync } from "node:fs";
5
+ import { join } from "node:path";
6
+ import { CLI_COMMANDS, cliUsage, initDelegatesToWizard, resolveCliRoute } from "./cli-routing.js";
5
7
 
6
8
  const HANDLERS = ["init", "upgrade", "wizard", "version", "--version", "-v"];
7
9
  const argv = (...rest: string[]) => ["bun", "cli.js", ...rest];
@@ -55,3 +57,27 @@ describe("initDelegatesToWizard", () => {
55
57
  expect(initDelegatesToWizard("self-hosted")).toBe(false);
56
58
  });
57
59
  });
60
+
61
+ describe("CLI_COMMANDS / cliUsage", () => {
62
+ it("prints the usage line operators already know, built from the catalogue", () => {
63
+ expect(cliUsage().split("\n")[0]).toBe(
64
+ "Usage: cms-provision [wizard] [--provider <name> --domain <d> [--staging-domain <host>] --owner-email <e> --region <r>] [--local|--install-job] | <init|up|migrate|status|upgrade|backup|restore|rotate-secret|truncate|destroy|regenerate-caddy|admin-domain|staging-domain|owner-reset|state migrate|attach|pulumi-output-sync|version> [options]",
65
+ );
66
+ });
67
+
68
+ it("documents only sub-commands cli.ts actually dispatches", () => {
69
+ const cliSource = readFileSync(join(import.meta.dir, "cli.ts"), "utf8");
70
+ const handlerBlock = cliSource.slice(cliSource.indexOf("const handlers"));
71
+ for (const c of CLI_COMMANDS) {
72
+ const head = c.name.split(" ")[0] as string;
73
+ expect(handlerBlock).toMatch(new RegExp(`\\n\\s+"?${head}"?[:,]`));
74
+ }
75
+ });
76
+
77
+ it("gives every command a one-sentence summary", () => {
78
+ for (const c of CLI_COMMANDS) {
79
+ expect(c.summary.length).toBeGreaterThan(10);
80
+ expect(c.summary.endsWith(".")).toBe(true);
81
+ }
82
+ });
83
+ });