From 316689bc0e5153b2448cb66aa6f2f16d5c26b89a Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Tue, 2 Apr 2024 12:46:31 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Add=20function=20to=20load=20the?= =?UTF-8?q?=20asset=20url=20schema=20for=20all=20known=20providers=20(#363?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🧹 Add function to load the asset url schema for all known providers * make it safe to create schema multiple times --- providers-sdk/v1/inventory/asset_url.go | 26 +++++++++++++++++++++++- providers/providers.go | 27 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/providers-sdk/v1/inventory/asset_url.go b/providers-sdk/v1/inventory/asset_url.go index 24680ee13e..8ef8982c9a 100644 --- a/providers-sdk/v1/inventory/asset_url.go +++ b/providers-sdk/v1/inventory/asset_url.go @@ -149,10 +149,34 @@ func (a *AssetUrlSchema) Add(branch *AssetUrlBranch) error { } branch.setDepth(found.Depth + 1) - found.Values[lastKey] = branch + found.Values[lastKey] = copyBranch(branch) return nil } +func copyBranch(b *AssetUrlBranch) *AssetUrlBranch { + if b == nil { + return nil + } + + res := &AssetUrlBranch{ + PathSegments: make([]string, len(b.PathSegments)), + Key: b.Key, + Values: make(map[string]*AssetUrlBranch, len(b.Values)), + Title: b.Title, + References: make([]string, len(b.References)), + Depth: b.Depth, + } + + copy(res.PathSegments, b.PathSegments) + copy(res.References, b.References) + + for k, v := range b.Values { + res.Values[k] = copyBranch(v) + } + + return res +} + func (a *AssetUrlBranch) setDepth(i uint32) { a.Depth = i next := i + 1 diff --git a/providers/providers.go b/providers/providers.go index a76333fc56..5daebf187a 100644 --- a/providers/providers.go +++ b/providers/providers.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/afero" "github.com/ulikunitz/xz" "go.mondoo.com/cnquery/v10/cli/config" + "go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory" "go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin" "go.mondoo.com/cnquery/v10/providers-sdk/v1/resources" "go.mondoo.com/cnquery/v10/providers/core/resources/versions/semver" @@ -942,3 +943,29 @@ func convertToFields(keysAndValues ...interface{}) map[string]interface{} { } return fields } + +func LoadAssetUrlSchema() (*inventory.AssetUrlSchema, error) { + providers, err := ListAll() + if err != nil { + return nil, err + } + + s, err := inventory.NewAssetUrlSchema("technology") + if err != nil { + return nil, err + } + + for _, provider := range providers { + for _, b := range provider.AssetUrlTrees { + if err := s.Add(b); err != nil { + return nil, err + } + } + } + + if err := s.RefreshCache(); err != nil { + return nil, err + } + + return s, nil +}