| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/provision" |
| 10 | "github.com/carlosframework/platform/internal/registry" |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | func rootClaimRecord(ctx context.Context, store release.Store, c *hostclaim.Claim, dataDir string) (registry.Route, string, error) { |
| 15 | if c == nil || c.Holder == nil { |
| 16 | return registry.Route{}, "", os.ErrPermission |
| 17 | } |
| 18 | h := c.Holder |
| 19 | var rt registry.Route |
| 20 | var etag, inc string |
| 21 | if h.Family == "sidecar" { |
| 22 | if h.Key != provision.SidecarRecordKey(h.Account, h.App, h.Name) { |
| 23 | return rt, "", os.ErrPermission |
| 24 | } |
| 25 | body, e, err := store.Get(ctx, h.Key) |
| 26 | if err != nil { |
| 27 | return rt, "", err |
| 28 | } |
| 29 | var rec provision.SidecarRecord |
| 30 | if json.Unmarshal(body, &rec) != nil || provision.ValidateSidecarShape(rec) != nil || rec.Account != h.Account || rec.App != h.App || rec.Name != h.Name || rec.Host != c.Host { |
| 31 | return rt, "", os.ErrPermission |
| 32 | } |
| 33 | inc, err = hostclaim.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| 34 | if err != nil { |
| 35 | return rt, "", err |
| 36 | } |
| 37 | rt = registry.Route{Host: rec.Host, Account: rec.Account, App: rec.App, Kind: "sidecar", DBPath: provision.DefaultDBPath(dataDir, rec.Host)} |
| 38 | etag = e |
| 39 | } else { |
| 40 | rec, e, err := provision.FindRecordByHost(ctx, store, c.Host) |
| 41 | if err != nil { |
| 42 | return rt, "", err |
| 43 | } |
| 44 | inc, err = hostclaim.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| 45 | if err != nil { |
| 46 | return rt, "", err |
| 47 | } |
| 48 | if h.Key != provision.RecordKey(rec.Account, rec.App, rec.Host) { |
| 49 | return rt, "", os.ErrPermission |
| 50 | } |
| 51 | rt = registry.Route{Host: rec.Host, Account: rec.Account, App: rec.App, Kind: rec.KindOrDefault()} |
| 52 | etag = e |
| 53 | if rec.DerivesPaths() { |
| 54 | rt.DBPath = provision.NestedDBPath(dataDir, rec.Host) |
| 55 | } |
| 56 | } |
| 57 | if etag == "" || rt.Account != h.Account || rt.App != h.App || inc != h.Incarnation { |
| 58 | return registry.Route{}, "", os.ErrPermission |
| 59 | } |
| 60 | return rt, etag, nil |
| 61 | } |
| 62 | |