-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconfig.go
More file actions
177 lines (165 loc) · 4.22 KB
/
Copy pathconfig.go
File metadata and controls
177 lines (165 loc) · 4.22 KB
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
/*
* 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"
"fmt"
"io"
"time"
"github.com/juicedata/juicefs/pkg/version"
)
// Config for clients.
type Config struct {
Strict bool // update ctime
Retries int
CaseInsensi bool
ReadOnly bool
NoBGJob bool // disable background jobs like clean-up, backup, etc.
OpenCache time.Duration
Heartbeat time.Duration
MountPoint string
Subdir string
}
type Format struct {
Name string
UUID string
Storage string
Bucket string
AccessKey string
SecretKey string `json:",omitempty"`
BlockSize int
Compression string
Shards int
HashPrefix bool
Capacity uint64
Inodes uint64
EncryptKey string `json:",omitempty"`
KeyEncrypted bool
TrashDays int
MetaVersion int
MinClientVersion string
MaxClientVersion string
}
func (f *Format) RemoveSecret() {
if f.SecretKey != "" {
f.SecretKey = "removed"
}
if f.EncryptKey != "" {
f.EncryptKey = "removed"
}
}
func (f *Format) CheckVersion() error {
if f.MetaVersion > 1 {
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 == "" {
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)
}
nonce := make([]byte, 12)
encrypt := func(k string) string {
ciphertext := aesgcm.Seal(nil, nonce, []byte(k), nil)
buf := make([]byte, 12+len(ciphertext))
copy(buf, nonce)
copy(buf[12:], ciphertext)
return base64.StdEncoding.EncodeToString(buf)
}
if f.SecretKey != "" {
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return fmt.Errorf("generate nonce for secret key: %s", err)
}
f.SecretKey = encrypt(f.SecretKey)
}
if f.EncryptKey != "" {
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return fmt.Errorf("generate nonce for encrypt key: %s", err)
}
f.EncryptKey = encrypt(f.EncryptKey)
}
f.KeyEncrypted = true
return nil
}
func (f *Format) Decrypt() error {
if !f.KeyEncrypted || f.SecretKey == "" && f.EncryptKey == "" {
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) error {
buf, err := base64.StdEncoding.DecodeString(*k)
if err != nil {
return fmt.Errorf("decode key: %s", err)
}
plaintext, err := aesgcm.Open(nil, buf[:12], buf[12:], nil)
if err != nil {
return fmt.Errorf("open cipher: %s", err)
}
*k = string(plaintext)
return nil
}
if f.SecretKey != "" {
if err = decrypt(&f.SecretKey); err != nil {
return err
}
}
if f.EncryptKey != "" {
if err = decrypt(&f.EncryptKey); err != nil {
return err
}
}
f.KeyEncrypted = false
return nil
}