console: enable keeps its validation order; HA refusals get paired controls
The enable door again runs its bucket and apps-domain checks before reading the body and its domain-shape check after validating it (instancesConfigured, then instancesDomain), so a bad body answers 400 before an invalid PLATFORM_APPS_DOMAIN answers 503, as before Task 3. TestAPIInstanceEnableValidationOrder pins it. TestEnableHARegionNoLongerHA gains a case for a region dropped from CONSOLE_HA_REGIONS that still lists the pool, and every refusal there and in TestEnableHAManifestForBrowser is now followed by the same request succeeding once the refused condition is removed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+131
−22
internal/console/apiinstances.go+29 −11internal/console/apiinstances_haenable_test.go+63 −11internal/console/apiinstancesenable_test.go+39 −0
CI passed — run details
Intent
Attributed to this commit: 18.4 g CO₂e (2,310,967 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on g6-pool, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index 1a125cb29..4d2ecc753 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -3561,9 +3561,8 @@ func (s *Server) handleAPIInstanceEnable(w http.ResponseWriter, r *http.Request) |
| jsonError(w, http.StatusTooManyRequests, "too many requests — wait a minute and try again") |
| return |
| } |
| - domain, domainRef := s.instancesDomain(acct) |
| - if domainRef != nil { |
| - jsonError(w, domainRef.Status, domainRef.Msg) |
| + if ref := s.instancesConfigured(); ref != nil { |
| + jsonError(w, ref.Status, ref.Msg) |
| return |
| } |
| // Channel and Seed are pointers because "absent" and "empty" are |
| @@ -3599,6 +3598,14 @@ func (s *Server) handleAPIInstanceEnable(w http.ResponseWriter, r *http.Request) |
| } |
| } |
| } |
| + // After the request's own validation, as before the extraction: a body |
| + // the caller can fix answers 400 before a domain only an operator can |
| + // (TestAPIInstanceEnableValidationOrder). |
| + domain, domainRef := s.instancesDomain(acct) |
| + if domainRef != nil { |
| + jsonError(w, domainRef.Status, domainRef.Msg) |
| + return |
| + } |
| ctx, cancel := context.WithTimeout(r.Context(), instanceStoreTimeout) |
| defer cancel() |
| ctx, copiesUnlock, copiesLockRef := s.beginCopiesMutation(ctx, acct, app) |
| @@ -3658,22 +3665,33 @@ func (s *Server) handleAPIInstanceEnable(w http.ResponseWriter, r *http.Request) |
| }) |
| } |
| -// instancesDomain is the domain the enable door pins acct's manifests to, |
| -// <account-sqid>.<AppsDomain>, or the refusal when this console cannot |
| -// write one: no deployment bucket, no platform apps domain, or one that is |
| -// not a valid domain. Membership must already be proven: the refusals name |
| -// the operator act that fixes them. |
| -func (s *Server) instancesDomain(acct *Account) (string, *doorRefusal) { |
| +// instancesConfigured is the enable door's first configuration refusal, |
| +// before it reads the request body: no deployment bucket, or no platform |
| +// apps domain. Membership must already be proven: the refusals name the |
| +// operator act that fixes them. |
| +func (s *Server) instancesConfigured() *doorRefusal { |
| if s.cfg.Releases == nil { |
| - return "", &doorRefusal{Status: http.StatusServiceUnavailable, |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, |
| Msg: "no deployment bucket on this host — the console is running without one"} |
| } |
| if s.cfg.AppsDomain == "" { |
| // 503, not 409: the missing piece is deployment configuration |
| // (PLATFORM_APPS_DOMAIN on the console), not anything about this app. |
| - return "", &doorRefusal{Status: http.StatusServiceUnavailable, |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, |
| Msg: "this console has no platform apps domain configured — an operator enables instances in store mode (carlos instances enable) or sets PLATFORM_APPS_DOMAIN on the console"} |
| } |
| + return nil |
| +} |
| + |
| +// instancesDomain is the domain the enable door pins acct's manifests to, |
| +// <account-sqid>.<AppsDomain>, or the refusal when this console cannot |
| +// write one: instancesConfigured's, or an apps domain that is not a valid |
| +// domain. The door runs instancesConfigured before reading the body and |
| +// this after validating it; enableHAManifest runs this alone. |
| +func (s *Server) instancesDomain(acct *Account) (string, *doorRefusal) { |
| + if ref := s.instancesConfigured(); ref != nil { |
| + return "", ref |
| + } |
| domain := acct.Sqid + "." + s.cfg.AppsDomain |
| // The domain is about to be stamped into a manifest every future host |
| // gets checked against, and ValidHost only regex-checks the leading |
diff --git a/internal/console/apiinstances_haenable_test.go b/internal/console/apiinstances_haenable_test.go| index 33297e862..43b03e92c 100644 |
| --- a/internal/console/apiinstances_haenable_test.go |
| +++ b/internal/console/apiinstances_haenable_test.go |
| @@ -363,20 +363,63 @@ func TestEnableHARemovedAccountRefused(t *testing.T) { |
| wantBody(t, "listed enable", code, res, "ha", []string{"us"}) |
| } |
| +// addHAPool lists pool in region's ha_pools, as a capacity config naming |
| +// it there would (NewServer's parse): the region's pools and the |
| +// restricted set. |
| +func addHAPool(srv *Server, region, pool string) { |
| + srv.haPools[region] = append(slices.Clone(srv.haPools[region]), pool) |
| + srv.haRestrictedPools[pool] = true |
| +} |
| + |
| // TestEnableHARegionNoLongerHA: a born-HA app whose region is no longer |
| -// HA-scoped for its pool cannot write its first manifest. |
| +// HA-scoped for its pool cannot write its first manifest. Each refusal is |
| +// followed by the same enable with the refused condition removed. |
| func TestEnableHARegionNoLongerHA(t *testing.T) { |
| w := haEnableWorld(t, "us", nil) |
| + setRegion := func(app, region string) { |
| + t.Helper() |
| + if _, err := w.st.db.Exec(`UPDATE apps SET region = ? WHERE account_id = ? AND app = ?`, region, OpsAccountID, app); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + noManifest := func(app string) { |
| + t.Helper() |
| + if m, _ := w.manifestOf(t, app); m != nil { |
| + t.Fatalf("refused enable of %s wrote %+v", app, m) |
| + } |
| + } |
| + |
| + // A region that is not HA at all. |
| w.haApp(t, "haapp", "uk") |
| w.refused(t, "region uk", haAllow{}, func() { |
| code, res := w.enable(t, "haapp", `{}`) |
| wantHARefusal(t, "enable in uk", code, res, http.StatusConflict, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("uk")), "") |
| }) |
| - if m, _ := w.manifestOf(t, "haapp"); m != nil { |
| - t.Fatalf("refused enable wrote %+v", m) |
| - } |
| + noManifest("haapp") |
| + // Control: the same app, its region HA again. |
| + setRegion("haapp", "us") |
| + code, res := w.enable(t, "haapp", `{}`) |
| + wantBody(t, "enable in us", code, res, "ha", []string{"us"}) |
| + |
| + // A region switched off (dropped from CONSOLE_HA_REGIONS) that still |
| + // lists the pool: HA-region membership is checked on its own. |
| + w.haApp(t, "switched", "us") |
| + w.srv.cfg.HARegions = nil |
| + if !w.srv.poolInHAPools("us", "ha") { |
| + t.Fatal("fixture: us no longer lists pool ha") |
| + } |
| + w.refused(t, "us switched off", haAllow{}, func() { |
| + code, res := w.enable(t, "switched", `{}`) |
| + wantHARefusal(t, "enable with us switched off", code, res, http.StatusConflict, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("us")), "") |
| + }) |
| + noManifest("switched") |
| + // Control: the same enable with us switched on again. |
| + w.srv.cfg.HARegions = []string{"us"} |
| + code, res = w.enable(t, "switched", `{}`) |
| + wantBody(t, "enable with us switched on", code, res, "ha", []string{"us"}) |
| + |
| // A pool the HA region does not list (an unrestricted one, so the pool |
| - // gate passes) is refused the same way. |
| + // gate passes). |
| if _, err := w.st.CreateHAAppInRegion(OpsAccountID, "otherpool", "paul@keymail.dev", "us", "chosen", "other", time.Now()); err != nil { |
| t.Fatal(err) |
| } |
| @@ -384,12 +427,12 @@ func TestEnableHARegionNoLongerHA(t *testing.T) { |
| code, res := w.enable(t, "otherpool", `{}`) |
| wantHARefusal(t, "enable pool other", code, res, http.StatusConflict, fmt.Sprintf(haCopyRegion, w.srv.offlinePlace("us")), "") |
| }) |
| - // Control: the same app, its region HA again. |
| - if _, err := w.st.db.Exec(`UPDATE apps SET region = 'us' WHERE account_id = ? AND app = 'haapp'`, OpsAccountID); err != nil { |
| - t.Fatal(err) |
| - } |
| - code, res := w.enable(t, "haapp", `{}`) |
| - wantBody(t, "enable in us", code, res, "ha", []string{"us"}) |
| + noManifest("otherpool") |
| + // Control: the same enable once us lists pool other (bab is on the |
| + // allowlist, so the now-restricted pool passes the gate). |
| + addHAPool(w.srv, "us", "other") |
| + code, res = w.enable(t, "otherpool", `{}`) |
| + wantBody(t, "enable pool other listed", code, res, "other", []string{"us"}) |
| } |
| // TestEnableHAManifestForBrowser: enableHAManifest (the browser create's |
| @@ -434,4 +477,13 @@ func TestEnableHAManifestForBrowser(t *testing.T) { |
| if m, _ := w.manifestOf(t, "ukapp"); m != nil { |
| t.Fatalf("refused enableHAManifest wrote %+v", m) |
| } |
| + // Control: the same call once ukapp's region is HA. |
| + if _, err := w.st.db.Exec(`UPDATE apps SET region = 'us' WHERE account_id = ? AND app = 'ukapp'`, OpsAccountID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if ref := w.srv.enableHAManifest(ctx, acct, "ukapp", "203.0.113.9"); ref != nil { |
| + t.Fatalf("enableHAManifest in us = %+v, want nil", ref) |
| + } |
| + m, _ = w.manifestOf(t, "ukapp") |
| + wantManifest(t, "browser enable in us", m, "ha", []string{"us"}) |
| } |
diff --git a/internal/console/apiinstancesenable_test.go b/internal/console/apiinstancesenable_test.go| index 3310533d4..06fdf886e 100644 |
| --- a/internal/console/apiinstancesenable_test.go |
| +++ b/internal/console/apiinstancesenable_test.go |
| @@ -335,3 +335,42 @@ func TestAPIInstanceEnableRefusals(t *testing.T) { |
| t.Fatalf("no-apps-domain body = %s, want it to name the missing configuration", out) |
| } |
| } |
| + |
| +// TestAPIInstanceEnableValidationOrder: the deployment's configuration |
| +// refusals keep their places around the request's own validation. No |
| +// bucket or no apps domain answers 503 before the body is read; a body that |
| +// is malformed, or names a bad channel or seed, answers 400 before an apps |
| +// domain that is not a valid domain answers 503. |
| +func TestAPIInstanceEnableValidationOrder(t *testing.T) { |
| + fx, member := instanceFixture(t, "code-inst-enable-order") |
| + u := enableURL(fx, "bab", "titogo") |
| + bad := []struct{ what, body string }{ |
| + {"malformed JSON", `{"channel":`}, |
| + {"bad channel", `{"channel":"NOT a channel"}`}, |
| + {"empty seed arg", `{"seed":[""]}`}, |
| + } |
| + // Control: a valid domain enables. |
| + fx.srv.cfg.AppsDomain = "oncarlos.test" |
| + if status, _, out := rawReq(t, http.MethodPut, u, member, `{}`); status != http.StatusOK { |
| + t.Fatalf("control enable = %d %s, want 200", status, out) |
| + } |
| + |
| + fx.srv.cfg.AppsDomain = "*.oncarlos.test" |
| + for _, c := range bad { |
| + if status, _, out := rawReq(t, http.MethodPut, u, member, c.body); status != http.StatusBadRequest { |
| + t.Errorf("invalid apps domain, %s = %d %s, want 400 first", c.what, status, out) |
| + } |
| + } |
| + if status, _, out := rawReq(t, http.MethodPut, u, member, `{}`); status != http.StatusServiceUnavailable || |
| + !strings.Contains(out, "is not a valid domain") { |
| + t.Errorf("invalid apps domain, valid body = %d %s, want 503 naming the domain", status, out) |
| + } |
| + |
| + fx.srv.cfg.AppsDomain = "" |
| + for _, c := range bad { |
| + if status, _, out := rawReq(t, http.MethodPut, u, member, c.body); status != http.StatusServiceUnavailable || |
| + !strings.Contains(out, "PLATFORM_APPS_DOMAIN") { |
| + t.Errorf("no apps domain, %s = %d %s, want 503 first", c.what, status, out) |
| + } |
| + } |
| +} |