@caelo-cms/provisioning 0.11.20 → 0.12.1

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 (170) hide show
  1. package/dist/adapter.d.ts +6 -1
  2. package/dist/adapter.d.ts.map +1 -1
  3. package/dist/admin-internal-google.d.ts +13 -1
  4. package/dist/admin-internal-google.d.ts.map +1 -1
  5. package/dist/admin-internal-google.js +47 -0
  6. package/dist/admin-internal-google.js.map +1 -1
  7. package/dist/agent/command.d.ts +1 -1
  8. package/dist/agent-db.js +133 -8
  9. package/dist/bootstrap-token.d.ts +4 -3
  10. package/dist/bootstrap-token.d.ts.map +1 -1
  11. package/dist/bootstrap-token.js.map +1 -1
  12. package/dist/cli-routing.d.ts.map +1 -1
  13. package/dist/cli-routing.js +7 -2
  14. package/dist/cli-routing.js.map +1 -1
  15. package/dist/cli.js +9 -0
  16. package/dist/cli.js.map +1 -1
  17. package/dist/converge-checkpoint.d.ts +6 -0
  18. package/dist/converge-checkpoint.d.ts.map +1 -1
  19. package/dist/converge-job-gcp.d.ts +1 -16
  20. package/dist/converge-job-gcp.d.ts.map +1 -1
  21. package/dist/converge-job-gcp.js +10 -97
  22. package/dist/converge-job-gcp.js.map +1 -1
  23. package/dist/converge-job.d.ts +7 -46
  24. package/dist/converge-job.d.ts.map +1 -1
  25. package/dist/converge-job.js +22 -57
  26. package/dist/converge-job.js.map +1 -1
  27. package/dist/dns/manual.d.ts +5 -1
  28. package/dist/dns/manual.d.ts.map +1 -1
  29. package/dist/dns/manual.js +2 -1
  30. package/dist/dns/manual.js.map +1 -1
  31. package/dist/gateway-armor.d.ts.map +1 -1
  32. package/dist/gateway-armor.js +11 -3
  33. package/dist/gateway-armor.js.map +1 -1
  34. package/dist/gcp-names.d.ts +2 -0
  35. package/dist/gcp-names.d.ts.map +1 -1
  36. package/dist/gcp-names.js +4 -0
  37. package/dist/gcp-names.js.map +1 -1
  38. package/dist/index.d.ts +3 -2
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +1 -1
  41. package/dist/index.js.map +1 -1
  42. package/dist/infra-contract.d.ts +1 -1
  43. package/dist/infra-contract.d.ts.map +1 -1
  44. package/dist/infra-contract.js +14 -1
  45. package/dist/infra-contract.js.map +1 -1
  46. package/dist/install-job-runner.d.ts.map +1 -1
  47. package/dist/install-job-runner.js +4 -3
  48. package/dist/install-job-runner.js.map +1 -1
  49. package/dist/install-job.d.ts +25 -1
  50. package/dist/install-job.d.ts.map +1 -1
  51. package/dist/install-job.js +72 -3
  52. package/dist/install-job.js.map +1 -1
  53. package/dist/job-handover-gcp.d.ts +40 -0
  54. package/dist/job-handover-gcp.d.ts.map +1 -0
  55. package/dist/job-handover-gcp.js +108 -0
  56. package/dist/job-handover-gcp.js.map +1 -0
  57. package/dist/job-handover.d.ts +141 -0
  58. package/dist/job-handover.d.ts.map +1 -0
  59. package/dist/job-handover.js +141 -0
  60. package/dist/job-handover.js.map +1 -0
  61. package/dist/operator-access.d.ts +7 -2
  62. package/dist/operator-access.d.ts.map +1 -1
  63. package/dist/operator-access.js +37 -6
  64. package/dist/operator-access.js.map +1 -1
  65. package/dist/owner-setup-token.d.ts +52 -0
  66. package/dist/owner-setup-token.d.ts.map +1 -0
  67. package/dist/owner-setup-token.js +111 -0
  68. package/dist/owner-setup-token.js.map +1 -0
  69. package/dist/project-status.d.ts +128 -0
  70. package/dist/project-status.d.ts.map +1 -0
  71. package/dist/project-status.js +303 -0
  72. package/dist/project-status.js.map +1 -0
  73. package/dist/stack-converge.d.ts.map +1 -1
  74. package/dist/stack-converge.js +9 -5
  75. package/dist/stack-converge.js.map +1 -1
  76. package/dist/state-attach.d.ts +17 -0
  77. package/dist/state-attach.d.ts.map +1 -1
  78. package/dist/state-attach.js +27 -9
  79. package/dist/state-attach.js.map +1 -1
  80. package/dist/static-security-headers.d.ts +6 -0
  81. package/dist/static-security-headers.d.ts.map +1 -1
  82. package/dist/static-security-headers.js +11 -1
  83. package/dist/static-security-headers.js.map +1 -1
  84. package/dist/upgrader-assess.d.ts +30 -3
  85. package/dist/upgrader-assess.d.ts.map +1 -1
  86. package/dist/upgrader-assess.js +77 -24
  87. package/dist/upgrader-assess.js.map +1 -1
  88. package/dist/upgrader-gcp.d.ts +4 -2
  89. package/dist/upgrader-gcp.d.ts.map +1 -1
  90. package/dist/upgrader-gcp.js +16 -3
  91. package/dist/upgrader-gcp.js.map +1 -1
  92. package/dist/upgrader-iam-preflight.d.ts +14 -3
  93. package/dist/upgrader-iam-preflight.d.ts.map +1 -1
  94. package/dist/upgrader-iam-preflight.js +40 -6
  95. package/dist/upgrader-iam-preflight.js.map +1 -1
  96. package/dist/upgrader-job.d.ts +51 -9
  97. package/dist/upgrader-job.d.ts.map +1 -1
  98. package/dist/upgrader-job.js +92 -37
  99. package/dist/upgrader-job.js.map +1 -1
  100. package/dist/upgrader-setup.d.ts +1 -1
  101. package/dist/upgrader-setup.d.ts.map +1 -1
  102. package/dist/upgrader-setup.js +5 -2
  103. package/dist/upgrader-setup.js.map +1 -1
  104. package/dist/wizards/gcp-install-job.d.ts +27 -4
  105. package/dist/wizards/gcp-install-job.d.ts.map +1 -1
  106. package/dist/wizards/gcp-install-job.js +73 -33
  107. package/dist/wizards/gcp-install-job.js.map +1 -1
  108. package/dist/wizards/gcp-pulumi.d.ts +0 -13
  109. package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
  110. package/dist/wizards/gcp-pulumi.js +0 -12
  111. package/dist/wizards/gcp-pulumi.js.map +1 -1
  112. package/dist/wizards/gcp.d.ts +3 -0
  113. package/dist/wizards/gcp.d.ts.map +1 -1
  114. package/dist/wizards/gcp.js +30 -53
  115. package/dist/wizards/gcp.js.map +1 -1
  116. package/package.json +2 -2
  117. package/src/adapter.ts +6 -1
  118. package/src/admin-internal-google.ts +48 -0
  119. package/src/bootstrap-token.ts +4 -3
  120. package/src/caddy-redirects.docker.test.ts +5 -0
  121. package/src/cli-routing.test.ts +1 -1
  122. package/src/cli-routing.ts +9 -2
  123. package/src/cli.ts +9 -0
  124. package/src/converge-job-gcp.test.ts +1 -40
  125. package/src/converge-job-gcp.ts +10 -100
  126. package/src/converge-job.test.ts +9 -3
  127. package/src/converge-job.ts +33 -115
  128. package/src/dns/manual.ts +4 -1
  129. package/src/gateway-armor.ts +14 -3
  130. package/src/gateway-edge.test.ts +16 -1
  131. package/src/gcp-names.ts +4 -0
  132. package/src/gcp-url-map.test.ts +2 -0
  133. package/src/index.ts +3 -0
  134. package/src/infra-contract.test.ts +4 -3
  135. package/src/infra-contract.ts +14 -1
  136. package/src/install-job-runner.ts +4 -2
  137. package/src/install-job.ts +86 -4
  138. package/src/job-handover-gcp.test.ts +129 -0
  139. package/src/job-handover-gcp.ts +135 -0
  140. package/src/job-handover.ts +259 -0
  141. package/src/operator-access.test.ts +43 -0
  142. package/src/operator-access.ts +38 -6
  143. package/src/owner-setup-token.test.ts +195 -0
  144. package/src/owner-setup-token.ts +138 -0
  145. package/src/project-status.test.ts +571 -0
  146. package/src/project-status.ts +430 -0
  147. package/src/stack-converge.ts +12 -5
  148. package/src/state-attach.ts +46 -16
  149. package/src/static-security-headers.test.ts +16 -0
  150. package/src/static-security-headers.ts +12 -1
  151. package/src/upgrader-assess.test.ts +75 -1
  152. package/src/upgrader-assess.ts +121 -57
  153. package/src/upgrader-gcp.ts +19 -5
  154. package/src/upgrader-iam-preflight.test.ts +28 -1
  155. package/src/upgrader-iam-preflight.ts +46 -6
  156. package/src/upgrader-job.test.ts +365 -31
  157. package/src/upgrader-job.ts +177 -53
  158. package/src/upgrader-setup.test.ts +17 -0
  159. package/src/upgrader-setup.ts +5 -2
  160. package/src/url-map-redirects-access.test.ts +1 -1
  161. package/src/wizards/gcp-install-job.ts +101 -47
  162. package/src/wizards/gcp-pulumi.ts +0 -18
  163. package/src/wizards/gcp.ts +37 -67
  164. package/stacks/aws/README.md +4 -2
  165. package/stacks/aws/index.ts +4 -8
  166. package/stacks/azure/README.md +4 -2
  167. package/stacks/azure/index.ts +4 -8
  168. package/stacks/gcp/README.md +6 -3
  169. package/stacks/gcp/index.ts +7 -8
  170. package/stacks/gcp-firebase/index.ts +4 -6
