macaroons: add SetRootKey method

This adds a SetRootKey method which can be used to set the default
macaroon root key to a specified value.
This commit is contained in:
Daniel McNally committed 2022-08-12 11:34:43 -04:00
1 parent 86e5f9d0db
commit 4b14226e81
3 files changed
+77

No files matched your search

+26
View File
@@ -332,6 +332,32 @@ func (r *RootKeyStorage) GenerateNewRootKey() error {
}, func() {})
}
// SetRootKey sets the default macaroon root key, replacing the previous root
// key if it existed.
func (r *RootKeyStorage) SetRootKey(rootKey []byte) error {
if r.encKey == nil {
return ErrStoreLocked
}
if len(rootKey) != RootKeyLen {
return fmt.Errorf("root key must be %v bytes",
RootKeyLen)
}
encryptedKey, err := r.encKey.Encrypt(rootKey)
if err != nil {
return err
}
return kvdb.Update(r.Backend, func(tx kvdb.RwTx) error {
bucket := tx.ReadWriteBucket(rootKeyBucketName)
if bucket == nil {
return ErrRootKeyBucketNotFound
}
return bucket.Put(DefaultRootKeyID, encryptedKey)
}, func() {})
}
// Close closes the underlying database and zeroes the encryption key stored
// in memory.
func (r *RootKeyStorage) Close() error {