| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "github.com/carlosframework/platform/internal/activator" |
| 7 | "reflect" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/hostbox" |
| 10 | "github.com/carlosframework/platform/internal/hostclaim" |
| 11 | "github.com/carlosframework/platform/internal/hostfiles" |
| 12 | "github.com/carlosframework/platform/internal/registry" |
| 13 | "github.com/carlosframework/platform/internal/release" |
| 14 | ) |
| 15 | |
| 16 | // This boundary only reads current-version metadata. Normal C8 still proves |
| 17 | // and validates the selected manifest after admission; presence is not proof. |
| 18 | func (f *movesFiles) checkServingFilesEvidence(ctx context.Context, o hostclaim.Observed, rt registry.Route) error { |
| 19 | if f == nil || f.Data == nil || o.Claim == nil || o.Claim.Holder == nil { |
| 20 | return hostbox.ErrUnsupported |
| 21 | } |
| 22 | if _, err := checkFilesSeedLayout(f, rt, claimEmptyBootstrap(o.Claim.Holder)); err != nil { |
| 23 | return err |
| 24 | } |
| 25 | h := o.Claim.Holder |
| 26 | if claimEmptyBootstrap(h) { |
| 27 | return nil |
| 28 | } |
| 29 | if f.emptyRecovery != nil { |
| 30 | yes, err := f.emptyRecovery(ctx, o, h.Admissions) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | if yes { |
| 35 | return nil |
| 36 | } |
| 37 | } |
| 38 | bounded, cancel := context.WithTimeout(ctx, hostbox.RequestLimit) |
| 39 | defer cancel() |
| 40 | chain := movesFilesChain(h, f.HomeRegion, f.Region) |
| 41 | if h.Generation == "" { |
| 42 | chain = []string{""} |
| 43 | } |
| 44 | for _, gen := range chain { |
| 45 | version, err := f.manifestVersion(bounded, hostfiles.ManifestKey(movesFilesPrefix(h.Location, gen))) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | if version != "" { |
| 50 | return nil |
| 51 | } |
| 52 | } |
| 53 | if f.Evidence != nil { |
| 54 | pin, found, err := f.Evidence(bounded, o) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if found && pin.NeverPublished() { |
| 59 | return nil |
| 60 | } |
| 61 | } |
| 62 | return hostfiles.ErrNoEvidence |
| 63 | } |
| 64 | |
| 65 | func servingFilesPrerequisites(claims release.Store, files ClaimFilesManifest) func(context.Context, registry.Route, hostclaim.Observed) error { |
| 66 | return func(ctx context.Context, rt registry.Route, o hostclaim.Observed) error { |
| 67 | if o.Claim == nil || o.Claim.Op.Kind != hostclaim.KindServing { |
| 68 | return nil |
| 69 | } |
| 70 | f, ok := files.(interface { |
| 71 | checkServingFilesEvidence(context.Context, hostclaim.Observed, registry.Route) error |
| 72 | }) |
| 73 | if !ok || claims == nil { |
| 74 | return hostbox.ErrUnsupported |
| 75 | } |
| 76 | if err := f.checkServingFilesEvidence(ctx, o, rt); err != nil { |
| 77 | if errors.Is(err, hostfiles.ErrNoEvidence) && o.Claim.Holder != nil { |
| 78 | return &activator.ServingFilesMissing{Incarnation: o.Claim.Holder.Incarnation} |
| 79 | } |
| 80 | return err |
| 81 | } |
| 82 | fresh, err := hostclaim.Read(ctx, claims, o.Host) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | if fresh.Absent || fresh.ETag != o.ETag || !reflect.DeepEqual(fresh.Claim, o.Claim) { |
| 87 | return hostbox.ErrChanged |
| 88 | } |
| 89 | return ctx.Err() |
| 90 | } |
| 91 | } |
| 92 | |