diff --git a/NOTICE.txt b/NOTICE.txt index c418ba117db..04effbec62f 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -19960,12 +19960,12 @@ Exhibit B - "Incompatible With Secondary Licenses" Notice -------------------------------------------------------------------------------- -Dependency : github.com/hashicorp/golang-lru -Version: v0.6.0 +Dependency : github.com/hashicorp/golang-lru/v2 +Version: v2.0.7 Licence type (autodetected): MPL-2.0 -------------------------------------------------------------------------------- -Contents of probable licence file $GOMODCACHE/github.com/hashicorp/golang-lru@v0.6.0/LICENSE: +Contents of probable licence file $GOMODCACHE/github.com/hashicorp/golang-lru/v2@v2.0.7/LICENSE: Copyright (c) 2014 HashiCorp, Inc. diff --git a/go.mod b/go.mod index 4d626e055d5..8c2484ed01d 100644 --- a/go.mod +++ b/go.mod @@ -95,7 +95,6 @@ require ( github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 github.com/h2non/filetype v1.1.1 github.com/hashicorp/go-retryablehttp v0.7.7 - github.com/hashicorp/golang-lru v0.6.0 github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3 github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 github.com/insomniacslk/dhcp v0.0.0-20220119180841-3c283ff8b7dd @@ -209,6 +208,7 @@ require ( github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 github.com/gorilla/websocket v1.5.0 + github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/icholy/digest v0.1.22 github.com/klauspost/compress v1.17.11 github.com/meraki/dashboard-api-go/v3 v3.0.9 diff --git a/go.sum b/go.sum index c6c6a96b14e..09ce9f05978 100644 --- a/go.sum +++ b/go.sum @@ -609,8 +609,8 @@ github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/golang-lru v0.6.0 h1:uL2shRDx7RTrOrTCUZEGP/wJUFiUI8QT6E7z5o8jga4= -github.com/hashicorp/golang-lru v0.6.0/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3 h1:fgVfQ4AC1avVOnu2cfms8VAiD8lUq3vWI8mTocOXN/w= github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3/go.mod h1:svtxn6QnrQ69P23VvIWMR34tg3vmwLz4UdUzm1dSCgE= github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 h1:S4qyfL2sEm5Budr4KVMyEniCy+PbS55651I/a+Kn/NQ= diff --git a/x-pack/osquerybeat/beater/osquerybeat.go b/x-pack/osquerybeat/beater/osquerybeat.go index 7ed65de2dc6..6b659ed7dce 100644 --- a/x-pack/osquerybeat/beater/osquerybeat.go +++ b/x-pack/osquerybeat/beater/osquerybeat.go @@ -12,7 +12,7 @@ import ( "time" "github.com/gofrs/uuid/v5" - lru "github.com/hashicorp/golang-lru" + lru "github.com/hashicorp/golang-lru/v2" "github.com/osquery/osquery-go" kconfig "github.com/osquery/osquery-go/plugin/config" klogger "github.com/osquery/osquery-go/plugin/logger" @@ -254,7 +254,7 @@ func (bt *osquerybeat) runOsquery(ctx context.Context, b *beat.Beat, osq *osqd.O socketPath := osq.SocketPath() // Create a cache for queries types resolution - cache, err := lru.New(adhocOsqueriesTypesCacheSize) + cache, err := lru.New[string, map[string]string](adhocOsqueriesTypesCacheSize) if err != nil { bt.log.Errorf("Failed to create osquery query results types cache: %v", err) return err diff --git a/x-pack/osquerybeat/internal/osqdcli/cache.go b/x-pack/osquerybeat/internal/osqdcli/cache.go index de69943da6c..aaaf10ef8a6 100644 --- a/x-pack/osquerybeat/internal/osqdcli/cache.go +++ b/x-pack/osquerybeat/internal/osqdcli/cache.go @@ -4,15 +4,15 @@ package osqdcli -type Cache interface { - Add(key, value interface{}) (evicted bool) - Get(key interface{}) (value interface{}, ok bool) +type Cache[K comparable, V any] interface { + Add(K, V) (evicted bool) + Get(K) (value V, ok bool) Resize(size int) (evicted int) } -func WithCache(cache Cache, minSize int) Option { +func WithCache(cache Cache[string, map[string]string], minSize int) Option { return func(c *Client) { - nsc := &nullSafeCache{cache: cache} + nsc := &nullSafeCache[string, map[string]string]{cache: cache} if minSize > 0 { nsc.minSize = minSize } @@ -20,28 +20,28 @@ func WithCache(cache Cache, minSize int) Option { } } -type nullSafeCache struct { - cache Cache +type nullSafeCache[K comparable, V any] struct { + cache Cache[K, V] minSize int } -func (c *nullSafeCache) Add(key, value interface{}) (evicted bool) { +func (c *nullSafeCache[K, V]) Add(key K, value V) (evicted bool) { if c.cache == nil { - return + return false } return c.cache.Add(key, value) } -func (c *nullSafeCache) Get(key interface{}) (value interface{}, ok bool) { +func (c *nullSafeCache[K, V]) Get(key K) (value V, ok bool) { if c.cache == nil { - return + return value, ok } return c.cache.Get(key) } -func (c *nullSafeCache) Resize(size int) (evicted int) { +func (c *nullSafeCache[K, V]) Resize(size int) (evicted int) { if c.cache == nil { - return + return 0 } return c.cache.Resize(c.minSize + size) } diff --git a/x-pack/osquerybeat/internal/osqdcli/client.go b/x-pack/osquerybeat/internal/osqdcli/client.go index ca9ce790580..a0eb9f1e700 100644 --- a/x-pack/osquerybeat/internal/osqdcli/client.go +++ b/x-pack/osquerybeat/internal/osqdcli/client.go @@ -68,7 +68,7 @@ type Client struct { cli *osquery.ExtensionManagerClient mx sync.Mutex - cache Cache + cache Cache[string, map[string]string] cliLimiter *semaphore.Weighted } @@ -106,7 +106,7 @@ func New(socketPath string, opts ...Option) *Client { timeout: defaultTimeout, maxTimeout: defaultMaxTimeout, connectRetries: defaultConnectRetries, - cache: &nullSafeCache{}, + cache: &nullSafeCache[string, map[string]string]{}, cliLimiter: semaphore.NewWeighted(limit), } @@ -258,15 +258,9 @@ func (c *Client) resolveResult(ctx context.Context, sql string, hits []map[strin } func (c *Client) queryColumnTypes(ctx context.Context, sql string) (map[string]string, error) { - var colTypes map[string]string - - if v, ok := c.cache.Get(sql); ok { - colTypes, ok = v.(map[string]string) - if ok { - c.log.Debugf("using cached column types for query: %s", sql) - } else { - c.log.Error("failed get the column types from cache, incompatible type") - } + colTypes, ok := c.cache.Get(sql) + if ok { + c.log.Debugf("using cached column types for query: %s", sql) } if colTypes == nil {