Skip to content

Commit

Permalink
🐛 get id field from recordings + recording asset detection + runtime …
Browse files Browse the repository at this point in the history
…cloning

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 <[email protected]>
  • Loading branch information
arlimus committed Sep 26, 2023
1 parent 970e46f commit cbbac8f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
3 changes: 1 addition & 2 deletions apps/cnquery/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
32 changes: 26 additions & 6 deletions providers/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 9 additions & 0 deletions providers/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbbac8f

Please sign in to comment.