2017-08-03 17:24:27 +00:00
|
|
|
package inmem
|
2015-03-02 18:48:53 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2018-02-12 16:16:13 +00:00
|
|
|
"errors"
|
2019-05-01 17:47:41 +00:00
|
|
|
"fmt"
|
2018-09-07 21:35:46 +00:00
|
|
|
"os"
|
2019-05-01 17:47:41 +00:00
|
|
|
"strconv"
|
2015-03-02 18:48:53 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-02-12 16:16:13 +00:00
|
|
|
"sync/atomic"
|
2015-03-02 18:48:53 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
radix "github.com/armon/go-radix"
|
2015-03-02 18:48:53 +00:00
|
|
|
)
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
// Verify interfaces are satisfied
|
2018-01-20 01:44:24 +00:00
|
|
|
var _ physical.Backend = (*InmemBackend)(nil)
|
|
|
|
var _ physical.HABackend = (*InmemHABackend)(nil)
|
|
|
|
var _ physical.HABackend = (*TransactionalInmemHABackend)(nil)
|
|
|
|
var _ physical.Lock = (*InmemLock)(nil)
|
|
|
|
var _ physical.Transactional = (*TransactionalInmemBackend)(nil)
|
|
|
|
var _ physical.Transactional = (*TransactionalInmemHABackend)(nil)
|
2018-01-19 06:44:44 +00:00
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
var (
|
|
|
|
PutDisabledError = errors.New("put operations disabled in inmem backend")
|
|
|
|
GetDisabledError = errors.New("get operations disabled in inmem backend")
|
|
|
|
DeleteDisabledError = errors.New("delete operations disabled in inmem backend")
|
|
|
|
ListDisabledError = errors.New("list operations disabled in inmem backend")
|
|
|
|
)
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// 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
|
2019-05-01 17:47:41 +00:00
|
|
|
root *radix.Tree
|
|
|
|
permitPool *physical.PermitPool
|
|
|
|
logger log.Logger
|
|
|
|
failGet *uint32
|
|
|
|
failPut *uint32
|
|
|
|
failDelete *uint32
|
|
|
|
failList *uint32
|
|
|
|
logOps bool
|
|
|
|
maxValueSize int
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
type TransactionalInmemBackend struct {
|
|
|
|
InmemBackend
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// NewInmem constructs a new in-memory backend
|
2019-05-01 17:47:41 +00:00
|
|
|
func NewInmem(conf map[string]string, logger log.Logger) (physical.Backend, error) {
|
|
|
|
maxValueSize := 0
|
|
|
|
maxValueSizeStr, ok := conf["max_value_size"]
|
|
|
|
if ok {
|
|
|
|
var err error
|
|
|
|
maxValueSize, err = strconv.Atoi(maxValueSizeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
2019-05-01 17:47:41 +00:00
|
|
|
|
|
|
|
return &InmemBackend{
|
|
|
|
root: radix.New(),
|
|
|
|
permitPool: physical.NewPermitPool(physical.DefaultParallelOperations),
|
|
|
|
logger: logger,
|
|
|
|
failGet: new(uint32),
|
|
|
|
failPut: new(uint32),
|
|
|
|
failDelete: new(uint32),
|
|
|
|
failList: new(uint32),
|
|
|
|
logOps: os.Getenv("VAULT_INMEM_LOG_ALL_OPS") != "",
|
|
|
|
maxValueSize: maxValueSize,
|
|
|
|
}, nil
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-05-01 17:47:41 +00:00
|
|
|
func NewTransactionalInmem(conf map[string]string, logger log.Logger) (physical.Backend, error) {
|
|
|
|
maxValueSize := 0
|
|
|
|
maxValueSizeStr, ok := conf["max_value_size"]
|
|
|
|
if ok {
|
|
|
|
var err error
|
|
|
|
maxValueSize, err = strconv.Atoi(maxValueSizeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TransactionalInmemBackend{
|
2017-02-17 14:15:35 +00:00
|
|
|
InmemBackend: InmemBackend{
|
2019-05-01 17:47:41 +00:00
|
|
|
root: radix.New(),
|
|
|
|
permitPool: physical.NewPermitPool(1),
|
|
|
|
logger: logger,
|
|
|
|
failGet: new(uint32),
|
|
|
|
failPut: new(uint32),
|
|
|
|
failDelete: new(uint32),
|
|
|
|
failList: new(uint32),
|
|
|
|
logOps: os.Getenv("VAULT_INMEM_LOG_ALL_OPS") != "",
|
|
|
|
maxValueSize: maxValueSize,
|
2017-02-17 14:15:35 +00:00
|
|
|
},
|
2019-05-01 17:47:41 +00:00
|
|
|
}, nil
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Put is used to insert or update an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) Put(ctx context.Context, entry *physical.Entry) error {
|
2015-11-03 16:47:16 +00:00
|
|
|
i.permitPool.Acquire()
|
|
|
|
defer i.permitPool.Release()
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return i.PutInternal(ctx, entry)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
2015-11-03 19:48:05 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) PutInternal(ctx context.Context, entry *physical.Entry) error {
|
2018-09-07 21:35:46 +00:00
|
|
|
if i.logOps {
|
|
|
|
i.logger.Trace("put", "key", entry.Key)
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
if atomic.LoadUint32(i.failPut) != 0 {
|
2018-02-12 16:16:13 +00:00
|
|
|
return PutDisabledError
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:50:49 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:47:41 +00:00
|
|
|
if i.maxValueSize > 0 && len(entry.Value) > i.maxValueSize {
|
|
|
|
return fmt.Errorf("%s", physical.ErrValueTooLarge)
|
|
|
|
}
|
|
|
|
|
2017-11-30 14:43:07 +00:00
|
|
|
i.root.Insert(entry.Key, entry.Value)
|
2015-03-02 18:48:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:25:48 +00:00
|
|
|
func (i *InmemBackend) FailPut(fail bool) {
|
|
|
|
var val uint32
|
|
|
|
if fail {
|
|
|
|
val = 1
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
atomic.StoreUint32(i.failPut, val)
|
2018-02-12 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Get is used to fetch an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
|
2015-11-03 16:47:16 +00:00
|
|
|
i.permitPool.Acquire()
|
|
|
|
defer i.permitPool.Release()
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return i.GetInternal(ctx, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
2015-11-03 19:48:05 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) GetInternal(ctx context.Context, key string) (*physical.Entry, error) {
|
2018-09-07 21:35:46 +00:00
|
|
|
if i.logOps {
|
|
|
|
i.logger.Trace("get", "key", key)
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
if atomic.LoadUint32(i.failGet) != 0 {
|
2018-02-12 16:16:13 +00:00
|
|
|
return nil, GetDisabledError
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:50:49 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
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
|
2015-03-02 18:48:53 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:25:48 +00:00
|
|
|
func (i *InmemBackend) FailGet(fail bool) {
|
|
|
|
var val uint32
|
|
|
|
if fail {
|
|
|
|
val = 1
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
atomic.StoreUint32(i.failGet, val)
|
2018-02-12 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
// Delete is used to permanently delete an entry
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) Delete(ctx context.Context, key string) error {
|
2015-11-03 16:47:16 +00:00
|
|
|
i.permitPool.Acquire()
|
|
|
|
defer i.permitPool.Release()
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return i.DeleteInternal(ctx, key)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
2015-11-03 19:48:05 +00:00
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) DeleteInternal(ctx context.Context, key string) error {
|
2018-09-07 21:35:46 +00:00
|
|
|
if i.logOps {
|
|
|
|
i.logger.Trace("delete", "key", key)
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
if atomic.LoadUint32(i.failDelete) != 0 {
|
2018-02-12 16:16:13 +00:00
|
|
|
return DeleteDisabledError
|
|
|
|
}
|
2018-07-24 21:50:49 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2018-02-12 16:16:13 +00:00
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
i.root.Delete(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:25:48 +00:00
|
|
|
func (i *InmemBackend) FailDelete(fail bool) {
|
|
|
|
var val uint32
|
|
|
|
if fail {
|
|
|
|
val = 1
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
atomic.StoreUint32(i.failDelete, val)
|
2018-02-12 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 16:26:40 +00:00
|
|
|
// List is used to list all the keys under a given
|
2015-03-02 18:48:53 +00:00
|
|
|
// prefix, up to the next prefix.
|
2018-01-19 06:44:44 +00:00
|
|
|
func (i *InmemBackend) List(ctx context.Context, prefix string) ([]string, error) {
|
2015-11-03 16:47:16 +00:00
|
|
|
i.permitPool.Acquire()
|
|
|
|
defer i.permitPool.Release()
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
|
|
|
|
2018-07-24 21:50:49 +00:00
|
|
|
return i.ListInternal(ctx, prefix)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
2015-11-03 19:48:05 +00:00
|
|
|
|
2018-07-24 21:50:49 +00:00
|
|
|
func (i *InmemBackend) ListInternal(ctx context.Context, prefix string) ([]string, error) {
|
2018-09-07 21:35:46 +00:00
|
|
|
if i.logOps {
|
|
|
|
i.logger.Trace("list", "prefix", prefix)
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
if atomic.LoadUint32(i.failList) != 0 {
|
2018-02-12 16:16:13 +00:00
|
|
|
return nil, ListDisabledError
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
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)
|
|
|
|
|
2018-07-24 21:50:49 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:48:53 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
|
2018-02-12 16:25:48 +00:00
|
|
|
func (i *InmemBackend) FailList(fail bool) {
|
|
|
|
var val uint32
|
|
|
|
if fail {
|
|
|
|
val = 1
|
|
|
|
}
|
2018-06-09 19:35:22 +00:00
|
|
|
atomic.StoreUint32(i.failList, val)
|
2018-02-12 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
// Implements the transaction interface
|
2018-01-19 06:44:44 +00:00
|
|
|
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()
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return physical.GenericTransactionHandler(ctx, t, txns)
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|