libnetwork/datastore: prevent accidental recursion

The datastore cache only uses the reference to its datastore to get a
reference to the backing store. Modify the cache to take the backing
store reference directly so that methods on the datastore can't get
called, as that might result in infinite recursion between datastore and
cache methods.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-10-18 15:50:36 -04:00
parent b85185e659
commit 5b3086db1f
2 changed files with 4 additions and 7 deletions

View file

@ -13,10 +13,10 @@ type kvMap map[string]KVObject
type cache struct {
sync.Mutex
kmm map[string]kvMap
ds *Store
ds store.Store
}
func newCache(ds *Store) *cache {
func newCache(ds store.Store) *cache {
return &cache{kmm: make(map[string]kvMap), ds: ds}
}
@ -40,7 +40,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
return nil, errors.New("error while populating kmap, object does not implement KVConstructor interface")
}
kvList, err := c.ds.store.List(keyPrefix)
kvList, err := c.ds.List(keyPrefix)
if err != nil {
if err == store.ErrKeyNotFound {
// If the store doesn't have anything then there is nothing to

View file

@ -143,10 +143,7 @@ func newClient(kv string, addr string, config *store.Config) (*Store, error) {
return nil, err
}
ds := &Store{scope: scope.Local, store: s}
ds.cache = newCache(ds)
return ds, nil
return &Store{scope: scope.Local, store: s, cache: newCache(s)}, nil
}
// New creates a new Store instance.