-
Notifications
You must be signed in to change notification settings - Fork 43
/
secure_bucket_client.go
492 lines (408 loc) · 12 KB
/
secure_bucket_client.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
package textile
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"github.com/FleekHQ/space-daemon/core/keychain"
"github.com/FleekHQ/space-daemon/core/store"
"github.com/FleekHQ/space-daemon/core/textile/common"
"github.com/FleekHQ/space-daemon/core/textile/utils"
"github.com/FleekHQ/space-daemon/log"
"github.com/FleekHQ/space-daemon/core/textile/bucket/crypto"
"github.com/ipfs/go-cid"
ipfsfiles "github.com/ipfs/go-ipfs-files"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
threadsClient "github.com/textileio/go-threads/api/client"
bc "github.com/textileio/textile/v2/api/buckets/client"
bucketsClient "github.com/textileio/textile/v2/api/buckets/client"
bucketspb "github.com/textileio/textile/v2/api/buckets/pb"
"github.com/textileio/textile/v2/buckets"
)
var textileRelPathRegex = regexp.MustCompile(`/ip[f|n]s/[^/]*(?P<relPath>/.*)`)
// SecureBucketClient implements the BucketsClient Interface
// It encrypts data being pushed to the underlying textile client
// and also decrypts response from the underlying textile client
type SecureBucketClient struct {
client *bucketsClient.Client
kc keychain.Keychain
st store.Store
threads *threadsClient.Client
ipfsClient iface.CoreAPI
isRemote bool
}
func NewSecureBucketsClient(
client *bucketsClient.Client,
kc keychain.Keychain,
st store.Store,
threads *threadsClient.Client,
ipfsClient iface.CoreAPI,
isRemote bool,
) *SecureBucketClient {
return &SecureBucketClient{
client: client,
kc: kc,
st: st,
threads: threads,
ipfsClient: ipfsClient,
isRemote: isRemote,
}
}
func (s *SecureBucketClient) PushPath(ctx context.Context, key, path string, reader io.Reader, opts ...bc.Option) (result path.Resolved, root path.Resolved, err error) {
path = cleanBucketPath(path)
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return nil, nil, err
}
// encrypt path before uploading
encryptedPath, encryptedReader, err := s.encryptPathData(ctx, encryptionKey, path, reader)
if err != nil {
return nil, nil, err
}
return s.client.PushPath(ctx, key, encryptedPath, encryptedReader)
// For now ignoring parsing results since it not being used downstream
// but putting a TODO here in the meantime
}
func (s *SecureBucketClient) PushPathAccessRoles(ctx context.Context, key, path string, roles map[string]buckets.Role) error {
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return err
}
encryptedPath, _, err := s.encryptPathData(ctx, encryptionKey, path, nil)
if err != nil {
return err
}
return s.client.PushPathAccessRoles(ctx, key, encryptedPath, roles)
}
func (s *SecureBucketClient) PullPathAccessRoles(ctx context.Context, key, path string) (map[string]buckets.Role, error) {
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return nil, err
}
encryptedPath, _, err := s.encryptPathData(ctx, encryptionKey, path, nil)
if err != nil {
return nil, err
}
return s.client.PullPathAccessRoles(ctx, key, encryptedPath)
}
func (s *SecureBucketClient) PullPath(ctx context.Context, key, path string, writer io.Writer, opts ...bc.Option) error {
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return err
}
encryptedPath, _, err := s.encryptPathData(ctx, encryptionKey, path, nil)
if err != nil {
return err
}
errs := make(chan error)
pipeReader, pipeWriter := io.Pipe()
// pipe the writes from buckets to reader to be decrypted
go func() {
defer pipeWriter.Close()
if err := s.racePullFile(ctx, key, encryptedPath, pipeWriter, opts...); err != nil {
errs <- err
}
}()
go func() {
defer close(errs)
_, r, err := s.decryptPathData(ctx, encryptionKey, "", pipeReader)
if err != nil {
errs <- err
return
}
defer r.Close()
// copy decrypted reads to original writer
if _, err := io.Copy(writer, r); err != nil {
errs <- err
return
}
}()
err = <-errs
return err
}
func (s *SecureBucketClient) overwriteDecryptedItem(ctx context.Context, item *bucketspb.PathItem) error {
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return err
}
if utils.IsMetaFileName(item.Name) {
return nil
}
// decrypt file name
item.Name, _, err = s.decryptPathData(ctx, encryptionKey, item.Name, nil)
if err != nil {
return err
}
// decrypts file path
matchedPaths := textileRelPathRegex.FindStringSubmatch(item.Path)
if len(matchedPaths) > 1 {
item.Path, _, err = s.decryptPathData(ctx, encryptionKey, matchedPaths[1], nil)
if err != nil {
return err
}
}
// Item size is generally (content size + hmac (64 bytes))
if item.Size >= 64 {
item.Size = item.Size - 64
}
return nil
}
func (s *SecureBucketClient) ListIpfsPath(ctx context.Context, pth path.Path) (*bucketspb.ListIpfsPathResponse, error) {
return s.client.ListIpfsPath(ctx, pth)
}
func (s *SecureBucketClient) ListPath(ctx context.Context, key, path string) (*bucketspb.ListPathResponse, error) {
path = cleanBucketPath(path)
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return nil, err
}
encryptedPath, _, err := s.encryptPathData(ctx, encryptionKey, path, nil)
if err != nil {
return nil, err
}
result, err := s.client.ListPath(ctx, key, encryptedPath)
if err != nil {
return nil, err
}
// decrypt result items
for _, item := range result.Item.Items {
err = s.overwriteDecryptedItem(ctx, item)
if err != nil {
// Don't error on a single file not decrypted
log.Debug(fmt.Sprintf("Error decrypting a file: %s", err.Error()))
}
}
// decrypt root item
err = s.overwriteDecryptedItem(ctx, result.Item)
if err != nil {
// Don't error on a single file not decrypted
log.Debug(fmt.Sprintf("Error decrypting a file: %s", err.Error()))
}
return result, nil
}
func (s *SecureBucketClient) RemovePath(ctx context.Context, key, path string, opts ...bc.Option) (path.Resolved, error) {
path = cleanBucketPath(path)
encryptionKey, err := s.getBucketEncryptionKey(ctx)
if err != nil {
return nil, err
}
// encrypt path before submitting delete
encryptedPath, _, err := s.encryptPathData(ctx, encryptionKey, path, nil)
if err != nil {
return nil, err
}
return s.client.RemovePath(ctx, key, encryptedPath, opts...)
}
func (s *SecureBucketClient) getBucketEncryptionKey(ctx context.Context) ([]byte, error) {
if key, exists := common.BucketEncryptionKeyFromContext(ctx); exists {
return key, nil
}
return nil, errors.New("bucket encryption key missing")
}
func (s *SecureBucketClient) encryptPathData(
ctx context.Context,
key []byte,
path string,
dataReader io.Reader,
) (string, io.Reader, error) {
return crypto.EncryptPathItems(key, path, dataReader)
}
func (s *SecureBucketClient) decryptPathData(
ctx context.Context,
key []byte,
path string,
dataReader io.Reader,
) (string, io.ReadCloser, error) {
return crypto.DecryptPathItems(key, path, dataReader)
}
// Cleans path used to access data in buckets
// Currently only removes prefix path if exists.
// would later include logic to normalize paths from other operating systems like windows
func cleanBucketPath(path string) string {
return strings.TrimPrefix(path, "/")
}
type pathPullingFn func(context.Context, string, string, io.Writer, ...bc.Option) (bool, error)
type pullSuccessResponse struct {
file *os.File
shouldCache bool
}
func getTempFileName(encPath string) string {
tempFilePath := sha256.Sum256([]byte(encPath))
return hex.EncodeToString(tempFilePath[:])
}
func (s *SecureBucketClient) racePullFile(ctx context.Context, key, encPath string, w io.Writer, opts ...bc.Option) error {
pullers := []pathPullingFn{s.pullFileFromLocal, s.pullFileFromClient, s.pullFileFromDHT}
pullSuccess := make(chan *pullSuccessResponse)
errc := make(chan error)
defer close(pullSuccess)
ctxWithCancel, cancelPulls := context.WithCancel(ctx)
pendingFns := len(pullers)
erroredFns := 0
for _, fn := range pullers {
f, err := ioutil.TempFile("", "*-"+getTempFileName(encPath))
if err != nil {
cancelPulls()
return err
}
defer f.Close()
defer os.Remove(f.Name())
go func(fn pathPullingFn, f *os.File) {
shouldCache, err := fn(ctxWithCancel, key, encPath, f, opts...)
if err != nil {
errc <- err
return
}
chanRes := &pullSuccessResponse{
file: f,
shouldCache: shouldCache,
}
pullSuccess <- chanRes
errc <- nil
}(fn, f)
}
var pullErr error
// Wait for either all pullers to fail or for one to succeed
go func() {
for {
select {
case err := <-errc:
pendingFns--
if err != nil {
erroredFns++
pullErr = err
}
if pendingFns <= 0 && erroredFns >= len(pullers) {
// All functions failed. Stop waiting
pullSuccess <- nil
}
if pendingFns <= 0 {
close(errc)
return
}
}
}
}()
pullResponse := <-pullSuccess
cancelPulls()
// Return error if all pull functions failed
if erroredFns >= len(pullers) || pullResponse == nil {
return pullErr
}
finalFile := pullResponse.file
shouldCache := pullResponse.shouldCache
// Copy pulled file to upstream writer
resErrc := make(chan error)
defer close(resErrc)
go func() {
from, err := os.Open(finalFile.Name())
if err != nil {
resErrc <- err
return
}
defer from.Close()
_, err = io.Copy(w, from)
resErrc <- err
}()
// Copy pulled file to local cache
cacheErrc := make(chan error)
defer close(cacheErrc)
go func() {
var err error
if !shouldCache {
cacheErrc <- nil
return
}
from, err := os.Open(finalFile.Name())
if err != nil {
cacheErrc <- err
return
}
defer from.Close()
p, err := s.ipfsClient.Unixfs().Add(
ctx,
ipfsfiles.NewReaderFile(from),
options.Unixfs.Pin(false), // Turn to true when we enable DHT discovery
options.Unixfs.Progress(false),
options.Unixfs.CidVersion(1),
)
if err != nil {
cacheErrc <- err
return
}
bucketPath, err := s.client.ListPath(ctx, key, encPath)
if err != nil {
cacheErrc <- err
return
}
encCid := bucketPath.Item.Cid
cidBinary := p.Cid().Bytes()
err = s.st.Set(getFileCacheKey(encCid), cidBinary)
cacheErrc <- err
}()
if err := <-resErrc; err != nil {
return err
}
if err := <-cacheErrc; err != nil {
return err
}
return nil
}
const FileCachePrefix = "file_cache"
func getFileCacheKey(encCid string) []byte {
return []byte(FileCachePrefix + ":" + encCid)
}
func (s *SecureBucketClient) pullFileFromClient(ctx context.Context, key, encPath string, w io.Writer, opts ...bc.Option) (shouldCache bool, err error) {
shouldCache = true
if s.isRemote == false {
// File already in local bucket
shouldCache = false
}
if err = s.client.PullPath(ctx, key, encPath, w, opts...); err != nil {
return false, err
}
return shouldCache, nil
}
var errNoLocalClient = errors.New("No cache client available")
func (s *SecureBucketClient) pullFileFromLocal(ctx context.Context, key, encPath string, w io.Writer, opts ...bc.Option) (shouldCache bool, err error) {
shouldCache = false
bucketPath, err := s.client.ListPath(ctx, key, encPath)
if err != nil {
return false, err
}
encCid := bucketPath.Item.Cid
cidBinary, err := s.st.Get(getFileCacheKey(encCid))
if cidBinary == nil || err != nil {
return false, errors.New("CID not stored in local cache")
}
_, c, err := cid.CidFromBytes(cidBinary)
if err != nil {
return false, err
}
node, err := s.ipfsClient.Unixfs().Get(ctx, path.New(c.String()))
if err != nil {
return false, err
}
defer node.Close()
file := ipfsfiles.ToFile(node)
if file == nil {
return false, errors.New("File is a directory")
}
if _, err := io.Copy(w, file); err != nil {
return false, err
}
return shouldCache, nil
}
func (s *SecureBucketClient) pullFileFromDHT(ctx context.Context, key, encPath string, w io.Writer, opts ...bc.Option) (shouldCache bool, err error) {
shouldCache = true
// return shouldCache, nil
return false, errors.New("Not implemented")
}
const cacheBucketThreadName = "cache_bucket"