feat: update-trivy (#1316)
* feat: update-trivy * add v2 parser * implement v2 * refactor * feat: add show version to future-vuls * add test case for v2 * trivy v0.20.0 * support --list-all-pkgs * fix lint err * add test case for jar * add a test case for gemspec in container * remove v1 parser and change Library struct * Changed the field name in the model struct LibraryScanner * add comment * fix comment * fix comment * chore * add struct tag
This commit is contained in:
		@@ -16,12 +16,12 @@ import (
 | 
			
		||||
type LibraryScanners []LibraryScanner
 | 
			
		||||
 | 
			
		||||
// Find : find by name
 | 
			
		||||
func (lss LibraryScanners) Find(path, name string) map[string]types.Library {
 | 
			
		||||
	filtered := map[string]types.Library{}
 | 
			
		||||
func (lss LibraryScanners) Find(path, name string) map[string]Library {
 | 
			
		||||
	filtered := map[string]Library{}
 | 
			
		||||
	for _, ls := range lss {
 | 
			
		||||
		for _, lib := range ls.Libs {
 | 
			
		||||
			if ls.Path == path && lib.Name == name {
 | 
			
		||||
				filtered[ls.Path] = lib
 | 
			
		||||
			if ls.LockfilePath == path && lib.Name == name {
 | 
			
		||||
				filtered[ls.LockfilePath] = lib
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -40,8 +40,20 @@ func (lss LibraryScanners) Total() (total int) {
 | 
			
		||||
// LibraryScanner has libraries information
 | 
			
		||||
type LibraryScanner struct {
 | 
			
		||||
	Type string
 | 
			
		||||
	Path string
 | 
			
		||||
	Libs []types.Library
 | 
			
		||||
	Libs []Library
 | 
			
		||||
 | 
			
		||||
	// The path to the Lockfile is stored.
 | 
			
		||||
	LockfilePath string `json:"path,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Library holds the attribute of a package library
 | 
			
		||||
type Library struct {
 | 
			
		||||
	Name    string
 | 
			
		||||
	Version string
 | 
			
		||||
 | 
			
		||||
	// The Path to the library in the container image. Empty string when Lockfile scan.
 | 
			
		||||
	// This field is used to convert the result JSON of a `trivy image` using trivy-to-vuls.
 | 
			
		||||
	FilePath string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Scan : scan target library
 | 
			
		||||
@@ -92,7 +104,7 @@ func (s LibraryScanner) getVulnDetail(tvuln types.DetectedVulnerability) (vinfo
 | 
			
		||||
			Key:     s.GetLibraryKey(),
 | 
			
		||||
			Name:    tvuln.PkgName,
 | 
			
		||||
			FixedIn: tvuln.FixedVersion,
 | 
			
		||||
			Path:    s.Path,
 | 
			
		||||
			Path:    s.LockfilePath,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	return vinfo, nil
 | 
			
		||||
@@ -133,7 +145,7 @@ var LibraryMap = map[string]string{
 | 
			
		||||
 | 
			
		||||
// GetLibraryKey returns target library key
 | 
			
		||||
func (s LibraryScanner) GetLibraryKey() string {
 | 
			
		||||
	fileName := filepath.Base(s.Path)
 | 
			
		||||
	fileName := filepath.Base(s.LockfilePath)
 | 
			
		||||
	switch s.Type {
 | 
			
		||||
	case "jar", "war", "ear":
 | 
			
		||||
		return "java"
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,6 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/aquasecurity/trivy/pkg/types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
@@ -16,14 +14,14 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
		name string
 | 
			
		||||
		lss  LibraryScanners
 | 
			
		||||
		args args
 | 
			
		||||
		want map[string]types.Library
 | 
			
		||||
		want map[string]Library
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name: "single file",
 | 
			
		||||
			lss: LibraryScanners{
 | 
			
		||||
				{
 | 
			
		||||
					Path: "/pathA",
 | 
			
		||||
					Libs: []types.Library{
 | 
			
		||||
					LockfilePath: "/pathA",
 | 
			
		||||
					Libs: []Library{
 | 
			
		||||
						{
 | 
			
		||||
							Name:    "libA",
 | 
			
		||||
							Version: "1.0.0",
 | 
			
		||||
@@ -32,7 +30,7 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			args: args{"/pathA", "libA"},
 | 
			
		||||
			want: map[string]types.Library{
 | 
			
		||||
			want: map[string]Library{
 | 
			
		||||
				"/pathA": {
 | 
			
		||||
					Name:    "libA",
 | 
			
		||||
					Version: "1.0.0",
 | 
			
		||||
@@ -43,8 +41,8 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
			name: "multi file",
 | 
			
		||||
			lss: LibraryScanners{
 | 
			
		||||
				{
 | 
			
		||||
					Path: "/pathA",
 | 
			
		||||
					Libs: []types.Library{
 | 
			
		||||
					LockfilePath: "/pathA",
 | 
			
		||||
					Libs: []Library{
 | 
			
		||||
						{
 | 
			
		||||
							Name:    "libA",
 | 
			
		||||
							Version: "1.0.0",
 | 
			
		||||
@@ -52,8 +50,8 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
					},
 | 
			
		||||
				},
 | 
			
		||||
				{
 | 
			
		||||
					Path: "/pathB",
 | 
			
		||||
					Libs: []types.Library{
 | 
			
		||||
					LockfilePath: "/pathB",
 | 
			
		||||
					Libs: []Library{
 | 
			
		||||
						{
 | 
			
		||||
							Name:    "libA",
 | 
			
		||||
							Version: "1.0.5",
 | 
			
		||||
@@ -62,7 +60,7 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			args: args{"/pathA", "libA"},
 | 
			
		||||
			want: map[string]types.Library{
 | 
			
		||||
			want: map[string]Library{
 | 
			
		||||
				"/pathA": {
 | 
			
		||||
					Name:    "libA",
 | 
			
		||||
					Version: "1.0.0",
 | 
			
		||||
@@ -73,8 +71,8 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
			name: "miss",
 | 
			
		||||
			lss: LibraryScanners{
 | 
			
		||||
				{
 | 
			
		||||
					Path: "/pathA",
 | 
			
		||||
					Libs: []types.Library{
 | 
			
		||||
					LockfilePath: "/pathA",
 | 
			
		||||
					Libs: []Library{
 | 
			
		||||
						{
 | 
			
		||||
							Name:    "libA",
 | 
			
		||||
							Version: "1.0.0",
 | 
			
		||||
@@ -83,7 +81,7 @@ func TestLibraryScanners_Find(t *testing.T) {
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			args: args{"/pathA", "libB"},
 | 
			
		||||
			want: map[string]types.Library{},
 | 
			
		||||
			want: map[string]Library{},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, tt := range tests {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user