deploy: an account with no payment card gets the card as its first step #20
merged installer-card-prompt was squashed into main as 71b85ef by jonas, 2026-11-27 09:19 UTC. jonas opened it 2026-11-27 07:26 UTC.
Card: tasks/prompt-installer-cli-users-to-add-a-cloudflare-payment-card.md (Jonas, 27 Nov 2026, Slack: "the main thing the CLI should ask the user to do then is to add a payment card"). What changes in `iterate deploy` (core/lib/src/cli): - It reads whether the account has a payment card: GET /accounts/<id>/ai-gateway/billing/credit-balance, `has_default_payment_method`. - No card and Workers Paid off: the card is the first step, before the domain and before Workers Paid. The command opens the account's billing page and waits. It takes no card and buys nothing. It prints the command that goes on: `npx iterate@latest deploy --account <id>`. - A card on file, or Workers Paid on: nothing is asked. - A card that cannot be read is "unknown", never "none": the deploy goes on as before. Credential change, for the reviewer: a new sign-in asks for one more OAuth scope, `aig.read` (read-only), beside the deploy's scopes. Nothing needs it: it is in no `lacks`, and a kept sign-in from before it still deploys (`keptAccess` checks DEPLOY_SCOPES alone, unchanged). Tested: fixtures of Cloudflare's answer (no card, a card, a refused read, the wait that ends when the card arrives); `vitest run` of deploy.test.ts and cloudflare-access.test.ts, 69 passed; `pnpm typecheck` of core/lib. Mocked: every Cloudflare answer. Not tested: a live sign-in with `aig.read`, the live read, and the billing page's address. No purchase and no card entry. Session: claude 3eb581c8-1b51-4686-8d1d-b642d0822a42 on beelink ๐ค Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_014UJeUkb5YT4HuZzZEVabHj
Conversation
Nobody has said anything yet.
Sign in to comment or to merge.
Files
5 files changed
- M core/lib/src/cli/README.md
- M core/lib/src/cli/cloudflare-access.test.ts
- M core/lib/src/cli/cloudflare-access.ts
- M core/lib/src/cli/deploy.test.ts
- M core/lib/src/cli/deploy.ts
M core/lib/src/cli/README.md
@@ -111,11 +111,21 @@ Cloudflare login. `CLOUDFLARE_API_TOKEN` replaces the sign-in. - The sign-in asks for what one deploy of the stack needs and for nothing more (never "Account API Token Provisioning"): `STACK_SCOPES` in [cloudflare-access.ts](cloudflare-access.ts) says - which resource needs each scope. + which resource needs each scope. One scope more is asked for and not needed: AI Gateway's + read (`CARD_SCOPE`), which says whether the account has a payment card. A kept sign-in + (`iterate cloudflare sign-in`) does not ask for it. 2. **The account.** One that you selected on Cloudflare's page, or a new one in your name (`--new-account`): one Cloudflare login can own several accounts (up to five extra), giving the mind a clean, separate place. An approval covers only the accounts selected on that page, so for a new account, or one you did not select, Cloudflare asks once more. + - **A payment card comes first.** When Cloudflare says that the account has no card on file + and Workers Paid is off, the command's first step is the card: it opens the account's + billing page and waits until the card is there, before the domain and before Workers Paid. + The card is typed on Cloudflare's page. The command takes no card and buys nothing at this + step. If it stopped (or nobody is at the terminal), it prints the command that goes on: + `npx iterate@latest deploy --account <id>`. An account with a card is asked nothing. When + the card cannot be read (a kept sign-in or a token without the scope), the command goes on + as before, and the step for Workers Paid says that Cloudflare's page asks for a card. 3. **The domain.** One unified list puts **Buy a brand new domain** at the top (available names made from your name with their yearly price, plus **Enter a brand new domain name and buy that** to type a domain or keywords) and **Use an existing cloudflare zone** right below it. If
M core/lib/src/cli/cloudflare-access.test.ts
@@ -3,7 +3,9 @@ import { join } from "node:path"; import { expect, test } from "vitest"; import { + CARD_SCOPE, DEPLOY_SCOPES, + keptTokenServes, describeNetworkError, dropKeptAccess, KeptSignIn, @@ -212,4 +214,18 @@ } finally { rmSync(folder, { recursive: true, force: true }); } +}); + +test("a token in the config folder from before the card's scope still serves a deploy; one that lacks a needed scope, or ends soon, does not", () => { + const at = 1_800_000_000_000; + const before = { scopes: DEPLOY_SCOPES.join(" "), expiresAt: at + 40 * 60_000 }; + // the deploy asks a new sign-in for CARD_SCOPE too, and needs it for nothing + expect(keptTokenServes(before, DEPLOY_SCOPES, at)).toBe(true); + const withCard = { ...before, scopes: [...DEPLOY_SCOPES, CARD_SCOPE].join(" ") }; + expect(keptTokenServes(withCard, DEPLOY_SCOPES, at)).toBe(true); + expect(DEPLOY_SCOPES).not.toContain(CARD_SCOPE); + + const lacking = { ...before, scopes: DEPLOY_SCOPES.filter((s) => s !== "d1.write").join(" ") }; + expect(keptTokenServes(lacking, DEPLOY_SCOPES, at)).toBe(false); + expect(keptTokenServes({ ...before, expiresAt: at + 19 * 60_000 }, DEPLOY_SCOPES, at)).toBe(false); });
M core/lib/src/cli/cloudflare-access.ts
@@ -108,6 +108,14 @@ ...STACK_SCOPES, ]; +/** A SCOPE THAT `iterate deploy` ASKS FOR AND DOES NOT NEED: AI Gateway's read, by which + * Cloudflare says whether the account has a payment card on file (`paymentCardOf` in + * deploy.ts). It is in no list of needed scopes: a sign-in that did not grant it and an API + * token without it deploy as before, and the command then does not know about the card. ONLY + * THE ONE-HOUR SIGN-IN OF A PERSON AT THE TERMINAL ASKS FOR IT: a kept sign-in + * (`keptSignInOf`) never does. */ +export const CARD_SCOPE = "aig.read"; + /** WHAT `iterate recover` ASKS FOR: the reads that find the mind (its zone with the zone's * account, the zone's routes, the boundary Worker's settings), and D1's write for the two * statements. */ @@ -348,16 +356,32 @@ throw new Error("Nobody approved the sign-in at Cloudflare in time. Run the command again."); } +/** WHETHER THE TOKEN IN THE CONFIG FOLDER SERVES A RUN that needs `scopes`: its sign-in asked for + * each of them, and it lives long enough still. What else that sign-in asked for, or did not, + * decides nothing: a scope of `alsoAsk` is for a new sign-in alone and forces none, so a token + * of a version before such a scope is used and not signed in for again. */ +export const keptTokenServes = ( + kept: { scopes: string; expiresAt: number }, + scopes: readonly string[], + now: number, +) => { + const had = new Set(kept.scopes.split(" ")); + return scopes.every((scope) => had.has(scope)) && kept.expiresAt - now > LEAST_LEFT_MS; +}; + /** A token for Cloudflare's API: the environment's, the kept sign-in in the file `keptIn`, the * kept one of a sign-in that asked for these `scopes`, or a new sign-in, for which `show` gets the * page to open and the code the person checks there. `fresh` skips the token in the config - * folder. */ + * folder. `alsoAsk`: scopes that a new sign-in asks for beside `scopes` and that nothing needs: + * one that is not granted is in no `lacks`, and a kept sign-in is not asked for them. */ export async function cloudflareAccess( scopes: readonly string[], show: (page: { url: URL; code: string; minutes: number }) => void | Promise<void>, fresh = false, keptIn?: string, + alsoAsk: readonly string[] = [], ): Promise<Access> { + const asked = [...scopes, ...alsoAsk]; const given = process.env.CLOUDFLARE_API_TOKEN?.trim(); if (keptIn) { if (given) @@ -368,10 +392,10 @@ } if (given) return { token: given, from: "environment", lacks: [] }; const before = !fresh && existsSync(KEPT) ? parsed(Kept, readFileSync(KEPT, "utf8")) : undefined; - if (before && before.scopes === scopes.join(" ") && before.expiresAt - Date.now() > LEAST_LEFT_MS) + if (before && keptTokenServes(before, scopes, Date.now())) return { token: before.token, from: "sign-in", lacks: before.lacks }; - const answer = await deviceSignIn(scopes, show); + const answer = await deviceSignIn(asked, show); // none was asked for: one that comes all the same is ended at once await revoke(answer.refresh_token); const lacks = scopesLacking(scopes, answer.scope); @@ -381,7 +405,7 @@ JSON.stringify({ token: answer.access_token, expiresAt: Date.now() + (answer.expires_in || 3600) * 1000, - scopes: scopes.join(" "), + scopes: asked.join(" "), lacks, }), { mode: 0o600 }, @@ -587,7 +611,9 @@ /** `iterate cloudflare sign-in`: a sign-in with the deploy's scopes and `offline_access`, by the * same client and page as the deploy's. A grant that lacks a scope, or comes with no refresh - * token, could not deploy with nobody there: it is revoked and nothing is kept. */ + * token, could not deploy with nobody there: it is revoked and nothing is kept. IT ASKS FOR NO + * SCOPE THAT A DEPLOY DOES NOT NEED (`CARD_SCOPE`): this grant lives until it is signed out of, + * and whoever can read its file has it for that long. */ export async function keptSignInOf( show: (page: { url: URL; code: string; minutes: number }) => void | Promise<void>, ): Promise<KeptSignIn> {
M core/lib/src/cli/deploy.test.ts
@@ -40,6 +40,8 @@ onboardingUrlOf, ownKernelRepo, parseExtraOwnerEmails, + paymentCardOf, + paymentCardStep, pickDomain, planArgsOf, setupDestinationOf, @@ -60,6 +62,7 @@ WAITING_FOR_BOUNDARY, stageNameOf, unfinishedDeployIn, + until, workersPaidPrompt, zeroTrustStep, zoneOf, @@ -881,6 +884,134 @@ const created = workersPaidPrompt(true); expect(created).toContain(existing); expect(created).toContain("not authorized, wait a minute and reload"); +}); + +// FIXTURES OF CLOUDFLARE'S ANSWER to GET /accounts/<id>/ai-gateway/billing/credit-balance, in the +// shape that iterate's browser installer saw on 10 Nov 2026. No request leaves this file. +const cardAnswer = (status: number, result: unknown, error = "") => ({ + status, + result, + total: 0, + error, +}); +const noCard = cardAnswer(200, { has_default_payment_method: false, balance: 0 }); +const aCard = cardAnswer(200, { + has_default_payment_method: true, + payment_method: { brand: "visa", last4: "4242" }, + balance: 0, +}); +const cardAccount = "0123456789abcdef0123456789abcdef"; + +test("an account with no payment card gets the card as its first step: Cloudflare's billing page, no card typed here, no purchase, and the command that goes on", () => { + expect(paymentCardOf(noCard)).toBe("none"); + const step = paymentCardStep({ account: cardAccount, card: "none", paid: false, created: false }); + expect(step).toMatchObject({ + page: `https://dash.cloudflare.com/${cardAccount}/billing/payment-info`, + resume: `npx iterate@latest deploy --account ${cardAccount}`, + }); + expect(step!.words).toContain("Add a payment card to your Cloudflare account"); + expect(step!.words).toContain("Type the card on Cloudflare's page, never here"); + expect(step!.words).toContain("this step buys nothing"); + expect(step!.words).toContain(`If it has stopped, run ${step!.resume}`); + expect(step!.words).not.toContain("contact your administrator"); + + // the command that goes on is the person's own, every flag kept (the owner above all), with the + // account by its id in place of the flags that chose one; a new account gets the note + const named = paymentCardStep({ + account: cardAccount, + card: "none", + paid: false, + created: true, + args: [ + "deploy", + "--new-account", + "--domain", + "ada-labs.com", + "--owner", + "ada@example.org", + "--owner-email-patterns", + "*@ada's.example", + ], + }); + expect(named!.resume).toBe( + `npx iterate@latest deploy --domain ada-labs.com --owner ada@example.org --owner-email-patterns '*@ada'\\''s.example' --account ${cardAccount}`, + ); + expect(named!.words).toContain("contact your administrator"); + for (const chosen of [["--account", "Ada Labs"], ["--account=other"]]) + expect( + paymentCardStep({ + account: cardAccount, + card: "none", + paid: false, + created: false, + args: ["deploy", ...chosen, "--buy"], + })!.resume, + ).toBe(`npx iterate@latest deploy --buy --account ${cardAccount}`); +}); + +test("an account with a card is asked nothing, and a card that nobody could read is never read as no card", () => { + expect(paymentCardOf(aCard)).toBe("there"); + expect( + paymentCardStep({ account: cardAccount, card: "there", paid: false, created: false }), + ).toBeUndefined(); + // Workers Paid is on: Cloudflare sold it, so the account is asked for no card + expect( + paymentCardStep({ account: cardAccount, card: "none", paid: true, created: false }), + ).toBeUndefined(); + + // a sign-in without the scope, an outage, and an answer in another shape + for (const unread of [ + cardAnswer(403, null, "Authentication error (code 10000)"), + cardAnswer(429, null, "busy"), + cardAnswer(503, null, "unavailable"), + cardAnswer(200, {}), + cardAnswer(200, { has_default_payment_method: "yes" }), + cardAnswer(200, null), + ]) { + expect(paymentCardOf(unread)).toBe("unknown"); + } + expect( + paymentCardStep({ account: cardAccount, card: "unknown", paid: false, created: false }), + ).toBeUndefined(); + // only then do the words for Workers Paid say that Cloudflare's page asks for a card + expect(workersPaidPrompt(false, "unknown")).toContain("Cloudflare's page asks for one first"); + expect(workersPaidPrompt(false, "there")).toBe(workersPaidPrompt(false)); + expect(workersPaidPrompt(false, "none")).toBe(workersPaidPrompt(false)); +}); + +test("the wait for the card ends when Cloudflare says the card is there, and a second run of the command asks for no card", async () => { + // the person adds the card while the command waits: none, an outage, none, then the card + const answers = [noCard, cardAnswer(503, null, "unavailable"), noCard, aCard]; + const read: string[] = []; + const arrived = await until( + "Waiting for the payment card", + async () => { + const card = paymentCardOf(answers[Math.min(read.length, answers.length - 1)]!); + read.push(card); + return card === "there"; + }, + 1, + async () => undefined, + ); + expect(arrived).toBe(true); + expect(read).toEqual(["none", "unknown", "none", "there"]); + + // the command stopped, the person added the card, and runs the command that the step printed + const before = paymentCardStep({ + account: cardAccount, + card: paymentCardOf(noCard), + paid: false, + created: false, + }); + expect(before!.resume).toContain(`--account ${cardAccount}`); + expect( + paymentCardStep({ + account: cardAccount, + card: paymentCardOf(aCard), + paid: false, + created: false, + }), + ).toBeUndefined(); }); test("redactMindConfig and formatMindConfigForStdout print full MindConfig while redacting any sensitive keys", () => {
M core/lib/src/cli/deploy.ts
Not shown: the change is larger than this page shows. A clone has it.