diff --git a/trivy/pkg/constant/trivy.go b/trivy/pkg/constant/trivy.go index 48a0fe0..fa22073 100644 --- a/trivy/pkg/constant/trivy.go +++ b/trivy/pkg/constant/trivy.go @@ -82,3 +82,6 @@ const ArgDbDownloadUrl = "dbDownloadUrl" // ArgJavaDbDownloadUrl java漏洞库下载地址 const ArgJavaDbDownloadUrl = "javaDbDownloadUrl" + +// ConfigOffline 是否使用离线模式 +const ConfigOffline = "offline" diff --git a/trivy/pkg/scan_executor.go b/trivy/pkg/scan_executor.go index 75d41df..083c1d7 100644 --- a/trivy/pkg/scan_executor.go +++ b/trivy/pkg/scan_executor.go @@ -17,7 +17,10 @@ type TrivyExecutor struct{} // Execute 执行分析 func (e TrivyExecutor) Execute(config *object.ToolConfig, file *os.File) (*object.ToolOutput, error) { - offline := len(config.GetStringArg(constant.ArgDbDownloadUrl)) > 0 + offline, err := config.GetBoolArg(constant.ConfigOffline) + if err != nil { + offline = len(config.GetStringArg(constant.ArgDbDownloadUrl)) > 0 + } if offline { if err := downloadAllDB(config); err != nil { return nil, err @@ -34,16 +37,20 @@ func downloadAllDB(config *object.ToolConfig) error { downloader := &util.DefaultDownloader{} // download db url := config.GetStringArg(constant.ArgDbDownloadUrl) - dbDir := filepath.Join(constant.DbCacheDir, constant.DbDir) - if err := util.ExtractTarUrl(url, dbDir, 0770, downloader); err != nil { - return err + if len(url) > 0 { + dbDir := filepath.Join(constant.DbCacheDir, constant.DbDir) + if err := util.ExtractTarUrl(url, dbDir, 0770, downloader); err != nil { + return err + } } // download java db javaDbUrl := config.GetStringArg(constant.ArgJavaDbDownloadUrl) - javaDbDir := filepath.Join(constant.DbCacheDir, constant.JavaDbDir) - if err := util.ExtractTarUrl(javaDbUrl, javaDbDir, 0770, downloader); err != nil { - return err + if len(javaDbUrl) > 0 { + javaDbDir := filepath.Join(constant.DbCacheDir, constant.JavaDbDir) + if err := util.ExtractTarUrl(javaDbUrl, javaDbDir, 0770, downloader); err != nil { + return err + } } return nil }