console: billing configuration, live refuses a draft book, a book edited under its version refuses to start (plans 6a)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+446
−22
cmd/console/billing.go+86 −0cmd/console/billing_test.go+195 −0cmd/console/main.go+18 −0internal/console/billing.go+36 −0internal/console/billingstore.go+45 −0internal/console/billingstore_test.go+29 −0internal/console/server.go+10 −0internal/console/store.go+5 −1internal/console/store_gate.go+22 −21
diff --git a/cmd/console/billing.go b/cmd/console/billing.go| new file mode 100644 |
| index 000000000..21f46c529 |
| --- /dev/null |
| +++ b/cmd/console/billing.go |
| @@ -0,0 +1,86 @@ |
| +package main |
| + |
| +import ( |
| + "fmt" |
| + "strings" |
| + |
| + "github.com/carlosframework/platform/internal/billing" |
| + "github.com/carlosframework/platform/internal/billingstripe" |
| + "github.com/carlosframework/platform/internal/console" |
| + "github.com/carlosframework/platform/internal/plans" |
| +) |
| + |
| +// billingConfigError names the variable at fault and never its value: the |
| +// values are secrets, or carry them. |
| +func billingConfigError(variable string) error { |
| + return fmt.Errorf("console: billing configuration invalid: %s", variable) |
| +} |
| + |
| +// billingConfig reads the billing configuration from the console's |
| +// environment (plans-caps-packs spec §3, §9). CARLOS_BILLING unset or "off" |
| +// is nil, nil: billing is dormant and nothing else is read. The keys come |
| +// from the console's EnvironmentFile only, never a flag, a file path or the |
| +// bucket. A live console refuses a draft price book. |
| +func billingConfig(getenv func(string) string, cat *plans.Catalog) (*console.BillingConfig, error) { |
| + mode := strings.TrimSpace(getenv("CARLOS_BILLING")) |
| + if mode == "" || mode == "off" { |
| + return nil, nil |
| + } |
| + if mode != "test" && mode != "live" { |
| + return nil, billingConfigError("CARLOS_BILLING") |
| + } |
| + live := mode == "live" |
| + if cat == nil { |
| + return nil, billingConfigError("CARLOS_PLAN_CATALOG") |
| + } |
| + key := strings.TrimSpace(getenv("CARLOS_STRIPE_SECRET_KEY")) |
| + prefix := "rk_test_" |
| + if live { |
| + prefix = "rk_live_" |
| + } |
| + if !strings.HasPrefix(key, prefix) || len(key) <= len(prefix) { |
| + return nil, billingConfigError("CARLOS_STRIPE_SECRET_KEY") |
| + } |
| + account := strings.TrimSpace(getenv("CARLOS_STRIPE_ACCOUNT")) |
| + if !strings.HasPrefix(account, "acct_") || len(account) <= len("acct_") { |
| + return nil, billingConfigError("CARLOS_STRIPE_ACCOUNT") |
| + } |
| + primary := strings.TrimSpace(getenv("CARLOS_STRIPE_WEBHOOK_SECRET")) |
| + if !strings.HasPrefix(primary, "whsec_") || len(primary) <= len("whsec_") { |
| + return nil, billingConfigError("CARLOS_STRIPE_WEBHOOK_SECRET") |
| + } |
| + secrets := []string{primary} |
| + if rotation := strings.TrimSpace(getenv("CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS")); rotation != "" { |
| + seen := map[string]bool{primary: true} |
| + for _, v := range strings.Split(rotation, ",") { |
| + s := strings.TrimSpace(v) |
| + if !strings.HasPrefix(s, "whsec_") || len(s) <= len("whsec_") || seen[s] { |
| + return nil, billingConfigError("CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS") |
| + } |
| + seen[s] = true |
| + secrets = append(secrets, s) |
| + } |
| + if len(secrets) > 3 { |
| + return nil, billingConfigError("CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS") |
| + } |
| + } |
| + bookPath := strings.TrimSpace(getenv("CARLOS_PRICE_BOOK")) |
| + if bookPath == "" { |
| + return nil, billingConfigError("CARLOS_PRICE_BOOK") |
| + } |
| + book, err := plans.LoadPriceBook(bookPath, cat) |
| + if err != nil || (live && !book.Approved()) { |
| + return nil, billingConfigError("CARLOS_PRICE_BOOK") |
| + } |
| + prices, err := billing.ParsePriceMap(getenv("CARLOS_BILLING_PRICES"), book) |
| + if err != nil { |
| + return nil, billingConfigError("CARLOS_BILLING_PRICES") |
| + } |
| + provider, err := billingstripe.NewProvider(billingstripe.ProviderConfig{ |
| + SecretKey: key, Account: account, WebhookSecrets: secrets, LiveMode: live, |
| + }) |
| + if err != nil { |
| + return nil, billingConfigError("CARLOS_STRIPE_SECRET_KEY") |
| + } |
| + return &console.BillingConfig{LiveMode: live, Book: book, Prices: prices, Provider: provider}, nil |
| +} |
diff --git a/cmd/console/billing_test.go b/cmd/console/billing_test.go| new file mode 100644 |
| index 000000000..5aedc187a |
| --- /dev/null |
| +++ b/cmd/console/billing_test.go |
| @@ -0,0 +1,195 @@ |
| +package main |
| + |
| +import ( |
| + "encoding/json" |
| + "os" |
| + "path/filepath" |
| + "regexp" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/billing" |
| + "github.com/carlosframework/platform/internal/plans" |
| +) |
| + |
| +const ( |
| + exampleCatalogPath = "../../internal/plans/testdata/example-catalog.json" |
| + exampleBookPath = "../../internal/plans/testdata/example-pricebook.json" |
| + testSecretKey = "rk_test_51SecretValue" |
| +) |
| + |
| +func billingCatalog(t *testing.T) *plans.Catalog { |
| + t.Helper() |
| + c, err := plans.LoadCatalog(exampleCatalogPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return c |
| +} |
| + |
| +var keyChars = regexp.MustCompile(`[^A-Za-z0-9]`) |
| + |
| +// examplePrices is a full CARLOS_BILLING_PRICES for the example book. |
| +func examplePrices(t *testing.T, book *plans.PriceBook) map[string]string { |
| + t.Helper() |
| + m := map[string]string{} |
| + add := func(r billing.PriceRef) { m[r.Key()] = "price_" + keyChars.ReplaceAllString(r.Key(), "") } |
| + for name := range book.Plans { |
| + for _, c := range book.Currencies { |
| + add(billing.PriceRef{Book: book.Version, Kind: billing.KindPlan, Name: name, Currency: c}) |
| + } |
| + } |
| + for name := range book.Packs { |
| + for _, c := range book.Currencies { |
| + add(billing.PriceRef{Book: book.Version, Kind: billing.KindPack, Name: name, Currency: c}) |
| + } |
| + } |
| + return m |
| +} |
| + |
| +func billingEnv(t *testing.T) map[string]string { |
| + t.Helper() |
| + book, err := plans.LoadPriceBook(exampleBookPath, billingCatalog(t)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + prices, _ := json.Marshal(examplePrices(t, book)) |
| + return map[string]string{ |
| + "CARLOS_BILLING": "test", |
| + "CARLOS_PRICE_BOOK": exampleBookPath, |
| + "CARLOS_BILLING_PRICES": string(prices), |
| + "CARLOS_STRIPE_SECRET_KEY": testSecretKey, |
| + "CARLOS_STRIPE_ACCOUNT": "acct_1Example", |
| + "CARLOS_STRIPE_WEBHOOK_SECRET": "whsec_primary", |
| + } |
| +} |
| + |
| +func getenvFrom(m map[string]string) func(string) string { |
| + return func(k string) string { return m[k] } |
| +} |
| + |
| +func TestBillingConfigOff(t *testing.T) { |
| + for _, mode := range []string{"", "off"} { |
| + env := billingEnv(t) |
| + env["CARLOS_BILLING"] = mode |
| + env["CARLOS_STRIPE_SECRET_KEY"] = "garbage" |
| + cfg, err := billingConfig(getenvFrom(env), nil) |
| + if cfg != nil || err != nil { |
| + t.Errorf("CARLOS_BILLING=%q: %v, %v; want nil, nil", mode, cfg, err) |
| + } |
| + } |
| +} |
| + |
| +func TestBillingConfigRefuses(t *testing.T) { |
| + approved := filepath.Join(t.TempDir(), "approved.json") |
| + otherCatalog := filepath.Join(t.TempDir(), "other.json") |
| + src, err := os.ReadFile(exampleBookPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(approved, []byte(strings.Replace(string(src), `"status": "draft"`, `"status": "approved"`, 1)), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(otherCatalog, []byte(strings.Replace(string(src), `"2026-10-20-v1"`, `"2026-01-01-v0"`, 1)), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + cases := []struct { |
| + name, variable string |
| + edit func(env map[string]string) |
| + noCatalog bool |
| + }{ |
| + {"mode on", "CARLOS_BILLING", func(e map[string]string) { e["CARLOS_BILLING"] = "on" }, false}, |
| + {"test with a live key", "CARLOS_STRIPE_SECRET_KEY", func(e map[string]string) { e["CARLOS_STRIPE_SECRET_KEY"] = "rk_live_51SecretValue" }, false}, |
| + {"live with a test key", "CARLOS_STRIPE_SECRET_KEY", func(e map[string]string) { |
| + e["CARLOS_BILLING"] = "live" |
| + e["CARLOS_PRICE_BOOK"] = approved |
| + }, false}, |
| + {"full secret key", "CARLOS_STRIPE_SECRET_KEY", func(e map[string]string) { e["CARLOS_STRIPE_SECRET_KEY"] = "sk_test_51SecretValue" }, false}, |
| + {"bare prefix", "CARLOS_STRIPE_SECRET_KEY", func(e map[string]string) { e["CARLOS_STRIPE_SECRET_KEY"] = "rk_test_" }, false}, |
| + {"no account", "CARLOS_STRIPE_ACCOUNT", func(e map[string]string) { delete(e, "CARLOS_STRIPE_ACCOUNT") }, false}, |
| + {"account not acct_", "CARLOS_STRIPE_ACCOUNT", func(e map[string]string) { e["CARLOS_STRIPE_ACCOUNT"] = "cus_1" }, false}, |
| + {"no webhook secret", "CARLOS_STRIPE_WEBHOOK_SECRET", func(e map[string]string) { delete(e, "CARLOS_STRIPE_WEBHOOK_SECRET") }, false}, |
| + {"webhook secret not whsec_", "CARLOS_STRIPE_WEBHOOK_SECRET", func(e map[string]string) { e["CARLOS_STRIPE_WEBHOOK_SECRET"] = "secret" }, false}, |
| + {"three previous secrets", "CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS", func(e map[string]string) { |
| + e["CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS"] = "whsec_a,whsec_b,whsec_c" |
| + }, false}, |
| + {"repeated secret", "CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS", func(e map[string]string) { |
| + e["CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS"] = "whsec_primary" |
| + }, false}, |
| + {"previous secret not whsec_", "CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS", func(e map[string]string) { |
| + e["CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS"] = "whsec_a,," |
| + }, false}, |
| + {"no catalog", "CARLOS_PLAN_CATALOG", func(map[string]string) {}, true}, |
| + {"book path missing", "CARLOS_PRICE_BOOK", func(e map[string]string) { e["CARLOS_PRICE_BOOK"] = filepath.Join(t.TempDir(), "none.json") }, false}, |
| + {"no book", "CARLOS_PRICE_BOOK", func(e map[string]string) { delete(e, "CARLOS_PRICE_BOOK") }, false}, |
| + {"book over another catalog", "CARLOS_PRICE_BOOK", func(e map[string]string) { e["CARLOS_PRICE_BOOK"] = otherCatalog }, false}, |
| + {"live with a draft book", "CARLOS_PRICE_BOOK", func(e map[string]string) { |
| + e["CARLOS_BILLING"] = "live" |
| + e["CARLOS_STRIPE_SECRET_KEY"] = "rk_live_51SecretValue" |
| + }, false}, |
| + {"prices missing a key", "CARLOS_BILLING_PRICES", func(e map[string]string) { |
| + var m map[string]string |
| + _ = json.Unmarshal([]byte(e["CARLOS_BILLING_PRICES"]), &m) |
| + delete(m, "example-v1/pack/email/gbp") |
| + b, _ := json.Marshal(m) |
| + e["CARLOS_BILLING_PRICES"] = string(b) |
| + }, false}, |
| + {"prices not json", "CARLOS_BILLING_PRICES", func(e map[string]string) { e["CARLOS_BILLING_PRICES"] = "price_x" }, false}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + env := billingEnv(t) |
| + tc.edit(env) |
| + cat := billingCatalog(t) |
| + if tc.noCatalog { |
| + cat = nil |
| + } |
| + cfg, err := billingConfig(getenvFrom(env), cat) |
| + if err == nil || cfg != nil { |
| + t.Fatalf("accepted: %v", cfg) |
| + } |
| + if want := "console: billing configuration invalid: " + tc.variable; err.Error() != want { |
| + t.Errorf("error %q, want %q", err, want) |
| + } |
| + for _, v := range env { |
| + if len(v) > 6 && strings.Contains(err.Error(), v) { |
| + t.Errorf("error %q carries a value", err) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestBillingConfigTestModeAcceptsDraft(t *testing.T) { |
| + env := billingEnv(t) |
| + env["CARLOS_STRIPE_PREVIOUS_WEBHOOK_SECRETS"] = "whsec_old1, whsec_old2" |
| + cfg, err := billingConfig(getenvFrom(env), billingCatalog(t)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if cfg.LiveMode || cfg.Book == nil || cfg.Book.Version != "example-v1" || cfg.Provider == nil { |
| + t.Fatalf("cfg = %+v", cfg) |
| + } |
| + if _, ok := cfg.Prices.ID(billing.PriceRef{Book: "example-v1", Kind: billing.KindPlan, Name: "personal", Currency: "usd"}); !ok { |
| + t.Error("price map lacks personal/usd") |
| + } |
| +} |
| + |
| +func TestBillingConfigLiveAcceptsApproved(t *testing.T) { |
| + approved := filepath.Join(t.TempDir(), "approved.json") |
| + src, err := os.ReadFile(exampleBookPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(approved, []byte(strings.Replace(string(src), `"status": "draft"`, `"status": "approved"`, 1)), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + env := billingEnv(t) |
| + env["CARLOS_BILLING"] = "live" |
| + env["CARLOS_STRIPE_SECRET_KEY"] = "rk_live_51SecretValue" |
| + env["CARLOS_PRICE_BOOK"] = approved |
| + cfg, err := billingConfig(getenvFrom(env), billingCatalog(t)) |
| + if err != nil || !cfg.LiveMode || !cfg.Book.Approved() { |
| + t.Fatalf("live + approved: %+v, %v", cfg, err) |
| + } |
| +} |
diff --git a/cmd/console/main.go b/cmd/console/main.go| index 773114ee3..fdf6414ed 100644 |
| --- a/cmd/console/main.go |
| +++ b/cmd/console/main.go |
| @@ -341,6 +341,23 @@ func serve() { |
| // A broken catalog must never quietly mean "no limits". |
| log.Fatalf("console: CARLOS_PLAN_CATALOG: %v", err) |
| } |
| + // Billing (plans 6a): dormant unless CARLOS_BILLING is test or live. |
| + // Every failure names the variable, never its value. |
| + billingCfg, err := billingConfig(os.Getenv, catalog) |
| + if err != nil { |
| + log.Fatal(err) |
| + } |
| + if billingCfg != nil { |
| + // A book edited under a version the console has started with |
| + // refuses to start: approval is a new version (spec §3). |
| + err := st.NotePriceBook(billingCfg.Book.Version, billingCfg.Book.Digest(), billingCfg.Book.Status, time.Now()) |
| + if errors.Is(err, console.ErrPriceBookEdited) { |
| + log.Fatalf("console: price book %s changed without a new version", billingCfg.Book.Version) |
| + } |
| + if err != nil { |
| + log.Fatalf("console: recording price book %s: %v", billingCfg.Book.Version, err) |
| + } |
| + } |
| var callbackSigner *console.CallbackSigner |
| if os.Getenv("CARLOS_SCAN_WEBHOOK_KEY") == "" && os.Getenv("CARLOS_SCAN_WEBHOOK_KEY_PREVIOUS") != "" { |
| log.Fatal("console: CARLOS_SCAN_WEBHOOK_KEY_PREVIOUS is set without CARLOS_SCAN_WEBHOOK_KEY") |
| @@ -461,6 +478,7 @@ func serve() { |
| StoreMaxPerApp: envCount("CARLOS_STORE_MAX_PER_APP", 0), |
| StoreOperators: storeOperators(), |
| PlanCatalog: catalog, |
| + Billing: billingCfg, |
| TemporaryApps: os.Getenv("CARLOS_TEMPORARY_APPS") == "1", |
| // CARLOS_ACME_DELEGATION_ZONE — the same variable the edge reads |
| // (internal/edge/wildcard.go's newWildcardFromEnv) for the same |
diff --git a/internal/console/billing.go b/internal/console/billing.go| new file mode 100644 |
| index 000000000..7255dcf8c |
| --- /dev/null |
| +++ b/internal/console/billing.go |
| @@ -0,0 +1,36 @@ |
| +package console |
| + |
| +import ( |
| + "sync/atomic" |
| + |
| + "github.com/carlosframework/platform/internal/billing" |
| + "github.com/carlosframework/platform/internal/plans" |
| +) |
| + |
| +// BillingConfig is a console's billing configuration (plans-caps-packs spec |
| +// §9, §10). nil on Config means billing is off: the webhook answers 404, |
| +// the billing doors 409, the sync pass returns at once, and no billing row |
| +// is written. |
| +type BillingConfig struct { |
| + LiveMode bool |
| + Book *plans.PriceBook |
| + Prices billing.PriceMap |
| + Provider billing.Provider |
| +} |
| + |
| +// billingRuntime is a server's billing state. ready is set once the |
| +// provider's readiness check has passed; until then the webhook answers 503 |
| +// and nothing syncs. locks serialise one account's syncs: the console runs |
| +// as one process per deployment, so an in-process lock is enough. |
| +type billingRuntime struct { |
| + cfg *BillingConfig |
| + ready atomic.Bool |
| + locks keyedLocks |
| +} |
| + |
| +func newBillingRuntime(cfg *BillingConfig) *billingRuntime { |
| + if cfg == nil { |
| + return nil |
| + } |
| + return &billingRuntime{cfg: cfg} |
| +} |
diff --git a/internal/console/billingstore.go b/internal/console/billingstore.go| new file mode 100644 |
| index 000000000..3ba7e4c27 |
| --- /dev/null |
| +++ b/internal/console/billingstore.go |
| @@ -0,0 +1,45 @@ |
| +package console |
| + |
| +import ( |
| + "database/sql" |
| + "errors" |
| + "time" |
| +) |
| + |
| +// ErrPriceBookEdited refuses a price book whose content changed under a |
| +// version this console has already started with. Approval, like any edit, |
| +// is a new version (spec §3). |
| +var ErrPriceBookEdited = errors.New("price book changed without a new version") |
| + |
| +func (s *Store) migrateBilling() error { |
| + _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS billing_price_books ( |
| + version TEXT PRIMARY KEY, |
| + digest TEXT NOT NULL, |
| + status TEXT NOT NULL, |
| + first_seen INTEGER NOT NULL |
| +);`) |
| + return err |
| +} |
| + |
| +// NotePriceBook records a book version's digest the first time the console |
| +// starts with it, and refuses the same version with another digest. A |
| +// changed status is a changed digest, so a draft approved in place is |
| +// refused too. |
| +func (s *Store) NotePriceBook(version, digest, status string, now time.Time) error { |
| + if _, err := s.db.Exec(`INSERT INTO billing_price_books (version, digest, status, first_seen) |
| + VALUES (?, ?, ?, ?) ON CONFLICT(version) DO NOTHING`, version, digest, status, now.Unix()); err != nil { |
| + return err |
| + } |
| + var stored string |
| + err := s.db.QueryRow(`SELECT digest FROM billing_price_books WHERE version = ?`, version).Scan(&stored) |
| + if errors.Is(err, sql.ErrNoRows) { |
| + return errors.New("price book row vanished") |
| + } |
| + if err != nil { |
| + return err |
| + } |
| + if stored != digest { |
| + return ErrPriceBookEdited |
| + } |
| + return nil |
| +} |
diff --git a/internal/console/billingstore_test.go b/internal/console/billingstore_test.go| new file mode 100644 |
| index 000000000..83d115153 |
| --- /dev/null |
| +++ b/internal/console/billingstore_test.go |
| @@ -0,0 +1,29 @@ |
| +package console |
| + |
| +import ( |
| + "errors" |
| + "testing" |
| + "time" |
| +) |
| + |
| +var billingNow = time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| + |
| +func TestNotePriceBook(t *testing.T) { |
| + s := openTestStore(t) |
| + if err := s.NotePriceBook("example-v1", "aaaaaaaaaaaa", "draft", billingNow); err != nil { |
| + t.Fatalf("first: %v", err) |
| + } |
| + if err := s.NotePriceBook("example-v1", "aaaaaaaaaaaa", "draft", billingNow.Add(time.Hour)); err != nil { |
| + t.Fatalf("same digest again: %v", err) |
| + } |
| + if err := s.NotePriceBook("example-v1", "bbbbbbbbbbbb", "approved", billingNow); !errors.Is(err, ErrPriceBookEdited) { |
| + t.Fatalf("same version, other digest: %v, want ErrPriceBookEdited", err) |
| + } |
| + if err := s.NotePriceBook("example-v2", "bbbbbbbbbbbb", "approved", billingNow); err != nil { |
| + t.Fatalf("second version: %v", err) |
| + } |
| + var first int64 |
| + if err := s.db.QueryRow(`SELECT first_seen FROM billing_price_books WHERE version = 'example-v1'`).Scan(&first); err != nil || first != billingNow.Unix() { |
| + t.Errorf("first_seen = %d, %v; want the first start", first, err) |
| + } |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index c351563ad..39d814427 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -740,6 +740,9 @@ type Config struct { |
| // plans-caps-packs spec §4.1). nil means plans are off: no account has |
| // limits, no limits record is published, and the plan door answers 409. |
| PlanCatalog *plans.Catalog |
| + // Billing is the deployment's billing configuration (CARLOS_BILLING, |
| + // plans 6a). nil means billing is off. It needs PlanCatalog. |
| + Billing *BillingConfig |
| // TemporaryApps turns on POST /api/cli/apps/{account}/new-temporary |
| // (temporary static sites spec §3). Off by default, and only switched on |
| // after every box runs a binary that carries the tmp- reservation and |
| @@ -874,6 +877,9 @@ type Config struct { |
| // Server is the console's http.Handler. |
| type Server struct { |
| + // billing is nil when billing is off (Config.Billing, plans 6a). |
| + billing *billingRuntime |
| + |
| claimLossPageMu sync.Mutex |
| claimLossPageDone chan struct{} |
| @@ -1359,6 +1365,9 @@ func normalizeDelegationZone(raw string) string { |
| // NewServer wires a server over an opened Store, loading (or minting) the |
| // console's Keymail signing key. |
| func NewServer(cfg Config, st *Store) (*Server, error) { |
| + if cfg.Billing != nil && cfg.PlanCatalog == nil { |
| + return nil, errors.New("billing needs a plan catalog (CARLOS_PLAN_CATALOG)") |
| + } |
| if cfg.HostClaim && !st.claimGated() { |
| return nil, fmt.Errorf("CARLOS_HOST_CLAIM: %s", "the console database must be opened gated (console.OpenGated)") |
| } |
| @@ -1429,6 +1438,7 @@ func NewServer(cfg Config, st *Store) (*Server, error) { |
| // each other apart (relayops.go). |
| relayHolder: relayHolderID(), |
| scanQueue: objstore.NewQueueIndex(), |
| + billing: newBillingRuntime(cfg.Billing), |
| } |
| s.seedScratch = startSeedScratch(cfg.SeedScratchDir) |
| if s.lookupTXT == nil { |
diff --git a/internal/console/store.go b/internal/console/store.go| index 5049a2b4f..0aa7fd693 100644 |
| --- a/internal/console/store.go |
| +++ b/internal/console/store.go |
| @@ -551,7 +551,11 @@ func (s *Store) migrate() error { |
| return err |
| } |
| // Plans 4a's cap state (plangrace_store.go), for the same reason. |
| - return s.migratePlanGrace() |
| + if err := s.migratePlanGrace(); err != nil { |
| + return err |
| + } |
| + // Plans 6a's billing state (billingstore.go), for the same reason. |
| + return s.migrateBilling() |
| } |
| // mailCountsBackfillVersion is the user_version this migration stamps. The |
diff --git a/internal/console/store_gate.go b/internal/console/store_gate.go| index 5e1311baa..f28326dc2 100644 |
| --- a/internal/console/store_gate.go |
| +++ b/internal/console/store_gate.go |
| @@ -200,27 +200,28 @@ var claimSQLTables = map[string]claimSQLTable{ |
| "mail_event_deliveries": {Sqid: "account", App: "app"}, |
| // ---- not app-scoped: each with its reason ---- |
| - "sessions": {NonApp: "browser sessions, keyed by token hash and address"}, |
| - "users": {NonApp: "people, keyed by address"}, |
| - "grants": {NonApp: "keymail grants, keyed by address"}, |
| - "confirmations": {NonApp: "sign-in confirmation links, keyed by token hash and address"}, |
| - "keys": {NonApp: "the console's own signing keys"}, |
| - "accounts": {NonApp: "the tenancy primitive itself; names no app"}, |
| - "memberships": {NonApp: "account membership, keyed by account and address; names no app"}, |
| - "invites": {NonApp: "account invites, keyed by account and address; names no app"}, |
| - "account_plans": {NonApp: "an account's plan and packs, keyed by account; names no app"}, |
| - "plan_cap_state": {NonApp: "an account's plan cap warnings and grace clocks, keyed by account; names no app"}, |
| - "alert_seen": {NonApp: "operator alert memory, keyed by box, rule and address"}, |
| - "claim_loss_pages": {NonApp: "claim recovery page attempts per loss record and condition, and scan progress; no app state or lifecycle authority"}, |
| - "move_pages": {NonApp: "the move pass's last page attempt per finding (rule names the host and the move); no app column"}, |
| - "move_log_outbox": {NonApp: "a moves audit outbox that stores no app name: rows are keyed by host, move and entry and bound to an instance incarnation, verified against the host's claim and record at every write, written only by the moves recovery and log passes, and purged with the incarnation"}, |
| - "edge_proof_seen": {NonApp: "single-row edge proof marker"}, |
| - "cli_auth_requests": {NonApp: "device-code logins in flight, keyed by code hash"}, |
| - "step_ups": {NonApp: "passkey step-up marks, keyed by session hash"}, |
| - "claim_sql_identity": {NonApp: "private receipt database identity"}, |
| - "claim_sql_receipts": {NonApp: "private SQL effect receipts and fences"}, |
| - "email_challenges": {NonApp: "emailed sign-in and step-up links and codes in flight, keyed by id hash; carries no app column"}, |
| - "pending_actions": {NonApp: "replay-by-token actions, keyed by token hash; carries no app column and re-enters the ordinary handlers"}, |
| + "sessions": {NonApp: "browser sessions, keyed by token hash and address"}, |
| + "users": {NonApp: "people, keyed by address"}, |
| + "grants": {NonApp: "keymail grants, keyed by address"}, |
| + "confirmations": {NonApp: "sign-in confirmation links, keyed by token hash and address"}, |
| + "keys": {NonApp: "the console's own signing keys"}, |
| + "accounts": {NonApp: "the tenancy primitive itself; names no app"}, |
| + "memberships": {NonApp: "account membership, keyed by account and address; names no app"}, |
| + "invites": {NonApp: "account invites, keyed by account and address; names no app"}, |
| + "account_plans": {NonApp: "an account's plan and packs, keyed by account; names no app"}, |
| + "plan_cap_state": {NonApp: "an account's plan cap warnings and grace clocks, keyed by account; names no app"}, |
| + "billing_price_books": {NonApp: "price book versions and digests the console has started with; names no app"}, |
| + "alert_seen": {NonApp: "operator alert memory, keyed by box, rule and address"}, |
| + "claim_loss_pages": {NonApp: "claim recovery page attempts per loss record and condition, and scan progress; no app state or lifecycle authority"}, |
| + "move_pages": {NonApp: "the move pass's last page attempt per finding (rule names the host and the move); no app column"}, |
| + "move_log_outbox": {NonApp: "a moves audit outbox that stores no app name: rows are keyed by host, move and entry and bound to an instance incarnation, verified against the host's claim and record at every write, written only by the moves recovery and log passes, and purged with the incarnation"}, |
| + "edge_proof_seen": {NonApp: "single-row edge proof marker"}, |
| + "cli_auth_requests": {NonApp: "device-code logins in flight, keyed by code hash"}, |
| + "step_ups": {NonApp: "passkey step-up marks, keyed by session hash"}, |
| + "claim_sql_identity": {NonApp: "private receipt database identity"}, |
| + "claim_sql_receipts": {NonApp: "private SQL effect receipts and fences"}, |
| + "email_challenges": {NonApp: "emailed sign-in and step-up links and codes in flight, keyed by id hash; carries no app column"}, |
| + "pending_actions": {NonApp: "replay-by-token actions, keyed by token hash; carries no app column and re-enters the ordinary handlers"}, |
| } |
| // claimSQLReservedName is the TEMP permit table. A main-schema table by that |