Skip to content

Commit 36e24b1

Browse files
authored
fix(rpc): add PkgPath field to client / server mode (#1643)
1 parent 8831174 commit 36e24b1

File tree

5 files changed

+249
-409
lines changed

5 files changed

+249
-409
lines changed

pkg/rpc/convert.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"google.golang.org/protobuf/types/known/structpb"
99

1010
ftypes "github.com/aquasecurity/fanal/types"
11-
deptypes "github.com/aquasecurity/go-dep-parser/pkg/types"
1211
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
1312
"github.com/aquasecurity/trivy/pkg/log"
1413
"github.com/aquasecurity/trivy/pkg/types"
@@ -33,6 +32,7 @@ func ConvertToRPCPkgs(pkgs []ftypes.Package) []*common.Package {
3332
SrcEpoch: int32(pkg.SrcEpoch),
3433
License: pkg.License,
3534
Layer: ConvertToRPCLayer(pkg.Layer),
35+
FilePath: pkg.FilePath,
3636
})
3737
}
3838
return rpcPkgs
@@ -54,37 +54,12 @@ func ConvertFromRPCPkgs(rpcPkgs []*common.Package) []ftypes.Package {
5454
SrcEpoch: int(pkg.SrcEpoch),
5555
License: pkg.License,
5656
Layer: ConvertFromRPCLayer(pkg.Layer),
57+
FilePath: pkg.FilePath,
5758
})
5859
}
5960
return pkgs
6061
}
6162

62-
// ConvertFromRPCLibraries returns list of Fanal library
63-
func ConvertFromRPCLibraries(rpcLibs []*common.Library) []ftypes.Package {
64-
var pkgs []ftypes.Package
65-
for _, l := range rpcLibs {
66-
pkgs = append(pkgs, ftypes.Package{
67-
Name: l.Name,
68-
Version: l.Version,
69-
License: l.License,
70-
})
71-
}
72-
return pkgs
73-
}
74-
75-
// ConvertToRPCLibraries returns list of libraries
76-
func ConvertToRPCLibraries(libs []deptypes.Library) []*common.Library {
77-
var rpcLibs []*common.Library
78-
for _, l := range libs {
79-
rpcLibs = append(rpcLibs, &common.Library{
80-
Name: l.Name,
81-
Version: l.Version,
82-
License: l.License,
83-
})
84-
}
85-
return rpcLibs
86-
}
87-
8863
// ConvertToRPCVulns returns common.Vulnerability
8964
func ConvertToRPCVulns(vulns []types.DetectedVulnerability) []*common.Vulnerability {
9065
var rpcVulns []*common.Vulnerability
@@ -128,6 +103,7 @@ func ConvertToRPCVulns(vulns []types.DetectedVulnerability) []*common.Vulnerabil
128103
VulnerabilityId: vuln.VulnerabilityID,
129104
VendorIds: vuln.VendorIDs,
130105
PkgName: vuln.PkgName,
106+
PkgPath: vuln.PkgPath,
131107
InstalledVersion: vuln.InstalledVersion,
132108
FixedVersion: vuln.FixedVersion,
133109
Title: vuln.Title,
@@ -264,6 +240,7 @@ func ConvertFromRPCVulns(rpcVulns []*common.Vulnerability) []types.DetectedVulne
264240
VulnerabilityID: vuln.VulnerabilityId,
265241
VendorIDs: vuln.VendorIds,
266242
PkgName: vuln.PkgName,
243+
PkgPath: vuln.PkgPath,
267244
InstalledVersion: vuln.InstalledVersion,
268245
FixedVersion: vuln.FixedVersion,
269246
Vulnerability: dbTypes.Vulnerability{
@@ -364,7 +341,7 @@ func ConvertFromRPCApplications(rpcApps []*common.Application) []ftypes.Applicat
364341
apps = append(apps, ftypes.Application{
365342
Type: rpcApp.Type,
366343
FilePath: rpcApp.FilePath,
367-
Libraries: ConvertFromRPCLibraries(rpcApp.Libraries),
344+
Libraries: ConvertFromRPCPkgs(rpcApp.Libraries),
368345
})
369346
}
370347
return apps
@@ -478,18 +455,10 @@ func ConvertToRPCBlobInfo(diffID string, blobInfo ftypes.BlobInfo) *cache.PutBlo
478455

479456
var applications []*common.Application
480457
for _, app := range blobInfo.Applications {
481-
var libs []*common.Library
482-
for _, lib := range app.Libraries {
483-
libs = append(libs, &common.Library{
484-
Name: lib.Name,
485-
Version: lib.Version,
486-
License: lib.License,
487-
})
488-
}
489458
applications = append(applications, &common.Application{
490459
Type: app.Type,
491460
FilePath: app.FilePath,
492-
Libraries: libs,
461+
Libraries: ConvertToRPCPkgs(app.Libraries),
493462
})
494463
}
495464

pkg/rpc/convert_test.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
fos "github.com/aquasecurity/fanal/analyzer/os"
1111
ftypes "github.com/aquasecurity/fanal/types"
12-
ptypes "github.com/aquasecurity/go-dep-parser/pkg/types"
1312
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
1413
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
1514
"github.com/aquasecurity/trivy/pkg/types"
@@ -134,68 +133,6 @@ func TestConvertFromRpcPkgs(t *testing.T) {
134133
}
135134
}
136135

