@caelo-cms/provisioning 0.12.11 → 0.12.12

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.
@@ -338,13 +338,15 @@ const ANY_WRITE =
338
338
  /\b(create|update|update-traffic|delete|add-iam-policy-binding|remove-iam-policy-binding|set-iam-policy|execute|enable|add|restore)\b/;
339
339
 
340
340
  /**
341
- * #1328 — the only writes allowed before the backup: the MCP-Auth-Proxy's
342
- * account, its secretAccessor on its two secrets, and the services an older
343
- * install lacks (mcp-auth-proxy-setup.ts), with (#1654) the staging
344
- * gateway's read of its own visitor-AI key. Additive; nothing serves on them.
341
+ * #1328 — the only writes allowed before the backup: the gateway's account
342
+ * and the CLI-generated secrets an older install lacks (#1682), the
343
+ * MCP-Auth-Proxy's account, its secretAccessor on its two secrets, and the
344
+ * services an older install lacks (mcp-auth-proxy-setup.ts), with (#1654)
345
+ * the staging gateway's read of its own visitor-AI key. Additive; nothing
346
+ * serves on them.
345
347
  */
346
348
  const PRE_PLAN_WRITE =
347
- /^(iam service-accounts create caelo-production-(mcp-proxy-sa|stg-gw-sa) |secrets add-iam-policy-binding caelo-production-(mcp-oauth-seal-key|operator-oauth-client) .*--member=serviceAccount:caelo-production-mcp-proxy-sa@|secrets add-iam-policy-binding caelo-production-(public-staging-role-password|visitor-ai-staging-key) .*--member=serviceAccount:caelo-production-stg-gw-sa@|storage buckets (create|add-iam-policy-binding) gs:\/\/acme-caelo-production-staging |run services replace |run deploy caelo-production-(mcp-auth-proxy|staging-gateway) |run services add-iam-policy-binding caelo-production-staging-gateway .*--member=allUsers )/;
349
+ /^(iam service-accounts create caelo-production-(gateway-sa|mcp-proxy-sa|stg-gw-sa) |secrets (create|versions add) caelo-production-\S+ |secrets add-iam-policy-binding caelo-production-(mcp-oauth-seal-key|operator-oauth-client) .*--member=serviceAccount:caelo-production-mcp-proxy-sa@|secrets add-iam-policy-binding caelo-production-(public-staging-role-password|visitor-ai-staging-key) .*--member=serviceAccount:caelo-production-stg-gw-sa@|storage buckets (create|add-iam-policy-binding) gs:\/\/acme-caelo-production-staging |run services replace |run deploy caelo-production-(mcp-auth-proxy|staging-gateway) |run services add-iam-policy-binding caelo-production-staging-gateway .*--member=allUsers )/;
348
350
 
