console: D1 refuses a selected release it cannot read (M2 review P2)
readChannelHeadChecked tells no selected release (no pointer, or one naming no version) from one it could not read (a pointer or manifest read that failed, or a pointer that does not verify). The create door passes the error to fleetCreateLabel, which answers C39 (503, Retry-After 30) before any write. readChannelHead and its other callers are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+103
−18
internal/console/apiinstances.go+9 −4internal/console/apiinstances_test.go+58 −3internal/console/fleetcreate.go+11 −5internal/console/rootfs.go+25 −6
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go| index 4f68ff250..bbe145a0e 100644 |
| --- a/internal/console/apiinstances.go |
| +++ b/internal/console/apiinstances.go |
| @@ -2146,10 +2146,15 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| } |
| // The channel's head, read once (pointer, then manifest) for the two |
| // checks that ask it: an exec-shaped create is the only one either |
| - // asks about. nil when it was not read or could not be. |
| - var chHead *channelHead |
| + // asks about. nil when it was not read or could not be; chHeadErr says |
| + // it could not be (a pointer or manifest read that failed), which only |
| + // D1 asks (fleetCreateLabel refuses it, M2 review P2). |
| + var ( |
| + chHead *channelHead |
| + chHeadErr error |
| + ) |
| if kind := strings.TrimSpace(req.Kind); kind == "" || kind == "instance" { |
| - chHead = s.readChannelHead(ctx, acct.Sqid, app, channel) |
| + chHead, chHeadErr = s.readChannelHeadChecked(ctx, acct.Sqid, app, channel) |
| } |
| if ref := staticHeadRefusal(chHead, app, channel, strings.TrimSpace(req.Kind), host); ref != nil { |
| jsonError(w, ref.Status, ref.Msg) |
| @@ -2533,7 +2538,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request) |
| // record the platform cannot move (rule 11), no box to choose and a mark |
| // that fails all leave the record unlabelled. |
| if fleetName != "" && s.fleetCreateApplies(fleetSeen, fleetErr, rec) { |
| - if ref := s.fleetCreateLabel(ctx, reg, fleetName, fleetSeen, fleetErr, &rec, chHead); ref != nil { |
| + if ref := s.fleetCreateLabel(ctx, reg, fleetName, fleetSeen, fleetErr, &rec, chHead, chHeadErr); ref != nil { |
| if ref.Msg == msgFleetCreateUnavailable(fleetName) { |
| w.Header().Set("Retry-After", fleetCreateRetryAfter) // C39 only |
| } |
diff --git a/internal/console/apiinstances_test.go b/internal/console/apiinstances_test.go| index b0012ebf0..164826c1f 100644 |
| --- a/internal/console/apiinstances_test.go |
| +++ b/internal/console/apiinstances_test.go |
| @@ -3233,7 +3233,7 @@ func TestCreateRefusesAlwaysOnStatefulOnMultiBox(t *testing.T) { |
| c.mut(&rec) |
| } |
| v, verr := fx.srv.loadFleetView(ctx, "pi") |
| - ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, head) |
| + ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, head, nil) |
| want := "a.example.com cannot run on a fleet with more than one box: " + c.reason |
| if ref == nil || ref.Status != http.StatusConflict || ref.Msg != want { |
| t.Fatalf("refusal %+v; want 409 %q", ref, want) |
| @@ -3264,7 +3264,7 @@ func TestCreateLabelsAlwaysOnStateless(t *testing.T) { |
| t.Fatal("an always-on binary is movable without its manifest") |
| } |
| v, verr := fx.srv.loadFleetView(ctx, "pi") |
| - if ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, head); ref != nil { |
| + if ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, head, nil); ref != nil { |
| t.Fatalf("refusal %+v; want labelled", ref) |
| } |
| wantLabel(t, &rec, "pi-1") |
| @@ -3375,6 +3375,61 @@ func TestCreateRefusesATamperedManifest(t *testing.T) { |
| } |
| } |
| +// getFailStore fails the first n Gets of key (every one when n < 0) with |
| +// a store error that is not "does not exist". |
| +type getFailStore struct { |
| + release.Store |
| + key string |
| + n int |
| +} |
| + |
| +func (g *getFailStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if key == g.key && g.n != 0 { |
| + g.n-- |
| + return nil, "", errors.New("injected read failure") |
| + } |
| + return g.Store.Get(ctx, key) |
| +} |
| + |
| +// M2 review P2 (Astra P2, Fable P3-3): a selected release D1 cannot read |
| +// is not judged as no release. A channel pointer or a manifest read that |
| +// fails is C39 (503, Retry-After 30) with nothing written or marked, as |
| +// the tampered manifest is. The manifest fails once, at the head's read: |
| +// a manifest that cannot be read at all already fails the rootfs check |
| +// (C08, 502) before D1. |
| +func TestCreateRefusesAnUnreadableSelectedRelease(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + key func(sqid string) string |
| + n int |
| + }{ |
| + {"pointer", func(sqid string) string { return release.ChannelKey(sqid, "shop", "edge") }, -1}, |
| + {"manifest", func(sqid string) string { return release.ManifestKey(sqid, "shop", "v1") }, 1}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + fx, token, reg := d1World(t, "code-d1-unread-"+c.name, "pi-1", "pi-2") |
| + shipVersion(t, fx, "shop", "v1") |
| + seedChannel(t, fx, "shop", "edge", "v1") |
| + fx.forgetFrame("shop") |
| + cs := newCountStore(fx.srv.cfg.Releases) |
| + fx.srv.cfg.Releases = &getFailStore{Store: cs, key: c.key(fx.sqid), n: c.n} |
| + code, hdr, out := rawReq(t, http.MethodPost, instancesURL(fx, fx.sqid, "shop"), token, `{"host":"a.example.com","environment":"prod"}`) |
| + if code != http.StatusServiceUnavailable || hdr.Get("Retry-After") != "30" || errorOf(t, out) != fleetCreateC39 { |
| + t.Fatalf("create = %d (Retry-After %q) %s; want 503, Retry-After 30 and C39", code, hdr.Get("Retry-After"), out) |
| + } |
| + if n := cs.putsAll(); n != 0 { |
| + t.Fatalf("a refused create made %d writes: %v", n, cs.puts) |
| + } |
| + noShopRecord(t, fx, "a.example.com") |
| + for _, b := range []string{"pi-1", "pi-2"} { |
| + if boxRow(t, reg, b).LabelledAt != 0 { |
| + t.Fatalf("%s marked labelled", b) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| // Fix round 1: a unit-backed record is refused with C42 even with no |
| // manifest at all (no release shipped yet). Since M2 review P1 movable |
| // refuses it too; D1 keeps its own early answer. |
| @@ -3384,7 +3439,7 @@ func TestCreateRefusesAUnitWithNoManifest(t *testing.T) { |
| rec := provision.Record{Schema: 1, Host: "a.example.com", Slug: "a", App: "shop", Account: fx.sqid, |
| Region: "fleet/pi", Channel: "edge", CreatedAt: time.Now().UTC(), Unit: "shop.service"} |
| v, verr := fx.srv.loadFleetView(ctx, "pi") |
| - ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, nil) |
| + ref := fx.srv.fleetCreateLabel(ctx, reg, "pi", v, verr, &rec, nil, nil) |
| want := "a.example.com cannot run on a fleet with more than one box: it runs as a system unit" |
| if ref == nil || ref.Status != http.StatusConflict || ref.Msg != want { |
| t.Fatalf("refusal %+v; want 409 %q", ref, want) |
diff --git a/internal/console/fleetcreate.go b/internal/console/fleetcreate.go| index 06c575163..e4af764a0 100644 |
| --- a/internal/console/fleetcreate.go |
| +++ b/internal/console/fleetcreate.go |
| @@ -69,10 +69,12 @@ func (s *Server) fleetCreateApplies(v fleetView, verr error, rec provision.Recor |
| // fleetCreateLabel is D1's decision for rec on fleet, whose view (v, verr) |
| // the caller read under the fleet's lock and still holds. head is the |
| // channel head the create follows (nil when there is none yet); only its |
| -// version is trusted. In order: a unit-backed record is C42 (409), whatever |
| -// its manifest; the head's manifest is read again and verified against its |
| -// signed promotion record (release.AuthorizedManifest), and one that cannot |
| -// be is C39 (503); a record the platform cannot place or move, judged with |
| +// version is trusted. headErr is the head's read failure |
| +// (readChannelHeadChecked): a selected release that could not be read is |
| +// not one that does not exist. In order: a unit-backed record is C42 (409), |
| +// whatever its manifest; a head that could not be read is C39 (503); the |
| +// head's manifest is read again and verified against its signed promotion |
| +// record (release.AuthorizedManifest), and one that cannot be is C39; a record the platform cannot place or move, judged with |
| // that verified manifest, is C42; then an incomplete view, sidecars on the fleet, any active box or the console |
| // below release.FleetRecordLabelFloor (fleetAtRecordFloor, the label |
| // door's check), no destination, and a failed mark are C39 (503), with the |
| @@ -80,7 +82,7 @@ func (s *Server) fleetCreateApplies(v fleetView, verr error, rec provision.Recor |
| // Otherwise the box is marked labelled and rec carries it (FleetBox, |
| // FleetBoxAt). A new record has no data, so it is not FleetBoxMoved: an |
| // empty first start is right for it. |
| -func (s *Server) fleetCreateLabel(ctx context.Context, reg consoleRegistry, fleet string, v fleetView, verr error, rec *provision.Record, head *channelHead) *doorRefusal { |
| +func (s *Server) fleetCreateLabel(ctx context.Context, reg consoleRegistry, fleet string, v fleetView, verr error, rec *provision.Record, head *channelHead, headErr error) *doorRefusal { |
| unavailable := &doorRefusal{Status: http.StatusServiceUnavailable, Msg: msgFleetCreateUnavailable(fleet)} |
| tag := fmt.Sprintf("console: api instances create %s/%s/%s: fleet %s", rec.Account, rec.App, rec.Host, fleet) |
| // A system unit is never placed on one box. movable refuses it too |
| @@ -88,6 +90,10 @@ func (s *Server) fleetCreateLabel(ctx context.Context, reg consoleRegistry, flee |
| if rec.Unit != "" { |
| return &doorRefusal{Status: http.StatusConflict, Msg: msgFleetCreateUnplaceable(rec.Host, unmovableReasonText["unit"])} |
| } |
| + if headErr != nil { |
| + log.Printf("%s: selected release cannot be read (%v); refused", tag, headErr) |
| + return unavailable |
| + } |
| // The head's manifest was read without its digest checked |
| // (readChannelHead): only the verified one decides eligibility and the |
| // stateless destination. |
diff --git a/internal/console/rootfs.go b/internal/console/rootfs.go| index e0e4a7dae..8a1b56fdf 100644 |
| --- a/internal/console/rootfs.go |
| +++ b/internal/console/rootfs.go |
| @@ -8,6 +8,7 @@ import ( |
| "log" |
| "maps" |
| "net/http" |
| + "os" |
| "slices" |
| "time" |
| @@ -184,18 +185,36 @@ type channelHead struct { |
| // callers decide what an unread head means (staticHeadRefusal passes it, |
| // rootfsHeadFrom falls back to the app's history). |
| func (s *Server) readChannelHead(ctx context.Context, sqid, app, channel string) *channelHead { |
| + head, _ := s.readChannelHeadChecked(ctx, sqid, app, channel) |
| + return head |
| +} |
| + |
| +// readChannelHeadChecked is readChannelHead that tells "no selected |
| +// release" from "could not read it" (M2 review P2): (nil, nil) when there |
| +// is no store, no pointer, or a pointer naming no version; (nil, err) when |
| +// the pointer cannot be read or verified, or the manifest it names cannot |
| +// be read. D1 refuses on the error; the other callers keep nil. |
| +func (s *Server) readChannelHeadChecked(ctx context.Context, sqid, app, channel string) (*channelHead, error) { |
| if s.cfg.Releases == nil { |
| - return nil |
| + return nil, nil |
| } |
| ptr, err := release.Channel(ctx, s.cfg.Releases, s.cfg.ReleasePub, sqid, app, channel) |
| - if err != nil || ptr == nil || ptr.Version == "" { |
| - return nil |
| + switch { |
| + case errors.Is(err, os.ErrNotExist): |
| + return nil, nil |
| + case err != nil: |
| + return nil, fmt.Errorf("channel %s: %w", channel, err) |
| + case ptr == nil || ptr.Version == "": |
| + return nil, nil |
| } |
| m, err := release.GetManifest(ctx, s.cfg.Releases, sqid, app, ptr.Version) |
| - if err != nil || m == nil { |
| - return nil |
| + if err != nil { |
| + return nil, fmt.Errorf("manifest %s: %w", ptr.Version, err) |
| + } |
| + if m == nil { |
| + return nil, fmt.Errorf("manifest %s: none", ptr.Version) |
| } |
| - return &channelHead{Version: ptr.Version, Manifest: m} |
| + return &channelHead{Version: ptr.Version, Manifest: m}, nil |
| } |
| // rootfsHeadFrom is rootfsHeadFor on a head already read (Astra r1 P2-2: |