plans 6c: Astra round 2 fixes (no tracing, own secret file, Connect)
Stopping rule 2 triggered for secret handling and launch coverage, so both get structural fixes rather than another patch. Tracing (P1): the script refuses to run before touching any key if xtrace is on ($- has x, SHELLOPTS has xtrace), BASH_XTRACEFD is set, or BASH_ENV is set. Tests: bash -x, inherited SHELLOPTS, BASH_XTRACEFD, BASH_ENV. Secret file (P1): --secret-out is gone. Before any write, --apply makes its own 0700 directory with mktemp -d and creates the secret file in it under umask 077 with noclobber, held open on fd 9; the secret is written through that descriptor and only the path is printed. Without a new endpoint both are removed on exit, by exact path. Launch window (P1): Paul ruled 2026-11-01 that "existing" means created before the cutoff; an account created during switch-on is a new account on the normal plan. The by-hand gap sponsoring and its console.db query are removed. sponsored-unlimited.json stays for one-off sponsoring. Connect endpoints (P1): discovery and read-back require application == null; a Connect endpoint at the same URL is ignored and the account's endpoint is made. P2: the house command names --console https://console.carloku.com.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+151
−92
cmd/console/stripesetup_script_test.go+71 −33docs/superpowers/plans/2026-10-27-plans-6c-stripe-setup.md+14 −29internal/console/plansponsor_file_test.go+3 −3scripts/stripe-setup.sh+63 −27
CI cancelled — superseded by a newer push — run details
diff --git a/cmd/console/stripesetup_script_test.go b/cmd/console/stripesetup_script_test.go| index 0b017f177..06fe505e2 100644 |
| --- a/cmd/console/stripesetup_script_test.go |
| +++ b/cmd/console/stripesetup_script_test.go |
| @@ -154,6 +154,7 @@ func fakeStripe(args []string) int { |
| case "webhook_endpoints": |
| o["status"] = "enabled" |
| o["secret"] = "whsec_FakeSecret" |
| + o["application"] = nil // an account endpoint, not a Connect one |
| } |
| stored := map[string]any{} |
| for k, v := range o { |
| @@ -298,15 +299,24 @@ func posts(calls [][]string) int { |
| // from the caller's environment. |
| func (w *stripeWorld) run(env []string, args ...string) (string, string, int) { |
| w.t.Helper() |
| - cmd := exec.Command(stripeSetupScript, args...) |
| + return w.runWith(exec.Command(stripeSetupScript, args...), env) |
| +} |
| + |
| +// runWith runs cmd, the script under some interpreter, as run does. |
| +func (w *stripeWorld) runWith(cmd *exec.Cmd, env []string) (string, string, int) { |
| + w.t.Helper() |
| for _, e := range os.Environ() { |
| + if strings.HasPrefix(e, "BASH_ENV=") || strings.HasPrefix(e, "SHELLOPTS=") || strings.HasPrefix(e, "BASH_XTRACEFD=") || |
| + strings.HasPrefix(e, "TMPDIR=") { |
| + continue |
| + } |
| if !strings.HasPrefix(e, "STRIPE_") && !strings.HasPrefix(e, "PORTAL_") && !strings.HasPrefix(e, "PRICE_BOOK") && |
| !strings.HasPrefix(e, "HOUSE_COUPON_NAME=") && !strings.HasPrefix(e, "PRODUCT_NAME_") && !strings.HasPrefix(e, "PATH=") { |
| cmd.Env = append(cmd.Env, e) |
| } |
| } |
| cmd.Env = append(cmd.Env, "PATH="+w.bin+string(os.PathListSeparator)+os.Getenv("PATH"), |
| - "FAKE_STRIPE_DIR="+w.dir, "CONSOLE_URL=https://console.example.com") |
| + "FAKE_STRIPE_DIR="+w.dir, "CONSOLE_URL=https://console.example.com", "TMPDIR="+w.dir) |
| cmd.Env = append(cmd.Env, env...) |
| var stdout, stderr strings.Builder |
| cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| @@ -326,8 +336,7 @@ func (w *stripeWorld) run(env []string, args ...string) (string, string, int) { |
| func TestStripeSetupLiveApply(t *testing.T) { |
| w := newStripeWorld(t, liveAccount, true) |
| env := []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount} |
| - secretFile := filepath.Join(w.dir, "webhook-secret") |
| - stdout, stderr, code := w.run(env, "--live", "--apply", "--secret-out", secretFile) |
| + stdout, stderr, code := w.run(env, "--live", "--apply") |
| if code != 0 { |
| t.Fatalf("live --apply failed (%d):\n%s\n%s", code, stdout, stderr) |
| } |
| @@ -369,15 +378,22 @@ func TestStripeSetupLiveApply(t *testing.T) { |
| if strings.Contains(stdout+stderr, "FakeSecret") { |
| t.Errorf("the signing secret reached the output") |
| } |
| - if !strings.Contains(stdout, secretFile) { |
| - t.Errorf("stdout does not name the secret file") |
| + m := regexp.MustCompile(`signing secret is in (\S+) `).FindStringSubmatch(stdout) |
| + if m == nil { |
| + t.Fatalf("stdout does not name the secret file:\n%s", stdout) |
| } |
| - fi, err := os.Stat(secretFile) |
| - if err != nil { |
| - t.Fatal(err) |
| + secretFile := m[1] |
| + if filepath.Dir(filepath.Dir(secretFile)) != w.dir { |
| + t.Errorf("secret file %s is not in a new directory under TMPDIR", secretFile) |
| } |
| - if fi.Mode().Perm() != 0o600 { |
| - t.Errorf("secret file mode %v, want 0600", fi.Mode().Perm()) |
| + for path, want := range map[string]os.FileMode{secretFile: 0o600, filepath.Dir(secretFile): 0o700} { |
| + fi, err := os.Lstat(path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if fi.Mode().Perm() != want { |
| + t.Errorf("%s mode %v, want %v", path, fi.Mode().Perm(), want) |
| + } |
| } |
| if b, _ := os.ReadFile(secretFile); string(b) != "CARLOS_STRIPE_WEBHOOK_SECRET=whsec_FakeSecret\n" { |
| t.Errorf("secret file holds %q", b) |
| @@ -395,7 +411,7 @@ func TestStripeSetupLiveApply(t *testing.T) { |
| // A sandbox --apply still never passes --live and makes test-mode objects. |
| func TestStripeSetupSandboxApply(t *testing.T) { |
| w := newStripeWorld(t, sandboxAccount, false) |
| - if stdout, stderr, code := w.run(nil, "--apply", "--secret-out", filepath.Join(w.dir, "secret")); code != 0 { |
| + if stdout, stderr, code := w.run(nil, "--apply"); code != 0 { |
| t.Fatalf("sandbox --apply failed (%d):\n%s\n%s", code, stdout, stderr) |
| } |
| for _, c := range w.calls() { |
| @@ -474,10 +490,12 @@ func TestStripeSetupLiveRefusals(t *testing.T) { |
| args: []string{"--live", "--apply"}, env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount}, want: "not in live mode"}, |
| {name: "a sandbox run against live", account: sandboxAccount, live: true, args: []string{"--apply"}, |
| want: "balance is not in test mode"}, |
| - {name: "apply without --secret-out", account: liveAccount, live: true, args: []string{"--live", "--apply", "--no-secret-out"}, |
| - env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount}, want: "needs --secret-out", noCallsAt: true}, |
| - {name: "secret file already there", account: liveAccount, live: true, args: []string{"--live", "--apply", "--secret-exists"}, |
| - env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount}, want: "already exists", noCallsAt: true}, |
| + {name: "inherited SHELLOPTS xtrace", account: liveAccount, live: true, args: []string{"--live", "--apply"}, |
| + env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount, "SHELLOPTS=xtrace"}, want: "refusing to run traced", noCallsAt: true}, |
| + {name: "BASH_XTRACEFD set", account: liveAccount, live: true, args: []string{"--live", "--apply"}, |
| + env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount, "BASH_XTRACEFD=2"}, want: "refusing to run traced", noCallsAt: true}, |
| + {name: "BASH_ENV set", account: liveAccount, live: true, args: []string{"--live", "--apply"}, |
| + env: []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount, "BASH_ENV=/dev/null"}, want: "BASH_ENV", noCallsAt: true}, |
| } |
| for _, tc := range cases { |
| t.Run(tc.name, func(t *testing.T) { |
| @@ -486,21 +504,7 @@ func TestStripeSetupLiveRefusals(t *testing.T) { |
| w.save(fakeStripeWorld{Account: tc.account, Live: tc.live, |
| Objects: map[string][]map[string]any{"products": tc.seed}}) |
| } |
| - // An apply names a new secret file unless the case is about it. |
| - args := slices.Clone(tc.args) |
| - switch { |
| - case slices.Contains(args, "--no-secret-out"): |
| - args = slices.DeleteFunc(args, func(a string) bool { return a == "--no-secret-out" }) |
| - case slices.Contains(args, "--secret-exists"): |
| - f := filepath.Join(w.dir, "secret") |
| - if err := os.WriteFile(f, nil, 0o600); err != nil { |
| - t.Fatal(err) |
| - } |
| - args = append(slices.DeleteFunc(args, func(a string) bool { return a == "--secret-exists" }), "--secret-out", f) |
| - case slices.Contains(args, "--apply"): |
| - args = append(args, "--secret-out", filepath.Join(w.dir, "secret")) |
| - } |
| - stdout, stderr, code := w.run(tc.env, args...) |
| + stdout, stderr, code := w.run(tc.env, tc.args...) |
| if code == 0 { |
| t.Fatalf("not refused:\n%s", stdout) |
| } |
| @@ -514,17 +518,51 @@ func TestStripeSetupLiveRefusals(t *testing.T) { |
| if n := posts(calls); n != 0 { |
| t.Fatalf("refused after %d writes", n) |
| } |
| + if left, _ := filepath.Glob(filepath.Join(w.dir, "tmp.*")); len(left) != 0 { |
| + t.Fatalf("a refused run left %v behind", left) |
| + } |
| }) |
| } |
| } |
| +// bash -x, the explicit form of tracing, is refused before any call. |
| +func TestStripeSetupRefusesBashX(t *testing.T) { |
| + w := newStripeWorld(t, liveAccount, true) |
| + stdout, stderr, code := w.runWith(exec.Command("bash", "-x", stripeSetupScript, "--live", "--apply"), |
| + []string{"STRIPE_LIVE_ACCOUNT=" + liveAccount}) |
| + if code == 0 || !strings.Contains(stderr, "refusing to run traced") { |
| + t.Fatalf("bash -x was not refused (%d):\n%s\n%s", code, stdout, stderr) |
| + } |
| + if n := len(w.calls()); n != 0 { |
| + t.Fatalf("refused after %d calls to Stripe", n) |
| + } |
| +} |
| + |
| +// A Connect application's endpoint at the console's URL is not the |
| +// account's endpoint: it is ignored, and the account's own is made. |
| +func TestStripeSetupIgnoresConnectEndpoint(t *testing.T) { |
| + w := newStripeWorld(t, sandboxAccount, false) |
| + connect := map[string]any{"id": "we_Connect", "object": "webhook_endpoint", "livemode": false, "status": "enabled", |
| + "application": "ca_Other", "url": "https://console.example.com/billing/webhook", "api_version": "2025-02-24.acacia", |
| + "enabled_events": []any{"checkout.session.completed", "customer.subscription.created", "customer.subscription.updated", |
| + "customer.subscription.deleted", "invoice.paid", "invoice.payment_failed"}} |
| + w.save(fakeStripeWorld{Account: sandboxAccount, Objects: map[string][]map[string]any{"webhook_endpoints": {connect}}}) |
| + if stdout, stderr, code := w.run(nil, "--apply"); code != 0 { |
| + t.Fatalf("apply failed (%d):\n%s\n%s", code, stdout, stderr) |
| + } |
| + eps := w.load().Objects["webhook_endpoints"] |
| + if len(eps) != 2 || eps[0]["id"] != "we_Connect" || eps[1]["application"] != nil { |
| + t.Fatalf("endpoints after apply: %v", eps) |
| + } |
| +} |
| + |
| // A price archived (lookup key cleared) after it was made is not made again |
| // under the same book version: Stripe answers the create by replaying the |
| // first response, which still says active, so the script reads the price |
| // back and stops, naming the way out (a new book version). |
| func TestStripeSetupReplayedArchivedPriceStops(t *testing.T) { |
| w := newStripeWorld(t, sandboxAccount, false) |
| - if stdout, stderr, code := w.run(nil, "--apply", "--secret-out", filepath.Join(w.dir, "secret-1")); code != 0 { |
| + if stdout, stderr, code := w.run(nil, "--apply"); code != 0 { |
| t.Fatalf("first apply failed (%d):\n%s\n%s", code, stdout, stderr) |
| } |
| fw := w.load() |
| @@ -539,7 +577,7 @@ func TestStripeSetupReplayedArchivedPriceStops(t *testing.T) { |
| t.Fatal("no personal/usd price was made") |
| } |
| w.save(fw) |
| - stdout, stderr, code := w.run(nil, "--apply", "--secret-out", filepath.Join(w.dir, "secret-2")) |
| + stdout, stderr, code := w.run(nil, "--apply") |
| if code == 0 { |
| t.Fatalf("a replayed archived price was accepted:\n%s", stdout) |
| } |
diff --git a/docs/superpowers/plans/2026-10-27-plans-6c-stripe-setup.md b/docs/superpowers/plans/2026-10-27-plans-6c-stripe-setup.md| index 263b96190..9881fc978 100644 |
| --- a/docs/superpowers/plans/2026-10-27-plans-6c-stripe-setup.md |
| +++ b/docs/superpowers/plans/2026-10-27-plans-6c-stripe-setup.md |
| @@ -183,8 +183,7 @@ export CONSOLE_URL=https://platform.dev.carlosframework.com |
| export STRIPE_TAX_CODE=txcd_10103000 # the default; see 6.2 |
| scripts/stripe-setup.sh # dry run: sends nothing, prints every command |
| scripts/stripe-setup.sh --check # reads only: what exists, what is wrong, what is missing |
| -secrets="$(mktemp -d)" # a new 0700 directory for the signing secret |
| -scripts/stripe-setup.sh --apply --secret-out "$secrets/webhook-secret" # makes what is missing |
| +scripts/stripe-setup.sh --apply # makes what is missing |
| scripts/stripe-setup.sh --check # again: everything exists and nothing is wrong |
| ``` |
| @@ -197,7 +196,9 @@ What the script does: |
| - It never deletes, archives or edits anything. If something exists but breaks a rule in section 2, it stops, names the object and the rule, and leaves the fix to you in the Dashboard. |
| - Each create carries an idempotency key naming the book version (`stripe-setup:<book>:<kind>:<name>`). If a response gets lost, running again within 24 hours returns the same object instead of a second one. |
| - After every create it reads the object back by id. It stops unless the object is active, in the run's mode, and the one it asked for (key, amount, metadata). A create Stripe answers by replaying an earlier response, for an object archived since, therefore stops the run (section 7). |
| -- `--apply` needs `--secret-out <file>`, a file that does not exist yet. If it makes the webhook endpoint, it writes `CARLOS_STRIPE_WEBHOOK_SECRET=whsec_...` there, mode 0600, and prints only the path. The secret never goes to stdout or stderr, so it stays out of logs and transcripts. Move it into 1Password and the console's EnvironmentFile (4.4), then delete the file and its directory. If you lose it, roll the secret in the Dashboard. |
| +- If it makes the webhook endpoint, it writes `CARLOS_STRIPE_WEBHOOK_SECRET=whsec_...` to a file of its own and prints only the file's path. Before any write, `--apply` makes a new 0700 directory with `mktemp -d` (under `$TMPDIR`) and creates the file in it, 0600, holding it open; the secret goes through that descriptor. If no endpoint is made, the file and directory are removed. The secret never goes to stdout or stderr, so it stays out of logs and transcripts. Move it into 1Password and the console's EnvironmentFile (4.4), then delete the file and its directory. If you lose it, roll the secret in the Dashboard. |
| +- It refuses to run traced (`bash -x`, `xtrace` in an inherited `SHELLOPTS`, or `BASH_XTRACEFD` set) or with `BASH_ENV` set, before it touches a key: a trace would print the secret. |
| +- It looks only at the account's own webhook endpoints. An endpoint of a Connect application (`application` set) at the same URL gets connected accounts' events, so it is ignored and the account's endpoint is made beside it. |
| - At the end it prints the console's non-secret variables, ready to paste. |
| Two questions only the sandbox can answer: |
| @@ -249,7 +250,7 @@ The dev console's systemd unit, `/etc/systemd/system/console.service`, exists on |
| 3. Write `/etc/carlos/billing.env`, root:root, mode 0600, holding: |
| - the block the script printed (`CARLOS_BILLING=test`, `CARLOS_PLAN_CATALOG`, `CARLOS_PRICE_BOOK`, `CARLOS_BILLING_PRICES` in single quotes, `CARLOS_STRIPE_ACCOUNT`, `CARLOS_STRIPE_PORTAL_CONFIGURATION`, `CARLOS_STRIPE_HOUSE_COUPON`); |
| - `CARLOS_STRIPE_SECRET_KEY=rk_test_...`, the key from 4.3; |
| - - `CARLOS_STRIPE_WEBHOOK_SECRET=whsec_...`, the line in the script's `--secret-out` file. |
| + - `CARLOS_STRIPE_WEBHOOK_SECRET=whsec_...`, the line in the file whose path the script printed. |
| Keep the secrets off any command line, and out of SSM send-command output. Either put them in SSM Parameter Store as SecureStrings and have the box read them (the pattern in `2026-10-05-scanner-host-rollout.md`, Task 9, steps 3 and 5), or type them in an interactive session on the box. |
| 4. Add `/etc/systemd/system/console.service.d/billing.conf`: |
| @@ -309,6 +310,8 @@ Also record the answers to the questions the code leaves open: |
| - **Price book (2026-11-01, "Go ahead with live price book"):** `deploy/billing/pricebook-2026-11-01-v1.json`, status `approved`, with the draft's amounts, currencies and country map. Its price keys are `2026-11-01-v1/<plan>/<currency>` and `2026-11-01-v1/pack/<pack>/<currency>`. |
| - **Tax (2026-10-31):** as Tito Go does it. Irish customers always pay VAT. An EU business with a VAT number gets the reverse charge, and other EU customers pay VAT through OSS. Everyone else pays no tax. Prices are exclusive: tax is added on top. |
| - **Existing accounts (2026-10-31):** every account that exists at switch-on is sponsored with no limits, through the catalog's `sponsor_accounts_before` (6f1f6ca, in v0.33.1 and later; `docs/site/plans.md`, "Sponsored accounts"). This replaces per-account `carlos accounts plan --sponsored` and the measurement tool, which is not needed for launch. It meets the switch-on condition (2026-10-27, #439): existing users do not start paying. |
| +- **What "existing" means (2026-11-01):** an account created before the cutoff; one created at or after it, during the switch-on window included, is a new account on the normal plan, like any later signup. |
| +- **One-off sponsoring:** `deploy/billing/sponsored-unlimited.json` holds exactly the limits the console gives at launch, for an operator who later sponsors one account by hand: `carlos accounts plan --console https://console.carloku.com --account <sqid> --set free --sponsored deploy/billing/sponsored-unlimited.json --reason "..."`. `TestSponsoredUnlimitedFileIsTheLaunchSponsorship` keeps it equal to the console's own and checks the plan door accepts it. |
| - **The ops account** (`bab`) is exempt from plans and billing (a2e23ba, in v0.33.0 and later). Nothing to do for it. |
| ### 6.2 Tax in the code |
| @@ -326,7 +329,7 @@ The product tax code is `txcd_10103000`, "Software as a service (SaaS), personal |
| - Every call carries `--live`. `GET /v1/account` must report `STRIPE_LIVE_ACCOUNT`, `GET /v1/balance` must say `livemode: true`, and every object it lists or makes must say `livemode: true`. A test object in a live list stops the run. |
| - `STRIPE_API_KEY`, if set, overrides the CLI's login. With `--live` it must be an `sk_live_` or `rk_live_` key. Without `--live` a live key is refused. |
| - The dry run sends nothing, and `--check` only reads, exactly as in the sandbox. |
| -- As in the sandbox, every create is read back by id before it is used, and the webhook signing secret goes only to the `--secret-out` file (4.2). |
| +- As in the sandbox, every create is read back by id before it is used, the webhook signing secret goes only to the script's own file, and a traced run is refused (4.2). |
| - The printed block says `CARLOS_BILLING=live`. |
| `cmd/console/stripesetup_script_test.go` runs every one of these against a fake stripe CLI. |
| @@ -357,13 +360,11 @@ Then us: |
| export STRIPE_LIVE_ACCOUNT=acct_... # from step 3 |
| scripts/stripe-setup.sh --live # dry run: sends nothing |
| scripts/stripe-setup.sh --live --check # reads only |
| - secrets="$(mktemp -d)" # a new 0700 directory |
| - scripts/stripe-setup.sh --live --apply --secret-out "$secrets/webhook-secret" |
| - # makes the 8 products, 24 prices, portal, coupon, endpoint |
| + scripts/stripe-setup.sh --live --apply # makes the 8 products, 24 prices, portal, coupon, endpoint |
| scripts/stripe-setup.sh --live --check # everything exists, nothing wrong |
| ``` |
| - The webhook signing secret is only in `$secrets/webhook-secret`. Move it into 1Password, and from there into the flagship's `billing.env` (step 6). Then delete the file and the directory: `rm "$secrets/webhook-secret" && rmdir "$secrets"`. Whatever the sandbox run learnt about the portal (4.2) applies here too. |
| + The webhook signing secret is only in the file whose path the `--apply` run printed. Move it into 1Password, and from there into the flagship's `billing.env` (step 6). Then delete the file and its directory, by the printed path. Whatever the sandbox run learnt about the portal (4.2) applies here too. |
| 6. **The flagship console's files,** shaped as in 4.4, on a console running v0.33.1 or later: |
| - `/etc/carlos/plans.json`: the example catalog, version `2026-10-20-v1`, plus `"sponsor_accounts_before"`, written last (step 7). Keep the version: the book prices `2026-10-20-v1`, and a new catalog version would need a new book. |
| - `/etc/carlos/pricebook-2026-11-01-v1.json`, root:root 0644, never edited afterwards. |
| @@ -371,38 +372,22 @@ Then us: |
| - `console.service.d/billing.conf` with `EnvironmentFile=/etc/carlos/billing.env`, no leading `-`. |
| With the catalog in `billing.env`, plans and billing go on together. |
| -7. **The cutoff, then one restart.** The console refuses a cutoff later than its start, so the cutoff comes before the restart. Accounts created between the two are not sponsored by the console, and are sponsored by hand in step 8. |
| +7. **The cutoff, then one restart.** The console refuses a cutoff later than its start, so the cutoff comes before the restart. An account created between the two is a new account (6.1), not sponsored. |
| 1. With every other file in place, set `sponsor_accounts_before` in `/etc/carlos/plans.json` to the next whole second, for example the output of `date -u -d '+1 second' +%Y-%m-%dT%H:%M:%SZ`. |
| 2. Once that second has passed, `systemctl daemon-reload && systemctl restart console.service`. |
| 3. In the log: |
| - `console: sponsored N existing accounts with no limits (created before ...)`. N is every account created before the cutoff but the ops one. `PLANS ARE OFF` instead means sponsoring failed: plans and billing both stay off, and the log says why. |
| - `console: billing is ready`. `billing not ready:` names the object and the rule. `scripts/stripe-setup.sh --live --check` shows the same from Stripe's side. |
| - `https://console.carloku.com/billing/webhook` no longer answers 503. |
| -8. **Accounts created between the cutoff and the start,** straight after the start. No `carlos` command lists accounts by creation time, so read the console's database, read-only, on the console box (`CONSOLE_DB`, `/data/console/console.db` by default): |
| - |
| - ```sh |
| - cutoff=$(date -u -d '<the cutoff from step 7>' +%s) |
| - start=$(date -d "$(systemctl show console.service -p ExecMainStartTimestamp --value)" +%s) |
| - python3 -c 'import sqlite3, sys; db = sqlite3.connect("file:/data/console/console.db?mode=ro", uri=True); [print(*r) for r in db.execute("SELECT sqid, name, created_at FROM accounts WHERE created_at >= ? AND created_at <= ? ORDER BY created_at", (int(sys.argv[1]), int(sys.argv[2])))]' "$cutoff" "$start" |
| - ``` |
| - |
| - The window includes the start's own second, because the old console may have created an account in it. The cost is that an account made in the new console's first second is sponsored too. Sponsor each account listed, with the launch sponsorship's own limits: |
| - |
| - ```sh |
| - carlos accounts plan --console https://console.carloku.com --account <sqid> --set free \ |
| - --sponsored deploy/billing/sponsored-unlimited.json --reason "existing account at plan launch" |
| - ``` |
| - |
| - `free` is the catalog's `default_plan`. `deploy/billing/sponsored-unlimited.json` is exactly what the console gives an account it sponsors at start. `TestSponsoredUnlimitedFileIsTheLaunchSponsorship` keeps it so, and checks that the plan door accepts it. An empty list means nothing to do. |
| -9. **A first house-account Checkout at zero,** on a new account. Billing refuses Checkout for a sponsored account, and every account from before the cutoff is sponsored now, so make a fresh one. Never use `bab`, which has no billing: |
| +8. **A first house-account Checkout at zero,** on a new account. Billing refuses Checkout for a sponsored account, and every account from before the cutoff is sponsored now, so make a fresh one. Never use `bab`, which has no billing: |
| ```sh |
| carlos accounts create --console https://console.carloku.com --name house-checkout |
| - carlos billing house -account <its sqid> -on -note "first live checkout" |
| + carlos billing house --console https://console.carloku.com -account <its sqid> -on -note "first live checkout" |
| ``` |
| Buy Personal from its billing page. Checkout shows a total of 0, the webhook delivery succeeds in the Dashboard, and the account's billing page shows Personal. |
| -10. Then publish `https://console.carloku.com/upgrade?plan=<plan>¤cy=<cur>` and the pricing page (Plan 7). |
| +9. Then publish `https://console.carloku.com/upgrade?plan=<plan>¤cy=<cur>` and the pricing page (Plan 7). |
| To turn billing off again, set `CARLOS_BILLING=off` and restart. Nothing at Stripe changes. Plans stay on, and the existing accounts stay sponsored. |
diff --git a/internal/console/plansponsor_file_test.go b/internal/console/plansponsor_file_test.go| index f6d16d83c..cd042d233 100644 |
| --- a/internal/console/plansponsor_file_test.go |
| +++ b/internal/console/plansponsor_file_test.go |
| @@ -11,9 +11,9 @@ import ( |
| ) |
| // deploy/billing/sponsored-unlimited.json is the limits file the plans 6c |
| -// runbook hands `carlos accounts plan --sponsored` for an account created |
| -// between the launch cutoff and the console's start. It must be exactly the |
| -// launch sponsorship's own limits, and the plan door must accept it. |
| +// runbook gives an operator for one-off sponsoring with `carlos accounts |
| +// plan --sponsored`. It must be exactly the launch sponsorship's own |
| +// limits, and the plan door must accept it. |
| func TestSponsoredUnlimitedFileIsTheLaunchSponsorship(t *testing.T) { |
| b, err := os.ReadFile("../../deploy/billing/sponsored-unlimited.json") |
| if err != nil { |
diff --git a/scripts/stripe-setup.sh b/scripts/stripe-setup.sh| index cd8fa1326..5fc236c7d 100755 |
| --- a/scripts/stripe-setup.sh |
| +++ b/scripts/stripe-setup.sh |
| @@ -21,10 +21,11 @@ |
| # Read only. Checks the account and its mode, finds what exists, |
| # checks it the way the console's readiness check does, and prints |
| # what --apply would create. |
| -# CONSOLE_URL=... scripts/stripe-setup.sh --apply --secret-out <file> |
| +# CONSOLE_URL=... scripts/stripe-setup.sh --apply |
| # As --check, then creates whatever is missing. If it makes the |
| -# webhook endpoint, it writes the endpoint's signing secret to <file>, |
| -# mode 0600, and prints only the path. <file> must not exist yet. |
| +# webhook endpoint, it writes the endpoint's signing secret to a file |
| +# of its own, mode 0600 in a new 0700 directory, and prints only the |
| +# file's path. |
| # Add --live to any of the three for the live account. Live needs |
| # STRIPE_LIVE_ACCOUNT=acct_... naming the live account, and an approved |
| # price book. Start with a live dry run, then a live --check. |
| @@ -73,8 +74,15 @@ |
| # Lookups see only live objects: active products, prices and portal |
| # configurations, valid coupons, enabled endpoints. So once a wrong |
| # object is archived in the Dashboard, the next run makes a new one. |
| -# - The webhook signing secret never goes to stdout or stderr. It goes |
| -# only to the --secret-out file, which --apply requires up front. |
| +# - It refuses to run traced (bash -x, an inherited SHELLOPTS xtrace, |
| +# BASH_XTRACEFD) or with BASH_ENV set: a trace would print the keys and |
| +# the webhook response it handles. |
| +# - The webhook signing secret never goes to stdout or stderr. Before |
| +# any write, --apply makes a 0700 directory with mktemp -d and creates |
| +# the secret file in it, 0600, holding it open; the secret is written |
| +# through that descriptor. If no endpoint is made, both are removed. |
| +# - Webhook endpoints of a Connect application (application set) are |
| +# not this account's endpoint: they are ignored, never reused. |
| # - After every create it reads the object back by id, and stops unless |
| # it is active, in the run's mode and the object it asked for. Each |
| # create's idempotency key names the book version, so a create Stripe |
| @@ -85,6 +93,15 @@ |
| # Needs bash, jq and the stripe CLI. |
| set -euo pipefail |
| +# No tracing, before anything touches a key or a Stripe response: a trace |
| +# prints every assignment, the webhook endpoint's signing secret included. |
| +# BASH_ENV runs arbitrary code (a DEBUG trap, say) before this line. |
| +if [[ "$-" == *x* || ":${SHELLOPTS:-}:" == *:xtrace:* || -n "${BASH_XTRACEFD+set}" || -n "${BASH_ENV+set}" ]]; then |
| + set +x |
| + printf 'stripe-setup.sh: refusing to run traced (bash -x, SHELLOPTS xtrace or BASH_XTRACEFD) or with BASH_ENV set: a trace would print secrets\n' >&2 |
| + exit 1 |
| +fi |
| + |
| readonly API_VERSION="2025-02-24.acacia" |
| readonly WEBHOOK_EVENTS=( |
| checkout.session.completed |
| @@ -104,29 +121,16 @@ usage() { sed -n '4,/^set -euo/p' "${BASH_SOURCE[0]}" | sed -e '/^set -euo/d' -e |
| mode="dry-run" |
| live=false |
| -secret_out="" |
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| -h | --help) usage; exit 0 ;; |
| --apply) mode="apply"; shift ;; |
| --check) mode="check"; shift ;; |
| --live) live=true; shift ;; |
| - --secret-out) |
| - [[ $# -ge 2 && -n "$2" ]] || die "--secret-out needs a file path" |
| - secret_out="$2"; shift 2 ;; |
| *) die "unknown argument: $1 (try --help)" ;; |
| esac |
| done |
| if [[ "$live" == true ]]; then mode_word="live"; else mode_word="test"; fi |
| -# The webhook signing secret goes to a file and never to a terminal or a log, |
| -# so an apply, which may make the endpoint, needs that file named up front. |
| -if [[ "$mode" == apply ]]; then |
| - [[ -n "$secret_out" ]] || die "--apply needs --secret-out <file>: where the new webhook endpoint's signing secret goes (mode 0600)" |
| -fi |
| -if [[ -n "$secret_out" ]]; then |
| - [[ ! -e "$secret_out" && ! -L "$secret_out" ]] || die "--secret-out $secret_out already exists. Name a new file" |
| - [[ -d "$(dirname "$secret_out")" ]] || die "--secret-out $secret_out: its directory does not exist" |
| -fi |
| command -v jq >/dev/null || die "jq is needed" |
| if [[ "$mode" != "dry-run" ]]; then |
| @@ -400,7 +404,8 @@ readonly COUPON_PROBLEM=' |
| else "" end' |
| readonly WEBHOOK_PROBLEM=' |
| - if .livemode != '"$live"' then "not in '"$mode_word"' mode" |
| + if .application != null then "the endpoint of a Connect application" |
| + elif .livemode != '"$live"' then "not in '"$mode_word"' mode" |
| elif .status != "enabled" then "not enabled" |
| elif .api_version != $version then "its API version is not \($version)" |
| elif (.enabled_events | sort) != ($events | sort) then "its events are not the six the console reads" |
| @@ -409,6 +414,34 @@ readonly WEBHOOK_PROBLEM=' |
| # --------------------------------------------------------------------------- |
| # 0. Preflight: the expected account, in the run's mode, nothing else. |
| +# The webhook signing secret's file, made before any write: a new 0700 |
| +# directory from mktemp -d, so nobody else can place anything in it, and in |
| +# it a new file, 0600, created exclusively and held open on descriptor 9. |
| +# The secret is written through the descriptor, never by reopening the |
| +# path. Unless a secret was written, both are removed on exit, by exact |
| +# path. |
| +secret_dir="" |
| +secret_file="" |
| +secret_written=0 |
| +cleanup_secret() { |
| + if [[ -n "$secret_dir" && "$secret_written" == 0 ]]; then |
| + rm -f -- "$secret_file" |
| + rmdir -- "$secret_dir" 2>/dev/null || true |
| + fi |
| +} |
| +if [[ "$mode" == apply ]]; then |
| + secret_dir="$(mktemp -d)" || die "mktemp -d failed" |
| + trap cleanup_secret EXIT |
| + secret_file="$secret_dir/webhook-secret" |
| + old_umask="$(umask)" |
| + umask 077 |
| + set -C |
| + exec 9>"$secret_file" || die "could not create $secret_file" |
| + set +C |
| + umask "$old_umask" |
| + [[ -f "$secret_file" && ! -L "$secret_file" ]] || die "$secret_file is not a new regular file" |
| +fi |
| + |
| case "$mode" in |
| dry-run) echo "# stripe-setup.sh dry run: nothing below has been sent to Stripe." ;; |
| check) echo "# stripe-setup.sh --check: reads only." ;; |
| @@ -624,14 +657,16 @@ echo |
| events_json="$(printf '%s\n' "${WEBHOOK_EVENTS[@]}" | jq -R . | jq -sc .)" |
| echo "## The webhook endpoint: $webhook_url" |
| list_all /v1/webhook_endpoints |
| -found="$(jq -c --arg u "$webhook_url" '[.[] | select(.url == $u and .status == "enabled")]' <<<"$out")" |
| +# A Connect application's endpoint at the same URL gets connected accounts' |
| +# events, not this account's: it is not the endpoint, and is left alone. |
| +found="$(jq -c --arg u "$webhook_url" '[.[] | select(.url == $u and .status == "enabled" and .application == null)]' <<<"$out")" |
| n="$(jq length <<<"$found")" |
| if ((n > 1)); then die "$n enabled webhook endpoints point at $webhook_url. Delete the extra ones in the Dashboard and run again"; fi |
| if ((n == 1)); then |
| we="$(jq -r '.[0].id' <<<"$found")" |
| problem="$(jq -r --arg version "$API_VERSION" --argjson events "$events_json" ".[0] | $WEBHOOK_PROBLEM" <<<"$found")" |
| [[ -z "$problem" ]] || die "webhook endpoint $we: $problem. Fix its events in the Dashboard, or, for a wrong API version (fixed when an endpoint is made), disable it and run again" |
| - echo "# exists: $we. Its signing secret went to the --secret-out file when it was made." |
| + echo "# exists: $we. Its signing secret went to the file the run that made it named." |
| echo "# If you do not have it, roll it in the Dashboard (Developers, Webhooks, the endpoint, Roll secret)." |
| else |
| echo "# none yet; this creates it:" |
| @@ -652,14 +687,15 @@ else |
| --arg version "$API_VERSION" --argjson events "$events_json" --arg url "$webhook_url" |
| out="" |
| # noclobber: the file is made new, never written through an existing one. |
| - if ! (umask 077 && set -C && printf 'CARLOS_STRIPE_WEBHOOK_SECRET=%s\n' "$secret" >"$secret_out") 2>/dev/null; then |
| + if ! printf 'CARLOS_STRIPE_WEBHOOK_SECRET=%s\n' "$secret" >&9; then |
| secret="" |
| - die "could not write $secret_out. The endpoint $id exists: roll its secret in the Dashboard (Developers, Webhooks, the endpoint, Roll secret)" |
| + die "could not write $secret_file. The endpoint $id exists: roll its secret in the Dashboard (Developers, Webhooks, the endpoint, Roll secret)" |
| fi |
| secret="" |
| - chmod 600 "$secret_out" |
| - echo "# created: $id. Its signing secret is in $secret_out (mode 0600), and nowhere else." |
| - echo "# Move it into the password manager and the console's EnvironmentFile, then delete $secret_out." |
| + exec 9>&- |
| + secret_written=1 |
| + echo "# created: $id. Its signing secret is in $secret_file (mode 0600, in a new 0700 directory), and nowhere else." |
| + echo "# Move it into the password manager and the console's EnvironmentFile, then delete the file and its directory." |
| fi |
| fi |
| echo |
| @@ -680,4 +716,4 @@ jq -r 'to_entries[] | "\(.key)=\(.value)"' <<<"$ids" |
| echo |
| echo "## Secrets: set them by hand in the same file. Never in the repo." |
| echo "# CARLOS_STRIPE_SECRET_KEY the rk_${mode_word}_ restricted key made in the Dashboard (the plans 6c runbook)" |
| -echo "# CARLOS_STRIPE_WEBHOOK_SECRET the whsec_ signing secret, in the --secret-out file when the endpoint is made" |
| +echo "# CARLOS_STRIPE_WEBHOOK_SECRET the whsec_ signing secret, in the file named when the endpoint is made" |