forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
214 lines (192 loc) · 5.2 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package local
import (
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/docker/docker/pkg/idtools"
intoto "github.com/in-toto/in-toto-golang/in_toto"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/exporter"
"github.com/moby/buildkit/exporter/attestation"
"github.com/moby/buildkit/exporter/util/epoch"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/result"
"github.com/moby/buildkit/util/staticfs"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil"
fstypes "github.com/tonistiigi/fsutil/types"
)
const (
keyAttestationPrefix = "attestation-prefix"
// keyPlatformSplit is an exporter option which can be used to split result
// in subfolders when multiple platform references are exported.
keyPlatformSplit = "platform-split"
)
type CreateFSOpts struct {
Epoch *time.Time
AttestationPrefix string
PlatformSplit bool
}
func (c *CreateFSOpts) Load(opt map[string]string) (map[string]string, error) {
rest := make(map[string]string)
c.PlatformSplit = true
var err error
c.Epoch, opt, err = epoch.ParseExporterAttrs(opt)
if err != nil {
return nil, err
}
for k, v := range opt {
switch k {
case keyAttestationPrefix:
c.AttestationPrefix = v
case keyPlatformSplit:
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Wrapf(err, "non-bool value for %s: %s", keyPlatformSplit, v)
}
c.PlatformSplit = b
default:
rest[k] = v
}
}
return rest, nil
}
func CreateFS(ctx context.Context, sessionID string, k string, ref cache.ImmutableRef, attestations []exporter.Attestation, defaultTime time.Time, opt CreateFSOpts) (fsutil.FS, func() error, error) {
var cleanup func() error
var src string
var err error
var idmap *idtools.IdentityMapping
if ref == nil {
src, err = os.MkdirTemp("", "buildkit")
if err != nil {
return nil, nil, err
}
cleanup = func() error { return os.RemoveAll(src) }
} else {
mount, err := ref.Mount(ctx, true, session.NewGroup(sessionID))
if err != nil {
return nil, nil, err
}
lm := snapshot.LocalMounter(mount)
src, err = lm.Mount()
if err != nil {
return nil, nil, err
}
idmap = mount.IdentityMapping()
cleanup = lm.Unmount
}
outputFS, err := fsutil.NewFS(src)
if err != nil {
return nil, nil, err
}
// wrap the output filesystem, applying appropriate filters
filterOpt := &fsutil.FilterOpt{}
var idMapFunc func(p string, st *fstypes.Stat) fsutil.MapResult
if idmap != nil {
idMapFunc = func(p string, st *fstypes.Stat) fsutil.MapResult {
uid, gid, err := idmap.ToContainer(idtools.Identity{
UID: int(st.Uid),
GID: int(st.Gid),
})
if err != nil {
return fsutil.MapResultExclude
}
st.Uid = uint32(uid)
st.Gid = uint32(gid)
return fsutil.MapResultKeep
}
}
filterOpt.Map = func(p string, st *fstypes.Stat) fsutil.MapResult {
res := fsutil.MapResultKeep
if idMapFunc != nil {
// apply host uid/gid
res = idMapFunc(p, st)
}
if opt.Epoch != nil {
// apply used-specified epoch time
st.ModTime = opt.Epoch.UnixNano()
}
return res
}
outputFS, err = fsutil.NewFilterFS(outputFS, filterOpt)
if err != nil {
return nil, nil, err
}
attestations = attestation.Filter(attestations, nil, map[string][]byte{
result.AttestationInlineOnlyKey: []byte(strconv.FormatBool(true)),
})
attestations, err = attestation.Unbundle(ctx, session.NewGroup(sessionID), attestations)
if err != nil {
return nil, nil, err
}
if len(attestations) > 0 {
subjects := []intoto.Subject{}
err = outputFS.Walk(ctx, "", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.Type().IsRegular() {
return nil
}
f, err := outputFS.Open(path)
if err != nil {
return err
}
defer f.Close()
d := digest.Canonical.Digester()
if _, err := io.Copy(d.Hash(), f); err != nil {
return err
}
subjects = append(subjects, intoto.Subject{
Name: path,
Digest: result.ToDigestMap(d.Digest()),
})
return nil
})
if err != nil {
return nil, nil, err
}
stmts, err := attestation.MakeInTotoStatements(ctx, session.NewGroup(sessionID), attestations, subjects)
if err != nil {
return nil, nil, err
}
stmtFS := staticfs.NewFS()
names := map[string]struct{}{}
for i, stmt := range stmts {
dt, err := json.MarshalIndent(stmt, "", " ")
if err != nil {
return nil, nil, errors.Wrap(err, "failed to marshal attestation")
}
name := opt.AttestationPrefix + path.Base(attestations[i].Path)
if !opt.PlatformSplit {
nameExt := path.Ext(name)
namBase := strings.TrimSuffix(name, nameExt)
name = fmt.Sprintf("%s.%s%s", namBase, strings.ReplaceAll(k, "/", "_"), nameExt)
}
if _, ok := names[name]; ok {
return nil, nil, errors.Errorf("duplicate attestation path name %s", name)
}
names[name] = struct{}{}
st := fstypes.Stat{
Mode: 0600,
Path: name,
ModTime: defaultTime.UnixNano(),
}
if opt.Epoch != nil {
st.ModTime = opt.Epoch.UnixNano()
}
stmtFS.Add(name, st, dt)
}
outputFS = staticfs.NewMergeFS(outputFS, stmtFS)
}
return outputFS, cleanup, nil
}