deploy: an account with no payment card gets the card as its first step
Jonas Templestein, 2026-11-27 07:26 UTC · 247c6d7308c30a94a8320d9a9b717fbd617a3d50 · parent f356cf3
4 files changed
- M core/lib/src/cli/README.md
- 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,20 @@ 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. 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.ts
@@ -108,6 +108,13 @@ ...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, a kept sign-in + * from before it and an API token without it deploy as before, and the command then does not + * know about the card. */ +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. */ @@ -351,13 +358,16 @@ /** 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 +378,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 && before.scopes === asked.join(" ") && before.expiresAt - Date.now() > LEAST_LEFT_MS) 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 +391,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,11 +597,12 @@ /** `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. `CARD_SCOPE` is + * asked for and not needed: the file's `scopes` stay the deploy's own. */ export async function keptSignInOf( show: (page: { url: URL; code: string; minutes: number }) => void | Promise<void>, ): Promise<KeptSignIn> { - const answer = await deviceSignIn([...DEPLOY_SCOPES, "offline_access"], show); + const answer = await deviceSignIn([...DEPLOY_SCOPES, CARD_SCOPE, "offline_access"], show); const lacks = scopesLacking(DEPLOY_SCOPES, answer.scope); const refreshToken = answer.refresh_token; if (lacks.length > 0 || !refreshToken) {
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,114 @@ 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"); + + // a domain that a flag named is in the command that goes on; a new account gets the note + const named = paymentCardStep({ + account: cardAccount, + card: "none", + paid: false, + created: true, + domain: "ada-labs.com", + }); + expect(named!.resume).toBe( + `npx iterate@latest deploy --account ${cardAccount} --domain ada-labs.com`, + ); + expect(named!.words).toContain("contact your administrator"); +}); + +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.