package host import ( "context" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostguard" "github.com/carlosframework/platform/internal/release" "github.com/carlosframework/platform/internal/rootparticipation" "github.com/carlosframework/platform/internal/rootstate" ) const rootMigrationDir = "/var/lib/carlos-claims/authority/migration" var openRootMigrationParticipation = rootparticipation.OpenForUpdate // openRootMigrationState opens the root-owned migration record directory // read-only. A variable so tests can stand in for the root-owned directory. var openRootMigrationState = func() (rootstate.Store, error) { return rootstate.Open(rootMigrationDir, false, false) } func verifyRootMigrationRetained(ctx context.Context, root *hostguard.RootAuthority, snapshot rootMigrationSnapshot) error { views, err := root.MigrationInstallations(ctx) if err != nil { return err } byHost := map[string]hostguard.FenceView{} for _, v := range views { byHost[v.Host] = v } for _, item := range snapshot.Items { v, ok := byHost[item.Installation.Host] if !ok { return errRootMigrationBusy } in := item.Installation denied, stopped := false, false for _, d := range v.MigrationDenies { if d.Incarnation == in.Incarnation && d.Stamp >= in.Stamp && d.Session >= item.Session { denied = true } } for _, r := range v.Receipts { if r.Target.Host == in.Host && r.Target.Incarnation == in.Incarnation && r.Target.Stamp == in.Stamp && r.Target.Session == item.Session && r.Target.Box == in.Box && r.Target.Enrollment == in.Enrollment && len(r.Bytes) > 0 && !r.EvidenceAt.IsZero() { stopped = true } } if !denied || !stopped || v.Installation.InventoryRevision == 0 || v.Installation.Artifacts.Schema != 1 { return errRootMigrationBusy } } return nil } func verifyRootMigrationState(ctx context.Context, state, execution rootstate.Store, id rootMigrationIdentity, root *hostguard.RootAuthority) error { r, err := readRootMigrationState(state, execution, id) if err != nil { return err } return verifyRootMigrationRetained(ctx, root, r.Snapshot) } func readRootMigrationState(state, execution rootstate.Store, id rootMigrationIdentity) (rootMigrationRecord, error) { var r rootMigrationRecord c, err := (rootparticipation.Ledger{State: execution}).RecoveryCensus() if err != nil { return r, err } if c.Box != id.Box || c.Enrollment != id.Enrollment || c.Migration != id.Generation || !c.Complete || c.NeverEnrolled { return r, errRootMigrationBusy } raw, err := state.Read("migration.json") if err != nil { return r, err } if rootparticipation.Decode(raw, &r) != nil || r.Schema != 1 || r.Identity != id || r.Phase != "complete" || r.Snapshot.preflight(id) != nil { return r, errRootMigrationBusy } return r, nil } func verifyRootMigrationStartup(ctx context.Context, id rootFloorIdentity, inventory *hostbox.StopInventoryDB) error { l, required, err := openRootMigrationParticipation() if l.State != nil { defer l.State.Close() } if err != nil || !required { return err } c, err := l.RecoveryCensus() if err != nil { return err } state, err := openRootMigrationState() if err != nil { return err } defer state.Close() fences, err := openClaimRootAuthority() if err != nil { return err } defer fences.Close() root := hostguard.NewRootAuthority(fences) if err := verifyRootMigrationState(ctx, state, l.State, rootMigrationIdentity{Deployment: id.Deployment, Box: id.Box, Enrollment: id.Enrollment, Generation: c.Migration}, root); err != nil { return err } views, err := root.MigrationInstallations(ctx) if err != nil { return err } for _, v := range views { if inventory == nil { return errRootMigrationBusy } in, revision, err := inventory.Snapshot(ctx, v.Host, hostguard.Captured{Incarnation: v.Installation.Incarnation, Stamp: v.Installation.Stamp}) // A settled installation whose first inventory write never landed // has no inventory, and root's record says so: nothing to clean. if err == nil && v.Settled && revision == 0 && v.Installation.InventoryRevision == 0 && v.Installation.Box == id.Box && v.Installation.Enrollment == id.Enrollment { continue } if err != nil || !in.Complete || revision == 0 || revision != v.Installation.InventoryRevision || v.Installation.Box != id.Box || v.Installation.Enrollment != id.Enrollment { return errRootMigrationBusy } } return rootMigrationStartupFloors(release.OptDir(), id) } // rootMigrationStartupFloors is the startup's installed-binary floor check. // A variable so tests without root-owned floor binaries can stand in for it; // the census before it runs unchanged. var rootMigrationStartupFloors = verifyRootMigrationFloors func verifyRootMigrationFloors(opt string, id rootFloorIdentity) error { bound, err := readRootProtocolBound(rootFloorDir(opt), id) if err != nil { return err } for _, path := range []string{systemBinaryPath(opt), systemGuardDir(opt) + "/carlos"} { b, err := readRootFloorBinary(opt, path) if err != nil { return err } if !b.supports(nil) { return errRootFloorHistory } if !b.supports(bound) { rootFloorAlert() } if _, err := readRootSeedsBound(opt, id); err != nil { return err } } return nil }