@@ -9,7 +9,12 @@
9
9
  import { describe, expect, it } from "bun:test";
10
10
  import type { StoredUpdatePolicy } from "@caelo-cms/shared";
11
11
  import type { ReleaseManifest, ReleaseManifestResult } from "./release-manifest.js";
12
- import { assessUpgradeRequest, type InstalledState } from "./upgrader-assess.js";
12
+ import {
13
+ type AssessmentInput,
14
+ assessHandover,
15
+ assessUpgradeRequest,
16
+ type InstalledState,
17
+ } from "./upgrader-assess.js";
13
18
 
14
19
  const D = (c: string) => `sha256:${c.repeat(64)}`;
15
20
 
@@ -416,3 +421,72 @@ describe("assessUpgradeRequest — the converge route (#819)", () => {
416
421
  expect(d.kind === "stop" ? d.reason : "").toContain("converge job");
417
422
  });
418
423
  });
424
+
425
+ describe("assessHandover (#1029: stage 1, only what must hold before the target's code runs)", () => {
426
+ const input = (over: Partial<AssessmentInput> = {}): AssessmentInput => ({
427
+ request: { targetVersion: "0.11.0", trigger: "manual" },
428
+ manifest: { ok: true, manifest: MANIFEST },
429
+ installed: { version: "0.10.50", infraContractVersion: 2 },
430
+ policy: POLICY,
431
+ now: new Date("2026-10-13T12:00:00Z"),
432
+ ...over,
433
+ });
434
+
435
+ it("proceeds with the verified manifest", () => {
436
+ const d = assessHandover(input());
437
+ expect(d).toEqual({ kind: "proceed", manifest: MANIFEST, warnings: [] });
438
+ });
439
+
440
+ it("leaves everything else to the target's code", () => {
441
+ const tooFar = { ...MANIFEST, minFromVersion: "0.10.99" };
442
+ expect(assessHandover(input({ manifest: { ok: true, manifest: tooFar } })).kind).toBe(
443
+ "proceed",
444
+ );
445
+ const destructive = {
446
+ ...MANIFEST,
447
+ migrations: { ...MANIFEST.migrations, contract: true },
448
+ };
449
+ expect(assessHandover(input({ manifest: { ok: true, manifest: destructive } })).kind).toBe(
450
+ "proceed",
451
+ );
452
+ });
453
+
454
+ it("the same release passes: install.json may be the stale side", () => {
455
+ expect(
456
+ assessHandover(input({ installed: { version: "0.11.0", infraContractVersion: 2 } })).kind,
457
+ ).toBe("proceed");
458
+ });
459
+
460
+ it("refuses a release older than install.json's, for any trigger", () => {
461
+ const d = assessHandover(input({ installed: { version: "0.11.1", infraContractVersion: 2 } }));
462
+ expect(d).toMatchObject({ kind: "stop", status: "refused", codes: ["not-newer"] });
463
+ });
464
+
465
+ it("refuses what the manifest does not vouch for", () => {
466
+ const bad = (code: "signature" | "schema") =>
467
+ assessHandover(input({ manifest: { ok: false, error: { code, message: "x" } } }));
468
+ expect(bad("signature")).toMatchObject({ status: "refused", codes: ["signature"] });
469
+ expect(bad("schema")).toMatchObject({
470
+ status: "needs_cloud_shell",
471
+ codes: ["manifest-unreadable"],
472
+ });
473
+ });
474
+
475
+ it("stops when the install does not know what it runs", () => {
476
+ const d = assessHandover(input({ installed: { version: null, infraContractVersion: 2 } }));
477
+ expect(d).toMatchObject({ codes: ["install-version-unknown"] });
478
+ });
479
+
480
+ it("an infrastructure change is not this job's", () => {
481
+ const d = assessHandover(input({ installed: { version: "0.10.50", infraContractVersion: 1 } }));
482
+ expect(d).toMatchObject({ status: "needs_cloud_shell", codes: ["infra-contract"] });
483
+ });
484
+
485
+ it("an automatic request is judged in full: nobody asked for it", () => {
486
+ const auto = input({
487
+ request: { targetVersion: "0.11.0", trigger: "auto" },
488
+ policy: { ...POLICY, mode: "notify" },
489
+ });
490
+ expect(assessHandover(auto)).toMatchObject({ kind: "stop", codes: ["policy"] });
491
+ });
492
+ });
@@ -47,6 +47,11 @@
47
47
  * converge route needs the Owner's `autoConverge` (#864) on top of
