github.com/hashicorp/vault/sdk@v0.13.0/logical/storage_inmem.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package logical 5 6 import ( 7 "context" 8 "sync" 9 10 "github.com/hashicorp/vault/sdk/physical" 11 "github.com/hashicorp/vault/sdk/physical/inmem" 12 ) 13 14 // InmemStorage implements Storage and stores all data in memory. It is 15 // basically a straight copy of physical.Inmem, but it prevents backends from 16 // having to load all of physical's dependencies (which are legion) just to 17 // have some testing storage. 18 type InmemStorage struct { 19 underlying physical.Backend 20 once sync.Once 21 } 22 23 func (s *InmemStorage) Get(ctx context.Context, key string) (*StorageEntry, error) { 24 s.once.Do(s.init) 25 26 entry, err := s.underlying.Get(ctx, key) 27 if err != nil { 28 return nil, err 29 } 30 if entry == nil { 31 return nil, nil 32 } 33 return &StorageEntry{ 34 Key: entry.Key, 35 Value: entry.Value, 36 SealWrap: entry.SealWrap, 37 }, nil 38 } 39 40 func (s *InmemStorage) Put(ctx context.Context, entry *StorageEntry) error { 41 s.once.Do(s.init) 42 43 return s.underlying.Put(ctx, &physical.Entry{ 44 Key: entry.Key, 45 Value: entry.Value, 46 SealWrap: entry.SealWrap, 47 }) 48 } 49 50 func (s *InmemStorage) Delete(ctx context.Context, key string) error { 51 s.once.Do(s.init) 52 53 return s.underlying.Delete(ctx, key) 54 } 55 56 func (s *InmemStorage) List(ctx context.Context, prefix string) ([]string, error) { 57 s.once.Do(s.init) 58 59 return s.underlying.List(ctx, prefix) 60 } 61 62 func (s *InmemStorage) Underlying() *inmem.InmemBackend { 63 s.once.Do(s.init) 64 65 return s.underlying.(*inmem.InmemBackend) 66 } 67 68 func (s *InmemStorage) FailPut(fail bool) *InmemStorage { 69 s.Underlying().FailPut(fail) 70 return s 71 } 72 73 func (s *InmemStorage) FailGet(fail bool) *InmemStorage { 74 s.Underlying().FailGet(fail) 75 return s 76 } 77 78 func (s *InmemStorage) FailDelete(fail bool) *InmemStorage { 79 s.Underlying().FailDelete(fail) 80 return s 81 } 82 83 func (s *InmemStorage) FailList(fail bool) *InmemStorage { 84 s.Underlying().FailList(fail) 85 return s 86 } 87 88 func (s *InmemStorage) init() { 89 s.underlying, _ = inmem.NewInmem(nil, nil) 90 }