feat(misconf): Add --reset-policy-bundle for policy bundle (#4167)

This commit is contained in:
simar7
2023-05-18 02:54:01 -06:00
committed by GitHub
parent 33fb04763d
commit 92f9e98d04
13 changed files with 52 additions and 1 deletions

View File

@@ -1160,7 +1160,7 @@ func validateArgs(cmd *cobra.Command, args []string) error {
// '--clear-cache', '--download-db-only', '--download-java-db-only', '--reset' and '--generate-default-config' don't conduct the subsequent scanning
if viper.GetBool(flag.ClearCacheFlag.ConfigName) || viper.GetBool(flag.DownloadDBOnlyFlag.ConfigName) ||
viper.GetBool(flag.ResetFlag.ConfigName) || viper.GetBool(flag.GenerateDefaultConfigFlag.ConfigName) ||
viper.GetBool(flag.DownloadJavaDBOnlyFlag.ConfigName) {
viper.GetBool(flag.DownloadJavaDBOnlyFlag.ConfigName) || viper.GetBool(flag.ResetPolicyBundleFlag.ConfigName) {
return nil
}

View File

@@ -6,6 +6,8 @@ import (
"fmt"
"os"
"github.com/aquasecurity/trivy/pkg/policy"
"github.com/hashicorp/go-multierror"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
@@ -387,6 +389,18 @@ func (r *runner) initCache(opts flag.Options) error {
}
return SkipScan
}
if opts.ResetPolicyBundle {
c, err := policy.NewClient(fsutils.CacheDir(), true)
if err != nil {
return xerrors.Errorf("failed to instantiate policy client: %w", err)
}
if err := c.Clear(); err != nil {
return xerrors.Errorf("failed to remove the cache: %w", err)
}
return SkipScan
}
if opts.ClearCache {
defer cacheClient.Close()
if err = cacheClient.ClearArtifacts(); err != nil {