Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unify Library and Package structs #6633

Merged
merged 35 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
9bb9014
refactor: unifty Library and Package
knqyf263 May 6, 2024
9abf7ac
refactor: replace Library with Package
knqyf263 May 6, 2024
8704363
test(integration): fix golden
knqyf263 May 6, 2024
a19dbe8
refactor: replace Libraries with Packages in Application
knqyf263 May 6, 2024
93c9c5a
refactor(rpc): replace Libraries with Packages
knqyf263 May 6, 2024
725e4fd
test: sort packages
knqyf263 May 6, 2024
ff481eb
refactor: replace lib with pkg
knqyf263 May 7, 2024
398aeaf
refactor: replace lib with pkg
knqyf263 May 7, 2024
48cbd27
refactor: replace lib with pkg
knqyf263 May 7, 2024
3718e95
refactor: replace lib with pkg
knqyf263 May 7, 2024
020e3b5
refactor(gradle): use sort.Sort
knqyf263 May 7, 2024
4e33bc7
refactor: replace lib with pkg
knqyf263 May 7, 2024
2468e08
refactor: replace lib with pkg
knqyf263 May 7, 2024
84cf0be
refactor: replace lib with pkg
knqyf263 May 7, 2024
05aa799
refactor: replace lib with pkg
knqyf263 May 7, 2024
793c85d
refactor: replace lib with pkg
knqyf263 May 7, 2024
964e98f
refactor: replace lib with pkg
knqyf263 May 7, 2024
8598e4f
refactor: replace lib with pkg
knqyf263 May 7, 2024
129fff0
refactor: replace lib with pkg
knqyf263 May 7, 2024
9dcccd5
refactor: replace lib with pkg
knqyf263 May 7, 2024
52cb6d1
refactor: replace lib with pkg
knqyf263 May 7, 2024
bff98d2
refactor: replace lib with pkg
knqyf263 May 7, 2024
d416271
refactor: replace lib with pkg
knqyf263 May 7, 2024
4c3ddab
refactor: replace lib with pkg
knqyf263 May 7, 2024
ce70874
refactor: replace lib with pkg
knqyf263 May 7, 2024
82f95d3
refactor: replace lib with pkg
knqyf263 May 7, 2024
b02d21f
refactor: replace lib with pkg
knqyf263 May 7, 2024
e854249
refactor: replace lib with pkg
knqyf263 May 7, 2024
07da4e2
refactor(hex): use sort.Sort
knqyf263 May 7, 2024
5f43652
refactor(pnpm): use sort.Sort
knqyf263 May 7, 2024
d284d01
refactor(npm): replace lib with pkg
knqyf263 May 7, 2024
fee8c55
refactor(yarn): use sort.Sort
knqyf263 May 7, 2024
f83ca44
refactor: renaming
knqyf263 May 7, 2024
2c73669
refactor: rename lib to pkg
DmitriyLewen May 7, 2024
b2d35b1
refactor: use sort.Sort instead of sort.Slice
DmitriyLewen May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions integration/testdata/conan.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@
"Class": "lang-pkgs",
"Type": "conan",
"Packages": [
{
"ID": "poco/1.9.4",
"Name": "poco",
"Identifier": {
"PURL": "pkg:conan/poco@1.9.4",
"UID": "312753cebe80c0eb"
},
"Version": "1.9.4",
"Relationship": "direct",
"DependsOn": [
"pcre/8.43",
"zlib/1.2.12",
"expat/2.4.8",
"sqlite3/3.39.2",
"openssl/1.1.1q"
],
"Layer": {},
"Locations": [
{
"StartLine": 12,
"EndLine": 25
}
]
},
{
"ID": "bzip2/1.0.8",
"Name": "bzip2",
Expand Down Expand Up @@ -97,30 +121,6 @@
}
]
},
{
"ID": "poco/1.9.4",
"Name": "poco",
"Identifier": {
"PURL": "pkg:conan/poco@1.9.4",
"UID": "312753cebe80c0eb"
},
"Version": "1.9.4",
"Relationship": "direct",
"DependsOn": [
"pcre/8.43",
"zlib/1.2.12",
"expat/2.4.8",
"sqlite3/3.39.2",
"openssl/1.1.1q"
],
"Layer": {},
"Locations": [
{
"StartLine": 12,
"EndLine": 25
}
]
},
{
"ID": "sqlite3/3.39.2",
"Name": "sqlite3",
Expand Down
22 changes: 11 additions & 11 deletions integration/testdata/poetry.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
],
"Layer": {}
},
{
"ID": "werkzeug@0.14",
"Name": "werkzeug",
"Identifier": {
"PURL": "pkg:pypi/werkzeug@0.14",
"UID": "4176be111ad01070"
},
"Version": "0.14",
"Relationship": "direct",
"Layer": {}
},
{
"ID": "colorama@0.4.6",
"Name": "colorama",
Expand All @@ -46,17 +57,6 @@
"Indirect": true,
"Relationship": "indirect",
"Layer": {}
},
{
"ID": "werkzeug@0.14",
"Name": "werkzeug",
"Identifier": {
"PURL": "pkg:pypi/werkzeug@0.14",
"UID": "4176be111ad01070"
},
"Version": "0.14",
"Relationship": "direct",
"Layer": {}
}
],
"Vulnerabilities": [
Expand Down
43 changes: 21 additions & 22 deletions pkg/dependency/parser/c/conan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency"
"github.com/aquasecurity/trivy/pkg/dependency/types"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
Expand Down Expand Up @@ -44,37 +43,37 @@ type Parser struct {
logger *log.Logger
}

func NewParser() types.Parser {
func NewParser() *Parser {
return &Parser{
logger: log.WithPrefix("conan"),
}
}

func (p *Parser) parseV1(lock LockFile) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
var deps []types.Dependency
func (p *Parser) parseV1(lock LockFile) ([]ftypes.Package, []ftypes.Dependency, error) {
var pkgs []ftypes.Package
var deps []ftypes.Dependency
var directDeps []string
if root, ok := lock.GraphLock.Nodes["0"]; ok {
directDeps = root.Requires
}

// Parse packages
parsed := make(map[string]types.Library)
parsed := make(map[string]ftypes.Package)
for i, node := range lock.GraphLock.Nodes {
if node.Ref == "" {
continue
}
lib, err := toLibrary(node.Ref, node.StartLine, node.EndLine)
pkg, err := toPackage(node.Ref, node.StartLine, node.EndLine)
if err != nil {
p.logger.Debug("Parse ref error", log.Err(err))
continue
}

// Determine if the package is a direct dependency or not
direct := slices.Contains(directDeps, i)
lib.Relationship = lo.Ternary(direct, types.RelationshipDirect, types.RelationshipIndirect)
pkg.Relationship = lo.Ternary(direct, ftypes.RelationshipDirect, ftypes.RelationshipIndirect)

parsed[i] = lib
parsed[i] = pkg
}

// Parse dependency graph
Expand All @@ -91,33 +90,33 @@ func (p *Parser) parseV1(lock LockFile) ([]types.Library, []types.Dependency, er
}
}
if len(childDeps) != 0 {
deps = append(deps, types.Dependency{
deps = append(deps, ftypes.Dependency{
ID: lib.ID,
DependsOn: childDeps,
})
}

libs = append(libs, lib)
pkgs = append(pkgs, lib)
}
return libs, deps, nil
return pkgs, deps, nil
}

func (p *Parser) parseV2(lock LockFile) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
func (p *Parser) parseV2(lock LockFile) ([]ftypes.Package, []ftypes.Dependency, error) {
var pkgs []ftypes.Package

for _, req := range lock.Requires {
lib, err := toLibrary(req.Dependency, req.StartLine, req.EndLine)
pkg, err := toPackage(req.Dependency, req.StartLine, req.EndLine)
if err != nil {
p.logger.Debug("Creating library entry from requirement failed", err)
continue
}

libs = append(libs, lib)
pkgs = append(pkgs, pkg)
}
return libs, []types.Dependency{}, nil
return pkgs, []ftypes.Dependency{}, nil
}

func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
var lock LockFile

input, err := io.ReadAll(r)
Expand Down Expand Up @@ -153,16 +152,16 @@ func parsePackage(text string) (string, string, error) {
return ss[0], ss[1], nil
}

func toLibrary(pkg string, startLine, endLine int) (types.Library, error) {
func toPackage(pkg string, startLine, endLine int) (ftypes.Package, error) {
name, version, err := parsePackage(pkg)
if err != nil {
return types.Library{}, err
return ftypes.Package{}, err
}
return types.Library{
return ftypes.Package{
ID: dependency.ID(ftypes.Conan, name, version),
Name: name,
Version: version,
Locations: []types.Location{
Locations: []ftypes.Location{
{
StartLine: startLine,
EndLine: endLine,
Expand Down
42 changes: 21 additions & 21 deletions pkg/dependency/parser/c/conan/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import (
"github.com/stretchr/testify/require"

"github.com/aquasecurity/trivy/pkg/dependency/parser/c/conan"
"github.com/aquasecurity/trivy/pkg/dependency/types"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
inputFile string // Test input file
wantLibs []types.Library
wantDeps []types.Dependency
wantLibs []ftypes.Package
wantDeps []ftypes.Dependency
}{
{
name: "happy path",
inputFile: "testdata/happy_v1_case1.lock",
wantLibs: []types.Library{
wantLibs: []ftypes.Package{
{
ID: "pkga/0.0.1",
Name: "pkga",
Version: "0.0.1",
Relationship: types.RelationshipDirect,
Locations: []types.Location{
Relationship: ftypes.RelationshipDirect,
Locations: []ftypes.Location{
{
StartLine: 13,
EndLine: 22,
Expand All @@ -40,8 +40,8 @@ func TestParse(t *testing.T) {
ID: "pkgb/system",
Name: "pkgb",
Version: "system",
Relationship: types.RelationshipIndirect,
Locations: []types.Location{
Relationship: ftypes.RelationshipIndirect,
Locations: []ftypes.Location{
{
StartLine: 23,
EndLine: 29,
Expand All @@ -52,16 +52,16 @@ func TestParse(t *testing.T) {
ID: "pkgc/0.1.1",
Name: "pkgc",
Version: "0.1.1",
Relationship: types.RelationshipDirect,
Locations: []types.Location{
Relationship: ftypes.RelationshipDirect,
Locations: []ftypes.Location{
{
StartLine: 30,
EndLine: 35,
},
},
},
},
wantDeps: []types.Dependency{
wantDeps: []ftypes.Dependency{
{
ID: "pkga/0.0.1",
DependsOn: []string{
Expand All @@ -73,13 +73,13 @@ func TestParse(t *testing.T) {
{
name: "happy path. lock file with revisions support",
inputFile: "testdata/happy_v1_case2.lock",
wantLibs: []types.Library{
wantLibs: []ftypes.Package{
{
ID: "openssl/3.0.3",
Name: "openssl",
Version: "3.0.3",
Relationship: types.RelationshipDirect,
Locations: []types.Location{
Relationship: ftypes.RelationshipDirect,
Locations: []ftypes.Location{
{
StartLine: 12,
EndLine: 22,
Expand All @@ -90,16 +90,16 @@ func TestParse(t *testing.T) {
ID: "zlib/1.2.12",
Name: "zlib",
Version: "1.2.12",
Relationship: types.RelationshipIndirect,
Locations: []types.Location{
Relationship: ftypes.RelationshipIndirect,
Locations: []ftypes.Location{
{
StartLine: 23,
EndLine: 30,
},
},
},
},
wantDeps: []types.Dependency{
wantDeps: []ftypes.Dependency{
{
ID: "openssl/3.0.3",
DependsOn: []string{
Expand All @@ -111,12 +111,12 @@ func TestParse(t *testing.T) {
{
name: "happy path conan v2",
inputFile: "testdata/happy_v2.lock",
wantLibs: []types.Library{
wantLibs: []ftypes.Package{
{
ID: "matrix/1.3",
Name: "matrix",
Version: "1.3",
Locations: []types.Location{
Locations: []ftypes.Location{
{
StartLine: 5,
EndLine: 5,
Expand All @@ -127,15 +127,15 @@ func TestParse(t *testing.T) {
ID: "sound32/1.0",
Name: "sound32",
Version: "1.0",
Locations: []types.Location{
Locations: []ftypes.Location{
{
StartLine: 4,
EndLine: 4,
},
},
},
},
wantDeps: []types.Dependency{},
wantDeps: []ftypes.Dependency{},
},
{
name: "happy path. lock file without dependencies",
Expand Down
Loading