package host import ( "context" "strconv" "time" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostops" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) func verifySidecarWake(ctx context.Context, s release.Store, rt registry.Route, w release.SidecarWake) error { if w.Schema != 1 || w.AppID == "" || w.Incarnation == "" || w.RequestID == "" { return hostops.ErrIdentity } acct := release.RouteAccount(rt) a, err := hostops.ReadAdmission(ctx, s, acct, rt.App, w.AppID) if err != nil { return err } if a.Value.State != "open" || a.Value.Op != nil { return hostops.ErrPending } effect, ok := a.Value.Effects[w.RequestID] key := release.SidecarWakeKey(acct, rt.App, rt.Slug) if !ok || effect.State != "complete" || effect.ID != w.RequestID || effect.OperationID != w.RequestID || effect.AppID != w.AppID || effect.Action != "wake" || effect.Resource != "control-object:"+key || effect.ResultReference != w.RequestID || effect.Inputs["key"] != key || effect.Inputs["incarnation"] != w.Incarnation || effect.Inputs["request_id"] != w.RequestID || effect.Inputs["admission_epoch"] != strconv.FormatUint(a.Value.Epoch, 10) { return hostops.ErrIdentity } created, err := time.Parse(time.RFC3339Nano, effect.Inputs["created_at"]) if err != nil || created.IsZero() { return hostops.ErrIdentity } // Recovery may remove a settled operation's registration. The durable // completed effect remains the request's binding after that cleanup. if reg, present := a.Value.Registrations[w.RequestID]; present { if reg.Kind != "wake" || reg.Owner != effect.Owner || reg.AppID != w.AppID || reg.Epoch != a.Value.Epoch || reg.Host != rt.Host || reg.Incarnation != w.Incarnation || !reg.CreatedAt.Equal(created) { return hostops.ErrIdentity } } obs, err := hostclaim.Read(ctx, s, rt.Host) if err != nil { return err } if obs.Absent || obs.Claim.Holder == nil { return hostops.ErrPending } h := obs.Claim.Holder if obs.Claim.Op.Kind != hostclaim.KindServing || h.Family != "sidecar" || h.Account != acct || h.App != rt.App || h.Name != rt.Slug || h.Incarnation != w.Incarnation || !h.CreatedAt.Equal(created) { return hostops.ErrIdentity } return nil }