carlos / platform Public

region HA P1a Task 11: the host never mints an HA host's secrets (backfill skips; the schedule runner only reads)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 1683ad0af5c868779e2394f5d72fa2975036884b parent 6bc9e06
3 files changed, +146 −1
  • internal/host/hasecrets_test.go +90 −0
  • internal/host/instancekeys.go +22 −1
  • internal/host/schedule_wiring.go +34 −0
diff --git a/internal/host/hasecrets_test.go b/internal/host/hasecrets_test.go
new file mode 100644
index 000000000..0108f1c11
--- /dev/null
+++ b/internal/host/hasecrets_test.go
@@ -0,0 +1,90 @@
+package host
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "reflect"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// testRegistryHA is a registry holding host as a born-HA instance on the
+// nested layout.
+func testRegistryHA(t *testing.T, dd, host string) *registry.Registry {
+ t.Helper()
+ reg, err := registry.Open(filepath.Join(t.TempDir(), "r.db"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { reg.Close() })
+ if err := os.MkdirAll(provision.InstanceDir(dd, host), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.Upsert(registry.Route{
+ Host: host, Kind: "instance", Account: "bab", App: "shop", Status: "ready", Hibernate: true, HA: true,
+ DBPath: provision.NestedDBPath(dd, host), Socket: provision.NestedSocket(host),
+ }); err != nil {
+ t.Fatal(err)
+ }
+ return reg
+}
+
+// The schedule runner never mints an HA host's token: it only reads, and
+// answers "" when there is none (the activator fills the token from the
+// restored env after the wake).
+func TestHAScheduleTokenNeverMints(t *testing.T) {
+ const host = "shop.example.com"
+ for _, svc := range []*activator.Service{nil, {}} {
+ dd := t.TempDir()
+ reg := testRegistryHA(t, dd, host)
+ tok, err := scheduleToken(dd, svc, reg)(host)
+ nested := filepath.Join(provision.InstanceDir(dd, host), host+".env")
+ if err != nil || tok != "" || exists(nested) || exists(filepath.Join(dd, host+".env")) {
+ t.Fatalf("svc %v: token %q, err %v; minted %v", svc != nil, tok, err, exists(nested))
+ }
+ // A token already there is read, not rewritten.
+ if err := os.WriteFile(nested, []byte("CARLOS_ADMIN_TOKEN=held\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if tok, err := scheduleToken(dd, svc, reg)(host); err != nil || tok != "held" {
+ t.Fatalf("svc %v: token %q, err %v", svc != nil, tok, err)
+ }
+ }
+}
+
+// haKeys is a backfiller that also classifies, as the exec backend does.
+type haKeys struct {
+ fakeKeyBackfiller
+ ha map[string]bool
+}
+
+func (k *haKeys) IsHA(_ context.Context, rt registry.Route) (bool, error) { return k.ha[rt.Host], nil }
+
+// The instance-key backfill skips HA hosts: their key is minted only at
+// their launch, behind the mint gate. The row's mark and the box's
+// classification each count.
+func TestHABackfillSkipsHAHosts(t *testing.T) {
+ ready := func(host string, ha bool) registry.Route {
+ return registry.Route{Host: host, Kind: "instance", Status: "ready", Socket: "/run/carlos/" + host + ".sock", HA: ha}
+ }
+ marked, recorded, shop := ready("marked.example.com", true), ready("recorded.example.com", false), ready("shop.example.com", false)
+ keys := &haKeys{fakeKeyBackfiller: fakeKeyBackfiller{got: map[string]activator.InstanceKeyOutcome{shop.Host: activator.InstanceKeyMinted}},
+ ha: map[string]bool{marked.Host: true, recorded.Host: true}}
+ g := keyBackfillInSlot{keys: keys, slot: openSlot, route: routeOf(marked, recorded, shop)}
+ st := backfillInstanceKeys(t.Context(), []registry.Route{marked, recorded, shop}, g)
+ if !reflect.DeepEqual(keys.asked, []string{shop.Host}) || st != (instanceKeyBackfill{Minted: 1, Skipped: 2}) {
+ t.Fatalf("asked %v, stats %+v", keys.asked, st)
+ }
+ // A backfiller that cannot classify: the row's mark decides.
+ plain := &fakeKeyBackfiller{}
+ g = keyBackfillInSlot{keys: plain, slot: openSlot, route: routeOf(marked)}
+ if st := backfillInstanceKeys(t.Context(), []registry.Route{marked}, g); len(plain.asked) != 0 || st != (instanceKeyBackfill{Skipped: 1}) {
+ t.Fatalf("asked %v, stats %+v", plain.asked, st)
+ }
+}
+
+func exists(path string) bool { _, err := os.Lstat(path); return err == nil }
diff --git a/internal/host/instancekeys.go b/internal/host/instancekeys.go
index c4ece4f21..5f67b8741 100644
--- a/internal/host/instancekeys.go
+++ b/internal/host/instancekeys.go
@@ -82,7 +82,17 @@ func (g keyBackfillInSlot) BackfillInstanceKey(ctx context.Context, rt registry.
case fresh == nil || !instanceKeyRoute(*fresh):
err = fmt.Errorf("%w: its row changed after the backfill listed it", errInstanceKeyRowSkipped)
default:
- got, err = g.keys.BackfillInstanceKey(ctx, *fresh)
+ // An HA host's secrets are minted only on its launch, behind the
+ // mint gate, and published before it starts (region HA P1a).
+ ha, herr := haRoute(ctx, g.keys, *fresh)
+ switch {
+ case herr != nil:
+ err = fmt.Errorf("classify it: %w", herr)
+ case ha:
+ err = fmt.Errorf("%w: a high availability host's key is minted at its launch", errInstanceKeyRowSkipped)
+ default:
+ got, err = g.keys.BackfillInstanceKey(ctx, *fresh)
+ }
}
return "", nil
})
@@ -92,6 +102,17 @@ func (g keyBackfillInSlot) BackfillInstanceKey(ctx context.Context, rt registry.
return got, err
}
+// haRoute is the box's one HA classification of rt (activator's IsHA),
+// asked of c when it can answer, else the row's sticky mark.
+func haRoute(ctx context.Context, c any, rt registry.Route) (bool, error) {
+ if h, ok := c.(interface {
+ IsHA(context.Context, registry.Route) (bool, error)
+ }); ok {
+ return h.IsHA(ctx, rt)
+ }
+ return rt.HA, nil
+}
+
// instanceKeyRoute selects the rows the backfill writes for: ready,
// activator-spawned instances, the rows execSyncable delivers config to,
// without sidecars and without replicas (a replica is never minted a key,
diff --git a/internal/host/schedule_wiring.go b/internal/host/schedule_wiring.go
index 8bcd4dcd3..da6bca31c 100644
--- a/internal/host/schedule_wiring.go
+++ b/internal/host/schedule_wiring.go
@@ -187,6 +187,22 @@ func scheduleToken(dd string, svc *activator.Service, reg *registry.Registry) fu
paths := envPathFor(dd, reg)
read := schedule.TokenFromEnvPaths(paths)
prepare := func(host string) (string, error) {
+ // An HA host's token is minted only at its launch, behind the mint
+ // gate, and its env may be one a restore is about to replace: the
+ // tick only reads, "" when there is none, and the activator fills
+ // the token from the restored env after the wake
+ // (ServeScheduled).
+ ha, err := scheduleHostHA(svc, reg, host)
+ if err != nil {
+ return "", errors.New(activator.RedactPaths(err.Error()))
+ }
+ if ha {
+ tok, err := read(host)
+ if err != nil {
+ return "", nil
+ }
+ return tok, nil
+ }
if err := activator.EnsureAdminToken(paths(host)); err != nil {
return "", errors.New(activator.RedactPaths(err.Error()))
}
@@ -206,6 +222,24 @@ func scheduleToken(dd string, svc *activator.Service, reg *registry.Registry) fu
}
}
+// scheduleHostHA is the box's HA classification of host's row
+// (Service.IsHA), false for a host with no row or no registry.
+func scheduleHostHA(svc *activator.Service, reg *registry.Registry, host string) (bool, error) {
+ if reg == nil {
+ return false, nil
+ }
+ rt, err := reg.Get(host)
+ if err != nil {
+ return false, errors.Join(errors.New("read the row"), err)
+ }
+ if rt == nil {
+ return false, nil
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ return svc.IsHA(ctx, *rt)
+}
+
// envPathFor is scheduleToken's route to an instance's own env file: flat by
// default, and the nested layout's own <host>.env (design §7) when this
// box's registry classifies the host as a nested row. It is the SAME