@caelo-cms/provisioning 0.11.14 → 0.11.16
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.
- package/dist/__tests__/stack-under-mocks.d.ts +2 -0
- package/dist/__tests__/stack-under-mocks.d.ts.map +1 -0
- package/dist/__tests__/stack-under-mocks.js +59 -0
- package/dist/__tests__/stack-under-mocks.js.map +1 -0
- package/dist/adopt-existing.d.ts +0 -56
- package/dist/adopt-existing.d.ts.map +1 -1
- package/dist/adopt-existing.js +58 -2
- package/dist/adopt-existing.js.map +1 -1
- package/dist/cloudsql-tls.d.ts.map +1 -1
- package/dist/cloudsql-tls.js +3 -3
- package/dist/cloudsql-tls.js.map +1 -1
- package/dist/deployed-release.d.ts.map +1 -1
- package/dist/deployed-release.js +4 -2
- package/dist/deployed-release.js.map +1 -1
- package/dist/gateway-bootstrap.d.ts +68 -0
- package/dist/gateway-bootstrap.d.ts.map +1 -0
- package/dist/gateway-bootstrap.js +145 -0
- package/dist/gateway-bootstrap.js.map +1 -0
- package/dist/gcloud.d.ts +2 -0
- package/dist/gcloud.d.ts.map +1 -1
- package/dist/gcloud.js +14 -0
- package/dist/gcloud.js.map +1 -1
- package/dist/mcp-auth-proxy-setup.d.ts.map +1 -1
- package/dist/mcp-auth-proxy-setup.js +3 -6
- package/dist/mcp-auth-proxy-setup.js.map +1 -1
- package/dist/ops-backend.d.ts.map +1 -1
- package/dist/ops-backend.js +4 -6
- package/dist/ops-backend.js.map +1 -1
- package/dist/remote-mcp.d.ts.map +1 -1
- package/dist/remote-mcp.js +4 -5
- package/dist/remote-mcp.js.map +1 -1
- package/dist/runtime-secrets.d.ts.map +1 -1
- package/dist/runtime-secrets.js +4 -5
- package/dist/runtime-secrets.js.map +1 -1
- package/dist/stack-gateways.d.ts +31 -0
- package/dist/stack-gateways.d.ts.map +1 -0
- package/dist/stack-gateways.js +40 -0
- package/dist/stack-gateways.js.map +1 -0
- package/dist/staging-gateway-setup.d.ts.map +1 -1
- package/dist/staging-gateway-setup.js +7 -8
- package/dist/staging-gateway-setup.js.map +1 -1
- package/dist/state-command.d.ts.map +1 -1
- package/dist/state-command.js +3 -0
- package/dist/state-command.js.map +1 -1
- package/dist/state-lock-lease.d.ts +1 -1
- package/dist/state-lock-lease.d.ts.map +1 -1
- package/dist/state-lock-lease.js +2 -2
- package/dist/state-lock-lease.js.map +1 -1
- package/dist/wizards/gcp-pulumi.d.ts +7 -1
- package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
- package/dist/wizards/gcp-pulumi.js +2 -0
- package/dist/wizards/gcp-pulumi.js.map +1 -1
- package/dist/wizards/gcp.d.ts.map +1 -1
- package/dist/wizards/gcp.js +120 -13
- package/dist/wizards/gcp.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/stack-under-mocks.ts +69 -0
- package/src/adopt-existing.test.ts +36 -9
- package/src/adopt-existing.ts +2 -3
- package/src/cloudsql-tls.ts +3 -3
- package/src/deployed-release.test.ts +16 -0
- package/src/deployed-release.ts +4 -2
- package/src/gateway-bootstrap.test.ts +381 -0
- package/src/gateway-bootstrap.ts +195 -0
- package/src/gcloud.test.ts +24 -0
- package/src/gcloud.ts +17 -0
- package/src/gcp-url-map.test.ts +1 -1
- package/src/mcp-auth-proxy-setup.ts +3 -7
- package/src/ops-backend.ts +4 -7
- package/src/remote-mcp-stack-config.test.ts +2 -1
- package/src/remote-mcp.ts +4 -6
- package/src/runtime-secrets.ts +4 -5
- package/src/stack-cloud-run-names.test.ts +49 -0
- package/src/stack-gateways.ts +39 -0
- package/src/staging-gateway-setup.ts +7 -8
- package/src/state-command.ts +3 -0
- package/src/state-lock-lease.ts +2 -4
- package/src/wizards/gcp-pulumi.ts +9 -0
- package/src/wizards/gcp.ts +146 -14
- package/stacks/gcp/Pulumi.yaml +4 -0
- package/stacks/gcp/index.ts +83 -62
- package/stacks/gcp-firebase/index.ts +61 -42
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A fresh Google Cloud install deploys the public gateway only onto a
|
|
5
|
+
* migrated database (gateway-bootstrap.ts). Regression: from #767 on, the
|
|
6
|
+
* gateway refuses to boot while its login can CONNECT to cms_admin, the
|
|
7
|
+
* REVOKEs are migrations, and the wizard ran them after the `pulumi up` that
|
|
8
|
+
* created the gateway — so every fresh install failed in that `up` with
|
|
9
|
+
* "the API gateway's database login is not isolated from cms_admin".
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, expect, it } from "bun:test";
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
import { resolve } from "node:path";
|
|
15
|
+
import type * as automation from "@pulumi/pulumi/automation";
|
|
16
|
+
import {
|
|
17
|
+
deleteNeverStartedGateways,
|
|
18
|
+
deployOntoMigratedDatabase,
|
|
19
|
+
readGatewayStart,
|
|
20
|
+
} from "./gateway-bootstrap.js";
|
|
21
|
+
import type { GcloudResult } from "./gcloud.js";
|
|
22
|
+
import { fixedCloudRunServiceName } from "./stack-contract.js";
|
|
23
|
+
import { DEPLOY_GATEWAYS_CONFIG, parseDeployGateways } from "./stack-gateways.js";
|
|
24
|
+
import { applyStackConfig, type StackConfigInputs } from "./wizards/gcp-pulumi.js";
|
|
25
|
+
|
|
26
|
+
const ok = (stdout: string): GcloudResult => ({ ok: true, stdout, stderr: "", exitCode: 0 });
|
|
27
|
+
const failed = (stderr: string): GcloudResult => ({ ok: false, stdout: "", stderr, exitCode: 1 });
|
|
28
|
+
|
|
29
|
+
/** A gcloud fake: the first matching rule answers; every call is recorded. */
|
|
30
|
+
function fakeGcloud(rules: ReadonlyArray<[RegExp, GcloudResult]>) {
|
|
31
|
+
const calls: string[] = [];
|
|
32
|
+
const run = async (args: readonly string[]) => {
|
|
33
|
+
const line = args.join(" ");
|
|
34
|
+
calls.push(line);
|
|
35
|
+
for (const [re, result] of rules) if (re.test(line)) return result;
|
|
36
|
+
throw new Error(`unexpected gcloud call: ${line}`);
|
|
37
|
+
};
|
|
38
|
+
return { run, calls };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const TARGET = { provider: "gcp", projectId: "p-1", region: "europe-west1" } as const;
|
|
42
|
+
|
|
43
|
+
describe("parseDeployGateways", () => {
|
|
44
|
+
it("reads the installer's explicit value", () => {
|
|
45
|
+
expect(parseDeployGateways("true")).toBe(true);
|
|
46
|
+
expect(parseDeployGateways("false")).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("declares the gateway on a stack configured before the key existed (every such install is migrated)", () => {
|
|
50
|
+
expect(parseDeployGateways(undefined)).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("refuses anything else instead of guessing", () => {
|
|
54
|
+
expect(() => parseDeployGateways("no")).toThrow(DEPLOY_GATEWAYS_CONFIG);
|
|
55
|
+
expect(() => parseDeployGateways("")).toThrow(DEPLOY_GATEWAYS_CONFIG);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("readGatewayStart", () => {
|
|
60
|
+
it("a project without a gateway service has a gateway that never started", async () => {
|
|
61
|
+
const g = fakeGcloud([[/services list/, ok("")]]);
|
|
62
|
+
expect(await readGatewayStart(TARGET, g.run)).toEqual({
|
|
63
|
+
ok: true,
|
|
64
|
+
start: "never-started",
|
|
65
|
+
neverStarted: [],
|
|
66
|
+
});
|
|
67
|
+
expect(g.calls[0]).toContain("--filter=metadata.name~^caelo-production-gateway");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("a gateway whose create failed at its boot check (no ready revision) never started", async () => {
|
|
71
|
+
// The scratch install of 2026-11-04: the service exists, no revision became ready.
|
|
72
|
+
const g = fakeGcloud([
|
|
73
|
+
[/services list/, ok("caelo-production-gateway-1a2b3c4\n")],
|
|
74
|
+
[/describe caelo-production-gateway-1a2b3c4 .*latestReadyRevisionName/, ok("\n")],
|
|
75
|
+
]);
|
|
76
|
+
expect(await readGatewayStart(TARGET, g.run)).toEqual({
|
|
77
|
+
ok: true,
|
|
78
|
+
start: "never-started",
|
|
79
|
+
neverStarted: ["caelo-production-gateway-1a2b3c4"],
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("a gateway with a ready revision has started, even if its latest revision failed", async () => {
|
|
84
|
+
const g = fakeGcloud([
|
|
85
|
+
[/services list/, ok("caelo-production-gateway-1a2b3c4\n")],
|
|
86
|
+
[/describe/, ok("caelo-production-gateway-1a2b3c4-00007-xyz\n")],
|
|
87
|
+
]);
|
|
88
|
+
expect(await readGatewayStart(TARGET, g.run)).toEqual({ ok: true, start: "started" });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("looks for the data gateway on gcp-firebase, not the MCP-Auth-Proxy that kept the gateway's name", async () => {
|
|
92
|
+
const g = fakeGcloud([[/services list/, ok("")]]);
|
|
93
|
+
await readGatewayStart({ ...TARGET, provider: "gcp-firebase" }, g.run);
|
|
94
|
+
expect(g.calls[0]).toContain("--filter=metadata.name~^caelo-production-data-gateway");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("fails loudly when the services cannot be read — no guess either way", async () => {
|
|
98
|
+
const list = fakeGcloud([[/services list/, failed("PERMISSION_DENIED: run.services.list")]]);
|
|
99
|
+
const r = await readGatewayStart(TARGET, list.run);
|
|
100
|
+
expect(r.ok).toBe(false);
|
|
101
|
+
if (!r.ok) expect(r.error).toContain("PERMISSION_DENIED");
|
|
102
|
+
|
|
103
|
+
const describe = fakeGcloud([
|
|
104
|
+
[/services list/, ok("caelo-production-gateway-1a2b3c4\n")],
|
|
105
|
+
[/describe/, failed("boom")],
|
|
106
|
+
]);
|
|
107
|
+
const d = await readGatewayStart(TARGET, describe.run);
|
|
108
|
+
expect(d.ok).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("deleteNeverStartedGateways", () => {
|
|
113
|
+
it("deletes a gateway that never started (Pulumi cannot when deletionProtection is on)", async () => {
|
|
114
|
+
const g = fakeGcloud([
|
|
115
|
+
[/describe/, ok("")],
|
|
116
|
+
[/services delete caelo-production-gateway-1a2b3c4 /, ok("")],
|
|
117
|
+
]);
|
|
118
|
+
expect(
|
|
119
|
+
await deleteNeverStartedGateways(TARGET, ["caelo-production-gateway-1a2b3c4"], g.run),
|
|
120
|
+
).toEqual({ ok: true, deleted: ["caelo-production-gateway-1a2b3c4"] });
|
|
121
|
+
expect(g.calls.at(-1)).toContain("--quiet");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("never deletes one that has started meanwhile (another run deployed it)", async () => {
|
|
125
|
+
const g = fakeGcloud([[/describe/, ok("caelo-production-gateway-1a2b3c4-00001-abc\n")]]);
|
|
126
|
+
const r = await deleteNeverStartedGateways(TARGET, ["caelo-production-gateway-1a2b3c4"], g.run);
|
|
127
|
+
expect(r.ok).toBe(false);
|
|
128
|
+
expect(g.calls.some((c) => c.includes("services delete"))).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("treats an already-gone service as deleted, and fails loudly on any other error", async () => {
|
|
132
|
+
const gone = fakeGcloud([
|
|
133
|
+
[/describe/, ok("")],
|
|
134
|
+
[/services delete/, failed("NOT_FOUND: service not found")],
|
|
135
|
+
]);
|
|
136
|
+
expect((await deleteNeverStartedGateways(TARGET, ["s"], gone.run)).ok).toBe(true);
|
|
137
|
+
const denied = fakeGcloud([
|
|
138
|
+
[/describe/, ok("")],
|
|
139
|
+
[/services delete/, failed("PERMISSION_DENIED")],
|
|
140
|
+
]);
|
|
141
|
+
const r = await deleteNeverStartedGateways(TARGET, ["s"], denied.run);
|
|
142
|
+
expect(r.ok).toBe(false);
|
|
143
|
+
if (!r.ok) expect(r.error).toContain("PERMISSION_DENIED");
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("deployOntoMigratedDatabase — the install order", () => {
|
|
148
|
+
function recorder() {
|
|
149
|
+
const steps: string[] = [];
|
|
150
|
+
return {
|
|
151
|
+
steps,
|
|
152
|
+
deps: {
|
|
153
|
+
up: async (deployGateways: boolean) => {
|
|
154
|
+
steps.push(`up(deployGateways=${deployGateways})`);
|
|
155
|
+
},
|
|
156
|
+
prepareDatabase: async () => {
|
|
157
|
+
steps.push("isolate + migrate");
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
it("a gateway that never started: up without it, migrate, then up with it", async () => {
|
|
164
|
+
const r = recorder();
|
|
165
|
+
expect(await deployOntoMigratedDatabase("never-started", r.deps)).toEqual({
|
|
166
|
+
databasePrepared: true,
|
|
167
|
+
});
|
|
168
|
+
expect(r.steps).toEqual([
|
|
169
|
+
"up(deployGateways=false)",
|
|
170
|
+
"isolate + migrate",
|
|
171
|
+
"up(deployGateways=true)",
|
|
172
|
+
]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("a gateway that has started keeps the order every existing install had", async () => {
|
|
176
|
+
const r = recorder();
|
|
177
|
+
expect(await deployOntoMigratedDatabase("started", r.deps)).toEqual({
|
|
178
|
+
databasePrepared: false,
|
|
179
|
+
});
|
|
180
|
+
expect(r.steps).toEqual(["up(deployGateways=true)"]);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("never deploys the gateway when the migrations fail", async () => {
|
|
184
|
+
const steps: boolean[] = [];
|
|
185
|
+
await expect(
|
|
186
|
+
deployOntoMigratedDatabase("never-started", {
|
|
187
|
+
up: async (d) => {
|
|
188
|
+
steps.push(d);
|
|
189
|
+
},
|
|
190
|
+
prepareDatabase: async () => {
|
|
191
|
+
throw new Error("migrate-admin-exec failed");
|
|
192
|
+
},
|
|
193
|
+
}),
|
|
194
|
+
).rejects.toThrow("migrate-admin-exec");
|
|
195
|
+
expect(steps).toEqual([false]);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("applyStackConfig — deployGateways", () => {
|
|
200
|
+
const inputs: StackConfigInputs = {
|
|
201
|
+
provider: "gcp",
|
|
202
|
+
projectId: "p-1",
|
|
203
|
+
domain: "example.com",
|
|
204
|
+
stagingDomain: "staging.example.com",
|
|
205
|
+
ownerEmail: "o@example.com",
|
|
206
|
+
region: "europe-west1",
|
|
207
|
+
cloudSqlTier: "db-f1-micro",
|
|
208
|
+
cloudSqlHa: false,
|
|
209
|
+
adminMinInstances: 0,
|
|
210
|
+
adminMemory: "2Gi",
|
|
211
|
+
gatewayMinInstances: 0,
|
|
212
|
+
wafAdaptiveProtection: false,
|
|
213
|
+
iapAllowlist: ["user:o@example.com"],
|
|
214
|
+
imageDigests: { admin: "sha256:a", gateway: "sha256:g", "mcp-auth-proxy": "sha256:m" },
|
|
215
|
+
deployGateways: false,
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
it.each([false, true])("writes %p as the stack's config before every up", async (value) => {
|
|
219
|
+
const config: Record<string, string> = {};
|
|
220
|
+
const stack = {
|
|
221
|
+
setAllConfig: async (c: automation.ConfigMap) => {
|
|
222
|
+
for (const [k, v] of Object.entries(c)) config[k] = v.value;
|
|
223
|
+
},
|
|
224
|
+
setConfig: async (k: string, v: automation.ConfigValue) => {
|
|
225
|
+
config[k] = v.value;
|
|
226
|
+
},
|
|
227
|
+
getConfig: async (k: string) => {
|
|
228
|
+
if (!(k in config)) throw new Error(`configuration key '${k}' not found`);
|
|
229
|
+
return { value: config[k] as string };
|
|
230
|
+
},
|
|
231
|
+
} as unknown as automation.Stack;
|
|
232
|
+
await applyStackConfig(stack, { ...inputs, deployGateways: value }, []);
|
|
233
|
+
expect(config[`caelo-gcp:${DEPLOY_GATEWAYS_CONFIG}`]).toBe(String(value));
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
/** The resources a stack program declares under Pulumi mocks (src/__tests__/stack-under-mocks.ts). */
|
|
238
|
+
function declaredBy(
|
|
239
|
+
provider: "gcp" | "gcp-firebase",
|
|
240
|
+
deployGateways: "true" | "false",
|
|
241
|
+
): { type: string; name: string; cloudName?: string }[] {
|
|
242
|
+
const config = {
|
|
243
|
+
domain: "example.com",
|
|
244
|
+
ownerEmail: "o@example.com",
|
|
245
|
+
project: "p-1",
|
|
246
|
+
region: "europe-west1",
|
|
247
|
+
secretReplication: "regional",
|
|
248
|
+
[DEPLOY_GATEWAYS_CONFIG]: deployGateways,
|
|
249
|
+
};
|
|
250
|
+
const r = Bun.spawnSync(
|
|
251
|
+
[
|
|
252
|
+
process.execPath,
|
|
253
|
+
resolve(import.meta.dir, "__tests__", "stack-under-mocks.ts"),
|
|
254
|
+
provider,
|
|
255
|
+
JSON.stringify(config),
|
|
256
|
+
],
|
|
257
|
+
{ stdout: "pipe", stderr: "pipe" },
|
|
258
|
+
);
|
|
259
|
+
if (r.exitCode !== 0) throw new Error(`stack-under-mocks ${provider}: ${r.stderr.toString()}`);
|
|
260
|
+
return r.stdout
|
|
261
|
+
.toString()
|
|
262
|
+
.split("\n")
|
|
263
|
+
.filter(Boolean)
|
|
264
|
+
.map((l) => JSON.parse(l) as { type: string; name: string; cloudName?: string });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const SERVICE = "gcp:cloudrunv2/service:Service";
|
|
268
|
+
|
|
269
|
+
describe.each(["gcp", "gcp-firebase"] as const)("%s stack — one URN per resource", (provider) => {
|
|
270
|
+
it.each(["false", "true"] as const)("with deployGateways=%s", (deployGateways) => {
|
|
271
|
+
// Pulumi fails the whole program on a duplicate URN (#1434).
|
|
272
|
+
const urns = declaredBy(provider, deployGateways).map((r) => `${r.type}::${r.name}`);
|
|
273
|
+
expect(new Set(urns).size).toBe(urns.length);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe("gcp stack — the first up declares no gateway (Pulumi mocks)", () => {
|
|
278
|
+
// Everything that serves or routes visitor traffic to the gateway: the URL
|
|
279
|
+
// map goes with it, because the stack ignores its path rules after creation.
|
|
280
|
+
const GATEWAY_CHAIN = [
|
|
281
|
+
`${SERVICE}::caelo-production-gateway`,
|
|
282
|
+
"gcp:compute/regionNetworkEndpointGroup:RegionNetworkEndpointGroup::caelo-production-gateway-neg",
|
|
283
|
+
"gcp:compute/backendService:BackendService::caelo-production-gateway-backend",
|
|
284
|
+
"gcp:compute/uRLMap:URLMap::caelo-production-url-map",
|
|
285
|
+
"gcp:compute/targetHttpsProxy:TargetHttpsProxy::caelo-production-https-proxy",
|
|
286
|
+
"gcp:compute/globalForwardingRule:GlobalForwardingRule::caelo-production-https-fwd",
|
|
287
|
+
];
|
|
288
|
+
const keys = (d: ReturnType<typeof declaredBy>) => d.map((r) => `${r.type}::${r.name}`);
|
|
289
|
+
|
|
290
|
+
it("withholds the gateway and its route, but declares what the migrations need", () => {
|
|
291
|
+
const first = keys(declaredBy("gcp", "false"));
|
|
292
|
+
for (const k of GATEWAY_CHAIN) expect(first).not.toContain(k);
|
|
293
|
+
for (const k of [
|
|
294
|
+
`${SERVICE}::caelo-production-admin`,
|
|
295
|
+
"gcp:sql/databaseInstance:DatabaseInstance::caelo-production-pg",
|
|
296
|
+
"gcp:sql/database:Database::caelo-production-cms-admin-db",
|
|
297
|
+
"gcp:sql/database:Database::caelo-production-cms-public-db",
|
|
298
|
+
"gcp:sql/database:Database::caelo-production-cms-public-staging-db",
|
|
299
|
+
"gcp:sql/user:User::caelo-production-pg-public",
|
|
300
|
+
"gcp:sql/user:User::caelo-production-pg-public-staging",
|
|
301
|
+
]) {
|
|
302
|
+
expect(first).toContain(k);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("declares all of it once the database is migrated — and adds nothing else", () => {
|
|
307
|
+
const first = keys(declaredBy("gcp", "false"));
|
|
308
|
+
const full = keys(declaredBy("gcp", "true"));
|
|
309
|
+
for (const k of GATEWAY_CHAIN) expect(full).toContain(k);
|
|
310
|
+
expect(full.filter((k) => !first.includes(k)).sort()).toEqual([...GATEWAY_CHAIN].sort());
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe("gcp-firebase stack — the first up declares no data gateway (Pulumi mocks)", () => {
|
|
315
|
+
const dataGateway = fixedCloudRunServiceName("gcp-firebase", "production", "gateway");
|
|
316
|
+
const isDataGateway = (r: { type: string; cloudName?: string }) =>
|
|
317
|
+
r.type === SERVICE && r.cloudName === dataGateway;
|
|
318
|
+
|
|
319
|
+
it("withholds the data gateway and its bindings, keeps the admin and the MCP-Auth-Proxy", () => {
|
|
320
|
+
const first = declaredBy("gcp-firebase", "false");
|
|
321
|
+
expect(first.filter(isDataGateway)).toEqual([]);
|
|
322
|
+
expect(first.map((r) => r.name)).not.toContain("caelo-production-data-gateway-public-invoker");
|
|
323
|
+
expect(first.map((r) => r.name)).not.toContain(
|
|
324
|
+
"caelo-production-data-gateway-viewer-for-admin-sa",
|
|
325
|
+
);
|
|
326
|
+
expect(first.filter((r) => r.type === SERVICE).length).toBe(2);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("declares it once the database is migrated", () => {
|
|
330
|
+
const full = declaredBy("gcp-firebase", "true");
|
|
331
|
+
expect(full.filter(isDataGateway).length).toBe(1);
|
|
332
|
+
expect(full.map((r) => r.name)).toContain("caelo-production-data-gateway-public-invoker");
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("the installer wires the order (wizards/gcp.ts)", () => {
|
|
337
|
+
const src = readFileSync(resolve(import.meta.dir, "wizards", "gcp.ts"), "utf8");
|
|
338
|
+
const converge = src.slice(src.indexOf("export async function runGcpConverge("));
|
|
339
|
+
const body = converge.slice(0, converge.indexOf("\nasync function ") + 1 || undefined);
|
|
340
|
+
|
|
341
|
+
it("runs the first pulumi up through deployOntoMigratedDatabase, with the gateway decided by the live read", () => {
|
|
342
|
+
const firstUp = body.indexOf("stepPulumiUp(");
|
|
343
|
+
expect(body.indexOf("stepReadGatewayStart(")).toBeGreaterThan(-1);
|
|
344
|
+
expect(body.indexOf("deployOntoMigratedDatabase(gatewayStart")).toBeGreaterThan(-1);
|
|
345
|
+
expect(firstUp).toBeGreaterThan(body.indexOf("deployOntoMigratedDatabase(gatewayStart"));
|
|
346
|
+
expect(body.indexOf("stepReadGatewayStart(")).toBeLessThan(firstUp);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("every other up keeps the gateway declared", () => {
|
|
350
|
+
const calls = body.split("stepPulumiUp(").slice(1);
|
|
351
|
+
expect(calls.length).toBe(2);
|
|
352
|
+
expect(calls[0]).toMatch(/^installId, \{\s*\.\.\.upOpts,\s*deployGateways,/);
|
|
353
|
+
expect(calls[1]).toMatch(/^installId, \{\s*\.\.\.upOpts,\s*deployGateways: true,/);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("isolates the logins and migrates inside prepareDatabase, before the gateway's up", () => {
|
|
357
|
+
const prepare = body.slice(body.indexOf("prepareDatabase: async () => {"));
|
|
358
|
+
const prepareBody = prepare.slice(0, prepare.indexOf("\n },\n"));
|
|
359
|
+
expect(prepareBody.indexOf("stepPublicRoleIsolation(projectId)")).toBeGreaterThan(-1);
|
|
360
|
+
expect(prepareBody.indexOf("stepMigrations(projectId, region)")).toBeGreaterThan(
|
|
361
|
+
prepareBody.indexOf("stepPublicRoleIsolation(projectId)"),
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("looks at the gateway again under the state lock before an up that withholds it", () => {
|
|
366
|
+
const up = src.slice(src.indexOf("async function stepPulumiUp("));
|
|
367
|
+
const lock = up.indexOf("acquireStateLock(");
|
|
368
|
+
const recheck = up.indexOf("withholdGatewayUnderLock(");
|
|
369
|
+
const pulumi = up.indexOf("pulumiUpGcp(");
|
|
370
|
+
expect(lock).toBeGreaterThan(-1);
|
|
371
|
+
expect(recheck).toBeGreaterThan(lock);
|
|
372
|
+
expect(pulumi).toBeGreaterThan(recheck);
|
|
373
|
+
expect(up.slice(pulumi, pulumi + 400)).toContain("deployGateways,");
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("migrates after the up only when it did not before", () => {
|
|
377
|
+
expect(body).toMatch(
|
|
378
|
+
/if \(!databasePrepared\) \{\s*phase\("Applying database migrations"\);\s*await stepMigrations\(projectId, region\);/,
|
|
379
|
+
);
|
|
380
|
+
});
|
|
381
|
+
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The installer's order on Google Cloud: no public gateway is deployed onto
|
|
5
|
+
* a database whose migrations have not run (CLAUDE.md §2, §11.C).
|
|
6
|
+
*
|
|
7
|
+
* The data gateway checks at boot that its login cannot reach cms_admin or
|
|
8
|
+
* the other environment's public database, and exits when it can
|
|
9
|
+
* (apps/api-gateway `databaseIsolationProblems`). Cloud SQL leaves CONNECT
|
|
10
|
+
* on every database granted to PUBLIC; the REVOKEs are migrations (0278,
|
|
11
|
+
* 0530), so migrations stay their single source. Cloud Run finishes creating
|
|
12
|
+
* a service only once its container listens, so a `pulumi up` that creates
|
|
13
|
+
* the gateway before the migrations ran fails on every fresh install — and
|
|
14
|
+
* the migration job needs the database, the network and the admin service
|
|
15
|
+
* that very `up` creates.
|
|
16
|
+
*
|
|
17
|
+
* So an install whose gateway has never started runs:
|
|
18
|
+
*
|
|
19
|
+
* 1. `pulumi up` without the gateway (stack-gateways.ts) — the database,
|
|
20
|
+
* the network, the secrets and the admin;
|
|
21
|
+
* 2. the gateway logins out of cloudsqlsuperuser, then the migrations;
|
|
22
|
+
* 3. `pulumi up` with the gateway, which now starts.
|
|
23
|
+
*
|
|
24
|
+
* An install whose gateway has started at least once has been migrated, and
|
|
25
|
+
* keeps the order it always had: `up` first (which may roll the admin onto
|
|
26
|
+
* the images the run deploys), migrations after. "Has started" is the live
|
|
27
|
+
* service's latest READY revision, not the Pulumi checkpoint: a first run
|
|
28
|
+
* that failed exactly here left a gateway that never started, and the next
|
|
29
|
+
* run must take the three steps, not repeat the failure. The staging gateway
|
|
30
|
+
* is the CLI's and is created after the migrations anyway
|
|
31
|
+
* (staging-gateway-setup.ts); `upgrade` never runs Pulumi and migrates
|
|
32
|
+
* before it rolls any gateway (upgrade/app-roll.ts).
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { isGcloudAuthError } from "./gcloud.js";
|
|
36
|
+
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
37
|
+
import { GCP_STACK_ENV } from "./gcp-names.js";
|
|
38
|
+
import { cloudRunServicePrefix, type GcpProvider } from "./stack-contract.js";
|
|
39
|
+
import { lookupFailure } from "./upgrade/gcp-lookup.js";
|
|
40
|
+
|
|
41
|
+
/** Whether the install's data gateway has ever started. */
|
|
42
|
+
export type GatewayStart = "started" | "never-started";
|
|
43
|
+
|
|
44
|
+
/** What {@link readGatewayStart} found. */
|
|
45
|
+
export type GatewayStartRead =
|
|
46
|
+
| { readonly ok: true; readonly start: "started" }
|
|
47
|
+
| {
|
|
48
|
+
readonly ok: true;
|
|
49
|
+
readonly start: "never-started";
|
|
50
|
+
/** Gateway services that exist but never had a ready revision (a failed first run). */
|
|
51
|
+
readonly neverStarted: readonly string[];
|
|
52
|
+
}
|
|
53
|
+
| { readonly ok: false; readonly error: string };
|
|
54
|
+
|
|
55
|
+
/** The install the gateway is looked up on. */
|
|
56
|
+
export interface GatewayStartTarget {
|
|
57
|
+
readonly provider: GcpProvider;
|
|
58
|
+
readonly projectId: string;
|
|
59
|
+
readonly region: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether the live data gateway has a READY revision (Cloud Run's
|
|
64
|
+
* `status.latestReadyRevisionName`): only a gateway that passed its boot
|
|
65
|
+
* check has one. No gateway service at all is "never-started". Read-only.
|
|
66
|
+
*
|
|
67
|
+
* @returns an error (never a guess) when the services cannot be read
|
|
68
|
+
*/
|
|
69
|
+
export async function readGatewayStart(
|
|
70
|
+
target: GatewayStartTarget,
|
|
71
|
+
run: GcloudRunner,
|
|
72
|
+
): Promise<GatewayStartRead> {
|
|
73
|
+
const prefix = cloudRunServicePrefix(target.provider, GCP_STACK_ENV, "gateway");
|
|
74
|
+
const list = await run([
|
|
75
|
+
"run",
|
|
76
|
+
"services",
|
|
77
|
+
"list",
|
|
78
|
+
`--region=${target.region}`,
|
|
79
|
+
`--project=${target.projectId}`,
|
|
80
|
+
`--filter=metadata.name~^${prefix}`,
|
|
81
|
+
"--format=value(metadata.name)",
|
|
82
|
+
]);
|
|
83
|
+
if (!list.ok) {
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
error: lookupFailure(
|
|
87
|
+
{ status: "error", stderr: list.stderr, authExpired: isGcloudAuthError(list.stderr) },
|
|
88
|
+
`${prefix}* Cloud Run service`,
|
|
89
|
+
"operator",
|
|
90
|
+
),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const names = list.stdout
|
|
94
|
+
.split("\n")
|
|
95
|
+
.map((s) => s.trim())
|
|
96
|
+
.filter(Boolean);
|
|
97
|
+
for (const name of names) {
|
|
98
|
+
const ready = await latestReadyRevision(target, name, run);
|
|
99
|
+
if (!ready.ok) return ready;
|
|
100
|
+
if (ready.revision) return { ok: true, start: "started" };
|
|
101
|
+
}
|
|
102
|
+
return { ok: true, start: "never-started", neverStarted: names };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function latestReadyRevision(
|
|
106
|
+
target: GatewayStartTarget,
|
|
107
|
+
name: string,
|
|
108
|
+
run: GcloudRunner,
|
|
109
|
+
): Promise<{ ok: true; revision: string } | { ok: false; error: string }> {
|
|
110
|
+
const r = await run([
|
|
111
|
+
"run",
|
|
112
|
+
"services",
|
|
113
|
+
"describe",
|
|
114
|
+
name,
|
|
115
|
+
`--region=${target.region}`,
|
|
116
|
+
`--project=${target.projectId}`,
|
|
117
|
+
"--format=value(status.latestReadyRevisionName)",
|
|
118
|
+
]);
|
|
119
|
+
return r.ok
|
|
120
|
+
? { ok: true, revision: r.stdout.trim() }
|
|
121
|
+
: { ok: false, error: `describe ${name}: ${r.stderr.trim()}` };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Delete gateway services a failed first run left behind, which never had a
|
|
126
|
+
* ready revision — so never served a request. Run under the install's state
|
|
127
|
+
* lock, right before the `up` that withholds the gateway: Pulumi would
|
|
128
|
+
* delete them in that `up` anyway, but cannot when the operator enabled
|
|
129
|
+
* `deletionProtection`, and a fixed-name one outside the state would block
|
|
130
|
+
* the later create. The `up`'s refresh then drops them from the state.
|
|
131
|
+
* Each is described again first; one that has become ready meanwhile is
|
|
132
|
+
* kept and reported, never deleted.
|
|
133
|
+
*
|
|
134
|
+
* @returns the services deleted, or an error naming the one that stopped it
|
|
135
|
+
*/
|
|
136
|
+
export async function deleteNeverStartedGateways(
|
|
137
|
+
target: GatewayStartTarget,
|
|
138
|
+
names: readonly string[],
|
|
139
|
+
run: GcloudRunner,
|
|
140
|
+
): Promise<{ ok: true; deleted: string[] } | { ok: false; error: string }> {
|
|
141
|
+
const deleted: string[] = [];
|
|
142
|
+
for (const name of names) {
|
|
143
|
+
const ready = await latestReadyRevision(target, name, run);
|
|
144
|
+
if (!ready.ok) return ready;
|
|
145
|
+
if (ready.revision) {
|
|
146
|
+
return {
|
|
147
|
+
ok: false,
|
|
148
|
+
error: `${name} has started meanwhile (revision ${ready.revision}), so another run has deployed the gateway; re-run the installer`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const r = await run([
|
|
152
|
+
"run",
|
|
153
|
+
"services",
|
|
154
|
+
"delete",
|
|
155
|
+
name,
|
|
156
|
+
`--region=${target.region}`,
|
|
157
|
+
`--project=${target.projectId}`,
|
|
158
|
+
"--quiet",
|
|
159
|
+
]);
|
|
160
|
+
if (!r.ok && !/NOT_FOUND|not found|could not be found/i.test(r.stderr)) {
|
|
161
|
+
return { ok: false, error: `delete ${name}: ${r.stderr.trim()}` };
|
|
162
|
+
}
|
|
163
|
+
deleted.push(name);
|
|
164
|
+
}
|
|
165
|
+
return { ok: true, deleted };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The steps {@link deployOntoMigratedDatabase} orders. */
|
|
169
|
+
export interface GatewayBootstrapSteps {
|
|
170
|
+
/** `pulumi up` with the stack's `deployGateways` set to the argument. */
|
|
171
|
+
readonly up: (deployGateways: boolean) => Promise<void>;
|
|
172
|
+
/** The gateway logins out of cloudsqlsuperuser, then the migrations. */
|
|
173
|
+
readonly prepareDatabase: () => Promise<void>;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Run the installer's `pulumi up` so that the gateway is deployed only onto
|
|
178
|
+
* a migrated database (see the module comment).
|
|
179
|
+
*
|
|
180
|
+
* @returns whether the database was prepared here; when not, the caller
|
|
181
|
+
* runs the migrations after this `up`, as before
|
|
182
|
+
*/
|
|
183
|
+
export async function deployOntoMigratedDatabase(
|
|
184
|
+
start: GatewayStart,
|
|
185
|
+
steps: GatewayBootstrapSteps,
|
|
186
|
+
): Promise<{ readonly databasePrepared: boolean }> {
|
|
187
|
+
if (start === "started") {
|
|
188
|
+
await steps.up(true);
|
|
189
|
+
return { databasePrepared: false };
|
|
190
|
+
}
|
|
191
|
+
await steps.up(false);
|
|
192
|
+
await steps.prepareDatabase();
|
|
193
|
+
await steps.up(true);
|
|
194
|
+
return { databasePrepared: true };
|
|
195
|
+
}
|
package/src/gcloud.test.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type GcloudResult,
|
|
7
7
|
grantProvisionerRoles,
|
|
8
8
|
isGcloudAuthError,
|
|
9
|
+
isGcloudNotFound,
|
|
9
10
|
PROVISIONER_ROLE_LIST,
|
|
10
11
|
projectDisplayName,
|
|
11
12
|
} from "./gcloud.js";
|
|
@@ -189,3 +190,26 @@ describe("checkGcloudAuth (#630)", () => {
|
|
|
189
190
|
expect(isGcloudAuthError("PERMISSION_DENIED: run.services.list")).toBe(false);
|
|
190
191
|
});
|
|
191
192
|
});
|
|
193
|
+
|
|
194
|
+
describe("isGcloudNotFound — one list of every service's wording", () => {
|
|
195
|
+
it.each([
|
|
196
|
+
"ERROR: (gcloud.run.services.describe) Cannot find service [caelo-production-gateway-aa7db32]",
|
|
197
|
+
"ERROR: (gcloud.secrets.describe) NOT_FOUND: Secret [x] not found or has no versions.",
|
|
198
|
+
"ERROR: (gcloud.compute.backend-services.describe) The resource 'projects/p/global/backendServices/b' was not found",
|
|
199
|
+
"ERROR: (gcloud.iam.service-accounts.describe) NOT_FOUND: Unknown service account.",
|
|
200
|
+
"HTTPError 404: Not Found",
|
|
201
|
+
'ERROR: {"error": {"code": 404, "message": "Requested entity was not found."}}',
|
|
202
|
+
"ERROR: (gcloud.run.jobs.executions.describe) Execution [e] could not be found.",
|
|
203
|
+
])("reads %p as missing", (stderr) => {
|
|
204
|
+
expect(isGcloudNotFound(stderr)).toBe(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it.each([
|
|
208
|
+
"ERROR: (gcloud.iam.service-accounts.describe) PERMISSION_DENIED",
|
|
209
|
+
"ERROR: (gcloud.run.services.describe) There was a problem refreshing your current auth tokens",
|
|
210
|
+
"ERROR: project 4041234 quota exceeded",
|
|
211
|
+
"ERROR: (gcloud.run.services.describe) PERMISSION_DENIED: on projects/caelo-404-prod/locations/europe-west1",
|
|
212
|
+
])("does not read %p as missing", (stderr) => {
|
|
213
|
+
expect(isGcloudNotFound(stderr)).toBe(false);
|
|
214
|
+
});
|
|
215
|
+
});
|
package/src/gcloud.ts
CHANGED
|
@@ -101,6 +101,23 @@ export function isGcloudAuthError(stderr: string): boolean {
|
|
|
101
101
|
return GCLOUD_AUTH_ERROR.test(stderr);
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Every way gcloud says a described resource does not exist. The wording
|
|
106
|
+
* differs per service: Cloud Run says "Cannot find service [<name>]." with
|
|
107
|
+
* no code, Secret Manager and IAM say NOT_FOUND, compute says "was not
|
|
108
|
+
* found", the REST APIs answer HTTP 404. A bare 404 is not enough: project
|
|
109
|
+
* and resource ids may contain one (`caelo-404-prod`). One list, because a
|
|
110
|
+
* describe that missed a wording reads a missing resource as an unknown one
|
|
111
|
+
* and stops a fresh install (scratch installs of v0.11.15, 2026-11-04).
|
|
112
|
+
*/
|
|
113
|
+
const GCLOUD_NOT_FOUND =
|
|
114
|
+
/\bNOT_FOUND\b|NotFound|not found|cannot find|could not be found|does not exist|\bHTTP(?:Error)? ?404\b|"code": ?404\b/i;
|
|
115
|
+
|
|
116
|
+
/** Whether a failed gcloud describe failed because the resource does not exist. */
|
|
117
|
+
export function isGcloudNotFound(stderr: string): boolean {
|
|
118
|
+
return GCLOUD_NOT_FOUND.test(stderr);
|
|
119
|
+
}
|
|
120
|
+
|
|
104
121
|
/** The fix for expired credentials on the Owner's machine or in Cloud Shell. */
|
|
105
122
|
export const GCLOUD_REAUTH_HINT =
|
|
106
123
|
"gcloud credentials expired. Run `gcloud auth login` (and `gcloud auth application-default login` where Pulumi runs), then run this command again.";
|
package/src/gcp-url-map.test.ts
CHANGED
|
@@ -156,7 +156,7 @@ describe("gcp stack wiring (source)", () => {
|
|
|
156
156
|
const src = readFileSync(resolve(import.meta.dir, "../stacks/gcp/index.ts"), "utf8");
|
|
157
157
|
|
|
158
158
|
it("builds the URL map through createSiteUrlMap", () => {
|
|
159
|
-
expect(src).toMatch(/
|
|
159
|
+
expect(src).toMatch(/return createSiteUrlMap\(\s*`\$\{namePrefix\}-url-map`/);
|
|
160
160
|
expect(src).not.toMatch(/new gcp\.compute\.URLMap\(\s*`\$\{namePrefix\}-url-map`/);
|
|
161
161
|
});
|
|
162
162
|
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
31
31
|
import { tmpdir } from "node:os";
|
|
32
32
|
import { join } from "node:path";
|
|
33
|
-
import { gcloud as defaultGcloud } from "./gcloud.js";
|
|
33
|
+
import { gcloud as defaultGcloud, isGcloudNotFound } from "./gcloud.js";
|
|
34
34
|
import { type GcloudRunner, realSleep, runWithRetry, type Sleep } from "./gcloud-retry.js";
|
|
35
35
|
import { gcpSecretId, mcpAuthProxyServiceAccountEmail } from "./gcp-names.js";
|
|
36
36
|
import { type EnsureOutcome, ensureMcpAuthProxyServiceAccount } from "./runtime-secrets.js";
|
|
@@ -82,10 +82,6 @@ export interface ReleaseServicesDeps {
|
|
|
82
82
|
readonly sleep?: Sleep;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
// gcloud words a missing Cloud Run service "Cannot find service [<name>]."
|
|
86
|
-
// with no NOT_FOUND code (searchVIU upgrade to v0.11.10, 2026-11-03).
|
|
87
|
-
const NOT_FOUND = /NOT_FOUND|not found|cannot find|does not exist|could not be found/i;
|
|
88
|
-
|
|
89
85
|
/** Ensure everything {@link ReleaseServicesTarget} needs; outcomes in order, failures included. */
|
|
90
86
|
export async function ensureReleaseServices(
|
|
91
87
|
target: ReleaseServicesTarget,
|
|
@@ -249,7 +245,7 @@ async function ensureDataGatewayService(
|
|
|
249
245
|
"--format=value(metadata.name)",
|
|
250
246
|
]);
|
|
251
247
|
if (existing.ok) return { id, status: "present" };
|
|
252
|
-
if (!
|
|
248
|
+
if (!isGcloudNotFound(existing.stderr)) {
|
|
253
249
|
return { id, status: "failed", error: existing.stderr.trim() };
|
|
254
250
|
}
|
|
255
251
|
// The source: the service that was the gateway and becomes the proxy.
|
|
@@ -383,7 +379,7 @@ export async function ensureMcpAuthProxyServiceGcp(
|
|
|
383
379
|
"--format=value(metadata.name)",
|
|
384
380
|
]);
|
|
385
381
|
if (existing.ok) return { id, status: "present" };
|
|
386
|
-
if (!
|
|
382
|
+
if (!isGcloudNotFound(existing.stderr)) {
|
|
387
383
|
return { id, status: "failed", error: existing.stderr.trim() };
|
|
388
384
|
}
|
|
389
385
|
const deployed = await run(gcpProxyDeployArgs(target));
|