@caelo-cms/provisioning 0.11.21 → 0.12.2
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/adapter.d.ts +6 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/agent-db.js +137 -9
- package/dist/bootstrap-token.d.ts +4 -3
- package/dist/bootstrap-token.d.ts.map +1 -1
- package/dist/bootstrap-token.js.map +1 -1
- package/dist/cli-routing.d.ts.map +1 -1
- package/dist/cli-routing.js +5 -0
- package/dist/cli-routing.js.map +1 -1
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/converge-job-gcp.d.ts +1 -16
- package/dist/converge-job-gcp.d.ts.map +1 -1
- package/dist/converge-job-gcp.js +10 -97
- package/dist/converge-job-gcp.js.map +1 -1
- package/dist/converge-job.d.ts +7 -46
- package/dist/converge-job.d.ts.map +1 -1
- package/dist/converge-job.js +22 -57
- package/dist/converge-job.js.map +1 -1
- package/dist/gateway-armor.d.ts.map +1 -1
- package/dist/gateway-armor.js +11 -3
- package/dist/gateway-armor.js.map +1 -1
- package/dist/gcp-names.d.ts +2 -0
- package/dist/gcp-names.d.ts.map +1 -1
- package/dist/gcp-names.js +4 -0
- package/dist/gcp-names.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/infra-contract.d.ts +1 -1
- package/dist/infra-contract.d.ts.map +1 -1
- package/dist/infra-contract.js +14 -1
- package/dist/infra-contract.js.map +1 -1
- package/dist/job-handover-gcp.d.ts +40 -0
- package/dist/job-handover-gcp.d.ts.map +1 -0
- package/dist/job-handover-gcp.js +108 -0
- package/dist/job-handover-gcp.js.map +1 -0
- package/dist/job-handover.d.ts +141 -0
- package/dist/job-handover.d.ts.map +1 -0
- package/dist/job-handover.js +141 -0
- package/dist/job-handover.js.map +1 -0
- package/dist/owner-setup-token.d.ts +52 -0
- package/dist/owner-setup-token.d.ts.map +1 -0
- package/dist/owner-setup-token.js +111 -0
- package/dist/owner-setup-token.js.map +1 -0
- package/dist/project-status.d.ts +33 -0
- package/dist/project-status.d.ts.map +1 -1
- package/dist/project-status.js +116 -26
- package/dist/project-status.js.map +1 -1
- package/dist/stack-converge.d.ts.map +1 -1
- package/dist/stack-converge.js +9 -5
- package/dist/stack-converge.js.map +1 -1
- package/dist/static-security-headers.d.ts +6 -0
- package/dist/static-security-headers.d.ts.map +1 -1
- package/dist/static-security-headers.js +11 -1
- package/dist/static-security-headers.js.map +1 -1
- package/dist/upgrader-assess.d.ts +30 -3
- package/dist/upgrader-assess.d.ts.map +1 -1
- package/dist/upgrader-assess.js +77 -24
- package/dist/upgrader-assess.js.map +1 -1
- package/dist/upgrader-gcp.d.ts +4 -2
- package/dist/upgrader-gcp.d.ts.map +1 -1
- package/dist/upgrader-gcp.js +16 -3
- package/dist/upgrader-gcp.js.map +1 -1
- package/dist/upgrader-iam-preflight.d.ts +14 -3
- package/dist/upgrader-iam-preflight.d.ts.map +1 -1
- package/dist/upgrader-iam-preflight.js +40 -6
- package/dist/upgrader-iam-preflight.js.map +1 -1
- package/dist/upgrader-job.d.ts +51 -9
- package/dist/upgrader-job.d.ts.map +1 -1
- package/dist/upgrader-job.js +92 -37
- package/dist/upgrader-job.js.map +1 -1
- package/dist/upgrader-setup.d.ts +1 -1
- package/dist/upgrader-setup.d.ts.map +1 -1
- package/dist/upgrader-setup.js +5 -2
- package/dist/upgrader-setup.js.map +1 -1
- package/dist/wizards/gcp-install-job.d.ts.map +1 -1
- package/dist/wizards/gcp-install-job.js +11 -1
- package/dist/wizards/gcp-install-job.js.map +1 -1
- package/dist/wizards/gcp-pulumi.d.ts +0 -13
- package/dist/wizards/gcp-pulumi.d.ts.map +1 -1
- package/dist/wizards/gcp-pulumi.js +0 -12
- package/dist/wizards/gcp-pulumi.js.map +1 -1
- package/dist/wizards/gcp.d.ts.map +1 -1
- package/dist/wizards/gcp.js +21 -50
- package/dist/wizards/gcp.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +6 -1
- package/src/bootstrap-token.ts +4 -3
- package/src/caddy-redirects.docker.test.ts +5 -0
- package/src/cli-routing.test.ts +1 -1
- package/src/cli-routing.ts +6 -0
- package/src/cli.ts +4 -0
- package/src/converge-job-gcp.test.ts +1 -40
- package/src/converge-job-gcp.ts +10 -100
- package/src/converge-job.test.ts +9 -3
- package/src/converge-job.ts +33 -115
- package/src/gateway-armor.ts +14 -3
- package/src/gateway-edge.test.ts +16 -1
- package/src/gcp-names.ts +4 -0
- package/src/gcp-url-map.test.ts +2 -0
- package/src/index.ts +3 -0
- package/src/infra-contract.test.ts +4 -3
- package/src/infra-contract.ts +14 -1
- package/src/job-handover-gcp.test.ts +129 -0
- package/src/job-handover-gcp.ts +135 -0
- package/src/job-handover.ts +259 -0
- package/src/owner-setup-token.test.ts +195 -0
- package/src/owner-setup-token.ts +138 -0
- package/src/project-status.test.ts +112 -3
- package/src/project-status.ts +141 -17
- package/src/stack-converge.ts +12 -5
- package/src/static-security-headers.test.ts +16 -0
- package/src/static-security-headers.ts +12 -1
- package/src/upgrader-assess.test.ts +75 -1
- package/src/upgrader-assess.ts +121 -57
- package/src/upgrader-gcp.ts +19 -5
- package/src/upgrader-iam-preflight.test.ts +28 -1
- package/src/upgrader-iam-preflight.ts +46 -6
- package/src/upgrader-job.test.ts +365 -31
- package/src/upgrader-job.ts +177 -53
- package/src/upgrader-setup.test.ts +17 -0
- package/src/upgrader-setup.ts +5 -2
- package/src/url-map-redirects-access.test.ts +1 -1
- package/src/wizards/gcp-install-job.ts +10 -1
- package/src/wizards/gcp-pulumi.ts +0 -18
- package/src/wizards/gcp.ts +24 -61
- package/stacks/aws/README.md +4 -2
- package/stacks/aws/index.ts +4 -8
- package/stacks/azure/README.md +4 -2
- package/stacks/azure/index.ts +4 -8
- package/stacks/gcp/README.md +6 -3
- package/stacks/gcp/index.ts +7 -8
- package/stacks/gcp-firebase/index.ts +4 -6
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* #1476 — the Owner setup token: stored for the admin, with its read access,
|
|
5
|
+
* minted once, and replaced only when it stopped being useful. The admin's
|
|
6
|
+
* half is packages/admin-core/src/__tests__/owner-bootstrap-secret.integration.test.ts.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, expect, it } from "bun:test";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { OWNER_BOOTSTRAP_TOKEN_SECRET, parseOwnerBootstrapToken } from "@caelo-cms/shared";
|
|
13
|
+
import type { GcloudResult } from "./gcloud.js";
|
|
14
|
+
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
15
|
+
import { GCP_STACK_ENV, gcpSecretId } from "./gcp-names.js";
|
|
16
|
+
import { INFRA_CONTRACT_STEPS } from "./infra-contract.js";
|
|
17
|
+
import { INSTALL_SETUP_LINK_SECRET } from "./install-job.js";
|
|
18
|
+
import { issueOwnerSetup, readOwnerBootstrapToken } from "./owner-setup-token.js";
|
|
19
|
+
|
|
20
|
+
const PROJECT = "acme-site";
|
|
21
|
+
const TARGET = { projectId: PROJECT, region: "europe-west1" };
|
|
22
|
+
const ADMIN = "https://admin.acme.test";
|
|
23
|
+
const NOW = Date.parse("2026-11-05T12:00:00.000Z");
|
|
24
|
+
const HOUR = 60 * 60 * 1000;
|
|
25
|
+
|
|
26
|
+
const ok = (stdout = ""): GcloudResult => ({ ok: true, stdout, stderr: "", exitCode: 0 });
|
|
27
|
+
const fail = (stderr: string): GcloudResult => ({ ok: false, stdout: "", stderr, exitCode: 1 });
|
|
28
|
+
|
|
29
|
+
/** A project's secrets as gcloud sees them; the calls it got. */
|
|
30
|
+
function secretManager(
|
|
31
|
+
initial: Record<string, string> = {},
|
|
32
|
+
refuse?: { readonly match: string; readonly stderr: string },
|
|
33
|
+
) {
|
|
34
|
+
const secrets = new Map(Object.entries(initial));
|
|
35
|
+
const calls: { args: string[]; stdin?: string }[] = [];
|
|
36
|
+
const run: GcloudRunner = async (args, opts) => {
|
|
37
|
+
calls.push({ args, ...(opts?.stdin === undefined ? {} : { stdin: opts.stdin }) });
|
|
38
|
+
const line = args.join(" ");
|
|
39
|
+
if (refuse && line.includes(refuse.match)) return fail(refuse.stderr);
|
|
40
|
+
if (line.startsWith("secrets versions access")) {
|
|
41
|
+
const id = args.find((a) => a.startsWith("--secret="))?.slice("--secret=".length) ?? "";
|
|
42
|
+
const value = secrets.get(id);
|
|
43
|
+
return value === undefined ? fail("NOT_FOUND: Secret not found") : ok(value);
|
|
44
|
+
}
|
|
45
|
+
if (line.startsWith("secrets describe")) {
|
|
46
|
+
const id = args[2] ?? "";
|
|
47
|
+
if (id.endsWith("postgres-password")) return ok('{"replication":{"automatic":{}}}');
|
|
48
|
+
return secrets.has(id) ? ok(id) : fail("NOT_FOUND: Secret not found");
|
|
49
|
+
}
|
|
50
|
+
if (line.startsWith("secrets versions add")) {
|
|
51
|
+
secrets.set(args[3] ?? "", opts?.stdin ?? "");
|
|
52
|
+
return ok(`projects/1/secrets/${args[3]}/versions/2`);
|
|
53
|
+
}
|
|
54
|
+
if (line.startsWith("secrets versions list")) return ok("");
|
|
55
|
+
if (line.startsWith("secrets create")) {
|
|
56
|
+
secrets.set(args[2] ?? "", "");
|
|
57
|
+
return ok();
|
|
58
|
+
}
|
|
59
|
+
if (line.startsWith("secrets add-iam-policy-binding")) return ok();
|
|
60
|
+
throw new Error(`unexpected gcloud call: ${line}`);
|
|
61
|
+
};
|
|
62
|
+
return { run, secrets, calls };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const stored = (expiresAt: number) =>
|
|
66
|
+
JSON.stringify({ token: "a".repeat(64), expiresAt: new Date(expiresAt).toISOString() });
|
|
67
|
+
|
|
68
|
+
describe("issueOwnerSetup", () => {
|
|
69
|
+
it("mints a token, stores it for the admin, and stores the link that carries it", async () => {
|
|
70
|
+
const gcp = secretManager({ "caelo-production-postgres-password": "x" });
|
|
71
|
+
const r = await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
72
|
+
if (!r.ok) throw new Error(r.error);
|
|
73
|
+
expect(r.minted).toBe(true);
|
|
74
|
+
const token = parseOwnerBootstrapToken(gcp.secrets.get(OWNER_BOOTSTRAP_TOKEN_SECRET) ?? "");
|
|
75
|
+
expect(token?.token).toMatch(/^[0-9a-f]{64}$/);
|
|
76
|
+
expect(r.link).toBe(`${ADMIN}/setup?token=${token?.token}`);
|
|
77
|
+
expect(gcp.secrets.get(INSTALL_SETUP_LINK_SECRET)).toBe(r.link);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("gives the admin's service account read access, on this secret only", async () => {
|
|
81
|
+
const gcp = secretManager({ "caelo-production-postgres-password": "x" });
|
|
82
|
+
await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
83
|
+
const grants = gcp.calls.filter((c) => c.args[1] === "add-iam-policy-binding");
|
|
84
|
+
expect(grants).toHaveLength(1);
|
|
85
|
+
expect(grants[0]?.args[2]).toBe(OWNER_BOOTSTRAP_TOKEN_SECRET);
|
|
86
|
+
expect(grants[0]?.args).toContain(
|
|
87
|
+
`--member=serviceAccount:caelo-production-run-sa@${PROJECT}.iam.gserviceaccount.com`,
|
|
88
|
+
);
|
|
89
|
+
expect(grants[0]?.args).toContain("--role=roles/secretmanager.secretAccessor");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("never puts the token on a command line", async () => {
|
|
93
|
+
const gcp = secretManager({ "caelo-production-postgres-password": "x" });
|
|
94
|
+
const r = await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
95
|
+
if (!r.ok) throw new Error(r.error);
|
|
96
|
+
const token = new URL(r.link).searchParams.get("token") ?? "";
|
|
97
|
+
expect(gcp.calls.some((c) => c.args.join(" ").includes(token))).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("keeps a token with time left, so a link already handed out stays good", async () => {
|
|
101
|
+
const gcp = secretManager({
|
|
102
|
+
[OWNER_BOOTSTRAP_TOKEN_SECRET]: stored(NOW + 5 * HOUR),
|
|
103
|
+
});
|
|
104
|
+
const r = await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
105
|
+
if (!r.ok) throw new Error(r.error);
|
|
106
|
+
expect(r.minted).toBe(false);
|
|
107
|
+
expect(r.link).toBe(`${ADMIN}/setup?token=${"a".repeat(64)}`);
|
|
108
|
+
expect(gcp.secrets.get(OWNER_BOOTSTRAP_TOKEN_SECRET)).toBe(stored(NOW + 5 * HOUR));
|
|
109
|
+
// An interrupted run may have stopped before the grant.
|
|
110
|
+
expect(gcp.calls.some((c) => c.args[1] === "add-iam-policy-binding")).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it.each([
|
|
114
|
+
["expired", stored(NOW - HOUR)],
|
|
115
|
+
["about to expire", stored(NOW + HOUR / 2)],
|
|
116
|
+
["not a token", "garbage"],
|
|
117
|
+
])("replaces a stored token that is %s", async (_case, value) => {
|
|
118
|
+
const gcp = secretManager({
|
|
119
|
+
"caelo-production-postgres-password": "x",
|
|
120
|
+
[OWNER_BOOTSTRAP_TOKEN_SECRET]: value,
|
|
121
|
+
});
|
|
122
|
+
const r = await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
123
|
+
if (!r.ok) throw new Error(r.error);
|
|
124
|
+
expect(r.minted).toBe(true);
|
|
125
|
+
expect(r.link).not.toContain("a".repeat(64));
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("says so when a run at the same time changed the token, instead of printing a dead link", async () => {
|
|
129
|
+
const gcp = secretManager({ "caelo-production-postgres-password": "x" });
|
|
130
|
+
const racing: GcloudRunner = async (args, opts) => {
|
|
131
|
+
const r = await gcp.run(args, opts);
|
|
132
|
+
if (args.includes("add") && args.includes(INSTALL_SETUP_LINK_SECRET)) {
|
|
133
|
+
gcp.secrets.set(OWNER_BOOTSTRAP_TOKEN_SECRET, stored(NOW + 20 * HOUR));
|
|
134
|
+
}
|
|
135
|
+
return r;
|
|
136
|
+
};
|
|
137
|
+
const r = await issueOwnerSetup(racing, TARGET, ADMIN, NOW);
|
|
138
|
+
expect(r.ok).toBe(false);
|
|
139
|
+
if (!r.ok) expect(r.error).toContain("Run this command again");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("fails loudly when the admin cannot be given access, with no link stored", async () => {
|
|
143
|
+
const gcp = secretManager(
|
|
144
|
+
{ "caelo-production-postgres-password": "x" },
|
|
145
|
+
{ match: "add-iam-policy-binding", stderr: "PERMISSION_DENIED: setIamPolicy" },
|
|
146
|
+
);
|
|
147
|
+
const r = await issueOwnerSetup(gcp.run, TARGET, ADMIN, NOW);
|
|
148
|
+
expect(r).toEqual({
|
|
149
|
+
ok: false,
|
|
150
|
+
error: `let the admin read ${OWNER_BOOTSTRAP_TOKEN_SECRET}: PERMISSION_DENIED: setIamPolicy`,
|
|
151
|
+
});
|
|
152
|
+
expect(gcp.secrets.has(INSTALL_SETUP_LINK_SECRET)).toBe(false);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("readOwnerBootstrapToken", () => {
|
|
157
|
+
it("is null without a token", async () => {
|
|
158
|
+
expect(await readOwnerBootstrapToken(secretManager().run, PROJECT)).toBeNull();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("names what the account needs when the read is refused", async () => {
|
|
162
|
+
const gcp = secretManager({}, { match: "versions access", stderr: "PERMISSION_DENIED: no" });
|
|
163
|
+
await expect(readOwnerBootstrapToken(gcp.run, PROJECT)).rejects.toThrow(
|
|
164
|
+
"secretmanager.versions.access",
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("where the token lives", () => {
|
|
170
|
+
const stack = (provider: string) =>
|
|
171
|
+
readFileSync(join(import.meta.dir, `../stacks/${provider}/index.ts`), "utf8");
|
|
172
|
+
|
|
173
|
+
it("the admin and the CLI name the same secret", () => {
|
|
174
|
+
expect(OWNER_BOOTSTRAP_TOKEN_SECRET).toBe(gcpSecretId(GCP_STACK_ENV, "owner-bootstrap-token"));
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it.each(["gcp", "gcp-firebase", "aws", "azure"])(
|
|
178
|
+
"%s mints no token in the Pulumi program: it would change on every up and sit in its state",
|
|
179
|
+
(provider) => {
|
|
180
|
+
const source = stack(provider);
|
|
181
|
+
expect(source).not.toContain("generateBootstrapToken");
|
|
182
|
+
expect(source).not.toContain("setup?token=");
|
|
183
|
+
},
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
it.each(["gcp", "gcp-firebase"])(
|
|
187
|
+
"%s neither mounts the secret nor binds it: existing installs get no converge step",
|
|
188
|
+
(provider) => {
|
|
189
|
+
expect(stack(provider)).not.toContain("owner-bootstrap-token");
|
|
190
|
+
// No contract step was added for the token (#1476): a later bump is
|
|
191
|
+
// another change, so check the steps, not the current number.
|
|
192
|
+
expect(Object.values(INFRA_CONTRACT_STEPS).join("\n")).not.toContain("#1476");
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Owner setup token of a GCP install (#1476): mint it, keep it in Secret
|
|
5
|
+
* Manager for the admin, and store the link that carries it.
|
|
6
|
+
*
|
|
7
|
+
* The token is minted here and not in the Pulumi program, which runs on every
|
|
8
|
+
* `pulumi up` and would hand out a new one each time; and at the end of the
|
|
9
|
+
* install, so its 24 hours start when the link is shown, not hours earlier
|
|
10
|
+
* while the install waits for DNS. This also keeps it out of Pulumi's state.
|
|
11
|
+
*
|
|
12
|
+
* The secret and the admin's read access on it are made here, through
|
|
13
|
+
* gcloud, rather than declared in the stacks. Only an install still waiting
|
|
14
|
+
* for its Owner needs them, so an install that has one gets no new resource,
|
|
15
|
+
* no new binding and no infrastructure-contract step: its admin never reads
|
|
16
|
+
* this secret (packages/admin-core/src/security/owner-bootstrap-secret.ts).
|
|
17
|
+
*
|
|
18
|
+
* The admin reads the newest version whenever `/setup` opens, so a new token
|
|
19
|
+
* works without a new revision.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
OWNER_BOOTSTRAP_TOKEN_SECRET,
|
|
24
|
+
type OwnerBootstrapToken,
|
|
25
|
+
ownerSetupLink,
|
|
26
|
+
parseOwnerBootstrapToken,
|
|
27
|
+
} from "@caelo-cms/shared";
|
|
28
|
+
import { generateBootstrapToken } from "./bootstrap-token.js";
|
|
29
|
+
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
30
|
+
import { GCP_STACK_ENV, runServiceAccountEmail } from "./gcp-names.js";
|
|
31
|
+
import { storeInstallSecretValue, storeSetupLink } from "./install-job.js";
|
|
32
|
+
|
|
33
|
+
/** A token with less time left than this is replaced: nobody should get a link about to die. */
|
|
34
|
+
const MIN_REMAINING_MS = 60 * 60 * 1000;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The token stored in `projectId`, or null when there is none or it is not
|
|
38
|
+
* one (a new one replaces it).
|
|
39
|
+
*
|
|
40
|
+
* @throws when it can't be read for any other reason, with what the account needs
|
|
41
|
+
*/
|
|
42
|
+
export async function readOwnerBootstrapToken(
|
|
43
|
+
run: GcloudRunner,
|
|
44
|
+
projectId: string,
|
|
45
|
+
): Promise<OwnerBootstrapToken | null> {
|
|
46
|
+
const r = await run([
|
|
47
|
+
"secrets",
|
|
48
|
+
"versions",
|
|
49
|
+
"access",
|
|
50
|
+
"latest",
|
|
51
|
+
`--secret=${OWNER_BOOTSTRAP_TOKEN_SECRET}`,
|
|
52
|
+
`--project=${projectId}`,
|
|
53
|
+
]);
|
|
54
|
+
if (r.ok) return parseOwnerBootstrapToken(r.stdout.trim());
|
|
55
|
+
if (/NOT_FOUND|not found|DISABLED|DESTROYED|FAILED_PRECONDITION/i.test(r.stderr)) return null;
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Could not read the Owner setup token (secret ${OWNER_BOOTSTRAP_TOKEN_SECRET} in ${projectId}): ${r.stderr.trim()}. Your gcloud account needs secretmanager.versions.access on the project (e.g. roles/secretmanager.secretAccessor or project Owner).`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A working link, and whether its token is new. */
|
|
62
|
+
export interface OwnerSetupIssued {
|
|
63
|
+
readonly link: string;
|
|
64
|
+
readonly expiresAt: string;
|
|
65
|
+
readonly minted: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Make sure the install has a working Owner setup link: keep a stored token
|
|
70
|
+
* with time left (a link already handed out stays good), otherwise mint one
|
|
71
|
+
* and store it; let the admin's service account read it; store the link.
|
|
72
|
+
* Safe to repeat. Runs as the installer or as the operator's gcloud account.
|
|
73
|
+
*
|
|
74
|
+
* @param adminUrl the admin's bare origin, as the Owner opens it
|
|
75
|
+
* @param now the current time in ms, for tests
|
|
76
|
+
*/
|
|
77
|
+
export async function issueOwnerSetup(
|
|
78
|
+
run: GcloudRunner,
|
|
79
|
+
target: { readonly projectId: string; readonly region: string },
|
|
80
|
+
adminUrl: string,
|
|
81
|
+
now: number = Date.now(),
|
|
82
|
+
): Promise<({ ok: true } & OwnerSetupIssued) | { ok: false; error: string }> {
|
|
83
|
+
let stored: OwnerBootstrapToken | null;
|
|
84
|
+
try {
|
|
85
|
+
stored = await readOwnerBootstrapToken(run, target.projectId);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
return { ok: false, error: e instanceof Error ? e.message : String(e) };
|
|
88
|
+
}
|
|
89
|
+
const kept =
|
|
90
|
+
stored !== null && Date.parse(stored.expiresAt) - now > MIN_REMAINING_MS ? stored : null;
|
|
91
|
+
const current = kept ?? generateBootstrapToken();
|
|
92
|
+
if (!kept) {
|
|
93
|
+
const written = await storeInstallSecretValue(
|
|
94
|
+
run,
|
|
95
|
+
target,
|
|
96
|
+
OWNER_BOOTSTRAP_TOKEN_SECRET,
|
|
97
|
+
JSON.stringify(current),
|
|
98
|
+
);
|
|
99
|
+
if (!written.ok) return written;
|
|
100
|
+
}
|
|
101
|
+
// Also for a kept token: an interrupted run may have stored it and stopped
|
|
102
|
+
// before this grant.
|
|
103
|
+
const granted = await run([
|
|
104
|
+
"secrets",
|
|
105
|
+
"add-iam-policy-binding",
|
|
106
|
+
OWNER_BOOTSTRAP_TOKEN_SECRET,
|
|
107
|
+
`--project=${target.projectId}`,
|
|
108
|
+
`--member=serviceAccount:${runServiceAccountEmail(target.projectId, GCP_STACK_ENV)}`,
|
|
109
|
+
"--role=roles/secretmanager.secretAccessor",
|
|
110
|
+
"--condition=None",
|
|
111
|
+
"--quiet",
|
|
112
|
+
"--format=none",
|
|
113
|
+
]);
|
|
114
|
+
if (!granted.ok) {
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
error: `let the admin read ${OWNER_BOOTSTRAP_TOKEN_SECRET}: ${granted.stderr.trim()}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const link = ownerSetupLink(adminUrl, current.token);
|
|
121
|
+
const linked = await storeSetupLink(run, target, link);
|
|
122
|
+
if (!linked.ok) return linked;
|
|
123
|
+
// Two secrets, no transaction: a run at the same time may have written its
|
|
124
|
+
// own token in between, and then this link is not the one the admin accepts.
|
|
125
|
+
let latest: OwnerBootstrapToken | null;
|
|
126
|
+
try {
|
|
127
|
+
latest = await readOwnerBootstrapToken(run, target.projectId);
|
|
128
|
+
} catch (e) {
|
|
129
|
+
return { ok: false, error: e instanceof Error ? e.message : String(e) };
|
|
130
|
+
}
|
|
131
|
+
if (latest?.token !== current.token) {
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
error: `another run changed the Owner setup token at the same time (secret ${OWNER_BOOTSTRAP_TOKEN_SECRET}). Run this command again.`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return { ok: true, link, expiresAt: current.expiresAt, minted: kept === null };
|
|
138
|
+
}
|
|
@@ -10,6 +10,7 @@ import { afterEach, describe, expect, it } from "bun:test";
|
|
|
10
10
|
import { existsSync, mkdtempSync, readdirSync, rmSync } from "node:fs";
|
|
11
11
|
import { tmpdir } from "node:os";
|
|
12
12
|
import { join } from "node:path";
|
|
13
|
+
import { OWNER_BOOTSTRAP_TOKEN_SECRET } from "@caelo-cms/shared";
|
|
13
14
|
import { readOwnerSetupState } from "./admin-internal-google.js";
|
|
14
15
|
import type { GcloudResult } from "./gcloud.js";
|
|
15
16
|
import type { GcloudRunner } from "./gcloud-retry.js";
|
|
@@ -26,6 +27,8 @@ import {
|
|
|
26
27
|
projectStatusFailure,
|
|
27
28
|
projectStatusLines,
|
|
28
29
|
readProjectStatus,
|
|
30
|
+
setupLinkLines,
|
|
31
|
+
setupLinkRefusal,
|
|
29
32
|
} from "./project-status.js";
|
|
30
33
|
import { selfCommand } from "./self-command.js";
|
|
31
34
|
import type { SyncGcloud } from "./state-store.js";
|
|
@@ -33,7 +36,7 @@ import { handoffLines } from "./wizards/gcp-install-job.js";
|
|
|
33
36
|
|
|
34
37
|
const PROJECT = "acme-site";
|
|
35
38
|
const BUCKET = "gs://acme-site-caelo-ops/install";
|
|
36
|
-
const LINK =
|
|
39
|
+
const LINK = `https://admin.acme.test/setup?token=${"a".repeat(64)}`;
|
|
37
40
|
const INSTALL = {
|
|
38
41
|
installId: "gcp-acme-site",
|
|
39
42
|
provider: "gcp",
|
|
@@ -77,6 +80,11 @@ function job(patch: Partial<InstallJobStatus>): InstallJobStatus {
|
|
|
77
80
|
};
|
|
78
81
|
}
|
|
79
82
|
|
|
83
|
+
/** "Now" of every test, and a token's expiry either side of it. */
|
|
84
|
+
const NOW = Date.parse("2026-11-05T12:00:00.000Z");
|
|
85
|
+
const VALID_UNTIL = "2026-11-06T10:30:00.000Z";
|
|
86
|
+
const EXPIRED_ON = "2026-11-05T11:00:00.000Z";
|
|
87
|
+
|
|
80
88
|
const ok = (stdout: string): GcloudResult => ({ ok: true, stdout, stderr: "", exitCode: 0 });
|
|
81
89
|
const fail = (stderr: string): GcloudResult => ({ ok: false, stdout: "", stderr, exitCode: 1 });
|
|
82
90
|
|
|
@@ -85,6 +93,8 @@ interface World {
|
|
|
85
93
|
/** Stderr of a refused `storage cat`. */
|
|
86
94
|
denied?: string;
|
|
87
95
|
setupLink?: string | null;
|
|
96
|
+
/** The stored token's expiry; null: no token stored. */
|
|
97
|
+
tokenExpiresAt?: string | null;
|
|
88
98
|
linkDenied?: string;
|
|
89
99
|
execution?: "running" | "gone";
|
|
90
100
|
ownerSetup?: "open" | "done" | "unknown";
|
|
@@ -113,8 +123,14 @@ function project(world: World): { deps: ProjectStatusDeps; calls: string[][]; pr
|
|
|
113
123
|
const run: GcloudRunner = async (args) => {
|
|
114
124
|
calls.push(args);
|
|
115
125
|
if (args[0] === "secrets") {
|
|
116
|
-
expect(args).toContain(`--secret=${INSTALL_SETUP_LINK_SECRET}`);
|
|
117
126
|
if (world.linkDenied) return fail(world.linkDenied);
|
|
127
|
+
if (args.includes(`--secret=${OWNER_BOOTSTRAP_TOKEN_SECRET}`)) {
|
|
128
|
+
const expiresAt = world.tokenExpiresAt === undefined ? VALID_UNTIL : world.tokenExpiresAt;
|
|
129
|
+
return expiresAt
|
|
130
|
+
? ok(JSON.stringify({ token: "a".repeat(64), expiresAt }))
|
|
131
|
+
: fail("NOT_FOUND: Secret not found");
|
|
132
|
+
}
|
|
133
|
+
expect(args).toContain(`--secret=${INSTALL_SETUP_LINK_SECRET}`);
|
|
118
134
|
return world.setupLink ? ok(world.setupLink) : fail("NOT_FOUND: Secret not found");
|
|
119
135
|
}
|
|
120
136
|
if (args[0] === "run") {
|
|
@@ -137,6 +153,7 @@ function project(world: World): { deps: ProjectStatusDeps; calls: string[][]; pr
|
|
|
137
153
|
return { ok: true, value: world.ownerSetup ?? "open" };
|
|
138
154
|
},
|
|
139
155
|
isTerminal: world.isTerminal ?? true,
|
|
156
|
+
now: () => NOW,
|
|
140
157
|
},
|
|
141
158
|
};
|
|
142
159
|
}
|
|
@@ -293,7 +310,52 @@ describe("status --project: Owner setup link", () => {
|
|
|
293
310
|
const out = await text({ ...finished, setupLink: null });
|
|
294
311
|
expect(out).toContain("no link is stored");
|
|
295
312
|
expect(out).toContain("https://admin.acme.test");
|
|
296
|
-
expect(out).toContain("--project
|
|
313
|
+
expect(out).toContain("setup-link --project acme-site");
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("says until when the link works", async () => {
|
|
317
|
+
const out = await text(finished);
|
|
318
|
+
expect(out).toContain(VALID_UNTIL);
|
|
319
|
+
expect(out).toContain("setup-link --project acme-site");
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("does not show an expired link, and says what to run instead", async () => {
|
|
323
|
+
const out = await text({ ...finished, tokenExpiresAt: EXPIRED_ON });
|
|
324
|
+
expect(out).not.toContain(LINK);
|
|
325
|
+
expect(out).not.toContain("token=");
|
|
326
|
+
expect(out).toContain(`expired on ${EXPIRED_ON}`);
|
|
327
|
+
expect(out).toContain("setup-link --project acme-site");
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("does not show a link the admin has no token for (an install from before #1476)", async () => {
|
|
331
|
+
const out = await text({ ...finished, tokenExpiresAt: null });
|
|
332
|
+
expect(out).not.toContain(LINK);
|
|
333
|
+
expect(out).toContain("setup-link --project acme-site");
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("does not show a link that carries another token than the stored one", async () => {
|
|
337
|
+
const old = "https://admin.acme.test/setup?token=abc123";
|
|
338
|
+
const out = await text({ ...finished, setupLink: old });
|
|
339
|
+
expect(out).not.toContain(old);
|
|
340
|
+
expect(out).toContain("setup-link --project acme-site");
|
|
341
|
+
expect(
|
|
342
|
+
setupLinkRefusal(
|
|
343
|
+
await readProjectStatus(PROJECT, project({ ...finished, setupLink: old }).deps),
|
|
344
|
+
),
|
|
345
|
+
).toBeNull();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("an install with an Owner never reads the token, so a refused read cannot hide that", async () => {
|
|
349
|
+
const p = project({ ...finished, ownerSetup: "done" });
|
|
350
|
+
const s = await readProjectStatus(PROJECT, p.deps);
|
|
351
|
+
expect(s.owner.kind).toBe("done");
|
|
352
|
+
expect(p.calls.some((c) => c.includes(`--secret=${OWNER_BOOTSTRAP_TOKEN_SECRET}`))).toBe(false);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("an install with an Owner is done whatever became of its token", async () => {
|
|
356
|
+
const out = await text({ ...finished, tokenExpiresAt: EXPIRED_ON, ownerSetup: "done" });
|
|
357
|
+
expect(out).toContain("done.");
|
|
358
|
+
expect(out).not.toContain("expired");
|
|
297
359
|
});
|
|
298
360
|
|
|
299
361
|
it("reports a refused secret read with what the account needs", async () => {
|
|
@@ -302,6 +364,53 @@ describe("status --project: Owner setup link", () => {
|
|
|
302
364
|
});
|
|
303
365
|
});
|
|
304
366
|
|
|
367
|
+
describe("setup-link", () => {
|
|
368
|
+
const finished = {
|
|
369
|
+
objects: {
|
|
370
|
+
[`${BUCKET}/install.json`]: INSTALL,
|
|
371
|
+
[`${BUCKET}/progress.json`]: PROGRESS,
|
|
372
|
+
[`${BUCKET}/install-job.json`]: job({}),
|
|
373
|
+
},
|
|
374
|
+
setupLink: LINK,
|
|
375
|
+
tokenExpiresAt: EXPIRED_ON,
|
|
376
|
+
};
|
|
377
|
+
const refusal = async (world: World) =>
|
|
378
|
+
setupLinkRefusal(await readProjectStatus(PROJECT, project(world).deps));
|
|
379
|
+
|
|
380
|
+
it("goes on for an expired link and for no link", async () => {
|
|
381
|
+
expect(await refusal(finished)).toBeNull();
|
|
382
|
+
expect(await refusal({ ...finished, setupLink: null })).toBeNull();
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("issues nothing once an Owner exists", async () => {
|
|
386
|
+
expect(await refusal({ ...finished, ownerSetup: "done" })).toContain("no link is needed");
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it("issues nothing before the install finished", async () => {
|
|
390
|
+
const running = {
|
|
391
|
+
...finished,
|
|
392
|
+
objects: {
|
|
393
|
+
...finished.objects,
|
|
394
|
+
[`${BUCKET}/install-job.json`]: job({ state: "running", phase: "Provisioning" }),
|
|
395
|
+
},
|
|
396
|
+
execution: "running" as const,
|
|
397
|
+
};
|
|
398
|
+
expect(await refusal(running)).toContain("has not finished");
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it("does not take a refused read for a missing link", async () => {
|
|
402
|
+
expect(await refusal({ ...finished, linkDenied: "PERMISSION_DENIED: nope" })).toContain(
|
|
403
|
+
"secretmanager.versions.access",
|
|
404
|
+
);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("keeps the link off a pipe", () => {
|
|
408
|
+
const issued = { link: LINK, expiresAt: VALID_UNTIL, minted: true };
|
|
409
|
+
expect(setupLinkLines(issued, true).join("\n")).toContain(LINK);
|
|
410
|
+
expect(setupLinkLines(issued, false).join("\n")).not.toContain("token=");
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
305
414
|
describe("status --project: failed reads and this machine", () => {
|
|
306
415
|
it("a project without an install says to check the id", async () => {
|
|
307
416
|
await expect(readProjectStatus(PROJECT, project({ objects: {} }).deps)).rejects.toThrow(
|