diff --git a/apps/cnspec/cmd/scan.go b/apps/cnspec/cmd/scan.go index bf58d4e2..a045b6cb 100644 --- a/apps/cnspec/cmd/scan.go +++ b/apps/cnspec/cmd/scan.go @@ -96,6 +96,7 @@ To manually configure a policy, use this: viper.BindPFlag("inventory-domainlist", cmd.Flags().Lookup("inventory-domainlist")) viper.BindPFlag("policy-bundle", cmd.Flags().Lookup("policy-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")) viper.BindPFlag("score-threshold", cmd.Flags().Lookup("score-threshold")) viper.BindPFlag("share", cmd.Flags().Lookup("share")) @@ -220,6 +221,14 @@ func getCobraScanConfig(cmd *cobra.Command, runtime *providers.Runtime, cliRes * 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/policy/scan/local_scanner.go b/policy/scan/local_scanner.go index 2e4d8128..a1713d8d 100644 --- a/policy/scan/local_scanner.go +++ b/policy/scan/local_scanner.go @@ -323,6 +323,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 + } + runtimeEnv := execruntime.Detect() var runtimeLabels map[string]string // If the runtime is an automated environment and the root asset is CI/CD, then we are doing a