From 48bba9cb068023714673956be5b7101f1411ed43 Mon Sep 17 00:00:00 2001 From: Marius Kimmina <38843153+mariuskimmina@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:53:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20--asset-name=20flag=20(#23?= =?UTF-8?q?65)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/cnquery/cmd/scan.go | 10 ++++++++++ explorer/scan/local_scanner.go | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/apps/cnquery/cmd/scan.go b/apps/cnquery/cmd/scan.go index 4f48cd3895..59d1c53442 100644 --- a/apps/cnquery/cmd/scan.go +++ b/apps/cnquery/cmd/scan.go @@ -87,6 +87,7 @@ To manually configure a query pack, use this: viper.BindPFlag("inventory-domainlist", cmd.Flags().Lookup("inventory-domainlist")) viper.BindPFlag("querypack-bundle", cmd.Flags().Lookup("querypack-bundle")) viper.BindPFlag("detect-cicd", cmd.Flags().Lookup("detect-cicd")) + viper.BindPFlag("asset-name", cmd.Flags().Lookup("asset-name")) viper.BindPFlag("category", cmd.Flags().Lookup("category")) // for all assets @@ -176,6 +177,15 @@ func getCobraScanConfig(cmd *cobra.Command, runtime *providers.Runtime, cliRes * for k, v := range annotations { optAnnotations[k] = v } + + assetName, err := cmd.Flags().GetString("asset-name") + if err != nil { + log.Fatal().Err(err).Msg("failed to parse asset-name") + } + if assetName != "" && cliRes.Asset != nil { + cliRes.Asset.Name = assetName + } + inv, err := inventoryloader.ParseOrUse(cliRes.Asset, viper.GetBool("insecure"), optAnnotations) if err != nil { log.Fatal().Err(err).Msg("failed to parse inventory") diff --git a/explorer/scan/local_scanner.go b/explorer/scan/local_scanner.go index e6f731eec5..2fd8d3c5ec 100644 --- a/explorer/scan/local_scanner.go +++ b/explorer/scan/local_scanner.go @@ -272,6 +272,14 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up return nil, false, nil } + // if there is exactly one asset, assure that the --asset-name is used + // TODO: make it so that the --asset-name is set for the root asset only even if multiple assets are there + // This is a temporary fix that only works if there is only one asset + if len(assets) == 1 && assetList[0].Name != "" && assetList[0].Name != assets[0].asset.Name { + log.Debug().Str("asset", assets[0].asset.Name).Msg("Overriding asset name with --asset-name flag") + assets[0].asset.Name = assetList[0].Name + } + justAssets := []*inventory.Asset{} for _, asset := range assets { asset.asset.KindString = asset.asset.GetPlatform().Kind