open-vault/physical/inmem/inmem.go

155 lines
3.7 KiB
Go
Raw Normal View History

package inmem
import (
"context"
"strings"
"sync"
"github.com/hashicorp/vault/physical"
2016-08-19 20:45:17 +00:00
log "github.com/mgutz/logxi/v1"
"github.com/armon/go-radix"
)
// Verify interfaces are satisfied
var _ physical.Backend = &InmemBackend{}
var _ physical.HABackend = &InmemHABackend{}
var _ physical.Lock = &InmemLock{}
var _ physical.Transactional = &TransactionalInmemBackend{}
var _ physical.Transactional = &TransactionalInmemHABackend{}
// InmemBackend is an in-memory only physical backend. It is useful
// for testing and development situations where the data is not
// expected to be durable.
type InmemBackend struct {
2017-02-17 14:15:35 +00:00
sync.RWMutex
root *radix.Tree
permitPool *physical.PermitPool
2016-08-19 20:45:17 +00:00
logger log.Logger
}
2017-02-17 14:15:35 +00:00
type TransactionalInmemBackend struct {
InmemBackend
}
// NewInmem constructs a new in-memory backend
func NewInmem(_ map[string]string, logger log.Logger) (physical.Backend, error) {
in := &InmemBackend{
root: radix.New(),
permitPool: physical.NewPermitPool(physical.DefaultParallelOperations),
logger: logger,
}
return in, nil
}
2017-02-17 14:15:35 +00:00
// Basically for now just creates a permit pool of size 1 so only one operation
// can run at a time
func NewTransactionalInmem(_ map[string]string, logger log.Logger) (physical.Backend, error) {
2017-02-17 14:15:35 +00:00
in := &TransactionalInmemBackend{
InmemBackend: InmemBackend{
root: radix.New(),
permitPool: physical.NewPermitPool(1),
2017-02-20 16:08:03 +00:00
logger: logger,
2017-02-17 14:15:35 +00:00
},
}
return in, nil
2017-02-17 14:15:35 +00:00
}
// Put is used to insert or update an entry
func (i *InmemBackend) Put(ctx context.Context, entry *physical.Entry) error {
i.permitPool.Acquire()
defer i.permitPool.Release()
2017-02-17 14:15:35 +00:00
i.Lock()
defer i.Unlock()
return i.PutInternal(ctx, entry)
2017-02-17 14:15:35 +00:00
}
2015-11-03 19:48:05 +00:00
func (i *InmemBackend) PutInternal(ctx context.Context, entry *physical.Entry) error {
2017-11-30 14:43:07 +00:00
i.root.Insert(entry.Key, entry.Value)
return nil
}
// Get is used to fetch an entry
func (i *InmemBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
i.permitPool.Acquire()
defer i.permitPool.Release()
2017-02-17 14:15:35 +00:00
i.RLock()
defer i.RUnlock()
return i.GetInternal(ctx, key)
2017-02-17 14:15:35 +00:00
}
2015-11-03 19:48:05 +00:00
func (i *InmemBackend) GetInternal(ctx context.Context, key string) (*physical.Entry, error) {
if raw, ok := i.root.Get(key); ok {
2017-11-30 14:43:07 +00:00
return &physical.Entry{
Key: key,
Value: raw.([]byte),
}, nil
}
return nil, nil
}
// Delete is used to permanently delete an entry
func (i *InmemBackend) Delete(ctx context.Context, key string) error {
i.permitPool.Acquire()
defer i.permitPool.Release()
2017-02-17 14:15:35 +00:00
i.Lock()
defer i.Unlock()
return i.DeleteInternal(ctx, key)
2017-02-17 14:15:35 +00:00
}
2015-11-03 19:48:05 +00:00
func (i *InmemBackend) DeleteInternal(ctx context.Context, key string) error {
i.root.Delete(key)
return nil
}
// List is used ot list all the keys under a given
// prefix, up to the next prefix.
func (i *InmemBackend) List(ctx context.Context, prefix string) ([]string, error) {
i.permitPool.Acquire()
defer i.permitPool.Release()
2017-02-17 14:15:35 +00:00
i.RLock()
defer i.RUnlock()
return i.ListInternal(prefix)
}
2015-11-03 19:48:05 +00:00
2017-02-17 14:15:35 +00:00
func (i *InmemBackend) ListInternal(prefix string) ([]string, error) {
var out []string
seen := make(map[string]interface{})
walkFn := func(s string, v interface{}) bool {
trimmed := strings.TrimPrefix(s, prefix)
sep := strings.Index(trimmed, "/")
if sep == -1 {
out = append(out, trimmed)
} else {
trimmed = trimmed[:sep+1]
if _, ok := seen[trimmed]; !ok {
out = append(out, trimmed)
seen[trimmed] = struct{}{}
}
}
return false
}
i.root.WalkPrefix(prefix, walkFn)
return out, nil
}
2017-02-17 14:15:35 +00:00
// Implements the transaction interface
func (t *TransactionalInmemBackend) Transaction(ctx context.Context, txns []*physical.TxnEntry) error {
2017-02-17 14:15:35 +00:00
t.permitPool.Acquire()
defer t.permitPool.Release()
t.Lock()
defer t.Unlock()
return physical.GenericTransactionHandler(ctx, t, txns)
2017-02-17 14:15:35 +00:00
}