package host import ( "context" "database/sql" "errors" "fmt" "os" "reflect" "slices" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" execlease "github.com/carlosframework/platform/internal/hostexec" "github.com/carlosframework/platform/internal/hostguard" "github.com/carlosframework/platform/internal/park" "github.com/carlosframework/platform/internal/rootparticipation" ) var errReinstallRetired = errors.New("hostname was retired on this box; reinstalling it is not yet supported") func (h ClaimRootHTTP) executionAuthority(ctx context.Context, q execlease.ExecutionRequest, _ execlease.ControllerProcess) (execlease.ExecutionAuthority, error) { var out execlease.ExecutionAuthority if h.InventoryActor == nil || h.Stop.Coordinator.Guard.Root == nil || h.Reconcile.Census == nil || h.Journals == nil { return out, hostbox.ErrUnsupported } actor, err := h.InventoryActor(ctx) if err != nil { return out, err } if h.RequireNewWork != nil { if err = h.RequireNewWork(ctx); err != nil { return out, err } } obs, err := hostclaim.Read(ctx, h.Stop.Coordinator.Evidence, q.Host) if err != nil { return out, err } c := obs.Claim if obs.Absent || c == nil || c.Holder == nil || c.Validate() != nil || c.Host != q.Host || c.Holder.Incarnation != q.Incarnation || c.Holder.Stamp != q.Stamp || c.Holder.Session != q.Session || c.Op.ID != q.Op || slices.Contains(c.Lost, actor.Box) { return out, hostbox.ErrChanged } if c.Op.Kind != hostclaim.KindServing && !slices.Contains(c.Op.Participants, actor.Box) { return out, hostbox.ErrChanged } if c.Op.Kind == hostclaim.KindServing && execlease.PreinstallationParticipant(c, actor.Box, actor.Enrollment) != nil { return out, hostbox.ErrChanged } retirement := c.Op.Kind == hostclaim.KindDeleting || c.Op.Kind == hostclaim.KindParking if retirement { home, homeErr := park.VerifiedHome(actor.FleetHomeRegion, actor.HomeRegion, actor.Region, c.Op.Intent["home_region"]) if homeErr != nil || actor.FleetHomeRegion == "" || actor.Region != actor.FleetHomeRegion || c.Holder.Family != "instance" || c.Holder.Location != "instances/"+c.Host+"/"+c.Holder.Incarnation || c.Op.Exec != actor.Box || c.Op.Intent["key"] != c.Holder.Key || c.Op.Intent["etag"] == "" { return out, hostbox.ErrChanged } for _, admission := range c.Holder.Admissions { if admission.Box != actor.Box || !park.SameRegion(home, admission.Region, actor.Region) || (admission.PredecessorRegion != "" && !park.SameRegion(home, admission.PredecessorRegion, actor.Region)) { return out, hostbox.ErrChanged } } } _, etag, err := rootClaimRecord(ctx, h.Stop.Coordinator.Evidence, c, h.Roots["data"]) if retirement && (c.Op.Kind == hostclaim.KindParking || errors.Is(err, os.ErrNotExist)) { if !errors.Is(err, os.ErrNotExist) { return out, hostbox.ErrChanged } if _, _, absent := h.Stop.Coordinator.Evidence.Get(ctx, c.Holder.Key); !errors.Is(absent, os.ErrNotExist) { return out, hostbox.ErrChanged } etag, err = c.Op.Intent["etag"], nil } if err != nil { return out, err } if retirement && etag != c.Op.Intent["etag"] { return out, hostbox.ErrChanged } out = execlease.ExecutionAuthority{Box: actor.Box, Enrollment: actor.Enrollment, Account: c.Holder.Account, App: c.Holder.App, ClaimRevision: obs.ETag, RecordRevision: etag, DirectoryRevision: actor.Binding, Kind: c.Op.Kind, Phase: c.Op.Phase} captured := hostguard.Captured{Incarnation: q.Incarnation, Stamp: q.Stamp} view, err := h.Stop.Coordinator.Guard.Root.View(ctx, q.Host, captured) if retirement && err != nil { return out, err } if errors.Is(err, hostguard.ErrStampMismatch) { if execlease.PreinstallationParticipant(c, actor.Box, actor.Enrollment) == nil { retired, checkErr := h.Stop.Coordinator.Guard.Root.HostRetired(ctx, q.Host) if checkErr != nil { return out, checkErr } if retired { return out, fmt.Errorf("%s: %w", q.Host, errReinstallRetired) } } return h.retainedExecutionSource(ctx, q, actor, c, out) } if errors.Is(err, hostguard.ErrFenced) { in, raw, pendingErr := h.Stop.Coordinator.Guard.Root.PendingInstallation(ctx, q.Host) if pendingErr != nil { return out, pendingErr } var pending hostbox.JournalRequest if rootparticipation.Decode(raw, &pending) != nil || pending.Op != "inventory-write" || pending.Revision != 0 || pending.RecordKey != c.Holder.Key || pending.RecordETag != etag || pending.Target.ClaimETag != obs.ETag || pending.Target.Host != q.Host || pending.Target.Incarnation != q.Incarnation || pending.Target.Stamp != q.Stamp || pending.Target.Session != q.Session || pending.Target.Op != q.Op || pending.Target.Phase != c.Op.Phase || pending.Target.Box != actor.Box || pending.Target.Enrollment != actor.Enrollment || in.Host != q.Host || in.Incarnation != q.Incarnation || in.Stamp != q.Stamp || in.Account != out.Account || in.App != out.App || in.Record != c.Holder.Key || in.Box != actor.Box || in.Enrollment != actor.Enrollment || in.Migration == 0 { return out, hostbox.ErrChanged } if err := execlease.PreinstallationParticipant(c, actor.Box, actor.Enrollment); err != nil { return out, err } inventory, revision, err := h.Journals.Inventory.Snapshot(ctx, q.Host, captured) if err != nil { return out, err } if revision == 0 { selected := hostbox.RecordSelection{Host: q.Host, Key: c.Holder.Key, ETag: etag, Incarnation: q.Incarnation, Family: c.Holder.Family} if err := h.rootEmptyCensus().CheckLocal(ctx, obs, selected); err != nil { return out, err } } else { expected := pending.Inventory if c.Op.Kind == hostclaim.KindLanding { expected.Entries = []hostbox.StopInventoryEntry{rootLandingEntry(pending, c, actor)} } if revision != in.InventoryRevision || !reflect.DeepEqual(inventory, expected) { return out, hostbox.ErrTypedInventory } } out.ParticipationOnly = true out.Migration = in.Migration out.Installation = fmt.Sprintf("%s/%d/%s/%s", in.Incarnation, in.Stamp, in.Box, in.Enrollment) return out, nil } if errors.Is(err, hostguard.ErrNoRecord) { if err = execlease.PreinstallationParticipant(c, actor.Box, actor.Enrollment); err != nil { return out, err } // An orphan journal is not a root registration, even when its complete bit is set. if _, err = h.Journals.Inventory.ReadStopInventory(ctx, q.Host); !errors.Is(err, sql.ErrNoRows) { return out, hostbox.ErrTypedInventory } selected := hostbox.RecordSelection{Host: q.Host, Key: c.Holder.Key, ETag: etag, Incarnation: q.Incarnation, Family: c.Holder.Family} if err = h.rootEmptyCensus().CheckLocal(ctx, obs, selected); err != nil { return out, err } out.ParticipationOnly = true return out, nil } if err != nil { return out, err } in := view.Installation if in.Host != q.Host || in.Incarnation != q.Incarnation || in.Stamp != q.Stamp || in.Account != out.Account || in.App != out.App || in.Record != c.Holder.Key || in.Box != actor.Box || in.Enrollment != actor.Enrollment || in.Migration == 0 || in.InventoryRevision == 0 { return out, hostbox.ErrChanged } if view.Barrier != nil || (slices.Contains(view.Retired, captured) && !(retirement && c.Op.Kind == hostclaim.KindParking)) { return out, hostguard.ErrFenced } denied := func(t hostguard.FenceTarget) bool { return t.Incarnation == q.Incarnation && t.Stamp == q.Stamp && t.Session == q.Session } if view.Pending != nil && denied(*view.Pending) { completed := false if retirement { for _, receipt := range view.Receipts { if receipt.Target == *view.Pending && verifyRetirementReceipt(ctx, receipt) == nil { completed = true } } } if !completed { return out, hostguard.ErrFenced } } for _, receipt := range view.Receipts { if denied(receipt.Target) && !retirement { return out, hostguard.ErrFenced } } _, revision, err := h.Journals.Inventory.Snapshot(ctx, q.Host, captured) if err != nil { return out, err } if revision != in.InventoryRevision { return out, hostbox.ErrChanged } out.ParticipationOnly = retirement out.Migration = in.Migration out.Installation = fmt.Sprintf("%s/%d/%s/%s", in.Incarnation, in.Stamp, in.Box, in.Enrollment) return out, nil }