-
Notifications
You must be signed in to change notification settings - Fork 995
/
Copy pathconfig.go
239 lines (223 loc) · 6.19 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
/*
* JuiceFS, Copyright 2020 Juicedata, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package meta
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"time"
"github.com/juicedata/juicefs/pkg/version"
)
// Config for clients.
type Config struct {
Strict bool // update ctime
Retries int
MaxDeletes int
SkipDirNlink int
CaseInsensi bool
ReadOnly bool
NoBGJob bool // disable background jobs like clean-up, backup, etc.
OpenCache time.Duration
OpenCacheLimit uint64 // max number of files to cache (soft limit)
Heartbeat time.Duration
MountPoint string
Subdir string
}
func DefaultConf() *Config {
return &Config{Strict: true, Retries: 10, MaxDeletes: 2, Heartbeat: 12 * time.Second}
}
func (c *Config) SelfCheck() {
if c.MaxDeletes == 0 {
logger.Warnf("Deleting object will be disabled since max-deletes is 0")
}
if c.Heartbeat < time.Second {
logger.Warnf("heartbeat should not be less than 1 second")
c.Heartbeat = time.Second
}
if c.Heartbeat > time.Minute*10 {
logger.Warnf("heartbeat shouldd not be greater than 10 minutes")
c.Heartbeat = time.Minute * 10
}
}
type Format struct {
Name string
UUID string
Storage string
Bucket string
AccessKey string `json:",omitempty"`
SecretKey string `json:",omitempty"`
SessionToken string `json:",omitempty"`
BlockSize int
Compression string `json:",omitempty"`
Shards int `json:",omitempty"`
HashPrefix bool `json:",omitempty"`
Capacity uint64 `json:",omitempty"`
Inodes uint64 `json:",omitempty"`
EncryptKey string `json:",omitempty"`
EncryptAlgo string `json:",omitempty"`
KeyEncrypted bool `json:",omitempty"`
TrashDays int
MetaVersion int `json:",omitempty"`
MinClientVersion string `json:",omitempty"`
MaxClientVersion string `json:",omitempty"`
}
func (f *Format) update(old *Format, force bool) error {
if force {
logger.Warnf("Existing volume will be overwrited: %s", old)
} else {
var args []interface{}
switch {
case f.Name != old.Name:
args = []interface{}{"name", old.Name, f.Name}
case f.Storage != old.Storage:
args = []interface{}{"storage", old.Storage, f.Storage}
case f.BlockSize != old.BlockSize:
args = []interface{}{"block size", old.BlockSize, f.BlockSize}
case f.Compression != old.Compression:
args = []interface{}{"compression", old.Compression, f.Compression}
case f.Shards != old.Shards:
args = []interface{}{"shards", old.Shards, f.Shards}
case f.HashPrefix != old.HashPrefix:
args = []interface{}{"hash prefix", old.HashPrefix, f.HashPrefix}
case f.MetaVersion != old.MetaVersion:
args = []interface{}{"meta version", old.MetaVersion, f.MetaVersion}
}
if args == nil {
f.UUID = old.UUID
} else {
return fmt.Errorf("cannot update volume %s from %v to %v", args...)
}
}
return nil
}
func (f *Format) RemoveSecret() {
if f.SecretKey != "" {
f.SecretKey = "removed"
}
if f.SessionToken != "" {
f.SessionToken = "removed"
}
if f.EncryptKey != "" {
f.EncryptKey = "removed"
}
}
func (f *Format) String() string {
t := *f
t.RemoveSecret()
s, _ := json.MarshalIndent(t, "", " ")
return string(s)
}
func (f *Format) CheckVersion() error {
if f.MetaVersion > MaxVersion {
return fmt.Errorf("incompatible metadata version: %d; please upgrade the client", f.MetaVersion)
}
if f.MinClientVersion != "" {
r, err := version.Compare(f.MinClientVersion)
if err == nil && r < 0 {
err = fmt.Errorf("allowed minimum version: %s; please upgrade the client", f.MinClientVersion)
}
if err != nil {
return err
}
}
if f.MaxClientVersion != "" {
r, err := version.Compare(f.MaxClientVersion)
if err == nil && r > 0 {
err = fmt.Errorf("allowed maximum version: %s; please use an older client", f.MaxClientVersion)
}
if err != nil {
return err
}
}
return nil
}
func (f *Format) Encrypt() error {
if f.KeyEncrypted || f.SecretKey == "" && f.EncryptKey == "" && f.SessionToken == "" {
return nil
}
key := md5.Sum([]byte(f.UUID))
block, err := aes.NewCipher(key[:])
if err != nil {
return fmt.Errorf("new cipher: %s", err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return fmt.Errorf("new GCM: %s", err)
}
encrypt := func(k *string) {
if *k == "" {
return
}
nonce := make([]byte, 12)
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
logger.Fatalf("generate nonce for secret key: %s", err)
}
ciphertext := aesgcm.Seal(nil, nonce, []byte(*k), nil)
buf := make([]byte, 12+len(ciphertext))
copy(buf, nonce)
copy(buf[12:], ciphertext)
*k = base64.StdEncoding.EncodeToString(buf)
}
encrypt(&f.SecretKey)
encrypt(&f.SessionToken)
encrypt(&f.EncryptKey)
f.KeyEncrypted = true
return nil
}
func (f *Format) Decrypt() error {
if !f.KeyEncrypted {
return nil
}
key := md5.Sum([]byte(f.UUID))
block, err := aes.NewCipher(key[:])
if err != nil {
return fmt.Errorf("new cipher: %s", err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return fmt.Errorf("new GCM: %s", err)
}
decrypt := func(k *string) {
if *k == "" {
return
}
if *k == "removed" {
err = fmt.Errorf("secret was removed; please correct it with `config` command")
return
}
buf, e := base64.StdEncoding.DecodeString(*k)
if e != nil {
err = fmt.Errorf("decode key: %s", e)
return
}
plaintext, e := aesgcm.Open(nil, buf[:12], buf[12:], nil)
if e != nil {
err = fmt.Errorf("open cipher: %s", e)
return
}
*k = string(plaintext)
}
decrypt(&f.EncryptKey)
decrypt(&f.SecretKey)
decrypt(&f.SessionToken)
f.KeyEncrypted = false
return err
}