| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 10 | "github.com/carlosframework/platform/internal/rootstate" |
| 11 | ) |
| 12 | |
| 13 | type rootLaunchVerifier struct { |
| 14 | exclude *release.ReleaseExclusion |
| 15 | open func() (rootstate.Store, error) |
| 16 | reconcile generationReconciler |
| 17 | } |
| 18 | |
| 19 | type launchRecoveryCensusKey struct{} |
| 20 | |
| 21 | func (v rootLaunchVerifier) verify(ctx context.Context, revision uint64) error { |
| 22 | if v.exclude.Check(ctx) != nil || v.open == nil || v.reconcile.read == nil || revision == 0 { |
| 23 | return rootparticipation.ErrUnavailable |
| 24 | } |
| 25 | store, err := v.open() |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if store == nil { |
| 30 | return rootparticipation.ErrUnavailable |
| 31 | } |
| 32 | defer store.Close() |
| 33 | ledger := &release.RootGenerations{Store: store} |
| 34 | records, err := ledger.Reservations(ctx) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | verifier := v.reconcile |
| 39 | read := verifier.read |
| 40 | verifier.read = func(ctx context.Context) (generationInventory, error) { |
| 41 | snapshot, err := read(ctx) |
| 42 | if err == nil && snapshot.Census.LaunchRevision != revision { |
| 43 | return generationInventory{}, rootparticipation.ErrUnavailable |
| 44 | } |
| 45 | return snapshot, err |
| 46 | } |
| 47 | if rootparticipation.IsAcquisitionRecovery(ctx) { |
| 48 | ctx = context.WithValue(ctx, launchRecoveryCensusKey{}, true) |
| 49 | } |
| 50 | _, err = ledger.Certify(ctx, records, verifier.reconcile) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | var rootLaunchStartup struct { |
| 55 | sync.Once |
| 56 | err error |
| 57 | } |
| 58 | var participationRelease = &release.ReleaseExclusion{Wait: 5 * time.Second, Open: func() (rootstate.Store, error) { |
| 59 | return rootstate.Open(rootReleasePrivate, true, false) |
| 60 | }} |
| 61 | |
| 62 | func registerProductionLaunchVerifier() error { |
| 63 | rootLaunchStartup.Do(func() { |
| 64 | v := rootLaunchVerifier{exclude: participationRelease, |
| 65 | open: func() (rootstate.Store, error) { return rootstate.Open(release.RootGenerationsDir, false, false) }, |
| 66 | reconcile: generationReconciler{read: productionLocalGenerationInventory, kernel: productionGenerationKernelCensus}, |
| 67 | } |
| 68 | rootLaunchStartup.err = rootparticipation.RegisterLaunchVerifier(v.verify, participationRelease.With) |
| 69 | }) |
| 70 | return rootLaunchStartup.err |
| 71 | } |
| 72 | |