349
351
  const indexOf = (events: string[], pattern: RegExp): number => {
350
352
  const i = events.findIndex((e) => pattern.test(e));
@@ -352,6 +354,38 @@ const indexOf = (events: string[], pattern: RegExp): number => {
352
354
  return i;
353
355
  };
354
356
 
357
+ /**
358
+ * #1682 — Secret Manager as on an install that predates the secrets `names`:
359
+ * each is missing until `secrets create` makes it, and a grant on a missing
360
+ * one answers 404, as gcloud does.
361
+ */
362
+ function secretsMissing(names: readonly string[]): [RegExp, Answer][] {
363
+ const ids = names.map((n) => `caelo-production-${n}`).join("|");
364
+ const created = new Set<string>();
365
+ const secretOf = (line: string) => line.split(" ")[line.startsWith("secrets versions") ? 3 : 2];
366
+ const known = (line: string, answer: GcloudResult): GcloudResult =>
367
+ created.has(secretOf(line) as string)
368
+ ? answer
369
+ : fail(
370
+ `ERROR: (gcloud.secrets) Status code: 404. Secret [projects/42/secrets/${secretOf(line)}] not found.`,
371
+ );
372
+ return [
373
+ [
374
+ new RegExp(`^secrets create (${ids}) `),
375
+ (l) => {
376
+ created.add(secretOf(l) as string);
377
+ return ok("");
378
+ },
379
+ ],
380
+ [new RegExp(`^secrets describe (${ids}) `), (l) => known(l, ok("name\n"))],
381
+ [
382
+ new RegExp(`^secrets versions list (${ids}) `),
383
+ (l) => known(l, ok(`projects/42/secrets/${secretOf(l)}/versions/1\n`)),
384
+ ],
385
+ [new RegExp(`^secrets add-iam-policy-binding (${ids}) `), (l) => known(l, ok("{}"))],
386
+ ];
387
+ }
388
+
355
389
  describe("upgradeGcpInstall — converge, then app-roll", () => {
356
390
  it("takes the backup first, migrates before rolling, checks health, records the digests", async () => {
357
391
  const events: string[] = [];
@@ -369,8 +403,7 @@ describe("upgradeGcpInstall — converge, then app-roll", () => {
369
403
  const deep = indexOf(events, /^health deep 0\.11\.0/);
370
404
  const digests = indexOf(events, /^record digests/);
371
405
 
372
- // Before the backup only the additive pre-plan step writes (#1328): the
373
- // MCP-Auth-Proxy's account and its two secret grants.
406
+ // Before the backup only the additive pre-plan step writes (#1328, #1682).
374
407
  for (const e of events.slice(0, backup)) expect(e).not.toMatch(/^(migrate|record)/);
375
408
  for (const e of events.slice(0, backup).filter((e) => e.startsWith("gcloud "))) {
376
409
  const call = e.slice("gcloud ".length);
@@ -389,6 +422,31 @@ describe("upgradeGcpInstall — converge, then app-roll", () => {
389
422
  expect(lines).toContain(`done: Pre-upgrade backup ${BACKUP_ID} of ${SQL} taken`);
390
423
  });
391
424
 
425
+ it("an install without the secrets a release adds gets them before anything is granted on them (#1682)", async () => {
426
+ // caelo-cms.com updating 0.12.6 → 0.12.11: the staging gateway's grant
427
+ // on its visitor-AI key ran in the pre-flight, the key was created after
428
+ // the backup, and every update stopped on a 404.
429
+ const added = ["visitor-ai-key", "visitor-ai-staging-key"];
430
+ for (const target of [TARGET, GCP_TARGET]) {
431
+ const events: string[] = [];
432
+ const { deps } = upgradeDeps(events, { overrides: secretsMissing(added) });
433
+ expect(await upgradeGcpInstall(target, deps)).toBe(true);
434
+ const backup = indexOf(events, /^gcloud sql backups create/);
435
+ for (const name of added) {
436
+ const created = indexOf(
437
+ events,
438
+ new RegExp(`^gcloud secrets create caelo-production-${name} `),
439
+ );
440
+ const granted = indexOf(
441
+ events,
442
+ new RegExp(`^gcloud secrets add-iam-policy-binding caelo-production-${name} `),
443
+ );
444
+ expect(created).toBeLessThan(granted);
445
+ expect(created).toBeLessThan(backup);
446
+ }
447
+ }
448
+ });
449
+
392
450
  it("sets up the upgrader job after migrations, before operator access keeps its caelo-mcp grant (#696)", async () => {
393
451
  const events: string[] = [];
394
452
  const { deps, lines } = upgradeDeps(events);
@@ -5,13 +5,15 @@
5
5
  * app-roll (epic #696). The order, and why:
6
6
  *
7
7
  * 1. plan: release digests, the services, the env contract — read-only
8
- * but for the services an older install lacks (#1328, additive and
9
- * serving nothing yet: mcp-auth-proxy-setup.ts)
8
+ * but for what an older install lacks, all additive and serving
9
+ * nothing yet: the gateway's account and the CLI-generated secrets
10
+ * first (#1682, runtime-secrets.ts), then the services (#1328,
11
+ * mcp-auth-proxy-setup.ts), which are granted those secrets
10
12
  * 2. verify the images' signatures
11
13
  * 3. pre-upgrade Cloud SQL backup — the first change; if it fails, the
12
14
  * install is exactly as it was
13
- * 4. converge, the part before the roll (keyless provisioner, IAM, secrets,
14
- * MCP access)
15
+ * 4. converge, the part before the roll (keyless provisioner, IAM, MCP
16
+ * access)
15
17
  * 5. app-roll: health baseline, migrations, the converge steps that must
16
18
  * follow migrations, admin, gateway and MCP-Auth-Proxy rolls, deep
17
19
  * health check, rollback
@@ -33,10 +35,12 @@
33
35
  import type { GcloudRunner, Sleep } from "../gcloud-retry.js";
34
36
  import type { ImageDigests } from "../install-state.js";
35
37
  import { ensureReleaseServices } from "../mcp-auth-proxy-setup.js";
36
- import type { HttpFetch } from "../runtime-secrets.js";
38
+ import { ensureRuntimeIdentities, type HttpFetch } from "../runtime-secrets.js";
37
39
  import { SERVICE_IMAGE } from "../stack-contract.js";
40
+ import { liveContainerEnv } from "../stack-converge.js";
38
41
  import {
39
42
  type AppRollPlan,
43
+ describeLiveService,
40
44
  executeAppRoll,
41
45
  planAppRoll,
42
46
  type ReleaseImage,
@@ -213,19 +217,42 @@ export async function upgradeGcpInstallOutcome(
213
217
  sPre.failed("The gateway image's signature did not verify");
214
218
  return failed;
215
219
  }
216
- const ensured = await ensureReleaseServices(
217
- {
218
- provider: target.provider,
219
- projectId,
220
- region: target.region,
221
- env: target.env,
222
- domain: target.domain,
223
- stagingHost: target.stagingHost,
224
- mcpAuthProxyImageRef: proxyImage.imageRef,
225
- gatewayImageRef: gatewayImage.imageRef,
226
- },
227
- { run },
228
- );
220
+ // #1682 — the gateway's account and every CLI-generated secret first:
221
+ // the services set up next are granted the secrets they read, a secret
222
+ // this release adds included (#1654's visitor-AI keys). A secret the
223
+ // admin still runs as a plain env var keeps that value.
224
+ const install = {
225
+ provider: target.provider,
226
+ projectId,
227
+ region: target.region,
228
+ env: target.env,
229
+ domain: target.domain,
230
+ stagingHost: target.stagingHost,
231
+ credentials: target.credentials,
232
+ };
233
+ const admin = await describeLiveService(install, "admin", run);
234
+ if (!admin.ok) {
235
+ sPre.failed("Pre-flight failed");
236
+ r.error(admin.error);
237
+ return failed;
238
+ }
239
+ const identities = await ensureRuntimeIdentities(install, liveContainerEnv(admin.json), {
240
+ run,
241
+ ...(deps.sleep ? { sleep: deps.sleep } : {}),
242
+ });
243
+ const ensured = identities.some((o) => o.status === "failed")
244
+ ? identities
245
+ : [
246
+ ...identities,
247
+ ...(await ensureReleaseServices(
248
+ {
249
+ ...install,
250
+ mcpAuthProxyImageRef: proxyImage.imageRef,
251
+ gatewayImageRef: gatewayImage.imageRef,
252
+ },
253
+ { run, ...(deps.sleep ? { sleep: deps.sleep } : {}) },
254
+ )),
255
+ ];
229
256
  const ensureFailed = ensured.filter((o) => o.status === "failed");
230
257
  if (ensureFailed.length > 0) {
231
258
  sPre.failed("The services this release runs could not be set up. Nothing was changed.");
@@ -235,20 +262,7 @@ export async function upgradeGcpInstallOutcome(
235
262
  for (const o of ensured.filter((o) => o.status === "applied")) {
236
263
  r.info(` created: ${o.id}${o.detail ? ` (${o.detail})` : ""}`);
237
264
  }
238
- const planned = await planAppRoll(
239
- {
240
- provider: target.provider,
241
- projectId,
242
- region: target.region,
243
- env: target.env,
244
- domain: target.domain,
245
- stagingHost: target.stagingHost,
246
- credentials: target.credentials,
247
- },
248
- images.images,
249
- run,
250
- { secretVolumes: true },
251
- );
265
+ const planned = await planAppRoll(install, images.images, run, { secretVolumes: true });
252
266
  if (!planned.ok) {
253
267
  sPre.failed("Pre-flight failed");
254
268
  r.error(planned.error);