carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "context"
5 "crypto/sha256"
6 "encoding/hex"
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/carlosframework/platform/internal/hostbox"
14 "github.com/carlosframework/platform/internal/hostclaim"
15 "github.com/carlosframework/platform/internal/hostguard"
16 "github.com/carlosframework/platform/internal/hostwrite"
17 "golang.org/x/sys/unix"
18)
19
20func (a *ClaimRoleAssembly) prepareStateRemote(ctx context.Context, o hostclaim.Observed, work hostwrite.Work, in ClaimLaunchInputs, actor hostbox.Actor) error {
21 if a.Claims == nil || a.Guard.FenceView == nil || o.Claim == nil || o.Claim.Holder == nil {
22 return hostbox.ErrUnsupported
23 }
24 h := o.Claim.Holder
25 _, etag, err := a.Claims.Get(ctx, h.Key)
26 if err != nil {
27 return err
28 }
29 selected := hostbox.RecordSelection{Host: o.Host, Key: h.Key, ETag: etag, Incarnation: h.Incarnation, Family: h.Family}
30 captured := hostguard.Captured{Incarnation: h.Incarnation, Stamp: h.Stamp}
31 client := hostbox.ReconcileAdapter{Reconciler: hostbox.Reconciler{Guard: a.Guard, Claims: a.Claims}, Box: actor.Box, Enrollment: actor.Enrollment}
32 for attempt := 0; attempt < 3; attempt++ {
33 inventory, revision, err := a.Inventory.Snapshot(ctx, o.Host, captured)
34 if err != nil {
35 return err
36 }
37 if !inventory.Complete || inventory.Host != o.Host || inventory.Incarnation != h.Incarnation || inventory.InstallationStamp != h.Stamp || inventory.Box != actor.Box || inventory.Enrollment != actor.Enrollment || inventory.DirectoryRevision != actor.Binding {
38 return hostbox.ErrTypedInventory
39 }
40 var expected hostbox.InventoryExpectation
41 var changed bool
42 err = work.With(ctx, in.Identity, func() error {
43 var err error
44 expected, err = client.CaptureInventory(ctx, selected, captured, revision)
45 if err != nil {
46 return err
47 }
48 changed, err = a.prepareStateInventory(in, &inventory, actor)
49 return err
50 })
51 if err != nil {
52 return err
53 }
54 if !changed {
55 return nil
56 }
57 revision, err = a.Inventory.WriteExpected(ctx, inventory, expected)
58 if err != nil {
59 return err
60 }
61 err = work.With(ctx, in.Identity, func() error { return client.RevalidateInventory(ctx, selected, captured, expected, revision) })
62 if err != nil {
63 return err
64 }
65 }
66 return hostbox.ErrChanged
67}
68
69func (a *ClaimRoleAssembly) prepareStateInventory(in ClaimLaunchInputs, inventory *hostbox.StopInventory, actor hostbox.Actor) (bool, error) {
70 root := a.ManagedRoots["data"]
71 relative, err := filepath.Rel(root, filepath.Dir(in.DBPath))
72 if err != nil || !filepath.IsLocal(relative) || relative == "." {
73 return false, hostbox.ErrChanged
74 }
75 ownerDigest := sha256.Sum256([]byte(fmt.Sprintf("%s\x00%s\x00%d\x00%s", inventory.Host, inventory.Incarnation, inventory.InstallationStamp, actor.Box)))
76 owner := hex.EncodeToString(ownerDigest[:])
77 index := -1
78 for i, e := range inventory.Entries {
79 if e.Kind == "path" && e.ManagedRoot == "data" && e.RelativePath == relative {
80 if index != -1 || e.Ownership != owner {
81 return false, hostbox.ErrTypedInventory
82 }
83 index = i
84 }
85 }
86 fd, err := unix.Open(root, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
87 if err != nil {
88 return false, err
89 }
90 defer func() { unix.Close(fd) }()
91 components := strings.Split(filepath.ToSlash(relative), "/")
92 for _, part := range components[:len(components)-1] {
93 next, e := unix.Openat(fd, part, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
94 if e != nil {
95 return false, e
96 }
97 unix.Close(fd)
98 fd = next
99 }
100 leaf := components[len(components)-1]
101 var stat unix.Stat_t
102 err = unix.Fstatat(fd, leaf, &stat, unix.AT_SYMLINK_NOFOLLOW)
103 if err == nil {
104 if index < 0 {
105 return false, hostbox.ErrTypedInventory
106 }
107 e := inventory.Entries[index]
108 if e.Materialization != "materialized" || stat.Mode&unix.S_IFMT != unix.S_IFDIR || verifyPreparedStateIdentity(root, e) != nil {
109 return false, hostbox.ErrTypedInventory
110 }
111 return false, nil
112 }
113 if !errors.Is(err, os.ErrNotExist) {
114 return false, err
115 }
116 if index >= 0 && inventory.Entries[index].Materialization == "materialized" {
117 return false, hostbox.ErrChanged
118 }
119 if index < 0 {
120 sum := sha256.Sum256([]byte(owner + "\x00state-dir\x00" + relative))
121 inventory.Entries = append(inventory.Entries, hostbox.StopInventoryEntry{Kind: "path", ArtifactID: hex.EncodeToString(sum[:]), ManagedRoot: "data", RelativePath: relative, Ownership: owner, Type: unix.S_IFDIR, Materialization: "intended"})
122 return true, nil
123 }
124 if err := unix.Mkdirat(fd, leaf, 0700); err != nil {
125 return false, err
126 }
127 if err := unix.Fstatat(fd, leaf, &stat, unix.AT_SYMLINK_NOFOLLOW); err != nil {
128 return false, err
129 }
130 if err := unix.Fsync(fd); err != nil {
131 return false, err
132 }
133 entry := &inventory.Entries[index]
134 entry.Identity, err = hostbox.CaptureInventoryIdentity(root, relative)
135 if err != nil {
136 return false, err
137 }
138 entry.Device = entry.Identity.Device
139 entry.Inode = entry.Identity.Inode
140 entry.Type = unix.S_IFDIR
141 entry.Materialization = "materialized"
142 return true, nil
143}
144
145func verifyPreparedStateIdentity(root string, e hostbox.StopInventoryEntry) error {
146 if err := hostbox.VerifyInventoryIdentity(root, e); err == nil || !errors.Is(err, hostbox.ErrChanged) {
147 return err
148 }
149 current, err := hostbox.CaptureInventoryIdentity(root, e.RelativePath)
150 if err != nil {
151 return err
152 }
153 if e.Identity.Device != e.Device || e.Identity.Inode != e.Inode || e.Identity.Mode&unix.S_IFMT != e.Type ||
154 current.Boot != e.Identity.Boot || current.SameMountNamespace(e.Identity) || !current.Same(e.Identity) {
155 return hostbox.ErrChanged
156 }
157 return nil
158}
159