forked from cloudflare/utahfs
/
config.go
457 lines (398 loc) · 13.5 KB
/
config.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
package config
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"syscall"
"github.com/cloudflare/utahfs"
"github.com/cloudflare/utahfs/persistent"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/yaml.v2"
)
func maxSize(numPtrs, dataSize int64) int64 {
// 8 = size of a single pointer
// 3 = size of length field before data
// 28 = encryption overhead
return (8 * numPtrs) + (3 + dataSize) + 28
}
type StorageProvider struct {
// Backblaze B2
B2AcctId string `yaml:"b2-acct-id"`
B2AppKey string `yaml:"b2-app-key"`
B2Bucket string `yaml:"b2-bucket"`
B2Url string `yaml:"b2-url"`
// AWS S3 and compatible APIs
S3AppId string `yaml:"s3-app-id"`
S3AppKey string `yaml:"s3-app-key"`
S3Bucket string `yaml:"s3-bucket"`
S3Url string `yaml:"s3-url"`
S3Region string `yaml:"s3-region"`
// Google Cloud Storage
GCSBucketName string `yaml:"gcs-bucket-name"`
GCSCredentialsPath string `yaml:"gcs-credentials-path"`
// Local disk storage
DiskPath string `yaml:"disk-path"`
Retry int `yaml:"retry"` // Max number of times to retry reqs that fail.
Prefix string `yaml:"prefix"` // Prefix to put on every key, like `folder-name/`.
}
func (sp *StorageProvider) hasB2() bool {
return sp.B2AcctId != "" || sp.B2AppKey != "" || sp.B2Bucket != "" || sp.B2Url != ""
}
func (sp *StorageProvider) hasS3() bool {
return sp.S3AppId != "" || sp.S3AppKey != "" || sp.S3Bucket != "" || sp.S3Url != "" || sp.S3Region != ""
}
func (sp *StorageProvider) hasGCS() bool {
return sp.GCSBucketName != ""
}
func (sp *StorageProvider) hasDisk() bool { return sp.DiskPath != "" }
func (sp *StorageProvider) hasMultiple() bool {
count := 0
if sp.hasB2() {
count++
}
if sp.hasS3() {
count++
}
if sp.hasGCS() {
count++
}
if sp.hasDisk() {
count++
}
return count > 1
}
func (sp *StorageProvider) Store() (persistent.ObjectStorage, error) {
if sp == nil || !sp.hasB2() && !sp.hasS3() && !sp.hasGCS() && !sp.hasDisk() {
return nil, fmt.Errorf("no object storage provider defined")
} else if sp.hasMultiple() {
return nil, fmt.Errorf("only one object storage provider may be defined")
}
// Connect to the user's chosen storage provider.
var (
out persistent.ObjectStorage
err error
)
if sp.hasB2() {
out, err = persistent.NewB2(sp.B2AcctId, sp.B2AppKey, sp.B2Bucket, sp.B2Url)
} else if sp.hasS3() {
out, err = persistent.NewS3(sp.S3AppId, sp.S3AppKey, sp.S3Bucket, sp.S3Url, sp.S3Region)
} else if sp.hasGCS() {
out, err = persistent.NewGCS(sp.GCSBucketName, sp.GCSCredentialsPath)
} else if sp.hasDisk() {
out, err = persistent.NewDisk(sp.DiskPath)
}
if err != nil {
return nil, err
}
// Configure retries if the user wants.
if sp.Retry > 1 {
out, err = persistent.NewRetry(out, sp.Retry)
if err != nil {
return nil, err
}
}
// Configure a key prefix if the user wants.
if sp.Prefix != "" {
out = persistent.NewPrefix(out, sp.Prefix)
}
return out, nil
}
type RemoteServer struct {
URL string `yaml:"url"` // URL of server.
TransportKey string `yaml:"transport-key"` // Pre-shared key for authenticating client and server.
}
type Client struct {
DataDir string `yaml:"data-dir"` // Directory where the WAL and pin file should be kept. Default: .utahfs
StorageProvider *StorageProvider `yaml:"storage-provider"`
MaxWALSize int `yaml:"max-wal-size"` // Max number of blocks to put in WAL before blocking on remote storage. Default: 128*1024 blocks
WALParallelism int `yaml:"wal-parallelism"` // Number of threads to use when draining the WAL. Default: 1
DiskCacheSize int64 `yaml:"disk-cache-size"` // Size of on-disk LRU cache. Default: 320*1024 blocks, -1 to disable.
DiskCacheLoc string `yaml:"disk-cache-loc"` // Special location for on-disk LRU cache. Default is to store cache inside data-dir.
MemCacheSize int `yaml:"mem-cache-size"` // Size of in-memory LRU cache. Default: 32*1024 blocks, -1 to disable.
KeepMetadata bool `yaml:"keep-metadata"` // Keep a local copy of metadata, always. Default: false.
RemoteServer *RemoteServer `yaml:"remote-server"`
Password string `yaml:"password"` // Password for encryption and integrity. User will be prompted if not provided.
NumPtrs int64 `yaml:"num-ptrs"` // Number of pointers in a file's skiplist. Default: 12
DataSize int64 `yaml:"data-size"` // Amount of data kept in each of a file's blocks. Default: 32 KiB
Archive bool `yaml:"archive"` // Whether or not to enforce archive mode.
ORAM bool `yaml:"oram"` // Whether or not to use ORAM.
}
func ClientFromFile(path string) (*Client, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
parsed := &Client{}
if err = yaml.UnmarshalStrict(raw, parsed); err != nil {
return nil, err
}
return parsed, nil
}
func (c *Client) localStorage() (persistent.ReliableStorage, error) {
// Setup object storage.
store, err := c.StorageProvider.Store()
if err != nil {
return nil, err
}
// Setup on-disk caching if desired.
if c.DiskCacheSize == 0 {
c.DiskCacheSize = 320 * 1024
}
if c.DiskCacheSize != -1 {
loc := c.DiskCacheLoc
if loc == "" {
loc = path.Join(c.DataDir, "cache")
}
exclude := []persistent.DataType{persistent.Unknown}
if c.KeepMetadata {
exclude = append(exclude, persistent.Metadata)
}
store, err = persistent.NewDiskCache(store, loc, c.DiskCacheSize, exclude)
if err != nil {
return nil, err
}
}
// Setup tiered caching for metadata if desired.
if c.KeepMetadata {
diskStore, err := persistent.NewDisk(path.Join(c.DataDir, "metadata"))
if err != nil {
return nil, err
}
store = persistent.NewTieredCache(persistent.Metadata, diskStore, store)
}
// Setup a local WAL.
if c.MaxWALSize == 0 {
c.MaxWALSize = 128 * 1024
}
if c.WALParallelism == 0 {
c.WALParallelism = 1
}
relStore, err := persistent.NewLocalWAL(store, path.Join(c.DataDir, "wal"), c.MaxWALSize, c.WALParallelism)
if err != nil {
return nil, err
}
// Setup caching if desired.
if c.MemCacheSize == 0 {
c.MemCacheSize = 32 * 1024
}
if c.MemCacheSize != -1 {
relStore = persistent.NewCache(relStore, c.MemCacheSize)
}
return relStore, nil
}
func (c *Client) remoteStorage() (persistent.ReliableStorage, error) {
if c.StorageProvider != nil {
return nil, fmt.Errorf("cannot set storage-provider with remote-server")
} else if c.MaxWALSize != 0 {
return nil, fmt.Errorf("cannot set max-wal-size with remote-server")
} else if c.WALParallelism != 0 {
return nil, fmt.Errorf("cannot set wal-parallelism with remote-server")
} else if c.DiskCacheSize != 0 {
return nil, fmt.Errorf("cannot set disk-cache-size with remote-server")
} else if c.DiskCacheLoc != "" {
return nil, fmt.Errorf("cannot set disk-cache-loc with remote-server")
} else if c.MemCacheSize != 0 {
return nil, fmt.Errorf("cannot set mem-cache-size with remote-server")
} else if c.KeepMetadata {
return nil, fmt.Errorf("cannot set keep-metadata with remote-server")
} else if c.RemoteServer.TransportKey == "" {
return nil, fmt.Errorf("no transport key was given for remote server")
} else if c.RemoteServer.TransportKey == c.Password {
return nil, fmt.Errorf("transport key should be generated independently of the encryption password")
}
return persistent.NewRemoteClient(c.RemoteServer.TransportKey, c.RemoteServer.URL, c.ORAM)
}
func (c *Client) FS(mountPath string) (*utahfs.BlockFilesystem, error) {
if c.DataDir == "" {
c.DataDir = path.Join(path.Dir(mountPath), ".utahfs")
}
// Stub out generation of the ReliableStorage interface, depending on if
// this client is standalone or backed by a server.
var (
relStore persistent.ReliableStorage
err error
)
if c.RemoteServer == nil {
relStore, err = c.localStorage()
} else {
relStore, err = c.remoteStorage()
}
if err != nil {
return nil, err
}
// Setup buffered block storage.
block := persistent.NewBufferedStorage(relStore)
// Setup encryption and integrity.
if c.Password == "" {
fmt.Print("Password: ")
password, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, fmt.Errorf("failed reading password from stdin")
} else if len(password) == 0 {
return nil, fmt.Errorf("no password given for encryption")
}
c.Password = string(password)
}
if !c.ORAM || c.RemoteServer == nil {
block, err = persistent.WithIntegrity(block, c.Password, path.Join(c.DataDir, "pin.json"))
if err != nil {
return nil, err
}
} else {
log.Println("WARNING: delegating rollback prevention to remote server because ORAM is enabled")
}
block = persistent.WithEncryption(block, c.Password)
// Configure defaults for the block-based filesystem. Do this early because
// the numbers might be needed for ORAM.
if c.NumPtrs == 0 {
c.NumPtrs = 12
}
if c.DataSize == 0 {
c.DataSize = 32 * 1024
}
// Setup ORAM if desired.
if c.ORAM && c.RemoteServer == nil {
if c.StorageProvider.hasDisk() {
log.Println("WARNING: ORAM provides no security properties when used with disk storage")
}
ostore, err := persistent.NewLocalOblivious(path.Join(c.DataDir, "oram"))
if err != nil {
return nil, err
}
block, err = persistent.WithORAM(block, ostore, maxSize(c.NumPtrs, c.DataSize))
if err != nil {
return nil, err
}
}
// Setup application storage.
appStore := persistent.NewAppStorage(block)
// Setup block-based filesystem.
bfs, err := utahfs.NewBlockFilesystem(appStore, c.NumPtrs, c.DataSize, !c.ORAM)
if err != nil {
return nil, err
}
return bfs, nil
}
type ORAMConfig struct {
Key string `yaml:"key"` // Fixed key for encrypting ORAM blocks before being sent to the remote storage provider.
NumPtrs int64 `yaml:"num-ptrs"` // Should be the same as num-ptrs in the client-side config.
DataSize int64 `yaml:"data-size"` // Should be the same as data-size in the client-side config.
}
type Server struct {
DataDir string `yaml:"data-dir"` // Directory where the WAL and cache should be kept. Default: utahfs-data
StorageProvider *StorageProvider `yaml:"storage-provider"`
MaxWALSize int `yaml:"max-wal-size"` // Max number of blocks to put in WAL before blocking on remote storage. Default: 320*1024 blocks
WALParallelism int `yaml:"wal-parallelism"` // Number of threads to use when draining the WAL. Default: 1
DiskCacheSize int64 `yaml:"disk-cache-size"` // Size of on-disk LRU cache. Default: 3200*1024 blocks, -1 to disable.
DiskCacheLoc string `yaml:"disk-cache-loc"` // Special location for on-disk LRU cache. Default is to store cache inside data-dir.
MemCacheSize int `yaml:"mem-cache-size"` // Size of in-memory LRU cache. Default: 32*1024 blocks, -1 to disable.
KeepMetadata bool `yaml:"keep-metadata"` // Keep a local copy of metadata, always. Default: false.
ORAM *ORAMConfig `yaml:"oram"` // Provided if ORAM should be used on the server-side.
TransportKey string `yaml:"transport-key"` // Pre-shared key for authenticating client and server.
}
func ServerFromFile(path string) (*Server, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
parsed := &Server{}
if err = yaml.UnmarshalStrict(raw, parsed); err != nil {
return nil, err
}
return parsed, nil
}
func (s *Server) Server() (*http.Server, error) {
if s.DataDir == "" {
s.DataDir = "./utahfs-data"
}
// Setup object storage.
store, err := s.StorageProvider.Store()
if err != nil {
return nil, err
}
// Setup on-disk caching if desired.
if s.DiskCacheSize == 0 {
s.DiskCacheSize = 3200 * 1024
}
if s.DiskCacheSize != -1 {
loc := s.DiskCacheLoc
if loc == "" {
loc = path.Join(s.DataDir, "cache")
}
exclude := []persistent.DataType{persistent.Unknown}
if s.KeepMetadata {
exclude = append(exclude, persistent.Metadata)
}
store, err = persistent.NewDiskCache(store, loc, s.DiskCacheSize, exclude)
if err != nil {
return nil, err
}
}
// Setup tiered caching for metadata, if desired.
if s.KeepMetadata {
diskStore, err := persistent.NewDisk(path.Join(s.DataDir, "metadata"))
if err != nil {
return nil, err
}
store = persistent.NewTieredCache(persistent.Metadata, diskStore, store)
}
// Setup a local WAL.
if s.MaxWALSize == 0 {
s.MaxWALSize = 32 * 1024
}
if s.WALParallelism == 0 {
s.WALParallelism = 1
}
relStore, err := persistent.NewLocalWAL(store, path.Join(s.DataDir, "wal"), s.MaxWALSize, s.WALParallelism)
if err != nil {
return nil, err
}
// Setup in-memory caching if desired.
if s.MemCacheSize == 0 {
s.MemCacheSize = 32 * 1024
}
if s.MemCacheSize != -1 {
relStore = persistent.NewCache(relStore, s.MemCacheSize)
}
// Setup ORAM if desired.
if s.ORAM != nil {
if s.StorageProvider.hasDisk() {
log.Println("WARNING: ORAM provides no security properties when used with disk storage")
}
// Setup defaults.
if s.ORAM.NumPtrs == 0 {
s.ORAM.NumPtrs = 12
}
if s.ORAM.DataSize == 0 {
s.ORAM.DataSize = 32 * 1024
}
ostore, err := persistent.NewLocalOblivious(path.Join(s.DataDir, "oram"))
if err != nil {
return nil, err
}
block, err := persistent.WithIntegrity(
persistent.NewBufferedStorage(relStore),
s.ORAM.Key,
path.Join(s.DataDir, "pin.json"),
)
if err != nil {
return nil, err
}
block, err = persistent.WithORAM(
persistent.WithEncryption(block, s.ORAM.Key),
ostore,
maxSize(s.ORAM.NumPtrs, s.ORAM.DataSize),
)
if err != nil {
return nil, err
}
relStore = persistent.NewBlockReliable(block)
}
// Setup the server we want to expose.
if s.TransportKey == "" {
return nil, fmt.Errorf("no transport key was given for remote clients")
}
return persistent.NewRemoteServer(relStore, s.TransportKey, s.ORAM != nil)
}