feat: Implement a bbolt backed secrets store.
This commit is contained in:
1 parent
ccbbfd200c
commit
f6991cbe1d
5 files changed
+112
-10
No files matched your search
@@ -1,20 +1,89 @@
|
||||
package secretsstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
var boltStoreBucketName []byte = []byte("secrets-store")
|
||||
|
||||
// bboltStore implements the SecretsStore interface on top of a bbolt bucket.
|
||||
type bboltStore struct {
|
||||
db *bolt.DB
|
||||
}
|
||||
|
||||
// newBboltStore opens a bolt db at path, creates a default bucket, and uses it
|
||||
// as the backing storage for a SecretsStore. Fails if dbpath does not have a
|
||||
// `.db` suffix.
|
||||
func newBboltStore(dbpath string) (*bboltStore, error) {
|
||||
if !strings.HasSuffix(dbpath, ".db") {
|
||||
return nil, errors.New("store path must end in `.db`")
|
||||
}
|
||||
db, err := bolt.Open(dbpath, 0600, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening db: %w", err)
|
||||
}
|
||||
// Ensure the default bucket.
|
||||
err = db.Update(func(tx *bolt.Tx) error {
|
||||
if _, err := tx.CreateBucketIfNotExists(boltStoreBucketName); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return &bboltStore{db: db}, nil
|
||||
}
|
||||
|
||||
func (s *bboltStore) Get(key Key) ([]byte, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
var res []byte
|
||||
err := s.db.View(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(boltStoreBucketName)
|
||||
if b == nil {
|
||||
return ErrInvalidStore
|
||||
}
|
||||
val := b.Get([]byte(key))
|
||||
if val == nil {
|
||||
return ErrKeyDoesNotExist
|
||||
}
|
||||
|
||||
// Must allocate and copy, memory is owned by bolt, and we should not
|
||||
// assume that its lifetime extends beyond the duration of this tx.
|
||||
res = slices.Clone(val)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *bboltStore) Put(key Key, val []byte) error {
|
||||
// TODO
|
||||
return nil
|
||||
return s.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(boltStoreBucketName)
|
||||
if b == nil {
|
||||
return ErrInvalidStore
|
||||
}
|
||||
err := b.Put([]byte(key), val)
|
||||
if err != nil {
|
||||
return errors.Join(ErrCouldNotStoreKey, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *bboltStore) Delete(key Key) error {
|
||||
// TODO
|
||||
return nil
|
||||
return s.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket(boltStoreBucketName)
|
||||
if b == nil {
|
||||
return ErrInvalidStore
|
||||
}
|
||||
err := b.Delete([]byte(key))
|
||||
if err != nil {
|
||||
return errors.Join(ErrCouldNotDeleteKey, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package secretsstore
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalidStore = errors.New("the store backing storage is corrupted or invalid")
|
||||
ErrKeyDoesNotExist = errors.New("the requested key does not exist in the store")
|
||||
ErrCouldNotStoreKey = errors.New("was not able to store the value in the provided key")
|
||||
ErrCouldNotDeleteKey = errors.New("key was not deleted")
|
||||
)
|
||||
@@ -9,7 +9,12 @@ type SecretsStore interface {
|
||||
Delete(key Key) error
|
||||
}
|
||||
|
||||
func New(path string) SecretsStore {
|
||||
// TODO: Actually instantiate the struct.
|
||||
return &bboltStore{}
|
||||
// New returns a SecretsStore that uses the file at path for storage.
|
||||
// path is created if it doesn't exist.
|
||||
func New(path string) (SecretsStore, error) {
|
||||
bstore, err := newBboltStore(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bstore, nil
|
||||
}
|
||||
Reference in new issue
Block a user