48
48
  * everything an automatic install needs; the scheduler queues one only
49
49
  * then, and the job re-checks it here against the policy it claimed.
50
+ *
51
+ * Stage 1 of the upgrader job (#1029) asks {@link assessHandover}, not this:
52
+ * it only decides whether the job may move onto the target's ops image.
53
+ * This full assessment then runs in the TARGET's code, so a fix to it reaches
54
+ * the installs that need it.
50
55
  */
51
56
 
52
57
  import {
@@ -130,8 +135,8 @@ const stop = (
130
135
  ...(manifest ? { manifest } : {}),
131
136
  });
132
137
 
133
- /** Decide what the job does with a request. See the file comment for the matrix. */
134
- export function assessUpgradeRequest(input: {
138
+ /** What {@link assessUpgradeRequest} decides from. */
139
+ export interface AssessmentInput {
135
140
  readonly request: {
136
141
  readonly targetVersion: string;
137
142
  readonly trigger: PlatformUpdateTrigger;
@@ -149,52 +154,123 @@ export function assessUpgradeRequest(input: {
149
154
  * contract bump is installed, not handed to Cloud Shell. Default `app_roll`.
150
155
  */
151
156
  readonly route?: "app_roll" | "converge";
152
- }): RequestDecision {
153
- const { request, installed } = input;
154
- const by = manualRun(input.manualPath ?? "cloud-shell");
155
- const target = request.targetVersion;
156
- if (!input.manifest.ok) {
157
- const e = input.manifest.error;
158
- switch (e.code) {
159
- case "registry":
160
- // Only an outage is worth retrying: a manifest the registry says
161
- // does not exist (404 / other 4xx) never appears, so retrying would
162
- // loop on "try again later" forever.
163
- if (e.transient) {
164
- return stop(
165
- "failed",
166
- "registry",
167
- `Could not reach the Caelo release registry to verify v${target} (${e.message}). Nothing was changed; try again later.`,
168
- );
169
- }
170
- return stop(
171
- "refused",
172
- "manifest-missing",
173
- `v${target} has no signed release manifest in the Caelo release registry, so it cannot be verified and will not be installed. Nothing was changed; retrying will not help. (${e.message})`,
174
- );
175
- case "schema":
176
- return stop(
177
- "needs_cloud_shell",
178
- "manifest-unreadable",
179
- `This upgrader is too old to read the release information of v${target}. Install it once ${by}; afterwards the admin can install updates again. (${e.message})`,
180
- );
181
- default:
157
+ }
158
+
159
+ /** Why the manifest cannot be used, as a stop; the first rows of the matrix. */
160
+ function manifestStop(
161
+ e: Extract<ReleaseManifestResult, { ok: false }>["error"],
162
+ target: string,
163
+ by: string,
164
+ ): RequestDecision {
165
+ switch (e.code) {
166
+ case "registry":
167
+ // Only an outage is worth retrying: a manifest the registry says
168
+ // does not exist (404 / other 4xx) never appears, so retrying would
169
+ // loop on "try again later" forever.
170
+ if (e.transient) {
182
171
  return stop(
183
- "refused",
184
- "signature",
185
- `The release information of v${target} is not signed by Caelo's release workflow, so it was not installed. Nothing was changed. (${e.message})`,
172
+ "failed",
173
+ "registry",
174
+ `Could not reach the Caelo release registry to verify v${target} (${e.message}). Nothing was changed; try again later.`,
186
175
  );
187
- }
176
+ }
177
+ return stop(
178
+ "refused",
179
+ "manifest-missing",
180
+ `v${target} has no signed release manifest in the Caelo release registry, so it cannot be verified and will not be installed. Nothing was changed; retrying will not help. (${e.message})`,
181
+ );
182
+ case "schema":
183
+ return stop(
184
+ "needs_cloud_shell",
185
+ "manifest-unreadable",
186
+ `This upgrader is too old to read the release information of v${target}. Install it once ${by}; afterwards the admin can install updates again. (${e.message})`,
187
+ );
188
+ default:
189
+ return stop(
190
+ "refused",
191
+ "signature",
192
+ `The release information of v${target} is not signed by Caelo's release workflow, so it was not installed. Nothing was changed. (${e.message})`,
193
+ );
188
194
  }
195
+ }
196
+
197
+ function versionUnknownStop(by: string, manifest: ReleaseManifest): RequestDecision {
198
+ return stop(
199
+ "needs_cloud_shell",
200
+ "install-version-unknown",
201
+ `This install has not recorded which Caelo release it runs yet. Upgrade it once ${by}; that records it, and the admin can install updates from then on.`,
202
+ manifest,
203
+ );
204
+ }
205
+
206
+ function olderStop(target: string, installed: string, manifest: ReleaseManifest): RequestDecision {
207
+ return stop(
208
+ "refused",
209
+ "not-newer",
210
+ `v${target} is not newer than the installed v${installed}; Caelo never installs an older release over a newer one.`,
211
+ manifest,
212
+ );
213
+ }
214
+
215
+ function infraChangeStop(
216
+ target: string,
217
+ installed: number,
218
+ required: number,
219
+ manifest: ReleaseManifest,
220
+ ): RequestDecision {
221
+ return stop(
222
+ "needs_cloud_shell",
223
+ "infra-contract",
224
+ `v${target} changes this install's cloud setup (infrastructure contract ${installed} → ${required}: ${infraStepsText(installed, required)}). The upgrader may not change cloud permissions, so Caelo installs this release with its converge job: start it again from Settings → Updates. On an install without the converge job, install it once with your own Google account in Cloud Shell.`,
225
+ manifest,
226
+ );
227
+ }
228
+
229
+ /**
230
+ * Stage 1 of the upgrader job (#1029): may this job move onto the target's
231
+ * ops image? Moving changes nothing on the site, so this is deliberately
232
+ * only what must hold before ANY of the target's code runs as the job's
233
+ * account, and everything else waits for {@link assessUpgradeRequest} in
234
+ * the target's code. Whatever this checks cannot be fixed forward.
235
+ *
236
+ * - The manifest verified, so the digest comes from Caelo's signature.
237
+ * - The target is not older than install.json's release. This is the
238
+ * line that keeps a compromised admin from pointing the job at an
239
+ * older signed release; the request row is untrusted. The same release
240
+ * passes: install.json may be stale, and the target's code sorts it out.
241
+ * - No infrastructure change: the upgrader job cannot install it.
242
+ * - An automatic request is judged in full. Nobody asked for it, so the
243
+ * job does not move unless the policy allows the install; a bug here
244
+ * costs an automatic run, and the Owner's click still gets through.
245
+ *
246
+ * @returns `proceed` when stage 1 may hand over, with the verified manifest
247
+ */
248
+ export function assessHandover(input: AssessmentInput): RequestDecision {
249
+ const { request, installed } = input;
250
+ const by = manualRun(input.manualPath ?? "cloud-shell");
251
+ const target = request.targetVersion;
252
+ if (!input.manifest.ok) return manifestStop(input.manifest.error, target, by);
189
253
  const manifest = input.manifest.manifest;
190
- if (installed.version === null) {
191
- return stop(
192
- "needs_cloud_shell",
193
- "install-version-unknown",
194
- `This install has not recorded which Caelo release it runs yet. Upgrade it once ${by}; that records it, and the admin can install updates from then on.`,
195
- manifest,
196
- );
254
+ if (installed.version === null) return versionUnknownStop(by, manifest);
255
+ if (compareSemver(target, installed.version) < 0) {
256
+ return olderStop(target, installed.version, manifest);
197
257
  }
258
+ const contract = installed.infraContractVersion ?? manifest.infraContractVersion;
259
+ if (needsConverge({ infraContractVersion: contract }, manifest.infraContractVersion)) {
260
+ return infraChangeStop(target, contract, manifest.infraContractVersion, manifest);
261
+ }
262
+ if (request.trigger === "auto") return assessUpgradeRequest(input);
263
+ return { kind: "proceed", manifest, warnings: [] };
264
+ }
265
+
266
+ /** Decide what the job does with a request. See the file comment for the matrix. */
267
+ export function assessUpgradeRequest(input: AssessmentInput): RequestDecision {
268
+ const { request, installed } = input;
269
+ const by = manualRun(input.manualPath ?? "cloud-shell");
270
+ const target = request.targetVersion;
271
+ if (!input.manifest.ok) return manifestStop(input.manifest.error, target, by);
272
+ const manifest = input.manifest.manifest;
273
+ if (installed.version === null) return versionUnknownStop(by, manifest);
198
274
  const order = compareSemver(target, installed.version);
199
275
  if (order === 0) {
200
276
  // Reached after a run that rolled but could not record it (#860): the
@@ -206,14 +282,7 @@ export function assessUpgradeRequest(input: {
206
282
  manifest,
207
283
  );
208
284
  }
209
- if (order < 0) {
210
- return stop(
211
- "refused",
212
- "not-newer",
213
- `v${target} is not newer than the installed v${installed.version}; Caelo never installs an older release over a newer one.`,
214
- manifest,
215
- );
216
- }
285
+ if (order < 0) return olderStop(target, installed.version, manifest);
217
286
  if (compareSemver(installed.version, manifest.minFromVersion) < 0) {
218
287
  return stop(
219
288
  "refused",
@@ -229,12 +298,7 @@ export function assessUpgradeRequest(input: {
229
298
  installed.infraContractVersion !== null &&
230
299
  needsConverge({ infraContractVersion }, manifest.infraContractVersion);
231
300
  if (infraChange && !converge) {
232
- return stop(
233
- "needs_cloud_shell",
234
- "infra-contract",
235
- `v${target} changes this install's cloud setup (infrastructure contract ${infraContractVersion} → ${manifest.infraContractVersion}: ${infraStepsText(infraContractVersion, manifest.infraContractVersion)}). The upgrader may not change cloud permissions, so Caelo installs this release with its converge job: start it again from Settings → Updates. On an install without the converge job, install it once with your own Google account in Cloud Shell.`,
236
- manifest,
237
- );
301
+ return infraChangeStop(target, infraContractVersion, manifest.infraContractVersion, manifest);
238
302
  }
239
303
  if (converge && request.trigger === "auto" && !input.policy.autoConverge) {
240
304
  return stop(
@@ -16,7 +16,7 @@
16
16
  import { mkdtempSync } from "node:fs";
17
17
  import { tmpdir } from "node:os";
18
18
  import { join } from "node:path";
19
- import { isIpLiteral } from "@caelo-cms/shared";
19
+ import { CAELO_VERSION, isIpLiteral } from "@caelo-cms/shared";
20
20
  import {
21
21
  ensureCloudSqlVerifyFull,
22
22
  liveDatabaseUrls,
@@ -25,8 +25,9 @@ import {
25
25
  import { findSqlInstance } from "./gateway-credentials.js";
26
26
  import { gcloud } from "./gcloud.js";
27
27
  import { type GcloudRunner, realSleep } from "./gcloud-retry.js";
28
- import { GCP_STACK_ENV } from "./gcp-names.js";
28
+ import { GCP_STACK_ENV, upgraderJobName } from "./gcp-names.js";
29
29
  import type { InstallMetadata } from "./install-state.js";
30
+ import { jobExecutionNameError, selfHandoverDeps } from "./job-handover-gcp.js";
30
31
  import { readAdminConfig, runMigrationsViaCloudRunJob } from "./migration-runner.js";
31
32
  import { fetchVerifiedReleaseManifest } from "./release-manifest.js";
32
33
  import type { HttpFetch } from "./runtime-secrets.js";
@@ -44,7 +45,7 @@ import { createIapHealthProbe } from "./upgrade/health.js";
44
45
  import { createReleaseImageForms } from "./upgrade/image-forms.js";
45
46
  import { httpsLoadBalancerGet } from "./upgrade/load-balancer-get.js";
46
47
  import { type IamTester, restIamTester } from "./upgrader-iam-preflight.js";
47
- import type { UpdateQueue, UpgraderJobDeps } from "./upgrader-job.js";
48
+ import type { UpgraderJobDeps, UpgraderQueue } from "./upgrader-job.js";
48
49
  import type { JobLog } from "./upgrader-reporter.js";
49
50
  import { UPGRADER_TASK_TIMEOUT_MINUTES } from "./upgrader-setup.js";
50
51
 
@@ -55,6 +56,11 @@ export interface UpgraderPlacement {
55
56
  readonly execution: string | null;
56
57
  }
57
58
 
59
+ /** Why `CLOUD_RUN_EXECUTION` cannot name a run of the upgrader job, or null when it can. */
60
+ export function upgraderExecutionNameError(execution: string | null): string | null {
61
+ return jobExecutionNameError(upgraderJobName(GCP_STACK_ENV), execution);
62
+ }
63
+
58
64
  /**
59
65
  * The Cloud SQL private IP the admin connects to — the job connects there
60
66
  * too (as its own IAM database user). Read from the deployed admin service.
@@ -182,7 +188,7 @@ export function gcloudIamTester(run: GcloudRunner, http: HttpFetch): IamTester {
182
188
  /** Every dependency of {@link runUpgraderJob} except the database and the log. */
183
189
  export function gcpUpgraderDeps(
184
190
  placement: UpgraderPlacement,
185
- queue: UpdateQueue,
191
+ queue: UpgraderQueue,
186
192
  log: JobLog,
187
193
  ): UpgraderJobDeps {
188
194
  const run: GcloudRunner = gcloud;
@@ -199,7 +205,15 @@ export function gcpUpgraderDeps(
199
205
  projectId: placement.projectId,
200
206
  region: placement.region,
201
207
  env: GCP_STACK_ENV,
202
- execution: placement.execution,
208
+ // #1029 — stage 1 moves this job onto the target's image, with the
209
+ // rights on itself it has always had for that (upgrader-setup.ts).
210
+ ...selfHandoverDeps(run, {
211
+ job: upgraderJobName(GCP_STACK_ENV),
212
+ projectId: placement.projectId,
213
+ region: placement.region,
214
+ execution: placement.execution,
215
+ }),
216
+ ownVersion: CAELO_VERSION,
203
217
  // #818 — the IAM self-check asks as the job's own account (gcloud's
204
218
  // token from the metadata server), so it sees exactly the job's grants.
205
219
  iam: gcloudIamTester(run, http),
@@ -21,6 +21,7 @@ import {
21
21
  testIamCall,
22
22
  UPGRADER_PREFLIGHT_VOICE,
23
23
  upgraderAccessChecks,
24
+ upgraderHandoverChecks,
24
25
  } from "./upgrader-iam-preflight.js";
25
26
  import {
26
27
  UPGRADER_FORBIDDEN_ROLES,
@@ -117,6 +118,32 @@ function tester(grants: Map<string, Set<string>>, asked: string[] = []): IamTest
117
118
  };
118
119
  }
119
120
 
121
+ describe("upgraderHandoverChecks (#1029)", () => {
122
+ const all = upgraderHandoverChecks(TARGET);
123
+
124
+ it("needs nothing setup does not already grant: the hand-over widens no right", async () => {
125
+ expect((await runIamPreflight(all, tester(setupGrants()))).kind).toBe("ok");
126
+ for (const p of all.flatMap((c) => c.permissions)) {
127
+ if (p === "iam.serviceAccounts.actAs") continue;
128
+ expect(UPGRADER_RUN_ROLE.permissions).toContain(p);
129
+ }
130
+ });
131
+
132
+ it("asks about the job's own job and own account, and is part of the full list", () => {
133
+ expect(all.map((c) => resourceName(c.resource))).toEqual([
134
+ "projects/acme/locations/europe-west1/jobs/caelo-production-upgrader",
135
+ "projects/acme",
136
+ "projects/acme/serviceAccounts/caelo-production-upgrader@acme.iam.gserviceaccount.com",
137
+ ]);
138
+ const full = new Set(upgraderAccessChecks(TARGET).flatMap((c) => c.permissions));
139
+ for (const p of all.flatMap((c) => c.permissions)) expect(full.has(p)).toBe(true);
140
+ });
141
+
142
+ it("never asks for overrides: an execution of the job runs what setup deployed", () => {
143
+ expect(all.flatMap((c) => c.permissions)).not.toContain("run.jobs.runWithOverrides");
144
+ });
145
+ });
146
+
120
147
  describe("upgraderAccessChecks", () => {
121
148
  it("asks only for what upgrader-setup.ts grants: a fresh setup passes the self-check", async () => {
122
149
  const r = await runIamPreflight(upgraderAccessChecks(TARGET), tester(setupGrants()));
@@ -190,7 +217,7 @@ describe("upgraderAccessChecks", () => {
190
217
  "run.jobs.create", // the migration job
191
218
  "run.jobs.run",
192
219
  "run.executions.get", // execute --wait, the lock lease
193
- "run.jobs.update", // the self-update
220
+ "run.jobs.update", // the hand-over, the audit and capture jobs
194
221
  "cloudsql.backupRuns.create", // the backup
195
222
  "iam.serviceAccounts.actAs",
196
223
  "iam.serviceAccounts.signJwt", // the IAP health check
@@ -123,12 +123,52 @@ export type PreflightOutcome<C extends AccessCheck = AccessCheck> =
123
123
  /** The object the `install/` probes name. It is never written: every probe fails its precondition. */
124
124
  export const PREFLIGHT_PROBE_OBJECT = ".caelo-iam-preflight";
125
125
 
126
+ const UPGRADER_MOVE_NEED = "move the upgrader itself onto the release it installs";
127
+ const UPGRADER_MOVE_PERMISSIONS = ["run.jobs.get", "run.jobs.update"];
128
+
129
+ /**
130
+ * What stage 1 of an upgrader run needs (#1029): the upgrader job's own
131
+ * rights on itself. The rest of {@link upgraderAccessChecks} is the target
132
+ * release's list, asked in its own code. The state lock's bucket grants
133
+ * are not here: a lock that cannot be taken stops the hand-over anyway.
134
+ */
135
+ export function upgraderHandoverChecks(target: {
136
+ readonly projectId: string;
137
+ readonly region: string;
138
+ readonly env: string;
139
+ }): AccessCheck[] {
140
+ const { projectId, region, env } = target;
141
+ return [
142
+ {
143
+ need: UPGRADER_MOVE_NEED,
144
+ resource: { kind: "run-job", projectId, region, name: upgraderJobName(env) },
145
+ permissions: UPGRADER_MOVE_PERMISSIONS,
146
+ },
147
+ {
148
+ // On the project, like the app-roll's own check of these two: that is
149
+ // where caeloUpgraderRun is bound.
150
+ need: "start the upgrader on that release, and see which image a run executes and whether it ended",
151
+ resource: { kind: "project", projectId },
152
+ permissions: ["run.jobs.run", "run.executions.get"],
153
+ },
154
+ {
155
+ need: "keep the upgrader job running as its own service account after it moves onto a release",
156
+ resource: {
157
+ kind: "service-account",
158
+ projectId,
159
+ email: upgraderServiceAccountEmail(projectId, env),
160
+ },
161
+ permissions: ["iam.serviceAccounts.actAs"],
162
+ },
163
+ ];
164
+ }
165
+
126
166
  /**
127
167
  * Every permission an upgrader run needs after its decision, grouped by
128
168
  * what it is for (upgrade/app-roll.ts, migration-runner.ts, backup.ts,
129
- * health.ts, state-store.ts, the self-update in upgrader-job.ts). Keep in
130
- * step with the grants in upgrader-setup.ts: a permission listed here that
131
- * setup does not grant stops every update.
169
+ * health.ts, state-store.ts). Keep in step with the grants in
170
+ * upgrader-setup.ts: a permission listed here that setup does not grant
171
+ * stops every update.
132
172
  */
133
173
  export function upgraderAccessChecks(target: {
134
174
  readonly projectId: string;
@@ -212,7 +252,7 @@ export function upgraderAccessChecks(target: {
212
252
  permissions: ["iam.serviceAccounts.actAs"],
213
253
  },
214
254
  {
215
- need: "keep the upgrader job running as its own service account after it updates itself",
255
+ need: "keep the upgrader job running as its own service account after it moves onto a release",
216
256
  resource: sa(upgraderServiceAccountEmail(projectId, env)),
217
257
  permissions: ["iam.serviceAccounts.actAs"],
218
258
  },
@@ -222,9 +262,9 @@ export function upgraderAccessChecks(target: {
222
262
  permissions: ["iam.serviceAccounts.signJwt"],
223
263
  },
224
264
  {
225
- need: "move the upgrader itself onto the new release",
265
+ need: UPGRADER_MOVE_NEED,
226
266
  resource: { kind: "run-job", projectId, region, name: upgraderJobName(env) },
227
- permissions: ["run.jobs.get", "run.jobs.update"],
267
+ permissions: UPGRADER_MOVE_PERMISSIONS,
228
268
  },
229
269
  {
230
270
  need: "list the install's ops bucket (every write there lists it first)",