console: env Save shows the section unavailable on any storage failure
Drops b3ac5cd3's read-tagging store wrapper: it hid the claims store's AppMutationRunner from release.WithAppMutation, so config writes answered 502 with claims on (CI). configwrite.go and envcopy.go are back at their pre-b3ac5cd3 bytes. Controller ruling: any configRefusalStore on Save renders the unavailable section with nothing posted drawn back; adds the late malformed-JSON read case.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+125
−86
internal/console/configwrite.go+5 −37internal/console/envcopy.go+0 −1internal/console/envformgate_test.go+60 −29internal/console/envsave.go+11 −5internal/console/envsave_test.go+49 −14
diff --git a/internal/console/configwrite.go b/internal/console/configwrite.go| index fba3fa10b..a2d9a12c4 100644 |
| --- a/internal/console/configwrite.go |
| +++ b/internal/console/configwrite.go |
| @@ -23,7 +23,6 @@ import ( |
| "fmt" |
| "log" |
| "net/http" |
| - "os" |
| "sort" |
| "github.com/carlosframework/platform/internal/appconfig" |
| @@ -56,12 +55,7 @@ const ( |
| configRefusalConflict configRefusalCode = "conflict" |
| configRefusalNothingToRemove configRefusalCode = "nothing-to-remove" |
| configRefusalStore configRefusalCode = "store" |
| - // configRefusalRead is a store failure on the bundle read inside the |
| - // write, before anything was written: the JSON door answers it exactly |
| - // as configRefusalStore; the settings form shows the section |
| - // unavailable, as for a failed read when the page was assembled. |
| - configRefusalRead configRefusalCode = "read" |
| - configRefusalSealKey configRefusalCode = "seal-key" |
| + configRefusalSealKey configRefusalCode = "seal-key" |
| ) |
| // configRefusal is one refused config write: a code every door can render, |
| @@ -221,38 +215,17 @@ func (s *Server) writeConfig(ctx context.Context, acct *Account, app, envName st |
| // checkConfigSets call, are its only callers. |
| func (s *Server) configWriteBundle(ctx context.Context, unlock func(), acct *Account, app, envName string, want *appconfig.BundleVersion, sets map[string]appconfig.Var, unset []string) (int, *configRefusal) { |
| return s.configWriteStore(ctx, unlock, acct, app, envName, func(ctx context.Context) (int, error) { |
| - st := configReadTagStore{s.cfg.Releases} |
| switch { |
| case want != nil: |
| - return appconfig.ApplyAtVersion(ctx, st, acct.Sqid, app, envName, *want, sets, unset) |
| + return appconfig.ApplyAtVersion(ctx, s.cfg.Releases, acct.Sqid, app, envName, *want, sets, unset) |
| case envName == "": |
| - return appconfig.Apply(ctx, st, acct.Sqid, app, sets, unset) |
| + return appconfig.Apply(ctx, s.cfg.Releases, acct.Sqid, app, sets, unset) |
| default: |
| - return appconfig.ApplyEnvironment(ctx, st, acct.Sqid, app, envName, sets, unset) |
| + return appconfig.ApplyEnvironment(ctx, s.cfg.Releases, acct.Sqid, app, envName, sets, unset) |
| } |
| }) |
| } |
| -// configReadError is a store read that failed inside a config write. Its |
| -// text is the store error's own, so logs and every message built from it |
| -// are unchanged; configStoreRefusal tells it apart as configRefusalRead. |
| -type configReadError struct{ err error } |
| - |
| -func (e *configReadError) Error() string { return e.err.Error() } |
| -func (e *configReadError) Unwrap() error { return e.err } |
| - |
| -// configReadTagStore marks the write's read failures (configReadError). |
| -// A missing object is not a failure: it passes through untouched. |
| -type configReadTagStore struct{ release.Store } |
| - |
| -func (s configReadTagStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| - b, etag, err := s.Store.Get(ctx, key) |
| - if err != nil && !errors.Is(err, os.ErrNotExist) { |
| - err = &configReadError{err} |
| - } |
| - return b, etag, err |
| -} |
| - |
| // configWriteBegin is a write's first half: the account lock, then the |
| // seal check under it. It returns the unlock (idempotent; a no-op when no |
| // lock was taken), which the caller must defer even on a refusal. |
| @@ -330,12 +303,7 @@ func configStoreRefusal(what string, err error) *configRefusal { |
| // nothing they didn't send. |
| return &configRefusal{Code: configRefusalNothingToRemove, Status: http.StatusConflict, Msg: err.Error()} |
| } |
| - code := configRefusalStore |
| - var rerr *configReadError |
| - if errors.As(err, &rerr) { |
| - code = configRefusalRead |
| - } |
| - return &configRefusal{Code: code, Status: http.StatusBadGateway, Msg: "could not store the config — try again"} |
| + return &configRefusal{Code: configRefusalStore, Status: http.StatusBadGateway, Msg: "could not store the config — try again"} |
| } |
| // configCheckSealedTo checks each sealed var of a write against the key the |
diff --git a/internal/console/envcopy.go b/internal/console/envcopy.go| index a7747db48..e6dfb6c0b 100644 |
| --- a/internal/console/envcopy.go |
| +++ b/internal/console/envcopy.go |
| @@ -344,7 +344,6 @@ func (c envPageCopy) refusal(ref *configRefusal, fleet string) string { |
| configRefusalConflict: c.RefConflict, |
| configRefusalNothingToRemove: c.RefNothingToRemove, |
| configRefusalStore: c.RefStore, |
| - configRefusalRead: c.RefStore, |
| configRefusalSealKey: c.RefSealKey, |
| }[ref.Code] |
| if line == "" { |
diff --git a/internal/console/envformgate_test.go b/internal/console/envformgate_test.go| index 7880fb011..d1259fd85 100644 |
| --- a/internal/console/envformgate_test.go |
| +++ b/internal/console/envformgate_test.go |
| @@ -370,6 +370,13 @@ func (e *envFx) mustRefuseUnread(t *testing.T, status int, body string, posted . |
| t.Fatalf("status = %d, want 409:\n%s", status, envBanner(body)) |
| } |
| e.noWrites(t) |
| + mustShowUnread(t, body, posted...) |
| +} |
| + |
| +// mustShowUnread: the section is drawn unavailable, with no form and |
| +// nothing posted drawn back, and Try again is a plain reload. |
| +func mustShowUnread(t *testing.T, body string, posted ...string) { |
| + t.Helper() |
| mustContain(t, body, envUnreadLine, `/apps/shop/settings/environments">Try again</a>`) |
| mustNotContain(t, body, "data-env-form", `name="newenv.`, `name="fp.`, `name="env.`, `name="new.`, `name="val.`, |
| `name="op" value="save"`, `class="env-outcome"`) |
| @@ -550,45 +557,69 @@ func TestEnvFormGateOrderAndBudget(t *testing.T) { |
| t.Fatalf("staging was never rate-limited in %d posts", cliWriteBurst+3) |
| } |
| -// A bundle read that fails inside the write (after the page read every |
| -// bundle) is the same refusal as one at page assembly (Astra R3 P2-1): no |
| -// write, no form, nothing posted drawn back. A write that fails keeps the |
| -// draft, as before. |
| +// Controller ruling on the branch review: any storage failure during |
| +// Save (a read inside the write that fails or does not decode, or the |
| +// write itself) draws the section unavailable, with nothing posted drawn |
| +// back: no draft, no values, no envelopes, no fingerprints. Nothing was |
| +// written past a failed read. |
| func TestEnvSaveLateBundleReadRefused(t *testing.T) { |
| - for _, target := range []string{"default", "staging"} { |
| - t.Run(target, func(t *testing.T) { |
| - e := newEnvFx(t) |
| - if _, err := appconfig.ApplyEnvironment(context.Background(), e.fx.rel, e.fx.sqid, "shop", "staging", |
| - map[string]appconfig.Var{"LOG_LEVEL": {Kind: "env", Value: "debug"}}, nil); err != nil { |
| - t.Fatal(err) |
| + late := map[string]func(e *envFx, key string){ |
| + "read fails": func(e *envFx, key string) { e.failRead(key) }, |
| + "malformed json": func(e *envFx, key string) { |
| + // Straight to the store underneath the spy: not a write the |
| + // form made. |
| + ctx := context.Background() |
| + _, etag, err := e.spy.Store.Get(ctx, key) |
| + if err == nil { |
| + _, err = e.spy.Store.Put(ctx, key, []byte("{not json"), etag) |
| } |
| - e.saving() |
| - key := appconfig.Key(e.fx.sqid, "shop") |
| - if target != "default" { |
| - key = appconfig.EnvironmentKey(e.fx.sqid, "shop", target) |
| + if err != nil { |
| + panic(err) |
| } |
| - f := envFields(t, e.page(t)) |
| - env := e.seal(t, "late-read") |
| - f.Set("new."+target+".MODE", "fast-r3") |
| - f.Set("newenv."+target+".NEW_SECRET", env) |
| - f.Set("fp."+target+".NEW_SECRET", f.Get("sealed_to")) |
| - f.Set("bundle", target) |
| - configPutBeforeApply = func() { e.failRead(key) } |
| - t.Cleanup(func() { configPutBeforeApply = nil }) |
| - status, body := e.post(t, f, "save") |
| - e.mustRefuseUnread(t, status, body, "fast-r3", "MODE", "NEW_SECRET", env, f.Get("sealed_to")) |
| - }) |
| + }, |
| + } |
| + for name, spoil := range late { |
| + for _, target := range []string{"default", "staging"} { |
| + t.Run(name+"/"+target, func(t *testing.T) { |
| + e := newEnvFx(t) |
| + if _, err := appconfig.ApplyEnvironment(context.Background(), e.fx.rel, e.fx.sqid, "shop", "staging", |
| + map[string]appconfig.Var{"LOG_LEVEL": {Kind: "env", Value: "debug"}}, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + e.saving() |
| + key := appconfig.Key(e.fx.sqid, "shop") |
| + if target != "default" { |
| + key = appconfig.EnvironmentKey(e.fx.sqid, "shop", target) |
| + } |
| + f := envFields(t, e.page(t)) |
| + env := e.seal(t, "late-read") |
| + f.Set("new."+target+".MODE", "fast-r3") |
| + f.Set("newenv."+target+".NEW_SECRET", env) |
| + f.Set("fp."+target+".NEW_SECRET", f.Get("sealed_to")) |
| + f.Set("bundle", target) |
| + configPutBeforeApply = func() { spoil(e, key) } |
| + t.Cleanup(func() { configPutBeforeApply = nil }) |
| + status, body := e.post(t, f, "save") |
| + if status != http.StatusBadGateway { |
| + t.Fatalf("status = %d, want 502:\n%s", status, envBanner(body)) |
| + } |
| + e.noWrites(t) |
| + mustShowUnread(t, body, "fast-r3", "MODE", "NEW_SECRET", env, f.Get("sealed_to")) |
| + }) |
| + } |
| } |
| - t.Run("write failure keeps the draft", func(t *testing.T) { |
| + t.Run("write failure draws nothing back", func(t *testing.T) { |
| e := newEnvFx(t) // writes refused by the spy |
| f := envFields(t, e.page(t)) |
| + env := e.seal(t, "late-write") |
| f.Set("new.default.MODE", "fast-r3") |
| + f.Set("newenv.default.NEW_SECRET", env) |
| + f.Set("fp.default.NEW_SECRET", f.Get("sealed_to")) |
| f.Set("bundle", "default") |
| status, body := e.post(t, f, "save") |
| if status != http.StatusBadGateway { |
| - t.Fatalf("status = %d: %s", status, envBanner(body)) |
| + t.Fatalf("status = %d, want 502: %s", status, envBanner(body)) |
| } |
| - mustContain(t, body, `name="new.default.MODE" value="fast-r3"`, "data-env-form") |
| - mustNotContain(t, body, envUnreadLine) |
| + mustShowUnread(t, body, "fast-r3", "MODE", "NEW_SECRET", env, f.Get("sealed_to")) |
| }) |
| } |
diff --git a/internal/console/envsave.go b/internal/console/envsave.go| index 50d7231a9..60b9e6e8d 100644 |
| --- a/internal/console/envsave.go |
| +++ b/internal/console/envsave.go |
| @@ -165,13 +165,19 @@ func (s *Server) envFormSave(w http.ResponseWriter, r *http.Request, p page, acc |
| return |
| } |
| } |
| - if ref.Code == configRefusalRead { |
| - // The bundle did not read inside the write: nothing was written, |
| - // and, as for a read that failed when the page was assembled, the |
| - // section is unavailable and nothing posted is drawn back. |
| + if ref.Code == configRefusalStore { |
| + // Storage failed during the save: a read or a write, of any kind. |
| + // As for a read that failed when the page was assembled, the |
| + // section is unavailable and nothing posted is drawn back (no |
| + // draft, no values, no envelopes, no fingerprints). The one place |
| + // the form door handles a storage failure. |
| sv.Envs, sv.EnvsUnavailable = nil, true |
| sv.EnvDraft, sv.EnvSummary, sv.EnvError = nil, "", "" |
| - s.render(w, http.StatusConflict, "app_settings", p) |
| + status := ref.Status |
| + if status == 0 { |
| + status = http.StatusBadGateway |
| + } |
| + s.render(w, status, "app_settings", p) |
| return |
| } |
| if ref.Code == configRefusalConflict { |
diff --git a/internal/console/envsave_test.go b/internal/console/envsave_test.go| index db293a949..4b7bdb3a1 100644 |
| --- a/internal/console/envsave_test.go |
| +++ b/internal/console/envsave_test.go |
| @@ -6,6 +6,7 @@ package console |
| import ( |
| "context" |
| + "encoding/json" |
| "fmt" |
| "net/http" |
| "net/url" |
| @@ -248,16 +249,9 @@ func TestEnvSaveRefusalsPersonCopy(t *testing.T) { |
| if status != http.StatusBadGateway { |
| t.Fatalf("status = %d:\n%s", status, body) |
| } |
| - banner := envBanner(body) |
| - if banner != "Couldn't save just now. Nothing changed. Try again." { |
| - t.Fatalf("banner = %q", banner) |
| - } |
| - // The draft stays, to try again; the refusal itself carries nothing. |
| - mustContain(t, body, `name="new.default.MODE" value="fast"`, "2 changes in All environments") |
| - if strings.Contains(banner, env) || strings.Contains(banner, "fast") { |
| - t.Fatalf("banner carries content: %q", banner) |
| - } |
| - mustNotContain(t, body, `class="env-outcome"`) |
| + // Controller ruling: a storage failure on Save draws the section |
| + // unavailable and nothing posted back, the draft included. |
| + mustShowUnread(t, body, env, `name="new.default.MODE"`, "2 changes in All environments") |
| if m := e.stored(t); m.Vars["LOG_LEVEL"].Value != "info" || m.Vars["STRIPE_KEY"].Envelope != e.envelope || len(m.Vars) != 2 { |
| t.Fatalf("stored moved: %+v", m.Vars) |
| } |
| @@ -514,6 +508,9 @@ func TestEnvOutcomeMatchesCLIWire(t *testing.T) { |
| "yours": {hostsOf(wire.Yours), rowHosts(o.Yours)}, |
| "starting": {hostsOf(wire.Starting), rowHosts(o.Starting)}, |
| "unseen": {wire.Unseen, o.Unseen}, |
| + // Branch review P2-1: every classifier Unresolved host survives |
| + // the wire, not only the legacy predicates' subset. |
| + "unresolved": {hostsOf(wire.UnresolvedRoutes), o.Unresolved}, |
| } { |
| if !reflect.DeepEqual(pair[0], pair[1]) { |
| t.Errorf("%s: CLI %v, page %v", name, pair[0], pair[1]) |
| @@ -541,12 +538,50 @@ func TestEnvOutcomeMatchesCLIWire(t *testing.T) { |
| }) |
| t.Run("every bucket", func(t *testing.T) { |
| compare(t, appconfig.WriteReach{ |
| - Self: []appconfig.ReachHost{{Host: "a.example.com", Shape: appconfig.ReachShapeUnit}}, |
| - Yours: []appconfig.ReachHost{{Host: "b.example.com", How: appconfig.ReachHowAddr}, {Host: "c.example.com", How: appconfig.ReachHowSidecar}}, |
| - Starting: []appconfig.ReachHost{{Host: "d.example.com", Why: appconfig.ReachWhyAsleep}, {Host: "e.example.com", Why: appconfig.ReachWhyNotRunning}}, |
| - Unseen: []string{"f.example.com"}, |
| + Self: []appconfig.ReachHost{{Host: "a.example.com", Shape: appconfig.ReachShapeUnit}}, |
| + Yours: []appconfig.ReachHost{{Host: "b.example.com", How: appconfig.ReachHowAddr}, {Host: "c.example.com", How: appconfig.ReachHowSidecar}}, |
| + Starting: []appconfig.ReachHost{{Host: "d.example.com", Why: appconfig.ReachWhyAsleep}, {Host: "e.example.com", Why: appconfig.ReachWhyNotRunning}}, |
| + Unseen: []string{"f.example.com"}, |
| + Unresolved: []appconfig.ReachHost{{Host: "g.example.com"}}, |
| }) |
| }) |
| + // The classifier's Unresolved shapes the legacy notice does not name: |
| + // a route bound to a bundle that does not exist, and an exec route |
| + // whose config file name another route owns. Each reaches the wire's |
| + // unresolved_routes (and so the CLI) as it reaches the page. |
| + for name, rt := range map[string][]registry.Route{ |
| + "missing bound bundle": {{Host: "bound.shop.example.com", Kind: "instance", Status: "ready", App: "shop", Socket: "/run/carlos/bound.sock", Environment: "gone"}}, |
| + "config-name collision": { |
| + {Host: "clash.shop.example.com", Kind: "instance", Status: "ready", App: "shop", Socket: "/run/carlos/clash.sock"}, |
| + {Host: "clash.shop.example.com.config", Kind: "instance", Status: "ready", App: "other", Socket: "/run/carlos/other.sock"}, |
| + }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + e := envBucketsFx(t) |
| + reg, err := registry.Open(e.fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, r := range rt { |
| + r.Account = e.fx.sqid |
| + if err := reg.Upsert(r); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + reach, ok := e.fx.srv.classifyConfigWrite(context.Background(), e.fx.sqid, "shop", "", []string{"LOG_LEVEL"}) |
| + if !ok || len(reach.Unresolved) != 1 || reach.Unresolved[0].Host != rt[0].Host { |
| + t.Fatalf("classification = %+v %v, want %s unresolved", reach, ok, rt[0].Host) |
| + } |
| + compare(t, reach) |
| + raw, err := json.Marshal(reachWire(reach)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := `"unresolved_routes":[{"host":"` + rt[0].Host + `"}]`; !strings.Contains(string(raw), want) { |
| + t.Fatalf("wire = %s, want %s", raw, want) |
| + } |
| + }) |
| + } |
| } |
| // Asleep reports whether the row is an asleep one (TestEnvOutcomeMatchesCLIWire). |