mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 23:26:39 -08:00
* wip: Add a failing test to demo severity override Signed-off-by: Simarpreet Singh <simar@linux.com> * scan.go: Return osFound for use in determining vendor. Signed-off-by: Simarpreet Singh <simar@linux.com> * pkg: Fix ScanImage return in case an OSFound Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Include a package-lock.json for happy path Signed-off-by: Simarpreet Singh <simar@linux.com> * wip: Add a test to include various reportResult types Signed-off-by: Simarpreet Singh <simar@linux.com> * Makefile: Add a target to generate mocks. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Pass reportType as argument for FillInfo. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add other types of vulnerabilities. Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Update golden files. Signed-off-by: Simarpreet Singh <simar@linux.com> * ospkg: Fix FillInfo for ospkg/server Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Add os.Family type to Response. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability_test.go: Add case where no vendor severity exists. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Fallback to NVD if it exists. Also add tests for other cases. Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Fix a few sites with reportType info and tests. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Remove VendorSeverity from displayed results Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add vulnerability source information. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Add VendorSeverity logic for lightDB as well. This commit also makes FillInfo logic common to both light and full DBs. Signed-off-by: Simarpreet Singh <simar@linux.com> * remove some crufty TODOs Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability_test: Add a case for light db for documentation purposes Signed-off-by: Simarpreet Singh <simar@linux.com> * mod: update trivy-db to point to master Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Remove cruft and bring back test cases Signed-off-by: Simarpreet Singh <simar@linux.com> * scan_test: Add pkg Type to mock return Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: reorder err check after err Signed-off-by: Simarpreet Singh <simar@linux.com> * client_test: Fix import ordering Signed-off-by: Simarpreet Singh <simar@linux.com> * convert.go: Use result.Type Signed-off-by: Simarpreet Singh <simar@linux.com> * convert: Use result.Type and simplify ConvertFromRpcResults signature Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Refactor calls to getVendorSeverity Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Remove centos-7-critical.json.golden There's no critical vulnerability in CentOS 7 anymore. In addition this test was not adding any value that is already not covered by existing tests cases. Signed-off-by: Simarpreet Singh <simar@linux.com> * rpc: Include severity source in tests. Signed-off-by: Simarpreet Singh <simar@linux.com> * integration: Update test db to include VendorSeverity. Test DB is now a snapshot of full database from trivy-db. Also update golden files to include SeveritySource. Signed-off-by: Simarpreet Singh <simar@linux.com> * vulnerability: Make centos7 use RHEL vendor severities Signed-off-by: Simarpreet Singh <simar@linux.com>
133 lines
3.3 KiB
Go
133 lines
3.3 KiB
Go
package standalone
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
l "log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/aquasecurity/trivy-db/pkg/db"
|
|
|
|
"github.com/aquasecurity/fanal/cache"
|
|
"github.com/aquasecurity/trivy/internal/operation"
|
|
"github.com/aquasecurity/trivy/internal/standalone/config"
|
|
"github.com/aquasecurity/trivy/pkg/log"
|
|
"github.com/aquasecurity/trivy/pkg/report"
|
|
"github.com/aquasecurity/trivy/pkg/scanner"
|
|
"github.com/aquasecurity/trivy/pkg/types"
|
|
"github.com/aquasecurity/trivy/pkg/utils"
|
|
"github.com/urfave/cli"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
func Run(cliCtx *cli.Context) error {
|
|
c, err := config.New(cliCtx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return run(c)
|
|
}
|
|
|
|
func run(c config.Config) (err error) {
|
|
if err = log.InitLogger(c.Debug, c.Quiet); err != nil {
|
|
l.Fatal(err)
|
|
}
|
|
|
|
// initialize config
|
|
if err = c.Init(); err != nil {
|
|
return xerrors.Errorf("failed to initialize options: %w", err)
|
|
}
|
|
|
|
// configure cache dir
|
|
utils.SetCacheDir(c.CacheDir)
|
|
cacheClient, err := cache.NewFSCache(c.CacheDir)
|
|
if err != nil {
|
|
return xerrors.Errorf("unable to initialize the cache: %w", err)
|
|
}
|
|
|
|
cacheOperation := operation.NewCache(cacheClient)
|
|
log.Logger.Debugf("cache dir: %s", utils.CacheDir())
|
|
|
|
if c.Reset {
|
|
return cacheOperation.Reset()
|
|
}
|
|
if c.ClearCache {
|
|
return cacheOperation.ClearImages()
|
|
}
|
|
|
|
// download the database file
|
|
noProgress := c.Quiet || c.NoProgress
|
|
if err = operation.DownloadDB(c.AppVersion, c.CacheDir, noProgress, c.Light, c.SkipUpdate); err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.DownloadDBOnly {
|
|
return nil
|
|
}
|
|
|
|
if err = db.Init(c.CacheDir); err != nil {
|
|
return xerrors.Errorf("error in vulnerability DB initialize: %w", err)
|
|
}
|
|
|
|
var scanner scanner.Scanner
|
|
ctx := context.Background()
|
|
|
|
cleanup := func() {}
|
|
if c.Input != "" {
|
|
// scan tar file
|
|
scanner, err = initializeArchiveScanner(ctx, c.Input, cacheClient, cacheClient, c.Timeout)
|
|
if err != nil {
|
|
return xerrors.Errorf("unable to initialize the archive scanner: %w", err)
|
|
}
|
|
} else {
|
|
// scan an image in Docker Engine or Docker Registry
|
|
scanner, cleanup, err = initializeDockerScanner(ctx, c.ImageName, cacheClient, cacheClient, c.Timeout)
|
|
if err != nil {
|
|
return xerrors.Errorf("unable to initialize the docker scanner: %w", err)
|
|
}
|
|
}
|
|
defer cleanup()
|
|
|
|
scanOptions := types.ScanOptions{
|
|
VulnType: c.VulnType,
|
|
ScanRemovedPackages: c.ScanRemovedPkgs,
|
|
}
|
|
log.Logger.Debugf("Vulnerability type: %s", scanOptions.VulnType)
|
|
|
|
results, err := scanner.ScanImage(scanOptions)
|
|
if err != nil {
|
|
return xerrors.Errorf("error in image scan: %w", err)
|
|
}
|
|
|
|
vulnClient := initializeVulnerabilityClient()
|
|
for i := range results {
|
|
vulnClient.FillInfo(results[i].Vulnerabilities, results[i].Type)
|
|
results[i].Vulnerabilities = vulnClient.Filter(results[i].Vulnerabilities,
|
|
c.Severities, c.IgnoreUnfixed, c.IgnoreFile)
|
|
}
|
|
|
|
template := c.Template
|
|
|
|
if strings.HasPrefix(c.Template, "@") {
|
|
buf, err := ioutil.ReadFile(strings.TrimPrefix(c.Template, "@"))
|
|
if err != nil {
|
|
return xerrors.Errorf("Error retrieving template from path: %w", err)
|
|
}
|
|
template = string(buf)
|
|
}
|
|
|
|
if err = report.WriteResults(c.Format, c.Output, results, template, c.Light); err != nil {
|
|
return xerrors.Errorf("unable to write results: %w", err)
|
|
}
|
|
|
|
if c.ExitCode != 0 {
|
|
for _, result := range results {
|
|
if len(result.Vulnerabilities) > 0 {
|
|
os.Exit(c.ExitCode)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|