console: write the fleet owner record a fleet box proves rootfs ownership with (rootfs spec §5.7, Plan 4 D7)
The fleet create door writes accounts/<acct>/fleets/<fleet>.json create-only after the registry insert; a failed write answers the existing failed-create 500 and the retry repairs it (Astra R1 finding 13): only the caller's own row is read or written, a stranger's create stays today's 409 and touches no record. The repaired answer lists the fleet's boxes (5a.5). A record naming anything else is never overwritten. Each console start backfills every fleet in a bounded goroutine after the claim runtime is configured (5a.4). hostops: the fleet-owner family admits only a create-only Put of exactly that key whose body strictly names the key's account and fleet; no Delete arm (5a.3). Record-write census entries layered on nearestRegionBWriteCensusInventory (5a.2).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+876
−12
cmd/console/main.go+11 −0internal/console/fleetowner.go+142 −0internal/console/fleetowner_test.go+484 −0internal/console/fleets.go+56 −11internal/hostops/fleetowner_classifier.go+49 −0internal/hostops/fleetowner_classifier_test.go+84 −0internal/hostops/mutation_classifier.go+3 −0internal/provision/plan9_b8_census_test.go+1 −1internal/provision/rootfs_apps_4_census_test.go+46 −0
diff --git a/cmd/console/main.go b/cmd/console/main.go| index 614159ac3..9aee7b747 100644 |
| --- a/cmd/console/main.go |
| +++ b/cmd/console/main.go |
| @@ -652,6 +652,17 @@ func serve() { |
| if err := srv.ConfigureClaimRuntime(context.Background()); err != nil { |
| log.Fatalf("console: claim recovery: %v", err) |
| } |
| + // The fleet owner record backfill (rootfs apps Plan 4 D7): once per |
| + // start, every fleet's accounts/<acct>/fleets/<fleet>.json, create-only, |
| + // so fleets made before the create door wrote it can run rootfs apps. |
| + // Started after ConfigureClaimRuntime, which swaps cfg.Releases for its |
| + // boundary, so the goroutine never races that swap. Bounded, and it |
| + // never holds up serving; a no-op with no store or registry. |
| + go func() { |
| + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) |
| + defer cancel() |
| + srv.RunFleetOwnerBackfill(ctx) |
| + }() |
| // The alert watcher polls alerts/boxes/*.json every minute and keymails |
| // operators on each fire/clear (internal/console/alertwatch.go). Started |
| // here rather than in NewServer so constructing a Server — which every |
diff --git a/internal/console/fleetowner.go b/internal/console/fleetowner.go| new file mode 100644 |
| index 000000000..d5ffcf029 |
| --- /dev/null |
| +++ b/internal/console/fleetowner.go |
| @@ -0,0 +1,142 @@ |
| +package console |
| + |
| +// The fleet owner record (rootfs apps Plan 4 D7, Plan 2 deviation 2). A |
| +// fleet box cannot read the platform registry, so it learns which account |
| +// owns its fleet from release.FleetOwnerKey(acct, fleet) in the deployment |
| +// store, and refuses every rootfs start (RFS-2-C04) until that record names |
| +// the row's account. The console writes it from the fleet row: at create |
| +// (handleAPIFleetNew), on the retry that repairs a failed create, and once |
| +// per console start for every fleet (backfillFleetOwners). It is only ever |
| +// created, never replaced: hostops' fleet-owner family admits nothing else. |
| +// Removing it belongs to the fleet removal change, which does not exist yet. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "log" |
| + "os" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// fleetOwnerWriteTimeout bounds one record write: the read, and the Put. |
| +const fleetOwnerWriteTimeout = 10 * time.Second |
| + |
| +// errFleetOwnerConflict is a record at the key that does not name this |
| +// fleet and account. It is never overwritten. |
| +var errFleetOwnerConflict = errors.New("the fleet owner record names another account or fleet") |
| + |
| +// writeFleetOwner puts the record create-only (If-None-Match "*"). An |
| +// existing record that names the same fleet and account is success, with no |
| +// write; one that names anything else, or does not parse, is an error and is |
| +// never overwritten. |
| +func (s *Server) writeFleetOwner(ctx context.Context, f registry.Fleet) error { |
| + st := s.cfg.Releases |
| + if st == nil { |
| + return errors.New("fleet owner record: no deployment store") |
| + } |
| + // Both are key segments; the registry validated the name at create, and |
| + // the account is the console's own sqid. Checked again here so a row |
| + // can never build a key outside accounts/<acct>/fleets/. |
| + if !release.ValidAccount(f.Account) || !registry.ValidTunnelName(f.Name) { |
| + return fmt.Errorf("fleet owner record: fleet %q of account %q is not a valid key", f.Name, f.Account) |
| + } |
| + ctx, cancel := context.WithTimeout(ctx, fleetOwnerWriteTimeout) |
| + defer cancel() |
| + key := release.FleetOwnerKey(f.Account, f.Name) |
| + if ok, err := fleetOwnerRecorded(ctx, st, key, f); err != nil || ok { |
| + return err |
| + } |
| + createdAt := f.CreatedAt |
| + if createdAt == 0 { |
| + createdAt = time.Now().Unix() |
| + } |
| + body, err := json.Marshal(release.FleetOwner{Fleet: f.Name, Account: f.Account, CreatedAt: createdAt}) |
| + if err != nil { |
| + return err |
| + } |
| + _, err = st.Put(ctx, key, body, "") |
| + if errors.Is(err, release.ErrCASConflict) { |
| + // Another writer created it between the read and the Put (a second |
| + // console's backfill, a concurrent retry): judge what it wrote. |
| + ok, rerr := fleetOwnerRecorded(ctx, st, key, f) |
| + if rerr != nil { |
| + return rerr |
| + } |
| + if !ok { |
| + return fmt.Errorf("fleet owner record %s: %w", key, err) |
| + } |
| + return nil |
| + } |
| + if err != nil { |
| + return fmt.Errorf("fleet owner record %s: %w", key, err) |
| + } |
| + return nil |
| +} |
| + |
| +// fleetOwnerRecorded reads the record at key: (true, nil) when it names f's |
| +// fleet and account, (false, nil) when there is none, and an error for a |
| +// failed read or a record that names anything else. It decodes as the box |
| +// does (internal/host fleetOwnerCheck), so the console never takes a record |
| +// as written that the box would not accept. |
| +func fleetOwnerRecorded(ctx context.Context, st release.Store, key string, f registry.Fleet) (bool, error) { |
| + body, _, err := st.Get(ctx, key) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return false, nil |
| + } |
| + if err != nil { |
| + return false, fmt.Errorf("read %s: %w", key, err) |
| + } |
| + var rec release.FleetOwner |
| + if err := json.Unmarshal(body, &rec); err != nil { |
| + return false, fmt.Errorf("read %s: %w", key, err) |
| + } |
| + if rec.Fleet != f.Name || rec.Account != f.Account { |
| + return false, fmt.Errorf("%s: %w", key, errFleetOwnerConflict) |
| + } |
| + return true, nil |
| +} |
| + |
| +// backfillFleetOwners writes the record for every fleet ListFleets returns. |
| +// It runs once per console start, is idempotent (a record already in place |
| +// costs one read and no write), logs and continues past one fleet's error, |
| +// and returns them all joined. A console with no deployment store or no |
| +// registry has nothing to do. |
| +func (s *Server) backfillFleetOwners(ctx context.Context) error { |
| + if s.cfg.Releases == nil || s.cfg.Registry == "" { |
| + return nil |
| + } |
| + reg, err := openRegistry(s.cfg.Registry) |
| + if err != nil { |
| + return fmt.Errorf("fleet owner backfill: open registry: %w", err) |
| + } |
| + fleets, err := reg.ListFleets() |
| + if err != nil { |
| + return fmt.Errorf("fleet owner backfill: list fleets: %w", err) |
| + } |
| + var errs []error |
| + for _, f := range fleets { |
| + if err := ctx.Err(); err != nil { |
| + errs = append(errs, fmt.Errorf("fleet owner backfill stopped before %s: %w", f.Name, err)) |
| + break |
| + } |
| + if err := s.writeFleetOwner(ctx, f); err != nil { |
| + log.Printf("console: fleet owner backfill %s/%s: %v", f.Account, f.Name, err) |
| + errs = append(errs, fmt.Errorf("fleet %s: %w", f.Name, err)) |
| + } |
| + } |
| + return errors.Join(errs...) |
| +} |
| + |
| +// RunFleetOwnerBackfill is backfillFleetOwners for cmd/console, started in |
| +// its own goroutine with a bounded ctx so it never holds up serving. It |
| +// logs the outcome; the next start tries again. |
| +func (s *Server) RunFleetOwnerBackfill(ctx context.Context) { |
| + if err := s.backfillFleetOwners(ctx); err != nil { |
| + log.Printf("console: fleet owner backfill: %v", err) |
| + } |
| +} |
diff --git a/internal/console/fleetowner_test.go b/internal/console/fleetowner_test.go| new file mode 100644 |
| index 000000000..034756df2 |
| --- /dev/null |
| +++ b/internal/console/fleetowner_test.go |
| @@ -0,0 +1,484 @@ |
| +package console |
| + |
| +// The fleet owner record (rootfs apps Plan 4 D7, Task 5a): the console |
| +// writes accounts/<acct>/fleets/<fleet>.json, naming the fleet row's |
| +// account, and a fleet box refuses every rootfs start until it can read |
| +// that record. The properties that matter: the record names exactly the |
| +// row's account, it is never overwritten, a failed write is repaired by |
| +// the retry the 500 asks for, and a stranger's create of a taken name |
| +// touches no owner record at all. |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "net/http" |
| + "os" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/hostops" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// fleetOwnerSpy records every Get and Put of a fleet owner key, and fails |
| +// the next failPuts Puts of one. |
| +type fleetOwnerSpy struct { |
| + release.Store |
| + mu sync.Mutex |
| + failPuts int |
| + gets map[string]int |
| + puts map[string]int |
| +} |
| + |
| +func isFleetOwnerKeyShape(key string) bool { |
| + return strings.HasPrefix(key, "accounts/") && strings.Contains(key, "/fleets/") |
| +} |
| + |
| +func (s *fleetOwnerSpy) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if isFleetOwnerKeyShape(key) { |
| + s.mu.Lock() |
| + s.gets[key]++ |
| + s.mu.Unlock() |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func (s *fleetOwnerSpy) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + if isFleetOwnerKeyShape(key) { |
| + s.mu.Lock() |
| + s.puts[key]++ |
| + fail := s.failPuts > 0 |
| + if fail { |
| + s.failPuts-- |
| + } |
| + s.mu.Unlock() |
| + if fail { |
| + return "", errors.New("spy: injected put failure") |
| + } |
| + } |
| + return s.Store.Put(ctx, key, body, ifMatch) |
| +} |
| + |
| +func (s *fleetOwnerSpy) counts() (gets, puts int) { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + for _, n := range s.gets { |
| + gets += n |
| + } |
| + for _, n := range s.puts { |
| + puts += n |
| + } |
| + return gets, puts |
| +} |
| + |
| +func (s *fleetOwnerSpy) reset() { |
| + s.mu.Lock() |
| + defer s.mu.Unlock() |
| + s.gets, s.puts = map[string]int{}, map[string]int{} |
| +} |
| + |
| +// fleetOwnerFixture is fleetFixture over a spied deployment store. |
| +func fleetOwnerFixture(t *testing.T, code string) (*consoleFixture, string, *fleetOwnerSpy) { |
| + t.Helper() |
| + spy := &fleetOwnerSpy{gets: map[string]int{}, puts: map[string]int{}} |
| + fx := newConsoleFixtureWith(t, consoleFixtureOptions{WithKey: true, WrapReleases: func(s release.Store) release.Store { |
| + spy.Store = s |
| + return spy |
| + }}) |
| + return fx, cliToken(t, fx, "paul@keymail.dev", code), spy |
| +} |
| + |
| +// readFleetOwner decodes the record at FleetOwnerKey(acct, fleet). |
| +func readFleetOwner(t *testing.T, st release.Store, acct, fleet string) (release.FleetOwner, error) { |
| + t.Helper() |
| + body, _, err := st.Get(context.Background(), release.FleetOwnerKey(acct, fleet)) |
| + if err != nil { |
| + return release.FleetOwner{}, err |
| + } |
| + var rec release.FleetOwner |
| + if err := json.Unmarshal(body, &rec); err != nil { |
| + t.Fatalf("owner record %s/%s: %v", acct, fleet, err) |
| + } |
| + return rec, nil |
| +} |
| + |
| +// boxOwns is the fleet box's verdict on the record, decoded exactly as |
| +// internal/host's fleetOwnerCheck reads it (rootfsplacement.go): Get the |
| +// key, a missing key is (false, nil), json.Unmarshal into |
| +// release.FleetOwner, and only a record naming both this fleet and this |
| +// account is (true, nil). fleetOwnerCheck is unexported in internal/host, |
| +// so this witness repeats its read rather than calling it (Plan 4 |
| +// amendment 5a.1). |
| +func boxOwns(st release.Store, fleet, acct string) (bool, error) { |
| + key := release.FleetOwnerKey(acct, fleet) |
| + body, _, err := st.Get(context.Background(), key) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return false, nil |
| + } |
| + if err != nil { |
| + return false, err |
| + } |
| + var rec release.FleetOwner |
| + if err := json.Unmarshal(body, &rec); err != nil { |
| + return false, err |
| + } |
| + return rec.Fleet == fleet && rec.Account == acct, nil |
| +} |
| + |
| +func TestFleetCreateWritesTheOwnerRecord(t *testing.T) { |
| + fx, token, _ := fleetOwnerFixture(t, "code-owner-create") |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "home", "bucket": "home-data"}, token); status != http.StatusOK { |
| + t.Fatalf("create = %d %v, want 200", status, body) |
| + } |
| + rec, err := readFleetOwner(t, fx.rel, fx.sqid, "home") |
| + if err != nil { |
| + t.Fatalf("owner record: %v", err) |
| + } |
| + if rec.Fleet != "home" || rec.Account != fx.sqid || rec.CreatedAt == 0 { |
| + t.Fatalf("owner record = %+v, want fleet home, account %s and a creation time", rec, fx.sqid) |
| + } |
| + // The record names exactly the fleet row's account. |
| + reg, err := openRegistry(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + row, err := reg.GetFleet("home") |
| + if err != nil || row == nil || row.Account != rec.Account { |
| + t.Fatalf("fleet row = %+v, %v; want the record's account %s", row, err, rec.Account) |
| + } |
| + // The bytes the console wrote pass the root binary's classifier, so the |
| + // same write goes through the claim boundary unchanged. |
| + stored, _, err := fx.rel.Get(context.Background(), release.FleetOwnerKey(fx.sqid, "home")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + c, err := hostops.ClassifyMutation(context.Background(), nil, nil, hostops.MutationTarget{Surface: "bucket", Method: "Put", Key: release.FleetOwnerKey(fx.sqid, "home"), After: stored}) |
| + if err != nil || c.Family != "fleet-owner" { |
| + t.Fatalf("classifier on the stored record = %+v, %v; want the fleet-owner family", c, err) |
| + } |
| +} |
| + |
| +func TestTheBoxReadsWhatTheConsoleWrote(t *testing.T) { |
| + fx, token, _ := fleetOwnerFixture(t, "code-owner-witness") |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "home", "bucket": "home-data"}, token); status != http.StatusOK { |
| + t.Fatalf("create = %d %v, want 200", status, body) |
| + } |
| + if ok, err := boxOwns(fx.rel, "home", fx.sqid); !ok || err != nil { |
| + t.Fatalf("box verdict for %s = (%v, %v), want (true, nil)", fx.sqid, ok, err) |
| + } |
| + if ok, err := boxOwns(fx.rel, "home", "other"); ok || err != nil { |
| + t.Fatalf("box verdict for another account = (%v, %v), want (false, nil)", ok, err) |
| + } |
| + if ok, err := boxOwns(fx.rel, "away", fx.sqid); ok || err != nil { |
| + t.Fatalf("box verdict on another fleet = (%v, %v), want (false, nil)", ok, err) |
| + } |
| +} |
| + |
| +func TestFleetOwnerRecordIsNeverOverwritten(t *testing.T) { |
| + ctx := context.Background() |
| + for _, c := range []struct{ name, body string }{ |
| + {"another account", `{"fleet":"home","account":"cdef"}`}, |
| + {"another fleet", `{"fleet":"away","account":"` + "%s" + `"}`}, |
| + {"not a record", `nope`}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + fx, token, _ := fleetOwnerFixture(t, "code-owner-overwrite") |
| + key := release.FleetOwnerKey(fx.sqid, "home") |
| + planted := []byte(strings.ReplaceAll(c.body, "%s", fx.sqid)) |
| + if _, err := fx.rel.Put(ctx, key, planted, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.writeFleetOwner(ctx, registry.Fleet{Name: "home", Account: fx.sqid, Bucket: "home-data"}); err == nil { |
| + t.Fatal("writeFleetOwner over a record naming something else = nil, want an error") |
| + } |
| + // The door refuses too, with its failed-create sentence, and the |
| + // row it inserted stays. |
| + status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "home", "bucket": "home-data"}, token) |
| + if status != http.StatusInternalServerError || body["error"] != "could not create the fleet — try again" { |
| + t.Fatalf("create over a foreign record = %d %v, want 500 with the failed-create sentence", status, body) |
| + } |
| + got, _, err := fx.rel.Get(ctx, key) |
| + if err != nil || string(got) != string(planted) { |
| + t.Fatalf("stored record = %q, %v; want the planted bytes unchanged", got, err) |
| + } |
| + if ok, _ := boxOwns(fx.rel, "home", fx.sqid); ok { |
| + t.Fatal("the box reads ownership from a record the console refused to accept") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// seedFleets inserts fleet rows straight into the fixture's registry. |
| +func seedFleets(t *testing.T, fx *consoleFixture, fleets ...registry.Fleet) { |
| + t.Helper() |
| + reg, err := openRegistry(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, f := range fleets { |
| + if created, err := reg.CreateFleet(f); err != nil || !created { |
| + t.Fatalf("seed fleet %s: created %v, %v", f.Name, created, err) |
| + } |
| + } |
| +} |
| + |
| +func TestBackfillWritesEveryFleetOnce(t *testing.T) { |
| + ctx := context.Background() |
| + fx, _, spy := fleetOwnerFixture(t, "code-owner-backfill") |
| + seedFleets(t, fx, |
| + registry.Fleet{Name: "home", Account: fx.sqid, Bucket: "home-data"}, |
| + registry.Fleet{Name: "away", Account: "cdef", Bucket: "away-data", CreatedAt: 1761000000}) |
| + if err := fx.srv.writeFleetOwner(ctx, registry.Fleet{Name: "home", Account: fx.sqid}); err != nil { |
| + t.Fatal(err) |
| + } |
| + spy.reset() |
| + if err := fx.srv.backfillFleetOwners(ctx); err != nil { |
| + t.Fatalf("backfill: %v", err) |
| + } |
| + for _, f := range []struct{ fleet, acct string }{{"home", fx.sqid}, {"away", "cdef"}} { |
| + if ok, err := boxOwns(fx.rel, f.fleet, f.acct); !ok || err != nil { |
| + t.Fatalf("after the backfill the box reads %s/%s as (%v, %v), want (true, nil)", f.acct, f.fleet, ok, err) |
| + } |
| + } |
| + if rec, _ := readFleetOwner(t, fx.rel, "cdef", "away"); rec.CreatedAt != 1761000000 { |
| + t.Fatalf("backfilled record = %+v, want the row's creation time", rec) |
| + } |
| + if _, puts := spy.counts(); puts != 1 { |
| + t.Fatalf("first backfill made %d owner puts, want 1 (home already had its record)", puts) |
| + } |
| + spy.reset() |
| + if err := fx.srv.backfillFleetOwners(ctx); err != nil { |
| + t.Fatalf("second backfill: %v", err) |
| + } |
| + if _, puts := spy.counts(); puts != 0 { |
| + t.Fatalf("second backfill made %d owner puts, want none", puts) |
| + } |
| +} |
| + |
| +// One fleet's failure is logged and reported, and the backfill still |
| +// writes every other fleet's record. |
| +func TestBackfillContinuesPastOneFleet(t *testing.T) { |
| + ctx := context.Background() |
| + fx, _, _ := fleetOwnerFixture(t, "code-owner-backfill-err") |
| + seedFleets(t, fx, |
| + registry.Fleet{Name: "alpha", Account: fx.sqid, Bucket: "alpha-data"}, |
| + registry.Fleet{Name: "beta", Account: fx.sqid, Bucket: "beta-data"}, |
| + registry.Fleet{Name: "gamma", Account: fx.sqid, Bucket: "gamma-data"}) |
| + planted := []byte(`{"fleet":"beta","account":"cdef"}`) |
| + if _, err := fx.rel.Put(ctx, release.FleetOwnerKey(fx.sqid, "beta"), planted, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.backfillFleetOwners(ctx); err == nil || !strings.Contains(err.Error(), "beta") { |
| + t.Fatalf("backfill = %v, want an error naming beta", err) |
| + } |
| + for _, f := range []string{"alpha", "gamma"} { |
| + if ok, err := boxOwns(fx.rel, f, fx.sqid); !ok || err != nil { |
| + t.Fatalf("fleet %s after the backfill = (%v, %v), want its record", f, ok, err) |
| + } |
| + } |
| + if got, _, _ := fx.rel.Get(ctx, release.FleetOwnerKey(fx.sqid, "beta")); string(got) != string(planted) { |
| + t.Fatalf("beta's record = %q, want it untouched", got) |
| + } |
| +} |
| + |
| +// Amendment 5a.4: no deployment store or no registry is a no-op. |
| +func TestBackfillWithoutAStoreOrRegistryDoesNothing(t *testing.T) { |
| + ctx := context.Background() |
| + spy := &fleetOwnerSpy{Store: release.NewDirStore(t.TempDir()), gets: map[string]int{}, puts: map[string]int{}} |
| + if err := (&Server{cfg: Config{Releases: spy}}).backfillFleetOwners(ctx); err != nil { |
| + t.Fatalf("backfill with no registry = %v, want nil", err) |
| + } |
| + if gets, puts := spy.counts(); gets+puts != 0 { |
| + t.Fatalf("backfill with no registry touched the store (%d gets, %d puts)", gets, puts) |
| + } |
| + fx, _, _ := fleetOwnerFixture(t, "code-owner-backfill-nil") |
| + seedFleets(t, fx, registry.Fleet{Name: "home", Account: fx.sqid, Bucket: "home-data"}) |
| + if err := (&Server{cfg: Config{Registry: fx.regPath}}).backfillFleetOwners(ctx); err != nil { |
| + t.Fatalf("backfill with no store = %v, want nil", err) |
| + } |
| +} |
| + |
| +// Astra R1 finding 13: the retry the 500 asks for repairs the record. |
| +func TestFleetCreateRetryRepairsAFailedOwnerRecord(t *testing.T) { |
| + fx, token, spy := fleetOwnerFixture(t, "code-owner-retry") |
| + key := release.FleetOwnerKey(fx.sqid, "home") |
| + spy.failPuts = 1 |
| + u := fleetsURL(fx, fx.sqid) + "/new" |
| + req := map[string]string{"name": "home", "bucket": "home-data"} |
| + status, body := postJSON(t, u, req, token) |
| + if status != http.StatusInternalServerError || body["error"] != "could not create the fleet — try again" { |
| + t.Fatalf("create with a failing record write = %d %v, want 500 with the failed-create sentence", status, body) |
| + } |
| + reg, err := openRegistry(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, err := reg.GetFleet("home"); err != nil || row == nil || row.Account != fx.sqid { |
| + t.Fatalf("fleet row after the failed write = %+v, %v; want it to stay", row, err) |
| + } |
| + if _, err := readFleetOwner(t, fx.rel, fx.sqid, "home"); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("owner record after the failed write: %v, want none", err) |
| + } |
| + // A box joins before the retry (amendment 5a.5): the repaired answer |
| + // shows the fleet as it is now. |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: "home", Label: "pi-1", TokenHash: strings.Repeat("a", 64), Status: "active"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + status, body = postJSON(t, u, req, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("retry = %d %v, want 200", status, body) |
| + } |
| + f, _ := body["fleet"].(map[string]any) |
| + boxes, _ := f["boxes"].([]any) |
| + if f["name"] != "home" || f["bucket"] != "home-data" || len(boxes) != 1 { |
| + t.Fatalf("retry's fleet = %v, want home, home-data and its one box", body["fleet"]) |
| + } |
| + if b, _ := boxes[0].(map[string]any); b["label"] != "pi-1" || b["status"] != "active" { |
| + t.Fatalf("retry's box = %v, want pi-1 active", boxes[0]) |
| + } |
| + rec, err := readFleetOwner(t, fx.rel, fx.sqid, "home") |
| + if err != nil || rec.Fleet != "home" || rec.Account != fx.sqid { |
| + t.Fatalf("owner record after the retry = %+v, %v; want fleet home, account %s", rec, err, fx.sqid) |
| + } |
| + if ok, err := boxOwns(fx.rel, "home", fx.sqid); !ok || err != nil { |
| + t.Fatalf("box verdict after the retry = (%v, %v), want (true, nil)", ok, err) |
| + } |
| + evs, err := fx.st.Events(OpsAccountID, "home", 10) |
| + if err != nil || len(evs) != 1 || evs[0].Action != "fleet-create" || evs[0].Detail != "created fleet home (cli)" { |
| + t.Fatalf("audit = %+v, %v; want one fleet-create row for the completed create", evs, err) |
| + } |
| + |
| + spy.mu.Lock() |
| + before := spy.puts[key] |
| + spy.mu.Unlock() |
| + if status, body := postJSON(t, u, req, token); status != http.StatusOK { |
| + t.Fatalf("third create = %d %v, want 200", status, body) |
| + } |
| + spy.mu.Lock() |
| + after := spy.puts[key] |
| + spy.mu.Unlock() |
| + if before != 2 || after != before { |
| + t.Fatalf("owner puts = %d then %d, want 2 (the failed one and the repair) and no more", before, after) |
| + } |
| +} |
| + |
| +func TestFleetCreateRetryWithAnotherBucketRepairsAndStillRefuses(t *testing.T) { |
| + fx, token, spy := fleetOwnerFixture(t, "code-owner-retry-bucket") |
| + spy.failPuts = 1 |
| + u := fleetsURL(fx, fx.sqid) + "/new" |
| + if status, body := postJSON(t, u, map[string]string{"name": "home", "bucket": "home-data"}, token); status != http.StatusInternalServerError { |
| + t.Fatalf("create with a failing record write = %d %v, want 500", status, body) |
| + } |
| + status, body := postJSON(t, u, map[string]string{"name": "home", "bucket": "other-data"}, token) |
| + if status != http.StatusConflict || body["error"] != "that name is taken" { |
| + t.Fatalf("retry with another bucket = %d %v, want 409 that name is taken", status, body) |
| + } |
| + if ok, err := boxOwns(fx.rel, "home", fx.sqid); !ok || err != nil { |
| + t.Fatalf("box verdict after the retry = (%v, %v), want (true, nil): the row is the caller's, so the record is repaired", ok, err) |
| + } |
| + reg, err := openRegistry(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if row, _ := reg.GetFleet("home"); row == nil || row.Bucket != "home-data" { |
| + t.Fatalf("fleet row = %+v, want the first bucket kept", row) |
| + } |
| +} |
| + |
| +func TestFleetCreateByAnotherAccountTouchesNoRecord(t *testing.T) { |
| + fx, token, spy := fleetOwnerFixture(t, "code-owner-stranger") |
| + spy.failPuts = 1 |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "home", "bucket": "home-data"}, token); status != http.StatusInternalServerError { |
| + t.Fatalf("create with a failing record write = %d %v, want 500", status, body) |
| + } |
| + if err := fx.st.CreateAccount(42, "cdef", "widgets", "bob@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + bobToken := cliToken(t, fx, "bob@keymail.dev", "code-owner-stranger-bob") |
| + spy.reset() |
| + for _, bucket := range []string{"home-data", "bob-data"} { |
| + status, body := postJSON(t, fleetsURL(fx, "cdef")+"/new", map[string]string{"name": "home", "bucket": bucket}, bobToken) |
| + if status != http.StatusConflict || body["error"] != "that name is taken" { |
| + t.Fatalf("another account's create of a taken name (bucket %s) = %d %v, want 409 that name is taken", bucket, status, body) |
| + } |
| + } |
| + if gets, puts := spy.counts(); gets+puts != 0 { |
| + t.Fatalf("another account's create touched owner records: %d gets, %d puts; want none", gets, puts) |
| + } |
| + for _, acct := range []string{fx.sqid, "cdef"} { |
| + if _, err := readFleetOwner(t, fx.rel, acct, "home"); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("owner record for %s: %v, want none", acct, err) |
| + } |
| + } |
| +} |
| + |
| +// A row whose account or name is not a key segment never builds a key, so |
| +// no write can land outside accounts/<acct>/fleets/. |
| +func TestWriteFleetOwnerRefusesARowThatIsNotAKey(t *testing.T) { |
| + fx, _, spy := fleetOwnerFixture(t, "code-owner-badrow") |
| + for _, f := range []registry.Fleet{ |
| + {Name: "home", Account: "BAB"}, |
| + {Name: "home", Account: ""}, |
| + {Name: "a/b", Account: fx.sqid}, |
| + {Name: "", Account: fx.sqid}, |
| + {Name: "Home", Account: fx.sqid}, |
| + } { |
| + if err := fx.srv.writeFleetOwner(context.Background(), f); err == nil { |
| + t.Errorf("writeFleetOwner(%+v) = nil, want a refusal", f) |
| + } |
| + } |
| + if gets, puts := spy.counts(); gets+puts != 0 { |
| + t.Fatalf("refused rows touched the store: %d gets, %d puts", gets, puts) |
| + } |
| +} |
| + |
| +// racingOwnerStore answers its first Get of key as missing and plants |
| +// planted there first, the shape of a second writer (another console's |
| +// backfill, a concurrent retry) landing between the read and the Put. |
| +type racingOwnerStore struct { |
| + release.Store |
| + key string |
| + planted []byte |
| + raced bool |
| +} |
| + |
| +func (s *racingOwnerStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if key == s.key && !s.raced { |
| + s.raced = true |
| + if _, err := s.Store.Put(ctx, key, s.planted, ""); err != nil { |
| + return nil, "", err |
| + } |
| + return nil, "", os.ErrNotExist |
| + } |
| + return s.Store.Get(ctx, key) |
| +} |
| + |
| +func TestWriteFleetOwnerJudgesARacingWriter(t *testing.T) { |
| + ctx := context.Background() |
| + for _, c := range []struct { |
| + name string |
| + planted string |
| + wantErr bool |
| + }{ |
| + {"the same record", `{"fleet":"home","account":"bab","created_at":1}`, false}, |
| + {"another account's record", `{"fleet":"home","account":"cdef"}`, true}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + dir := release.NewDirStore(t.TempDir()) |
| + key := release.FleetOwnerKey("bab", "home") |
| + st := &racingOwnerStore{Store: dir, key: key, planted: []byte(c.planted)} |
| + err := (&Server{cfg: Config{Releases: st}}).writeFleetOwner(ctx, registry.Fleet{Name: "home", Account: "bab"}) |
| + if (err != nil) != c.wantErr { |
| + t.Fatalf("writeFleetOwner after a racing write = %v, want error %v", err, c.wantErr) |
| + } |
| + if got, _, _ := dir.Get(ctx, key); string(got) != c.planted { |
| + t.Fatalf("stored record = %q, want the racing writer's bytes untouched", got) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index cc7b8adc3..e017b83e8 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -214,14 +214,63 @@ func (s *Server) handleAPIFleetNew(w http.ResponseWriter, r *http.Request) { |
| jsonError(w, http.StatusInternalServerError, "could not create the fleet — try again") |
| return |
| } |
| - if !created { |
| - jsonError(w, http.StatusConflict, "that name is taken") |
| - return |
| + view := fleetView{Name: name, Bucket: bucket, Boxes: []fleetBoxView{}} |
| + if created { |
| + // The fleet owner record (rootfs apps Plan 4 D7): written before the |
| + // door answers. A failed write leaves the row and answers the |
| + // failed-create sentence; the retry it asks for repairs it below. |
| + if err := s.writeFleetOwner(r.Context(), registry.Fleet{Name: name, Account: acct.Sqid, Bucket: bucket}); err != nil { |
| + log.Printf("console: fleet new %s/%s: owner record: %v", acct.Sqid, name, err) |
| + jsonError(w, http.StatusInternalServerError, "could not create the fleet — try again") |
| + return |
| + } |
| + } else { |
| + // The name exists. Another account's row is today's 409, with no |
| + // read or write of any owner record, so a stranger learns nothing |
| + // new and touches nothing. The caller's own row is the retry of a |
| + // create whose owner record failed (Astra R1 finding 13): write it |
| + // (a no-op when it is already there), then answer as the create |
| + // would when the bucket matches, and 409 when it does not. |
| + row, err := reg.GetFleet(name) |
| + if err != nil { |
| + log.Printf("console: fleet new %s/%s: read existing: %v", acct.Sqid, name, err) |
| + jsonError(w, http.StatusInternalServerError, "could not create the fleet — try again") |
| + return |
| + } |
| + if row == nil || row.Account != acct.Sqid { |
| + jsonError(w, http.StatusConflict, "that name is taken") |
| + return |
| + } |
| + if err := s.writeFleetOwner(r.Context(), *row); err != nil { |
| + log.Printf("console: fleet new %s/%s: owner record repair: %v", acct.Sqid, name, err) |
| + jsonError(w, http.StatusInternalServerError, "could not create the fleet — try again") |
| + return |
| + } |
| + if row.Bucket != bucket { |
| + jsonError(w, http.StatusConflict, "that name is taken") |
| + return |
| + } |
| + // Boxes may have joined since the first attempt (amendment 5a.5). |
| + boxes, err := reg.ListFleetBoxes(row.Name) |
| + if err != nil { |
| + log.Printf("console: fleet new %s/%s: boxes: %v", acct.Sqid, name, err) |
| + jsonError(w, http.StatusInternalServerError, "could not create the fleet — try again") |
| + return |
| + } |
| + view = fleetView{Name: row.Name, Bucket: row.Bucket, Boxes: fleetBoxViews(boxes)} |
| } |
| s.audit(acct, name, addr, "fleet-create", "created fleet "+name+" (cli)") |
| - writeJSON(w, http.StatusOK, map[string]any{ |
| - "fleet": fleetView{Name: name, Bucket: bucket, Boxes: []fleetBoxView{}}, |
| - }) |
| + writeJSON(w, http.StatusOK, map[string]any{"fleet": view}) |
| +} |
| + |
| +// fleetBoxViews is the wire shape of a fleet's boxes: label, status and |
| +// tags, never a token hash. Always a non-nil slice. |
| +func fleetBoxViews(boxes []registry.FleetBox) []fleetBoxView { |
| + bv := make([]fleetBoxView, 0, len(boxes)) |
| + for _, b := range boxes { |
| + bv = append(bv, fleetBoxView{Label: b.Label, Status: b.Status, Tags: b.Tags}) |
| + } |
| + return bv |
| } |
| // handleAPIFleetList answers `carlos fleets ls`: every fleet the caller's |
| @@ -254,11 +303,7 @@ func (s *Server) handleAPIFleetList(w http.ResponseWriter, r *http.Request) { |
| jsonError(w, http.StatusInternalServerError, "could not list fleets") |
| return |
| } |
| - bv := make([]fleetBoxView, 0, len(boxes)) |
| - for _, b := range boxes { |
| - bv = append(bv, fleetBoxView{Label: b.Label, Status: b.Status, Tags: b.Tags}) |
| - } |
| - out = append(out, fleetView{Name: f.Name, Bucket: f.Bucket, Boxes: bv}) |
| + out = append(out, fleetView{Name: f.Name, Bucket: f.Bucket, Boxes: fleetBoxViews(boxes)}) |
| } |
| writeJSON(w, http.StatusOK, map[string]any{"fleets": out}) |
| } |
diff --git a/internal/hostops/fleetowner_classifier.go b/internal/hostops/fleetowner_classifier.go| new file mode 100644 |
| index 000000000..6c45b95e1 |
| --- /dev/null |
| +++ b/internal/hostops/fleetowner_classifier.go |
| @@ -0,0 +1,49 @@ |
| +package hostops |
| + |
| +import ( |
| + "fmt" |
| + "strings" |
| + |
| + "github.com/carlosframework/platform/internal/controlname" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// isFleetOwnerShape routes a key to classifyFleetOwner: four segments, |
| +// accounts/<x>/fleets/<y>. classifyFleetOwner then judges every part. |
| +func isFleetOwnerShape(p []string) bool { |
| + return len(p) == 4 && p[0] == "accounts" && p[2] == "fleets" |
| +} |
| + |
| +// classifyFleetOwner admits the fleet owner record (rootfs apps Plan 4 D7, |
| +// release.FleetOwnerKey): the console's record of which account owns a |
| +// fleet, which a fleet box reads before it starts any rootfs app. The |
| +// console only ever creates it, so the one admitted shape is a create-only |
| +// Put (no ETag, no existing object) of exactly |
| +// accounts/<acct>/fleets/<fleet>.json, whose body decodes strictly as a |
| +// release.FleetOwner naming that same account and fleet. A record naming |
| +// another account, a replacement, and every delete are refused: no fleet |
| +// removal door exists yet, and the change that adds one reviews its own |
| +// Delete arm. |
| +func classifyFleetOwner(t MutationTarget) (Classification, error) { |
| + p := strings.Split(t.Key, "/") |
| + if !isFleetOwnerShape(p) { |
| + return Classification{}, fmt.Errorf("%w: %s", ErrUnclassified, t.Key) |
| + } |
| + acct := p[1] |
| + fleet, ok := strings.CutSuffix(p[3], ".json") |
| + if !ok || !controlname.ValidAccount(acct) || !registry.ValidTunnelName(fleet) || t.Key != release.FleetOwnerKey(acct, fleet) { |
| + return Classification{}, fmt.Errorf("%w: %s", ErrUnclassified, t.Key) |
| + } |
| + if t.Method != "Put" || t.ETag != "" || len(t.Before) != 0 { |
| + return Classification{}, fmt.Errorf("%w: %s is create-only", ErrUnclassified, t.Key) |
| + } |
| + var rec release.FleetOwner |
| + if err := strictJSON(t.After, &rec); err != nil { |
| + return Classification{}, fmt.Errorf("%w: %s: %v", ErrUnclassified, t.Key, err) |
| + } |
| + if rec.Account != acct || rec.Fleet != fleet { |
| + return Classification{}, fmt.Errorf("%w: %s: the record names another account or fleet", ErrUnclassified, t.Key) |
| + } |
| + return Classification{Family: "fleet-owner", NonApp: "the console's record of which account owns a fleet; holds no app record"}, nil |
| +} |
diff --git a/internal/hostops/fleetowner_classifier_test.go b/internal/hostops/fleetowner_classifier_test.go| new file mode 100644 |
| index 000000000..2bfa9a1e5 |
| --- /dev/null |
| +++ b/internal/hostops/fleetowner_classifier_test.go |
| @@ -0,0 +1,84 @@ |
| +package hostops |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// TestFleetOwnerClassifier: the fleet owner record (rootfs apps Plan 4 D7) |
| +// is a reviewed non-app family. The console only ever creates it, so the |
| +// family admits exactly one shape: a create-only Put of |
| +// accounts/<acct>/fleets/<fleet>.json whose body names that account and |
| +// that fleet. A record a fleet box trusts as its ownership proof must never |
| +// be replaced, deleted, or written for another account through this door. |
| +func TestFleetOwnerClassifier(t *testing.T) { |
| + ctx := context.Background() |
| + key := release.FleetOwnerKey("bab", "home") |
| + body := []byte(`{"fleet":"home","account":"bab","created_at":1761500000}`) |
| + for _, c := range []struct { |
| + name string |
| + target MutationTarget |
| + }{ |
| + {"create", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: body}}, |
| + {"create with no created_at", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"home","account":"bab"}`)}}, |
| + {"a fleet name with digits and a hyphen", MutationTarget{Surface: "bucket", Method: "Put", Key: release.FleetOwnerKey("bab", "edge-2"), After: []byte(`{"fleet":"edge-2","account":"bab"}`)}}, |
| + } { |
| + got, err := ClassifyMutation(ctx, nil, nil, c.target) |
| + if err != nil { |
| + t.Errorf("%s: %v", c.name, err) |
| + continue |
| + } |
| + if got.Family != "fleet-owner" || got.NonApp == "" || len(got.Scopes) != 0 || got.MailGate { |
| + t.Errorf("%s: classified %+v, want non-app family fleet-owner with no scopes", c.name, got) |
| + } |
| + } |
| + long := strings.Repeat("a", 33) |
| + for _, c := range []struct { |
| + name string |
| + target MutationTarget |
| + }{ |
| + {"a delete", MutationTarget{Surface: "bucket", Method: "Delete", Key: key, Before: body}}, |
| + {"a delete of an absent record", MutationTarget{Surface: "bucket", Method: "Delete", Key: key}}, |
| + {"a delete if match", MutationTarget{Surface: "bucket", Method: "DeleteIfMatch", Key: key, ETag: "x"}}, |
| + {"a delete carrying a matching body", MutationTarget{Surface: "bucket", Method: "Delete", Key: key, After: body}}, |
| + {"a put with an ETag over no read body", MutationTarget{Surface: "bucket", Method: "Put", Key: key, ETag: "x", After: body}}, |
| + {"a replacing put", MutationTarget{Surface: "bucket", Method: "Put", Key: key, ETag: "x", Before: body, After: body}}, |
| + {"a put over an existing record", MutationTarget{Surface: "bucket", Method: "Put", Key: key, Before: body, After: body}}, |
| + {"a body naming another account", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"home","account":"cdef"}`)}}, |
| + {"a body naming another fleet", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"away","account":"bab"}`)}}, |
| + {"a body naming no account", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"home"}`)}}, |
| + {"a body naming no fleet", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"account":"bab"}`)}}, |
| + {"no body", MutationTarget{Surface: "bucket", Method: "Put", Key: key}}, |
| + {"a body that is not JSON", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`home bab`)}}, |
| + {"a body with an unknown field", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"home","account":"bab","apps":["shop"]}`)}}, |
| + {"a body with a trailing value", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`{"fleet":"home","account":"bab"} {}`)}}, |
| + {"a body that is an array", MutationTarget{Surface: "bucket", Method: "Put", Key: key, After: []byte(`[{"fleet":"home","account":"bab"}]`)}}, |
| + {"a key with a subdirectory", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/x/home.json", After: body}}, |
| + {"a key below the record", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/home.json/x", After: body}}, |
| + {"the fleets prefix itself", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/", After: body}}, |
| + {"an empty fleet name", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/.json", After: []byte(`{"fleet":"","account":"bab"}`)}}, |
| + {"an upper-case fleet name", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/Home.json", After: []byte(`{"fleet":"Home","account":"bab"}`)}}, |
| + {"a fleet name with a dot", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/ho.me.json", After: []byte(`{"fleet":"ho.me","account":"bab"}`)}}, |
| + {"a fleet name of 33 characters", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/" + long + ".json", After: []byte(`{"fleet":"` + long + `","account":"bab"}`)}}, |
| + {"no .json suffix", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/home", After: []byte(`{"fleet":"home","account":"bab"}`)}}, |
| + {"a lookalike suffix", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleets/home.json.bak", After: body}}, |
| + {"an invalid account", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/BAB/fleets/home.json", After: []byte(`{"fleet":"home","account":"BAB"}`)}}, |
| + {"an account traversal", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/../fleets/home.json", After: []byte(`{"fleet":"home","account":".."}`)}}, |
| + {"a lookalike directory", MutationTarget{Surface: "bucket", Method: "Put", Key: "accounts/bab/fleet/home.json", After: body}}, |
| + } { |
| + if got, err := ClassifyMutation(ctx, nil, nil, c.target); !errors.Is(err, ErrUnclassified) { |
| + t.Errorf("%s: classified %+v, err %v; want ErrUnclassified", c.name, got, err) |
| + } |
| + } |
| + // The family's own function admits nothing outside its key, whatever |
| + // routed it there: an app key carrying a matching body stays refused. |
| + for _, k := range []string{"apps/bab/home/fleets/home.json", "accounts/bab/account.json", "accounts/bab/limits.json", "control/fleets/home.json", "control/bab/fleets/home.json", "usage/bab/fleets/home.json"} { |
| + if got, err := classifyFleetOwner(MutationTarget{Surface: "bucket", Method: "Put", Key: k, After: body}); !errors.Is(err, ErrUnclassified) { |
| + t.Errorf("classifyFleetOwner(%s): classified %+v, err %v; want ErrUnclassified", k, got, err) |
| + } |
| + } |
| +} |
diff --git a/internal/hostops/mutation_classifier.go b/internal/hostops/mutation_classifier.go| index 6bb692cd3..dc69e7b61 100644 |
| --- a/internal/hostops/mutation_classifier.go |
| +++ b/internal/hostops/mutation_classifier.go |
| @@ -150,6 +150,9 @@ func ClassifyMutation(ctx context.Context, raw release.Store, w *ControlWriter, |
| return Classification{}, fmt.Errorf("%w: %s refuses deletes", ErrUnclassified, t.Key) |
| } |
| return Classification{Family: "account-limits", NonApp: "account limits record published from the plan catalog; no app state"}, nil |
| + case isFleetOwnerShape(p): |
| + // rootfs apps Plan 4 D7: create-only, body names the key's account and fleet. |
| + return classifyFleetOwner(t) |
| case t.Key == RelayAuthorityKey: |
| return classifyRelayAuthority(t) |
| case t.Key == RelayIssuerKey: |
diff --git a/internal/provision/plan9_b8_census_test.go b/internal/provision/plan9_b8_census_test.go| index 1efefd276..5b2bd7d89 100644 |
| --- a/internal/provision/plan9_b8_census_test.go |
| +++ b/internal/provision/plan9_b8_census_test.go |
| @@ -366,7 +366,7 @@ func TestPlan9B8RecordWriteCensus(t *testing.T) { |
| if len(got) < 100 { |
| t.Fatalf("census found only %d units under %s; the walk is broken", len(got), root) |
| } |
| - inventory := nearestRegionBWriteCensusInventory() |
| + inventory := rootfsApps4WriteCensusInventory() |
| problems := append(censusDiff(inventory, got), censusPolicy(inventory)...) |
| if len(problems) > 0 { |
| t.Fatalf("record-write census rejected %d site(s); update the explicit inventory only after reviewing the source:\n %s", len(problems), strings.Join(problems, "\n ")) |
diff --git a/internal/provision/rootfs_apps_4_census_test.go b/internal/provision/rootfs_apps_4_census_test.go| new file mode 100644 |
| index 000000000..738da9dd0 |
| --- /dev/null |
| +++ b/internal/provision/rootfs_apps_4_census_test.go |
| @@ -0,0 +1,46 @@ |
| +package provision |
| + |
| +// Record-write census entries for rootfs apps Plan 4 Task 5a (the fleet |
| +// owner record, D7), layered on nearestRegionBWriteCensusInventory (the |
| +// chain tip on rootfs-apps-3-lifecycle). Reviewed by the branch author; |
| +// PROPOSED-rootfs-apps-4. Gate A's census pass carries the hostops rows |
| +// (internal/hostops is in the root binary); the console row needs no card |
| +// under census C2. |
| +// |
| +// New: |
| +// - Server.writeFleetOwner (console): the one Put of |
| +// accounts/<acct>/fleets/<fleet>.json, create-only (If-None-Match "*"), |
| +// after a read that turns an existing matching record into success and |
| +// any other record into an error. The body is release.FleetOwner, no |
| +// instance or sidecar record: nonrecord. |
| +// - classifyFleetOwner (hostops): the fleet-owner family. Admits only a |
| +// create-only Put of exactly that key whose body names the key's |
| +// account and fleet; "Put" is the method it compares. Writes nothing: |
| +// reference. |
| +// |
| +// Re-pinned: |
| +// - ClassifyMutation (hostops): one new arm routing accounts/<x>/fleets/<y> |
| +// to classifyFleetOwner. Every other arm unchanged: reference. |
| +// |
| +// No deleteFleetOwner: no fleet removal door exists (Plan 4 amendment 5a.3). |
| +func rootfsApps4WriteCensusInventory() []censusEntry { |
| + entries := append([]censusEntry(nil), nearestRegionBWriteCensusInventory()...) |
| + replacements := []censusEntry{ |
| + {"internal/hostops/mutation_classifier.go", "ClassifyMutation", "reference", "str:Put", "03f9ef0e54315c8342aea438dd350759bb6cfc603be6490d90bd3a0532c6bdc7"}, |
| + } |
| + for _, r := range replacements { |
| + found := false |
| + for i, e := range entries { |
| + if e.File == r.File && e.Unit == r.Unit { |
| + entries[i], found = r, true |
| + } |
| + } |
| + if !found { |
| + panic("rootfs-apps-4 re-pins an unlisted unit " + r.File + ":" + r.Unit) |
| + } |
| + } |
| + return append(entries, |
| + censusEntry{"internal/console/fleetowner.go", "Server.writeFleetOwner", "nonrecord", "sel:Put", "e955cfd3b9f4af9b216a499152971f59403aa3bbb498a9107ea38a11e97e2111"}, |
| + censusEntry{"internal/hostops/fleetowner_classifier.go", "classifyFleetOwner", "reference", "str:Put", "79d7cac1ec85dbe45908ada0ffb7d862e594b369817958c7e23ed37d0c6af96f"}, |
| + ) |
| +} |