carlos / platform Public

console: the rootfs floor binds the pointer snapshot it judged (whole-branch P1-3, P2-4)

rootfsFloorGate returns the ETag of the pointer it judged; promote and
rollback pass it to the preparation on the reserved path and the
unreserved fallback, so a channel that moved in between fails the CAS.
PrepareSystemPromotion and PromoteSystem take an optional expected ETag.
RollbackSystem takes the full options, so the unreserved fallback signs
the operator's override.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3c087d8e9c3485fa8d77815de65edda5831b7bcd parent d35fcb1
6 files changed, +192 −28
  • internal/console/apisystemupdate.go +1 −1
  • internal/console/hostactivation.go +13 −7
  • internal/console/rootfsfloor.go +16 −4
  • internal/console/rootfsfloor_test.go +101 −2
  • internal/release/systempointer.go +25 −11
  • internal/release/systempointer_test.go +36 −3
diff --git a/internal/console/apisystemupdate.go b/internal/console/apisystemupdate.go
index 9941ebe2f..8bbe83abf 100644
--- a/internal/console/apisystemupdate.go
+++ b/internal/console/apisystemupdate.go
@@ -403,7 +403,7 @@ func (s *Server) handleAPISystemUpdate(w http.ResponseWriter, r *http.Request) {
// again before it reserves or writes (promoteHostUpdate), which also
// covers the latest release and an upload.
if in.Version != "" {
- if err := s.rootfsFloorGate(r.Context(), in.Component, in.Channel, in.Version, in.AllowBelowRootfsFloor); errors.Is(err, errRootfsFloorPromotion) {
+ if _, err := s.rootfsFloorGate(r.Context(), in.Component, in.Channel, in.Version, in.AllowBelowRootfsFloor); errors.Is(err, errRootfsFloorPromotion) {
jsonError(w, http.StatusConflict, err.Error())
return
}
diff --git a/internal/console/hostactivation.go b/internal/console/hostactivation.go
index 98587ddc3..d0ea8b871 100644
--- a/internal/console/hostactivation.go
+++ b/internal/console/hostactivation.go
@@ -145,7 +145,7 @@ func (s *Server) reserveHostUpdate(ctx context.Context, rel *release.SystemRelea
if mode == "rollback" {
publication, err = release.PrepareSystemRollback(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), "router", channel, options, id, expectedETag...)
} else {
- publication, err = release.PrepareSystemPromotion(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), "router", channel, *rel, options, id)
+ publication, err = release.PrepareSystemPromotion(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), "router", channel, *rel, options, id, expectedETag...)
}
if err != nil {
return "", err
@@ -199,15 +199,18 @@ func (s *Server) reserveHostUpdate(ctx context.Context, rel *release.SystemRelea
}
func (s *Server) promoteHostUpdate(ctx context.Context, rel release.SystemRelease, component, channel string, options release.SystemPromoteOptions) (*release.SystemPointer, error) {
- if err := s.rootfsFloorGate(ctx, component, channel, rel.Version, options.AllowBelowRootfsFloor); err != nil {
+ // The floor decision binds the snapshot it judged: preparation, here
+ // or in the unreserved fallback, refuses a pointer that moved since.
+ etag, err := s.rootfsFloorGate(ctx, component, channel, rel.Version, options.AllowBelowRootfsFloor)
+ if err != nil {
return nil, err
}
- id, err := s.reserveHostUpdate(ctx, &rel, channel, "promote", options)
+ id, err := s.reserveHostUpdate(ctx, &rel, channel, "promote", options, etag)
if err != nil {
return nil, err
}
if id == "" {
- return release.PromoteSystem(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), component, channel, rel, options)
+ return release.PromoteSystem(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), component, channel, rel, options, etag)
}
d := hostactors.Directory{Store: s.cfg.Releases, PublicKey: release.RootSigner(s.cfg.Key).RootPublicKey(), Bucket: s.cfg.Bucket}
return d.PublishActivation(ctx, id)
@@ -251,15 +254,18 @@ func (s *Server) rollbackHostUpdate(ctx context.Context, component, channel, act
return nil, release.ErrNoSystemHistory
}
target := current.History[0]
- if err := s.rootfsFloorGate(ctx, component, channel, target.Version, allowBelowRootfsFloor); err != nil {
+ // The floor judges the snapshot the target came from, and both
+ // publication paths carry its ETag and the full options (P1-3, P2-4).
+ if err := rootfsFloorJudge(channel, current, target.Version, allowBelowRootfsFloor); err != nil {
return nil, err
}
- id, err := s.reserveHostUpdate(ctx, &target, channel, "rollback", release.SystemPromoteOptions{Actor: actor, AllowBelowRootfsFloor: allowBelowRootfsFloor}, etag)
+ options := release.SystemPromoteOptions{Actor: actor, AllowBelowRootfsFloor: allowBelowRootfsFloor}
+ id, err := s.reserveHostUpdate(ctx, &target, channel, "rollback", options, etag)
if err != nil {
return nil, err
}
if id == "" {
- return release.RollbackSystem(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), component, channel, actor, etag)
+ return release.RollbackSystem(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key), component, channel, options, etag)
}
return d.PublishActivation(ctx, id)
}
diff --git a/internal/console/rootfsfloor.go b/internal/console/rootfsfloor.go
index f91191d21..ef7a7dd03 100644
--- a/internal/console/rootfsfloor.go
+++ b/internal/console/rootfsfloor.go
@@ -40,14 +40,26 @@ func (e rootfsFloorError) Unwrap() error { return errRootfsFloorPromotion }
// it or does not parse; allow is the operator's override, which is logged.
// A channel with no pointer yet has nothing to protect. A pointer that
// cannot be read refuses: the promotion cannot tell what it would replace.
-func (s *Server) rootfsFloorGate(ctx context.Context, component, channel, target string, allow bool) error {
- cur, _, err := release.GetSystemPointer(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key).RootPublicKey(), component, channel)
+//
+// It returns the ETag of the pointer snapshot it judged ("" for none). A
+// caller that goes on to publish passes it as the preparation's expected
+// ETag, so the decision covers exactly the state the publication replaces:
+// a channel that moved in between (say to the floor) fails the CAS, never
+// takes a release nobody checked against it (whole-branch review P1-3).
+func (s *Server) rootfsFloorGate(ctx context.Context, component, channel, target string, allow bool) (string, error) {
+ cur, etag, err := release.GetSystemPointer(ctx, s.cfg.Releases, release.RootSigner(s.cfg.Key).RootPublicKey(), component, channel)
if errors.Is(err, os.ErrNotExist) {
- return nil
+ return etag, nil
}
if err != nil {
- return err
+ return "", err
}
+ return etag, rootfsFloorJudge(channel, cur, target, allow)
+}
+
+// rootfsFloorJudge is rootfsFloorGate's decision on a pointer already read
+// (rollbackHostUpdate judges the snapshot its target came from).
+func rootfsFloorJudge(channel string, cur *release.SystemPointer, target string, allow bool) error {
if release.RootfsFloorAllows(cur.Current.Version, target) {
return nil
}
diff --git a/internal/console/rootfsfloor_test.go b/internal/console/rootfsfloor_test.go
index 9b749d140..d90d29478 100644
--- a/internal/console/rootfsfloor_test.go
+++ b/internal/console/rootfsfloor_test.go
@@ -1,10 +1,12 @@
package console
import (
+ "context"
"errors"
"testing"
"github.com/carlosframework/platform/internal/hostactors"
+ "github.com/carlosframework/platform/internal/hostfloor"
"github.com/carlosframework/platform/internal/release"
)
@@ -49,13 +51,13 @@ func TestRootfsFloorRefusesBeforeReservation(t *testing.T) {
t.Fatalf("the pointer moved to %s", v)
}
for target, refused := range map[string]bool{"4f3c2a1": true, "": true, "v0.28.99": true, "v0.29.0": false, "v0.30.1": false} {
- err := s.rootfsFloorGate(t.Context(), "router", "stable", target, false)
+ _, err := s.rootfsFloorGate(t.Context(), "router", "stable", target, false)
if errors.Is(err, errRootfsFloorPromotion) != refused {
t.Errorf("rootfsFloorGate(%q) = %v, refused want %v", target, err, refused)
}
}
// A channel with no pointer yet has nothing to protect.
- if err := s.rootfsFloorGate(t.Context(), "router", "canary", "v0.28.25", false); err != nil {
+ if _, err := s.rootfsFloorGate(t.Context(), "router", "canary", "v0.28.25", false); err != nil {
t.Errorf("a channel with no pointer = %v", err)
}
@@ -126,3 +128,100 @@ func TestRootfsFloorOverrideIsPartOfTheRequest(t *testing.T) {
}
}
}
+
+// interleavingStore runs between once, just before the second read of the
+// stable pointer: after the console's floor check read it and before the
+// preparation reads it again.
+type interleavingStore struct {
+ release.Store
+ reads int
+ between func()
+}
+
+func (s *interleavingStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ if key == release.SystemPointerKey("router", "stable") {
+ s.reads++
+ if s.reads == 2 && s.between != nil {
+ s.between()
+ }
+ }
+ return s.Store.Get(ctx, key)
+}
+
+// unreserved removes the host floor and its authorization, the legacy mode
+// in which promotions publish without an activation reservation.
+func unreserved(t *testing.T, d hostactors.Directory) {
+ t.Helper()
+ for _, key := range []string{hostfloor.Key, hostfloor.AuthKey} {
+ if err := d.Store.Delete(t.Context(), key); err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
+// [Whole-branch review P1-3] The floor decision binds the pointer
+// snapshot it judged. A below-floor request checks against v0.28.20 and
+// passes; another console then moves the channel to v0.29.0; the request
+// resumes and must not publish over the newer pointer. Promote and
+// rollback, through an activation reservation and through the unreserved
+// fallback.
+func TestRootfsFloorBindsTheJudgedPointer(t *testing.T) {
+ for _, mode := range []string{"promote", "rollback"} {
+ for _, reserved := range []bool{true, false} {
+ name := mode + "/unreserved"
+ if reserved {
+ name = mode + "/reserved"
+ }
+ t.Run(name, func(t *testing.T) {
+ s, d, target := activationFixtureAt(t, "v0.28.20", "v0.28.25", "v0.28.0")
+ if !reserved {
+ unreserved(t, d)
+ }
+ floor := target
+ floor.Version, floor.ReleaseTag, floor.Source = "v0.29.0", "v0.29.0", "release:v0.29.0"
+ floor.Artifacts = map[string]release.SystemArtifact{"linux-amd64": {Key: release.SystemArtifactKey("v0.29.0", "linux-amd64"), SHA256: target.Artifacts["linux-amd64"].SHA256}}
+ promoted := false
+ s.cfg.Releases = &interleavingStore{Store: d.Store, between: func() {
+ if _, err := release.PromoteSystem(context.Background(), d.Store, release.RootSigner(s.cfg.Key), "router", "stable", floor, release.SystemPromoteOptions{}); err != nil {
+ t.Errorf("the other console's promotion: %v", err)
+ }
+ promoted = true
+ }}
+ var err error
+ if mode == "rollback" {
+ _, err = s.rollbackHostUpdate(t.Context(), "router", "stable", "operator", false)
+ } else {
+ _, err = s.promoteHostUpdate(t.Context(), target, "router", "stable", release.SystemPromoteOptions{})
+ }
+ if !promoted {
+ t.Fatal("the interleaving never ran: the request read the pointer once")
+ }
+ if !errors.Is(err, release.ErrCASConflict) {
+ t.Fatalf("the resumed request = %v, want a CAS conflict", err)
+ }
+ if v := currentVersion(t, s, d); v != "v0.29.0" {
+ t.Fatalf("the resumed request published over v0.29.0: the pointer is at %s", v)
+ }
+ noActivations(t, d)
+ })
+ }
+ }
+}
+
+// [Whole-branch review P2-4] An unreserved rollback carries the operator's
+// override onto the signed pointer, as the reserved one does.
+func TestRootfsFloorUnreservedRollbackKeepsTheOverride(t *testing.T) {
+ s, d, _ := activationFixtureAt(t, "v0.29.0", "v0.28.25", "v0.28.0")
+ unreserved(t, d)
+ p, err := s.rollbackHostUpdate(t.Context(), "router", "stable", "operator", true)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if p.Current.Version != "v0.28.25" || !p.AllowBelowRootfsFloor {
+ t.Fatalf("the unreserved rollback published %+v, want v0.28.25 with the override", p)
+ }
+ signed, _, err := release.GetSystemPointer(t.Context(), d.Store, d.PublicKey, "router", "stable")
+ if err != nil || !signed.AllowBelowRootfsFloor {
+ t.Fatalf("the signed pointer lost the override: %+v, %v", signed, err)
+ }
+}
diff --git a/internal/release/systempointer.go b/internal/release/systempointer.go
index 3fd4b4efb..18fd72bd4 100644
--- a/internal/release/systempointer.go
+++ b/internal/release/systempointer.go
@@ -266,29 +266,41 @@ type SystemPromoteOptions struct {
// PromoteSystem writes the pointer for one channel, CAS-guarded. The
// outgoing release goes whole into History unless it is the same version
// (a re-promote of the running version is a no-op for boxes and must not
-// push that version into its own history).
-func PromoteSystem(ctx context.Context, s Store, sg Signer, component, channel string, rel SystemRelease, o SystemPromoteOptions) (*SystemPointer, error) {
- prepared, err := PrepareSystemPromotion(ctx, s, sg, component, channel, rel, o, "")
+// push that version into its own history). expectedETag, when given, is
+// the ETag of the pointer snapshot the caller judged, as in
+// PrepareSystemPromotion.
+func PromoteSystem(ctx context.Context, s Store, sg Signer, component, channel string, rel SystemRelease, o SystemPromoteOptions, expectedETag ...string) (*SystemPointer, error) {
+ prepared, err := PrepareSystemPromotion(ctx, s, sg, component, channel, rel, o, "", expectedETag...)
if err != nil {
return nil, err
}
return PublishSystemPublication(ctx, s, sg.verifyPub(), prepared)
}
-func PrepareSystemPromotion(ctx context.Context, s Store, sg Signer, component, channel string, rel SystemRelease, o SystemPromoteOptions, id string) (SystemPublication, error) {
+// PrepareSystemPromotion signs the pointer that moves channel to rel over
+// the one it reads now. expectedETag, when given, binds the preparation to
+// the pointer snapshot the caller judged (the console's rootfs floor check;
+// "" means no pointer): a pointer that moved since is ErrCASConflict, never
+// a publication over a state nobody checked (whole-branch review P1-3).
+func PrepareSystemPromotion(ctx context.Context, s Store, sg Signer, component, channel string, rel SystemRelease, o SystemPromoteOptions, id string, expectedETag ...string) (SystemPublication, error) {
key := SystemPointerKey(component, channel)
var old *SystemPointer
body, etag, err := s.Get(ctx, key)
switch {
case err == nil:
- if old, err = verifySystemPointer(sg.verifyPub(), body, component, channel); err != nil {
- return SystemPublication{}, err
- }
case errors.Is(err, os.ErrNotExist):
- etag = ""
+ body, etag = nil, ""
default:
return SystemPublication{}, err
}
+ if len(expectedETag) > 1 || (len(expectedETag) == 1 && etag != expectedETag[0]) {
+ return SystemPublication{}, ErrCASConflict
+ }
+ if body != nil {
+ if old, err = verifySystemPointer(sg.verifyPub(), body, component, channel); err != nil {
+ return SystemPublication{}, err
+ }
+ }
if old != nil && old.CancellationID != "" && old.Current.Version == "" {
old = nil
}
@@ -320,9 +332,11 @@ func PrepareSystemPromotion(ctx context.Context, s Store, sg Signer, component,
// back another one", and repeating it walks the history down.
//
// It writes a NEW pointer — new PromotedAt — so every box's replay guard
-// sees a promote, not a replay.
-func RollbackSystem(ctx context.Context, s Store, sg Signer, component, channel, actor string, expectedETag ...string) (*SystemPointer, error) {
- prepared, err := PrepareSystemRollback(ctx, s, sg, component, channel, SystemPromoteOptions{Actor: actor}, "", expectedETag...)
+// sees a promote, not a replay. o carries the actor and the rootfs floor
+// override, which is signed onto the pointer as on every other path
+// (whole-branch review P2-4).
+func RollbackSystem(ctx context.Context, s Store, sg Signer, component, channel string, o SystemPromoteOptions, expectedETag ...string) (*SystemPointer, error) {
+ prepared, err := PrepareSystemRollback(ctx, s, sg, component, channel, o, "", expectedETag...)
if err != nil {
return nil, err
}
diff --git a/internal/release/systempointer_test.go b/internal/release/systempointer_test.go
index 42d258c26..a48e45ea8 100644
--- a/internal/release/systempointer_test.go
+++ b/internal/release/systempointer_test.go
@@ -69,7 +69,7 @@ func TestSystemPromoteKeepsWholeReleaseInHistoryAndRollbackRestoresIt(t *testing
t.Fatalf("history must carry the whole release: %+v", p.History)
}
time.Sleep(1100 * time.Millisecond)
- rb, err := RollbackSystem(ctx, s, sg, "router", "canary", "paul")
+ rb, err := RollbackSystem(ctx, s, sg, "router", "canary", SystemPromoteOptions{Actor: "paul"})
if err != nil {
t.Fatal(err)
}
@@ -82,7 +82,7 @@ func TestSystemPromoteKeepsWholeReleaseInHistoryAndRollbackRestoresIt(t *testing
if len(rb.History) != 0 {
t.Fatalf("history after rollback = %+v", rb.History)
}
- if _, err := RollbackSystem(ctx, s, sg, "router", "canary", "paul"); !errors.Is(err, ErrNoSystemHistory) {
+ if _, err := RollbackSystem(ctx, s, sg, "router", "canary", SystemPromoteOptions{Actor: "paul"}); !errors.Is(err, ErrNoSystemHistory) {
t.Fatalf("second rollback err = %v", err)
}
}
@@ -229,7 +229,7 @@ func TestSystemPointerCarriesTheRootfsFloorOverrideForOneWrite(t *testing.T) {
if err != nil || !got.AllowBelowRootfsFloor {
t.Fatalf("the verified pointer lost the override: %+v, %v", got, err)
}
- if _, err := RollbackSystem(ctx, s, RootSigner(priv), SystemComponentRouter, "stable", "paul"); err != nil {
+ if _, err := RollbackSystem(ctx, s, RootSigner(priv), SystemComponentRouter, "stable", SystemPromoteOptions{Actor: "paul"}); err != nil {
t.Fatal(err)
}
if got, _, err := GetSystemPointer(ctx, s, pub, SystemComponentRouter, "stable"); err != nil || got.AllowBelowRootfsFloor {
@@ -242,3 +242,36 @@ func TestSystemPointerCarriesTheRootfsFloorOverrideForOneWrite(t *testing.T) {
t.Fatalf("a later promote kept the override: %+v, %v", got, err)
}
}
+
+// [Whole-branch review P1-3, P2-4] A promotion bound to the snapshot its
+// caller judged refuses a pointer that moved since ("" names no pointer),
+// and a rollback signs the override it is given.
+func TestSystemPromotionBindsTheExpectedETag(t *testing.T) {
+ priv, pub := testKeys(t)
+ s := NewDirStore(t.TempDir())
+ ctx := context.Background()
+ sg := RootSigner(priv)
+ if _, err := PromoteSystem(ctx, s, sg, "router", "stable", testSystemRelease("v0.28.20"), SystemPromoteOptions{}, ""); err != nil {
+ t.Fatalf("a first promotion expecting no pointer: %v", err)
+ }
+ _, judged, err := GetSystemPointer(ctx, s, pub, "router", "stable")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := PromoteSystem(ctx, s, sg, "router", "stable", testSystemRelease("v0.29.0"), SystemPromoteOptions{}); err != nil {
+ t.Fatal(err)
+ }
+ for _, expected := range []string{judged, ""} {
+ if _, err := PromoteSystem(ctx, s, sg, "router", "stable", testSystemRelease("v0.28.25"), SystemPromoteOptions{}, expected); !errors.Is(err, ErrCASConflict) {
+ t.Fatalf("a promotion expecting %q over a moved pointer = %v, want ErrCASConflict", expected, err)
+ }
+ }
+ got, current, err := GetSystemPointer(ctx, s, pub, "router", "stable")
+ if err != nil || got.Current.Version != "v0.29.0" {
+ t.Fatalf("the pointer moved: %+v, %v", got, err)
+ }
+ p, err := RollbackSystem(ctx, s, sg, "router", "stable", SystemPromoteOptions{Actor: "paul", AllowBelowRootfsFloor: true}, current)
+ if err != nil || p.Current.Version != "v0.28.20" || !p.AllowBelowRootfsFloor {
+ t.Fatalf("the rollback with the override = %+v, %v", p, err)
+ }
+}