2020-06-24 14:38:42 -05:00
|
|
|
package cveinfo
|
|
|
|
|
|
|
|
import (
|
2021-10-04 16:27:26 -05:00
|
|
|
"flag"
|
2021-04-05 19:40:33 -05:00
|
|
|
"fmt"
|
2020-08-19 01:03:16 -05:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/commands/artifact"
|
|
|
|
"github.com/aquasecurity/trivy/pkg/commands/operation"
|
2020-06-26 14:09:10 -05:00
|
|
|
"github.com/aquasecurity/trivy/pkg/report"
|
2021-10-04 16:27:26 -05:00
|
|
|
"github.com/aquasecurity/trivy/pkg/types"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2020-06-24 14:38:42 -05:00
|
|
|
)
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
func getRoutePrefix(name string) string {
|
2021-12-13 14:23:31 -05:00
|
|
|
names := strings.SplitN(name, "/", 2) //nolint:gomnd
|
2020-06-24 14:38:42 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
if len(names) != 2 { // nolint: gomnd
|
|
|
|
// it means route is of global storage e.g "centos:latest"
|
|
|
|
if len(names) == 1 {
|
|
|
|
return "/"
|
|
|
|
}
|
2020-06-24 14:38:42 -05:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
return fmt.Sprintf("/%s", names[0])
|
2020-06-24 14:38:42 -05:00
|
|
|
}
|
2020-06-26 14:09:10 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
// UpdateCVEDb ...
|
|
|
|
func UpdateCVEDb(dbDir string, log log.Logger) error {
|
|
|
|
return operation.DownloadDB("dev", dbDir, false, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTrivyContext set some trivy configuration value and return a context.
|
|
|
|
func NewTrivyContext(dir string) *TrivyCtx {
|
|
|
|
trivyCtx := &TrivyCtx{}
|
|
|
|
|
|
|
|
app := &cli.App{}
|
|
|
|
|
|
|
|
flagSet := &flag.FlagSet{}
|
|
|
|
|
|
|
|
var cacheDir string
|
|
|
|
|
|
|
|
flagSet.StringVar(&cacheDir, "cache-dir", dir, "")
|
|
|
|
|
|
|
|
var vuln string
|
|
|
|
|
|
|
|
flagSet.StringVar(&vuln, "vuln-type", strings.Join([]string{types.VulnTypeOS, types.VulnTypeLibrary}, ","), "")
|
|
|
|
|
|
|
|
var severity string
|
|
|
|
|
|
|
|
flagSet.StringVar(&severity, "severity", strings.Join(dbTypes.SeverityNames, ","), "")
|
|
|
|
|
|
|
|
flagSet.StringVar(&trivyCtx.Input, "input", "", "")
|
|
|
|
|
|
|
|
var securityCheck string
|
|
|
|
|
|
|
|
flagSet.StringVar(&securityCheck, "security-checks", types.SecurityCheckVulnerability, "")
|
|
|
|
|
|
|
|
var reportFormat string
|
|
|
|
|
|
|
|
flagSet.StringVar(&reportFormat, "format", "table", "")
|
|
|
|
|
|
|
|
ctx := cli.NewContext(app, flagSet, nil)
|
|
|
|
|
|
|
|
trivyCtx.Ctx = ctx
|
|
|
|
|
|
|
|
return trivyCtx
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
func ScanImage(ctx *cli.Context) (report.Report, error) {
|
|
|
|
return artifact.TrivyImageRun(ctx)
|
2020-06-26 14:09:10 -05:00
|
|
|
}
|
2020-08-19 01:03:16 -05:00
|
|
|
|
2021-04-05 19:40:33 -05:00
|
|
|
func GetCVEInfo(storeController storage.StoreController, log log.Logger) (*CveInfo, error) {
|
|
|
|
cveController := CveTrivyController{}
|
2022-07-12 07:58:04 -05:00
|
|
|
layoutUtils := common.NewBaseOciLayoutUtils(storeController, log)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
subCveConfig := make(map[string]*TrivyCtx)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
if storeController.DefaultStore != nil {
|
|
|
|
imageStore := storeController.DefaultStore
|
|
|
|
|
|
|
|
rootDir := imageStore.RootDir()
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
ctx := NewTrivyContext(rootDir)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
cveController.DefaultCveConfig = ctx
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if storeController.SubStore != nil {
|
|
|
|
for route, storage := range storeController.SubStore {
|
|
|
|
rootDir := storage.RootDir()
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
ctx := NewTrivyContext(rootDir)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
subCveConfig[route] = ctx
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cveController.SubCveConfig = subCveConfig
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return &CveInfo{
|
|
|
|
Log: log, CveTrivyController: cveController, StoreController: storeController,
|
|
|
|
LayoutUtils: layoutUtils,
|
|
|
|
}, nil
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
func (cveinfo CveInfo) GetTrivyContext(image string) *TrivyCtx {
|
2021-04-05 19:40:33 -05:00
|
|
|
// Split image to get route prefix
|
|
|
|
prefixName := getRoutePrefix(image)
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
var trivyCtx *TrivyCtx
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
var rootDir string
|
|
|
|
|
|
|
|
// Get corresponding CVE trivy config, if no sub cve config present that means its default
|
2021-10-04 16:27:26 -05:00
|
|
|
trivyCtx, ok = cveinfo.CveTrivyController.SubCveConfig[prefixName]
|
2021-04-05 19:40:33 -05:00
|
|
|
if ok {
|
|
|
|
imgStore := cveinfo.StoreController.SubStore[prefixName]
|
|
|
|
|
|
|
|
rootDir = imgStore.RootDir()
|
|
|
|
} else {
|
2021-10-04 16:27:26 -05:00
|
|
|
trivyCtx = cveinfo.CveTrivyController.DefaultCveConfig
|
2021-04-05 19:40:33 -05:00
|
|
|
|
|
|
|
imgStore := cveinfo.StoreController.DefaultStore
|
|
|
|
|
|
|
|
rootDir = imgStore.RootDir()
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
trivyCtx.Input = path.Join(rootDir, image)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
return trivyCtx
|
2021-04-05 19:40:33 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 12:37:23 -05:00
|
|
|
func (cveinfo CveInfo) GetImageListForCVE(repo, cvid string, imgStore storage.ImageStore,
|
|
|
|
trivyCtx *TrivyCtx,
|
|
|
|
) ([]*string, error) {
|
2021-04-05 19:40:33 -05:00
|
|
|
tags := make([]*string, 0)
|
|
|
|
|
|
|
|
tagList, err := imgStore.GetImageTags(repo)
|
|
|
|
if err != nil {
|
|
|
|
cveinfo.Log.Error().Err(err).Msg("unable to get list of image tag")
|
|
|
|
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootDir := imgStore.RootDir()
|
|
|
|
|
|
|
|
for _, tag := range tagList {
|
2021-10-04 16:27:26 -05:00
|
|
|
image := fmt.Sprintf("%s:%s", repo, tag)
|
|
|
|
|
|
|
|
trivyCtx.Input = path.Join(rootDir, image)
|
2021-04-05 19:40:33 -05:00
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
isValidImage, _ := cveinfo.LayoutUtils.IsValidImageFormat(image)
|
2021-04-05 19:40:33 -05:00
|
|
|
if !isValidImage {
|
|
|
|
cveinfo.Log.Debug().Str("image", repo+":"+tag).Msg("image media type not supported for scanning")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cveinfo.Log.Info().Str("image", repo+":"+tag).Msg("scanning image")
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
report, err := ScanImage(trivyCtx.Ctx)
|
2021-04-05 19:40:33 -05:00
|
|
|
if err != nil {
|
|
|
|
cveinfo.Log.Error().Err(err).Str("image", repo+":"+tag).Msg("unable to scan image")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:27:26 -05:00
|
|
|
for _, result := range report.Results {
|
2021-04-05 19:40:33 -05:00
|
|
|
for _, vulnerability := range result.Vulnerabilities {
|
2021-12-13 14:23:31 -05:00
|
|
|
if vulnerability.VulnerabilityID == cvid {
|
2021-04-05 19:40:33 -05:00
|
|
|
copyImgTag := tag
|
|
|
|
tags = append(tags, ©ImgTag)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|