@@ -66,7 +66,6 @@ type watchKey struct {
66
66
67
67
// A sealedKey is an implementation of an encryption key that is encrypted using symmecrypt/seal.
68
68
type sealedKey struct {
69
- encryptedKey string
70
69
decryptedKey symmecrypt.Key
71
70
decrypted uint32
72
71
waitCh chan struct {}
@@ -157,13 +156,19 @@ func UnsealKey(k *KeyConfig, s *seal.Seal) (*KeyConfig, error) {
157
156
}, nil
158
157
}
159
158
160
- // ConfiguredKeys returns a list of all the encryption keys present in the configstore
159
+ // ConfiguredKeys returns a list of all the encryption keys present in the default store in configstore
161
160
// ensuring they are unsealed.
162
161
func ConfiguredKeys () ([]* KeyConfig , error ) {
162
+ return ConfiguredKeysFromStore (configstore .DefaultStore )
163
+ }
164
+
165
+ // ConfiguredKeys returns a list of all the encryption keys present in a specific store instance
166
+ // ensuring they are unsealed.
167
+ func ConfiguredKeysFromStore (store * configstore.Store ) ([]* KeyConfig , error ) {
163
168
164
169
ret := []* KeyConfig {}
165
170
166
- items , err := ConfigFilter .GetItemList ()
171
+ items , err := ConfigFilter .Store ( store ). GetItemList ()
167
172
if err != nil {
168
173
return nil , err
169
174
}
@@ -213,7 +218,7 @@ func configFactory() interface{} {
213
218
** CONSTRUCTORS
214
219
*/
215
220
216
- // LoadKey instantiates a new encryption key for a given identifier from the configstore.
221
+ // LoadKey instantiates a new encryption key for a given identifier from the default store in configstore.
217
222
//
218
223
// If several keys are found for the identifier, they are sorted by timestamp, and a composite key is returned.
219
224
// The most recent key will be used for encryption, and decryption will be done by any of them.
@@ -226,8 +231,24 @@ func configFactory() interface{} {
226
231
// Either use a built-in cipher, or make sure to register a proper factory for this cipher.
227
232
// This KeyFactory will be called, either directly or when the symmecrypt/seal global singleton gets unsealed, if applicable.
228
233
func LoadKey (identifier string ) (symmecrypt.Key , error ) {
234
+ return LoadKeyFromStore (identifier , configstore .DefaultStore )
235
+ }
229
236
230
- items , err := ConfigFilter .Slice (identifier ).GetItemList ()
237
+ // LoadKeyFromStore instantiates a new encryption key for a given identifier from a specific store instance.
238
+ //
239
+ // If several keys are found for the identifier, they are sorted by timestamp, and a composite key is returned.
240
+ // The most recent key will be used for encryption, and decryption will be done by any of them.
241
+ // There needs to be _only one_ key with the highest priority for the identifier.
242
+ //
243
+ // If the key configuration specifies it is sealed, the key returned will be wrapped by an unseal mechanism.
244
+ // When the symmecrypt/seal global singleton gets unsealed, the key will become usable instantly. It will return errors in the meantime.
245
+ //
246
+ // The key cipher name is expected to match a KeyFactory that got registered through RegisterCipher().
247
+ // Either use a built-in cipher, or make sure to register a proper factory for this cipher.
248
+ // This KeyFactory will be called, either directly or when the symmecrypt/seal global singleton gets unsealed, if applicable.
249
+ func LoadKeyFromStore (identifier string , store * configstore.Store ) (symmecrypt.Key , error ) {
250
+
251
+ items , err := ConfigFilter .Slice (identifier ).Store (store ).GetItemList ()
231
252
if err != nil {
232
253
return nil , err
233
254
}
@@ -279,18 +300,24 @@ func LoadKey(identifier string) (symmecrypt.Key, error) {
279
300
return comp , nil
280
301
}
281
302
282
- // LoadSingleKey instantiates a new encryption key using LoadKey from the configstore without specifying its identifier.
303
+ // LoadSingleKey instantiates a new encryption key using LoadKey from the default store in configstore without specifying its identifier.
283
304
// It will error if several different identifiers are found.
284
305
func LoadSingleKey () (symmecrypt.Key , error ) {
285
- ident , err := singleKeyIdentifier ()
306
+ return LoadSingleKeyFromStore (configstore .DefaultStore )
307
+ }
308
+
309
+ // LoadSingleKey instantiates a new encryption key using LoadKey from a specific store instance without specifying its identifier.
310
+ // It will error if several different identifiers are found.
311
+ func LoadSingleKeyFromStore (store * configstore.Store ) (symmecrypt.Key , error ) {
312
+ ident , err := singleKeyIdentifier (store )
286
313
if err != nil {
287
314
return nil , err
288
315
}
289
- return LoadKey (ident )
316
+ return LoadKeyFromStore (ident , store )
290
317
}
291
318
292
- func singleKeyIdentifier () (string , error ) {
293
- items , err := ConfigFilter .GetItemList ()
319
+ func singleKeyIdentifier (store * configstore. Store ) (string , error ) {
320
+ items , err := ConfigFilter .Store ( store ). GetItemList ()
294
321
if err != nil {
295
322
return "" , err
296
323
}
@@ -306,40 +333,52 @@ func singleKeyIdentifier() (string, error) {
306
333
return "" , errors .New ("ambiguous config: several encryption keys found and no identifier supplied" )
307
334
}
308
335
309
- // WatchKey instantiates a new hot-reloading encryption key from the configstore.
336
+ // WatchKey instantiates a new hot-reloading encryption key from the default store in configstore.
310
337
// It uses LoadKey(), so the underlying implementation can be anything supported (composite, sealed, any cipher, ...)
311
338
func WatchKey (identifier string ) (symmecrypt.Key , error ) {
312
- b , err := LoadKey (identifier )
339
+ return WatchKeyFromStore (identifier , configstore .DefaultStore )
340
+ }
341
+
342
+ // WatchKeyFromStore instantiates a new hot-reloading encryption key from a specific store instance.
343
+ // It uses LoadKey(), so the underlying implementation can be anything supported (composite, sealed, any cipher, ...)
344
+ func WatchKeyFromStore (identifier string , store * configstore.Store ) (symmecrypt.Key , error ) {
345
+ b , err := LoadKeyFromStore (identifier , store )
313
346
if err != nil {
314
347
return nil , err
315
348
}
316
349
317
350
holder := & watchKey {identifier : identifier , k : b }
318
- go holder .watch ()
351
+ go holder .watch (store )
319
352
320
353
return holder , nil
321
354
}
322
355
323
- // WatchSingleKey instantiates a new hot-relating encryption key from the configstore without specifying its identifier.
356
+ // WatchSingleKey instantiates a new hot-reloading encryption key from the default store in configstore without specifying its identifier.
324
357
// It will error if several different identifiers are found.
325
358
func WatchSingleKey () (symmecrypt.Key , error ) {
326
- ident , err := singleKeyIdentifier ()
359
+ return WatchSingleKeyFromStore (configstore .DefaultStore )
360
+ }
361
+
362
+ // WatchSingleKey instantiates a new hot-reloading encryption key from a specific store instance without specifying its identifier.
363
+ // It will error if several different identifiers are found.
364
+ func WatchSingleKeyFromStore (store * configstore.Store ) (symmecrypt.Key , error ) {
365
+ ident , err := singleKeyIdentifier (store )
327
366
if err != nil {
328
367
return nil , err
329
368
}
330
- return WatchKey (ident )
369
+ return WatchKeyFromStore (ident , store )
331
370
}
332
371
333
372
/*
334
373
** WATCH implementation: self updating encryption keys
335
374
*/
336
375
337
376
// Watch for configstore update notifications, then reload the key through LoadKey().
338
- func (kh * watchKey ) watch () {
339
- for range configstore .Watch () {
377
+ func (kh * watchKey ) watch (store * configstore. Store ) {
378
+ for range store .Watch () {
340
379
time .Sleep (10 * time .Millisecond )
341
380
// small sleep to yield to symmecrypt/seal in case of seal change
342
- b , err := LoadKey (kh .identifier )
381
+ b , err := LoadKeyFromStore (kh .identifier , store )
343
382
if err != nil {
344
383
logrus .Errorf ("symmecrypt/keyloader: configuration fetch error for key '%s': %s" , kh .identifier , err )
345
384
continue
0 commit comments