-
Notifications
You must be signed in to change notification settings - Fork 351
/
adapter.go
493 lines (456 loc) · 12.8 KB
/
adapter.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package local
import (
"context"
"crypto/md5" //nolint:gosec
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/logging"
)
type Adapter struct {
path string
removeEmptyDir bool
}
var (
ErrPathNotWritable = errors.New("path provided is not writable")
ErrInventoryNotSupported = errors.New("inventory feature not implemented for local storage adapter")
ErrInvalidUploadIDFormat = errors.New("invalid upload id format")
ErrBadPath = errors.New("bad path traversal blocked")
)
func WithRemoveEmptyDir(b bool) func(a *Adapter) {
return func(a *Adapter) {
a.removeEmptyDir = b
}
}
func NewAdapter(path string, opts ...func(a *Adapter)) (*Adapter, error) {
// Clean() the path so that misconfiguration does not allow path traversal.
path = filepath.Clean(path)
err := os.MkdirAll(path, 0o700) //nolint: gomnd
if err != nil {
return nil, err
}
if !isDirectoryWritable(path) {
return nil, ErrPathNotWritable
}
localAdapter := &Adapter{
path: path,
removeEmptyDir: true,
}
for _, opt := range opts {
opt(localAdapter)
}
return localAdapter, nil
}
func resolveNamespace(obj block.ObjectPointer) (block.QualifiedKey, error) {
qualifiedKey, err := block.ResolveNamespace(obj.StorageNamespace, obj.Identifier, obj.IdentifierType)
if err != nil {
return qualifiedKey, err
}
if qualifiedKey.StorageType != block.StorageTypeLocal {
return qualifiedKey, block.ErrInvalidNamespace
}
return qualifiedKey, nil
}
// verifyPath ensures that p is under the directory controlled by this adapter. It does not
// examine the filesystem and can mistakenly error out when symbolic links are involved.
func (l *Adapter) verifyPath(p string) error {
if !strings.HasPrefix(filepath.Clean(p), l.path) {
return fmt.Errorf("%s: %w", p, ErrBadPath)
}
return nil
}
func (l *Adapter) getPath(identifier block.ObjectPointer) (string, error) {
obj, err := resolveNamespace(identifier)
if err != nil {
return "", err
}
p := path.Join(l.path, obj.StorageNamespace, obj.Key)
if err = l.verifyPath(p); err != nil {
return "", err
}
return p, nil
}
// maybeMkdir verifies path is allowed and runs f(path), but if f fails due to file-not-found
// MkdirAll's its dir and then runs it again.
func (l *Adapter) maybeMkdir(path string, f func(p string) (*os.File, error)) (*os.File, error) {
if err := l.verifyPath(path); err != nil {
return nil, err
}
ret, err := f(path)
if !errors.Is(err, os.ErrNotExist) {
return ret, err
}
d := filepath.Dir(filepath.Clean(path))
if err = os.MkdirAll(d, 0o750); err != nil { //nolint: gomnd
return nil, err
}
return f(path)
}
func (l *Adapter) Path() string {
return l.path
}
func (l *Adapter) Put(_ context.Context, obj block.ObjectPointer, _ int64, reader io.Reader, _ block.PutOpts) error {
p, err := l.getPath(obj)
if err != nil {
return err
}
p = filepath.Clean(p)
f, err := l.maybeMkdir(p, os.Create)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
_, err = io.Copy(f, reader)
return err
}
func (l *Adapter) Remove(_ context.Context, obj block.ObjectPointer) error {
p, err := l.getPath(obj)
if err != nil {
return err
}
p = filepath.Clean(p)
err = os.Remove(p)
if err != nil {
return err
}
if l.removeEmptyDir {
dir := filepath.Dir(p)
removeEmptyDirUntil(dir, l.path)
}
return nil
}
func removeEmptyDirUntil(dir string, stopAt string) {
if stopAt == "" {
return
}
if !strings.HasSuffix(stopAt, "/") {
stopAt += "/"
}
for strings.HasPrefix(dir, stopAt) && dir != stopAt {
err := os.Remove(dir)
if err != nil {
break
}
dir = filepath.Dir(dir)
if dir == "/" {
break
}
}
}
func (l *Adapter) Copy(_ context.Context, sourceObj, destinationObj block.ObjectPointer) error {
source, err := l.getPath(sourceObj)
if err != nil {
return err
}
sourceFile, err := os.Open(filepath.Clean(source))
defer func() {
_ = sourceFile.Close()
}()
if err != nil {
return err
}
dest, err := l.getPath(destinationObj)
if err != nil {
return err
}
destinationFile, err := l.maybeMkdir(dest, os.Create)
if err != nil {
return err
}
defer func() {
_ = destinationFile.Close()
}()
_, err = io.Copy(destinationFile, sourceFile)
return err
}
func (l *Adapter) UploadCopyPart(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
if err := isValidUploadID(uploadID); err != nil {
return nil, err
}
r, err := l.Get(ctx, sourceObj, 0)
if err != nil {
return nil, err
}
md5Read := block.NewHashingReader(r, block.HashFunctionMD5)
fName := uploadID + fmt.Sprintf("-%05d", partNumber)
err = l.Put(ctx, block.ObjectPointer{StorageNamespace: destinationObj.StorageNamespace, Identifier: fName}, -1, md5Read, block.PutOpts{})
if err != nil {
return nil, err
}
etag := hex.EncodeToString(md5Read.Md5.Sum(nil))
return &block.UploadPartResponse{
ETag: etag,
}, nil
}
func (l *Adapter) UploadCopyPartRange(ctx context.Context, sourceObj, destinationObj block.ObjectPointer, uploadID string, partNumber int, startPosition, endPosition int64) (*block.UploadPartResponse, error) {
if err := isValidUploadID(uploadID); err != nil {
return nil, err
}
r, err := l.GetRange(ctx, sourceObj, startPosition, endPosition)
if err != nil {
return nil, err
}
md5Read := block.NewHashingReader(r, block.HashFunctionMD5)
fName := uploadID + fmt.Sprintf("-%05d", partNumber)
err = l.Put(ctx, block.ObjectPointer{StorageNamespace: destinationObj.StorageNamespace, Identifier: fName}, -1, md5Read, block.PutOpts{})
if err != nil {
return nil, err
}
etag := hex.EncodeToString(md5Read.Md5.Sum(nil))
return &block.UploadPartResponse{
ETag: etag,
}, err
}
func (l *Adapter) Get(_ context.Context, obj block.ObjectPointer, _ int64) (reader io.ReadCloser, err error) {
p, err := l.getPath(obj)
if err != nil {
return nil, err
}
f, err := os.OpenFile(filepath.Clean(p), os.O_RDONLY, 0o600) //nolint: gomnd
if os.IsNotExist(err) {
return nil, block.ErrDataNotFound
}
if err != nil {
return nil, err
}
return f, nil
}
func (l *Adapter) Walk(_ context.Context, walkOpt block.WalkOpts, walkFn block.WalkFunc) error {
p := filepath.Clean(path.Join(l.path, walkOpt.StorageNamespace, walkOpt.Prefix))
return filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
return walkFn(p)
})
}
func (l *Adapter) Exists(_ context.Context, obj block.ObjectPointer) (bool, error) {
p, err := l.getPath(obj)
if err != nil {
return false, err
}
_, err = os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func (l *Adapter) GetRange(_ context.Context, obj block.ObjectPointer, start int64, end int64) (io.ReadCloser, error) {
p, err := l.getPath(obj)
if err != nil {
return nil, err
}
f, err := os.Open(filepath.Clean(p))
if err != nil {
if os.IsNotExist(err) {
return nil, block.ErrDataNotFound
}
return nil, err
}
return &struct {
io.Reader
io.Closer
}{
Reader: io.NewSectionReader(f, start, end-start+1),
Closer: f,
}, nil
}
func (l *Adapter) GetProperties(_ context.Context, obj block.ObjectPointer) (block.Properties, error) {
p, err := l.getPath(obj)
if err != nil {
return block.Properties{}, err
}
_, err = os.Stat(p)
if err != nil {
return block.Properties{}, err
}
// No properties, just return that it exists
return block.Properties{}, nil
}
// isDirectoryWritable tests that pth, which must not be controllable by user input, is a
// writable directory. As there is no simple way to test this in windows, I prefer the "brute
// force" method of creating s dummy file. Will work in any OS. speed is not an issue, as
// this will be activated very few times during startup.
func isDirectoryWritable(pth string) bool {
f, err := os.CreateTemp(pth, "dummy")
if err != nil {
return false
}
_ = f.Close()
_ = os.Remove(f.Name())
return true
}
func (l *Adapter) CreateMultiPartUpload(_ context.Context, obj block.ObjectPointer, _ *http.Request, _ block.CreateMultiPartUploadOpts) (*block.CreateMultiPartUploadResponse, error) {
if strings.Contains(obj.Identifier, "/") {
fullPath, err := l.getPath(obj)
if err != nil {
return nil, err
}
fullDir := path.Dir(fullPath)
err = os.MkdirAll(fullDir, 0o750) //nolint: gomnd
if err != nil {
return nil, err
}
}
uidBytes := uuid.New()
uploadID := hex.EncodeToString(uidBytes[:])
return &block.CreateMultiPartUploadResponse{
UploadID: uploadID,
}, nil
}
func (l *Adapter) UploadPart(ctx context.Context, obj block.ObjectPointer, _ int64, reader io.Reader, uploadID string, partNumber int) (*block.UploadPartResponse, error) {
if err := isValidUploadID(uploadID); err != nil {
return nil, err
}
md5Read := block.NewHashingReader(reader, block.HashFunctionMD5)
fName := uploadID + fmt.Sprintf("-%05d", partNumber)
err := l.Put(ctx, block.ObjectPointer{StorageNamespace: obj.StorageNamespace, Identifier: fName}, -1, md5Read, block.PutOpts{})
etag := hex.EncodeToString(md5Read.Md5.Sum(nil))
return &block.UploadPartResponse{
ETag: etag,
}, err
}
func (l *Adapter) AbortMultiPartUpload(_ context.Context, obj block.ObjectPointer, uploadID string) error {
if err := isValidUploadID(uploadID); err != nil {
return err
}
files, err := l.getPartFiles(uploadID, obj)
if err != nil {
return err
}
if err = l.removePartFiles(files); err != nil {
return err
}
return nil
}
func (l *Adapter) CompleteMultiPartUpload(_ context.Context, obj block.ObjectPointer, uploadID string, multipartList *block.MultipartUploadCompletion) (*block.CompleteMultiPartUploadResponse, error) {
if err := isValidUploadID(uploadID); err != nil {
return nil, err
}
etag := computeETag(multipartList.Part) + "-" + strconv.Itoa(len(multipartList.Part))
partFiles, err := l.getPartFiles(uploadID, obj)
if err != nil {
return nil, fmt.Errorf("part files not found for %s: %w", uploadID, err)
}
size, err := l.unitePartFiles(obj, partFiles)
if err != nil {
return nil, fmt.Errorf("multipart upload unite for %s: %w", uploadID, err)
}
if err = l.removePartFiles(partFiles); err != nil {
return nil, err
}
return &block.CompleteMultiPartUploadResponse{
ETag: etag,
ContentLength: size,
}, nil
}
func computeETag(parts []block.MultipartPart) string {
var etagHex []string
for _, p := range parts {
e := strings.Trim(p.ETag, `"`)
etagHex = append(etagHex, e)
}
s := strings.Join(etagHex, "")
b, _ := hex.DecodeString(s)
md5res := md5.Sum(b) //nolint:gosec
csm := hex.EncodeToString(md5res[:])
return csm
}
func (l *Adapter) unitePartFiles(identifier block.ObjectPointer, filenames []string) (int64, error) {
p, err := l.getPath(identifier)
if err != nil {
return 0, err
}
unitedFile, err := os.Create(p)
if err != nil {
return 0, fmt.Errorf("create path %s: %w", p, err)
}
files := make([]*os.File, 0, len(filenames))
defer func() {
_ = unitedFile.Close()
for _, f := range files {
_ = f.Close()
}
}()
for _, name := range filenames {
if err := l.verifyPath(name); err != nil {
return 0, err
}
f, err := os.Open(filepath.Clean(name))
if err != nil {
return 0, fmt.Errorf("open file %s: %w", name, err)
}
files = append(files, f)
}
// convert slice file files to readers
readers := make([]io.Reader, len(files))
for i := range files {
readers[i] = files[i]
}
unitedReader := io.MultiReader(readers...)
return io.Copy(unitedFile, unitedReader)
}
func (l *Adapter) removePartFiles(files []string) error {
var firstErr error
for _, name := range files {
if err := l.verifyPath(name); err != nil {
if firstErr == nil {
firstErr = err
}
}
// If removal fails prefer to skip the error: "only" wasted space.
_ = os.Remove(name)
}
return firstErr
}
func (l *Adapter) getPartFiles(uploadID string, obj block.ObjectPointer) ([]string, error) {
newObj := block.ObjectPointer{
StorageNamespace: obj.StorageNamespace,
Identifier: uploadID,
}
globPathPattern, err := l.getPath(newObj)
if err != nil {
return nil, err
}
globPathPattern += "*"
names, err := filepath.Glob(globPathPattern)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
func (l *Adapter) GenerateInventory(_ context.Context, _ logging.Logger, _ string, _ bool, _ []string) (block.Inventory, error) {
return nil, ErrInventoryNotSupported
}
func (l *Adapter) BlockstoreType() string {
return block.BlockstoreTypeLocal
}
func (l *Adapter) GetStorageNamespaceInfo() block.StorageNamespaceInfo {
return block.DefaultStorageNamespaceInfo(block.BlockstoreTypeLocal)
}
func (l *Adapter) RuntimeStats() map[string]string {
return nil
}
func isValidUploadID(uploadID string) error {
_, err := hex.DecodeString(uploadID)
if err != nil {
return fmt.Errorf("%w: %s", ErrInvalidUploadIDFormat, err)
}
return nil
}