mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 07:40:48 -08:00
refactor: simplify Detect function signature (#9280)
This commit is contained in:
@@ -2,7 +2,6 @@ package ospkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"golang.org/x/xerrors"
|
||||
@@ -71,22 +70,22 @@ func RegisterDriver(name ftypes.OSType, drv driver.Driver) {
|
||||
}
|
||||
|
||||
// Detect detects the vulnerabilities
|
||||
func Detect(ctx context.Context, _, osFamily ftypes.OSType, osName string, repo *ftypes.Repository, _ time.Time, pkgs []ftypes.Package) ([]types.DetectedVulnerability, bool, error) {
|
||||
ctx = log.WithContextPrefix(ctx, string(osFamily))
|
||||
func Detect(ctx context.Context, target types.ScanTarget, _ types.ScanOptions) ([]types.DetectedVulnerability, bool, error) {
|
||||
ctx = log.WithContextPrefix(ctx, string(target.OS.Family))
|
||||
|
||||
d, err := newDriver(osFamily, pkgs)
|
||||
d, err := newDriver(target.OS.Family, target.Packages)
|
||||
if err != nil {
|
||||
return nil, false, ErrUnsupportedOS
|
||||
}
|
||||
|
||||
eosl := !d.IsSupportedVersion(ctx, osFamily, osName)
|
||||
eosl := !d.IsSupportedVersion(ctx, target.OS.Family, target.OS.Name)
|
||||
|
||||
// Package `gpg-pubkey` doesn't use the correct version.
|
||||
// We don't need to find vulnerabilities for this package.
|
||||
filteredPkgs := lo.Filter(pkgs, func(pkg ftypes.Package, _ int) bool {
|
||||
filteredPkgs := lo.Filter(target.Packages, func(pkg ftypes.Package, _ int) bool {
|
||||
return pkg.Name != "gpg-pubkey"
|
||||
})
|
||||
vulns, err := d.Detect(ctx, osName, repo, filteredPkgs)
|
||||
vulns, err := d.Detect(ctx, target.OS.Name, target.Repository, filteredPkgs)
|
||||
if err != nil {
|
||||
return nil, false, xerrors.Errorf("failed detection: %w", err)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -50,8 +49,7 @@ func (s *scanner) Scan(ctx context.Context, target types.ScanTarget, opts types.
|
||||
return result, false, nil
|
||||
}
|
||||
|
||||
vulns, eosl, err := ospkgDetector.Detect(ctx, "", target.OS.Family, target.OS.Name, target.Repository, time.Time{},
|
||||
target.Packages)
|
||||
vulns, eosl, err := ospkgDetector.Detect(ctx, target, opts)
|
||||
if err != nil {
|
||||
// Return a result for those who want to override the error handling.
|
||||
return result, false, xerrors.Errorf("failed vulnerability detection of OS packages: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user