@@ -66,7 +66,6 @@ type watchKey struct {
6666
6767// A sealedKey is an implementation of an encryption key that is encrypted using symmecrypt/seal.
6868type sealedKey struct {
69- encryptedKey string
7069 decryptedKey symmecrypt.Key
7170 decrypted uint32
7271 waitCh chan struct {}
@@ -157,13 +156,19 @@ func UnsealKey(k *KeyConfig, s *seal.Seal) (*KeyConfig, error) {
157156 }, nil
158157}
159158
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
161160// ensuring they are unsealed.
162161func 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 ) {
163168
164169 ret := []* KeyConfig {}
165170
166- items , err := ConfigFilter .GetItemList ()
171+ items , err := ConfigFilter .Store ( store ). GetItemList ()
167172 if err != nil {
168173 return nil , err
169174 }
@@ -213,7 +218,7 @@ func configFactory() interface{} {
213218** CONSTRUCTORS
214219 */
215220
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.
217222//
218223// If several keys are found for the identifier, they are sorted by timestamp, and a composite key is returned.
219224// 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{} {
226231// Either use a built-in cipher, or make sure to register a proper factory for this cipher.
227232// This KeyFactory will be called, either directly or when the symmecrypt/seal global singleton gets unsealed, if applicable.
228233func LoadKey (identifier string ) (symmecrypt.Key , error ) {
234+ return LoadKeyFromStore (identifier , configstore .DefaultStore )
235+ }
229236
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 ()
231252 if err != nil {
232253 return nil , err
233254 }
@@ -279,18 +300,24 @@ func LoadKey(identifier string) (symmecrypt.Key, error) {
279300 return comp , nil
280301}
281302
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.
283304// It will error if several different identifiers are found.
284305func 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 )
286313 if err != nil {
287314 return nil , err
288315 }
289- return LoadKey (ident )
316+ return LoadKeyFromStore (ident , store )
290317}
291318
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 ()
294321 if err != nil {
295322 return "" , err
296323 }
@@ -306,40 +333,52 @@ func singleKeyIdentifier() (string, error) {
306333 return "" , errors .New ("ambiguous config: several encryption keys found and no identifier supplied" )
307334}
308335
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.
310337// It uses LoadKey(), so the underlying implementation can be anything supported (composite, sealed, any cipher, ...)
311338func 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 )
313346 if err != nil {
314347 return nil , err
315348 }
316349
317350 holder := & watchKey {identifier : identifier , k : b }
318- go holder .watch ()
351+ go holder .watch (store )
319352
320353 return holder , nil
321354}
322355
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.
324357// It will error if several different identifiers are found.
325358func 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 )
327366 if err != nil {
328367 return nil , err
329368 }
330- return WatchKey (ident )
369+ return WatchKeyFromStore (ident , store )
331370}
332371
333372/*
334373** WATCH implementation: self updating encryption keys
335374 */
336375
337376// 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 () {
340379 time .Sleep (10 * time .Millisecond )
341380 // 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 )
343382 if err != nil {
344383 logrus .Errorf ("symmecrypt/keyloader: configuration fetch error for key '%s': %s" , kh .identifier , err )
345384 continue
0 commit comments