137-
func TestConvertFromRpcLibraries(t *testing.T) {
138-
type args struct {
139-
rpcLibs []*common.Library
140-
}
141-
tests := []struct {
142-
name string
143-
args args
144-
want []ftypes.Package
145-
}{
146-
{
147-
name: "happy path",
148-
args: args{
149-
rpcLibs: []*common.Library{
150-
{Name: "foo", Version: "1.2.3"},
151-
{Name: "bar", Version: "4.5.6"},
152-
},
153-
},
154-
want: []ftypes.Package{
155-
{Name: "foo", Version: "1.2.3"},
156-
{Name: "bar", Version: "4.5.6"},
157-
},
158-
},
159-
}
160-
for _, tt := range tests {
161-
t.Run(tt.name, func(t *testing.T) {
162-
got := ConvertFromRPCLibraries(tt.args.rpcLibs)
163-
assert.Equal(t, got, tt.want, tt.name)
164-
})
165-
}
166-
}
167-
168-
func TestConvertToRpcLibraries(t *testing.T) {
169-
type args struct {
170-
libs []ptypes.Library
171-
}
172-
tests := []struct {
173-
name string
174-
args args
175-
want []*common.Library
176-
}{
177-
{
178-
name: "happy path",
179-
args: args{
180-
libs: []ptypes.Library{
181-
{Name: "foo", Version: "1.2.3"},
182-
{Name: "bar", Version: "4.5.6"},
183-
},
184-
},
185-
want: []*common.Library{
186-
{Name: "foo", Version: "1.2.3"},
187-
{Name: "bar", Version: "4.5.6"},
188-
},
189-
},
190-
}
191-
for _, tt := range tests {
192-
t.Run(tt.name, func(t *testing.T) {
193-
got := ConvertToRPCLibraries(tt.args.libs)
194-
assert.Equal(t, got, tt.want, tt.name)
195-
})
196-
}
197-
}
198-
199136
func TestConvertToRpcVulns(t *testing.T) {
200137
fixedPublishedDate := time.Unix(1257894000, 0)
201138
fixedLastModifiedDate := time.Unix(1257894010, 0)

pkg/rpc/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func TestCacheServer_PutBlob(t *testing.T) {
359359
{
360360
Type: "composer",
361361
FilePath: "php-app/composer.lock",
362-
Libraries: []*common.Library{
362+
Libraries: []*common.Package{
363363
{
364364
Name: "guzzlehttp/guzzle",
365365
Version: "6.2.0",

0 commit comments

Comments
 (0)