-
Notifications
You must be signed in to change notification settings - Fork 172
/
vault.go
312 lines (260 loc) · 6.55 KB
/
vault.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
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package config
import (
"strings"
"sync"
"time"
"github.com/pydio/cells/v4/common/utils/configx"
)
// Vault returns the default vault
func Vault() configx.Entrypoint {
return stdvault
}
// RegisterVault sets the default vault
func RegisterVault(store Store) {
stdvault = store
}
// Config holds the main structure of a configuration
type vault struct {
config Store
vault Store
}
// NewVault creates a new vault with a standard config store and a vault store
func NewVault(vaultStore, configStore Store) Store {
return &vault{
configStore,
vaultStore,
}
}
func (v *vault) Lock() {
v.config.Lock()
v.vault.Lock()
}
func (v *vault) Unlock() {
v.config.Unlock()
v.vault.Unlock()
}
func (v *vault) Close() error {
if err := v.config.Close(); err != nil {
return err
}
if err := v.vault.Close(); err != nil {
return err
}
return nil
}
func (v *vault) Done() <-chan struct{} {
return v.config.Done()
}
// Save the config in the underlying storage
func (v *vault) Save(ctxUser string, ctxMessage string) error {
if err := v.vault.Save(ctxUser, ctxMessage); err != nil {
return err
}
return v.config.Save(ctxUser, ctxMessage)
}
func (v *vault) NewLocker(name string) sync.Locker {
configLocker := v.config.NewLocker(name)
vaultLocker := v.vault.NewLocker(name)
return &vaultStoreLocker{
configLocker: configLocker,
vaultLocker: vaultLocker,
}
}
type vaultStoreLocker struct {
configLocker sync.Locker
vaultLocker sync.Locker
}
func (v *vaultStoreLocker) Lock() {
if v.configLocker != nil {
v.configLocker.Lock()
}
if v.vaultLocker != nil {
v.vaultLocker.Lock()
}
}
func (v *vaultStoreLocker) Unlock() {
if v.configLocker != nil {
v.configLocker.Unlock()
}
if v.vaultLocker != nil {
v.vaultLocker.Unlock()
}
}
// Get access to the underlying structure at a certain path
func (v *vault) Get() configx.Value {
return v.vault.Get()
}
// Set new value
func (v *vault) Set(val interface{}) error {
return v.config.Set(val)
}
// Val of the path
func (v *vault) Val(s ...string) configx.Values {
return &vaultvalues{strings.Join(s, "/"), v.config.Val(s...), v.vault.Val()}
}
// Watch changes to the path
func (v *vault) Watch(opts ...configx.WatchOption) (configx.Receiver, error) {
return v.config.Watch(opts...)
}
// Del the value
func (v *vault) Del() error {
return v.config.Del()
}
type vaultvalues struct {
path string
configx.Values
vault configx.Values
}
// Val of the path
func (v *vaultvalues) Val(s ...string) configx.Values {
return &vaultvalues{v.path + "/" + strings.Join(s, "/"), v.Values.Val(s...), v.vault.Val()}
}
// Get retrieves the value as saved in the config (meaning the uuid if it is a registered key)
// Data will need to be retrieved from the vault via other means
func (v *vaultvalues) Get() configx.Value {
return v.Values
}
// Set ensures that the keys that have been target are saved encrypted in the vault
func (v *vaultvalues) Set(val interface{}) error {
// Checking we have a registered value
for _, p := range registeredVaultKeys {
if v.path == p {
return v.set(val)
}
if strings.HasPrefix(p, v.path) {
// First removing keys that don't exist anymore
current := v.Values.Map()
// Need to loop through all below
switch m := val.(type) {
case map[string]string:
for k := range current {
if _, ok := m[k]; !ok {
if err := v.Values.Val(k).Del(); err != nil {
return err
}
}
}
for mm, vv := range m {
if err := (&vaultvalues{v.path + "/" + mm, v.Values.Val(mm), v.vault}).Set(vv); err != nil {
return err
}
}
return nil
case map[string]interface{}:
for k := range current {
if _, ok := m[k]; !ok {
if err := v.Values.Val(k).Del(); err != nil {
return err
}
}
}
for mm, vv := range m {
if err := (&vaultvalues{v.path + "/" + mm, v.Values.Val(mm), v.vault}).Set(vv); err != nil {
return err
}
}
return nil
}
}
}
vval, ok := val.(configx.Values)
if ok {
if vval.Get() == nil {
// Nothing to set
return nil
}
return v.Values.Set(vval.Get())
}
return v.Values.Set(val)
}
// Default value
func (v *vaultvalues) Default(i interface{}) configx.Value {
return v.Get().Default(i)
}
// Bool value
func (v *vaultvalues) Bool() bool {
return v.Get().Bool()
}
// Int value
func (v *vaultvalues) Int() int {
return v.Get().Int()
}
// Int64 value
func (v *vaultvalues) Int64() int64 {
return v.Get().Int64()
}
// Bytes value
func (v *vaultvalues) Bytes() []byte {
return v.Get().Bytes()
}
// Duration value
func (v *vaultvalues) Duration() time.Duration {
return v.Get().Duration()
}
// String value
func (v *vaultvalues) String() string {
return v.Get().Default("").String()
}
// StringMap value
func (v *vaultvalues) StringMap() map[string]string {
return v.Get().StringMap()
}
// StringArray value
func (v *vaultvalues) StringArray() []string {
return v.Get().StringArray()
}
// Slice value
func (v *vaultvalues) Slice() []interface{} {
return v.Get().Slice()
}
// Map value
func (v *vaultvalues) Map() map[string]interface{} {
return v.Get().Map()
}
// MarshalJSON
func (v *vaultvalues) MarshalJSON() ([]byte, error) {
return []byte(v.Values.String()), nil
}
func (v *vaultvalues) set(val interface{}) error {
uuid := v.Values.String()
// Get the current value and do nothing if it hasn't change
current := v.vault.Val(uuid)
if current.String() == val.(string) || uuid == val.(string) {
// already set
return nil
}
// Removing old value if it was set
if uuid != "" {
if current.String() != "" {
if err := current.Del(); err != nil {
return err
}
}
}
uuid = NewKeyForSecret()
err := v.Values.Set(uuid)
if err != nil {
return err
}
// Do we need to set a new key each time it changes ?
return v.vault.Val(uuid).Set(val)
}