From cbbac8fc85b5d2d7a92bfc2026396fd9b26b35e6 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Tue, 26 Sep 2023 00:15:25 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20get=20id=20field=20from=20record?= =?UTF-8?q?ings=20+=20recording=20asset=20detection=20+=20runtime=20clonin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There are a few things that were wrong when replaying recordings for more discoverable services: 1. Any resource with the ID field would not correctly store or read it from recordings. The ID field can be grabbed from the resource in the recording. 2. Asset detection from recording was very inaccurate when using the Platform.Title. Fix it and make it actually usable. 3. Finally, when cloning runtimes during later stages of the discovery process, we loose all connections from the mock provider. Copy them over. Signed-off-by: Dominik Richter --- apps/cnquery/cmd/plugin.go | 3 +-- go.mod | 2 +- go.sum | 4 ++-- providers/recording.go | 32 ++++++++++++++++++++++++++------ providers/runtime.go | 9 +++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/apps/cnquery/cmd/plugin.go b/apps/cnquery/cmd/plugin.go index ec61406ed1..3b41f09ba9 100644 --- a/apps/cnquery/cmd/plugin.go +++ b/apps/cnquery/cmd/plugin.go @@ -117,8 +117,7 @@ func (c *cnqueryPlugin) RunQuery(conf *run.RunQueryConfig, runtime *providers.Ru for i := range assets { connectAsset := assets[i] - connectAssetRuntime := providers.Coordinator.NewRuntime() - connectAssetRuntime.Recording = runtime.Recording + connectAssetRuntime := providers.Coordinator.NewRuntimeFrom(runtime) if err := connectAssetRuntime.DetectProvider(connectAsset); err != nil { return err diff --git a/go.mod b/go.mod index f6e45b31ad..189daf14bb 100644 --- a/go.mod +++ b/go.mod @@ -65,7 +65,7 @@ require ( github.com/pierrec/lz4/v4 v4.1.18 github.com/pkg/sftp v1.13.6 github.com/pkg/term v1.2.0-beta.2 - github.com/rs/zerolog v1.30.0 + github.com/rs/zerolog v1.31.0 github.com/segmentio/fasthash v1.0.3 github.com/segmentio/ksuid v1.0.4 github.com/sethvargo/go-password v0.2.0 diff --git a/go.sum b/go.sum index 4cab89dc93..d2cd064208 100644 --- a/go.sum +++ b/go.sum @@ -782,8 +782,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c= -github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w= +github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryancurrah/gomodguard v1.3.0 h1:q15RT/pd6UggBXVBuLps8BXRvl5GPBcwVA7BJHMLuTw= diff --git a/providers/recording.go b/providers/recording.go index 51a779f8b8..043788e156 100644 --- a/providers/recording.go +++ b/providers/recording.go @@ -364,15 +364,31 @@ func (r *recording) findAssetConnID(asset *inventory.Asset, conf *inventory.Conf id = asset.Mrn } else if asset.Id != "" { id = asset.Id - } else if asset.Platform != nil { - id = asset.Platform.Title } found := -1 - for i := range r.Assets { - if r.Assets[i].Asset.ID == id { - found = i - break + + if id != "" { + for i := range r.Assets { + if r.Assets[i].Asset.ID == id { + found = i + break + } + } + if found != -1 { + return found, id + } + } + + if asset.Platform != nil { + for i := range r.Assets { + if r.Assets[i].Asset.Title == asset.Platform.Title { + found = i + break + } + } + if found != -1 { + return found, r.Assets[found].Asset.ID } } @@ -460,6 +476,10 @@ func (r *recording) GetData(connectionID uint32, resource string, id string, fie } data, ok := obj.Fields[field] + if !ok && field == "id" { + return llx.StringData(id), true + } + return data, ok } diff --git a/providers/runtime.go b/providers/runtime.go index 091fd4bc31..9b5e29b899 100644 --- a/providers/runtime.go +++ b/providers/runtime.go @@ -72,6 +72,15 @@ func (c *coordinator) NewRuntime() *Runtime { return res } +func (c *coordinator) NewRuntimeFrom(parent *Runtime) *Runtime { + res := c.NewRuntime() + res.Recording = parent.Recording + for k, v := range parent.providers { + res.providers[k] = v + } + return res +} + type shutdownResult struct { Response *plugin.ShutdownRes Error error