2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
package logical
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-15 21:26:48 +00:00
|
|
|
"sync"
|
2016-06-06 16:03:08 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical/inmem"
|
2015-03-15 21:26:48 +00:00
|
|
|
)
|
|
|
|
|
2017-07-26 21:59:14 +00:00
|
|
|
// InmemStorage implements Storage and stores all data in memory. It is
|
|
|
|
// basically a straight copy of physical.Inmem, but it prevents backends from
|
|
|
|
// having to load all of physical's dependencies (which are legion) just to
|
|
|
|
// have some testing storage.
|
2015-03-15 21:26:48 +00:00
|
|
|
type InmemStorage struct {
|
2018-02-12 16:16:13 +00:00
|
|
|
underlying physical.Backend
|
|
|
|
once sync.Once
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *InmemStorage) Get(ctx context.Context, key string) (*StorageEntry, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
s.once.Do(s.init)
|
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
entry, err := s.underlying.Get(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-06 16:03:08 +00:00
|
|
|
}
|
2018-02-12 16:16:13 +00:00
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &StorageEntry{
|
|
|
|
Key: entry.Key,
|
|
|
|
Value: entry.Value,
|
|
|
|
SealWrap: entry.SealWrap,
|
|
|
|
}, nil
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *InmemStorage) Put(ctx context.Context, entry *StorageEntry) error {
|
2015-03-15 21:26:48 +00:00
|
|
|
s.once.Do(s.init)
|
2017-07-26 21:59:14 +00:00
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
return s.underlying.Put(ctx, &physical.Entry{
|
|
|
|
Key: entry.Key,
|
|
|
|
Value: entry.Value,
|
|
|
|
SealWrap: entry.SealWrap,
|
2017-07-26 21:59:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *InmemStorage) Delete(ctx context.Context, key string) error {
|
2017-07-26 21:59:14 +00:00
|
|
|
s.once.Do(s.init)
|
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
return s.underlying.Delete(ctx, key)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (s *InmemStorage) List(ctx context.Context, prefix string) ([]string, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
s.once.Do(s.init)
|
2017-07-26 21:59:14 +00:00
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
return s.underlying.List(ctx, prefix)
|
|
|
|
}
|
2017-07-26 21:59:14 +00:00
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
func (s *InmemStorage) Underlying() *inmem.InmemBackend {
|
|
|
|
s.once.Do(s.init)
|
2017-07-26 21:59:14 +00:00
|
|
|
|
2018-02-12 16:16:13 +00:00
|
|
|
return s.underlying.(*inmem.InmemBackend)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:28:00 +00:00
|
|
|
func (s *InmemStorage) FailPut(fail bool) *InmemStorage {
|
|
|
|
s.Underlying().FailPut(fail)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) FailGet(fail bool) *InmemStorage {
|
|
|
|
s.Underlying().FailGet(fail)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) FailDelete(fail bool) *InmemStorage {
|
|
|
|
s.Underlying().FailDelete(fail)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InmemStorage) FailList(fail bool) *InmemStorage {
|
|
|
|
s.Underlying().FailList(fail)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
func (s *InmemStorage) init() {
|
2018-02-12 16:16:13 +00:00
|
|
|
s.underlying, _ = inmem.NewInmem(nil, nil)
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|