Skip to content

Commit a2482c1

Browse files
authored
fix(server): add Locations for Packages in client/server mode (aquasecurity#6366)
1 parent e866bd5 commit a2482c1

File tree

5 files changed

+655
-487
lines changed

5 files changed

+655
-487
lines changed

integration/client_server_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ func TestClientServer(t *testing.T) {
242242
},
243243
golden: "testdata/pom.json.golden",
244244
},
245+
{
246+
name: "scan package-lock.json with repo command in client/server mode",
247+
args: csArgs{
248+
Command: "repo",
249+
RemoteAddrOption: "--server",
250+
Target: "testdata/fixtures/repo/npm/",
251+
ListAllPackages: true,
252+
},
253+
golden: "testdata/npm.json.golden",
254+
},
245255
{
246256
name: "scan sample.pem with repo command in client/server mode",
247257
args: csArgs{
@@ -588,6 +598,10 @@ func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden st
588598
osArgs = append(osArgs, "--format", "json")
589599
}
590600

601+
if c.ListAllPackages {
602+
osArgs = append(osArgs, "--list-all-pkgs")
603+
}
604+
591605
if c.IgnoreUnfixed {
592606
osArgs = append(osArgs, "--ignore-unfixed")
593607
}

pkg/rpc/convert.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func ConvertToRPCPkgs(pkgs []ftypes.Package) []*common.Package {
6565
SrcRelease: pkg.SrcRelease,
6666
SrcEpoch: int32(pkg.SrcEpoch),
6767
Licenses: pkg.Licenses,
68+
Locations: ConvertToRPCLocations(pkg.Locations),
6869
Layer: ConvertToRPCLayer(pkg.Layer),
6970
FilePath: pkg.FilePath,
7071
DependsOn: pkg.DependsOn,
@@ -90,6 +91,17 @@ func ConvertToRPCPkgIdentifier(pkg ftypes.PkgIdentifier) *common.PkgIdentifier {
9091
}
9192
}
9293

94+
func ConvertToRPCLocations(pkgLocs []ftypes.Location) []*common.Location {
95+
var locations []*common.Location
96+
for _, pkgLoc := range pkgLocs {
97+
locations = append(locations, &common.Location{
98+
StartLine: int32(pkgLoc.StartLine),
99+
EndLine: int32(pkgLoc.EndLine),
100+
})
101+
}
102+
return locations
103+
}
104+
93105
func ConvertToRPCCustomResources(resources []ftypes.CustomResource) []*common.CustomResource {
94106
var rpcResources []*common.CustomResource
95107
for _, r := range resources {
@@ -207,6 +219,7 @@ func ConvertFromRPCPkgs(rpcPkgs []*common.Package) []ftypes.Package {
207219
SrcRelease: pkg.SrcRelease,
208220
SrcEpoch: int(pkg.SrcEpoch),
209221
Licenses: pkg.Licenses,
222+
Locations: ConvertFromRPCLocation(pkg.Locations),
210223
Layer: ConvertFromRPCLayer(pkg.Layer),
211224
FilePath: pkg.FilePath,
212225
DependsOn: pkg.DependsOn,
@@ -237,6 +250,17 @@ func ConvertFromRPCPkgIdentifier(pkg *common.PkgIdentifier) ftypes.PkgIdentifier
237250
return pkgID
238251
}
239252

253+
func ConvertFromRPCLocation(locs []*common.Location) []ftypes.Location {
254+
var pkgLocs []ftypes.Location
255+
for _, loc := range locs {
256+
pkgLocs = append(pkgLocs, ftypes.Location{
257+
StartLine: int(loc.StartLine),
258+
EndLine: int(loc.EndLine),
259+
})
260+
}
261+
return pkgLocs
262+
}
263+
240264
// ConvertToRPCVulns returns common.Vulnerability
241265
func ConvertToRPCVulns(vulns []types.DetectedVulnerability) []*common.Vulnerability {
242266
var rpcVulns []*common.Vulnerability

pkg/rpc/convert_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ func TestConvertToRpcPkgs(t *testing.T) {
3939
SrcRelease: "1",
4040
SrcEpoch: 2,
4141
Licenses: []string{"MIT"},
42+
Locations: []ftypes.Location{
43+
{
44+
StartLine: 10,
45+
EndLine: 20,
46+
},
47+
{
48+
StartLine: 22,
49+
EndLine: 32,
50+
},
51+
},
4252
Layer: ftypes.Layer{
4353
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
4454
DiffID: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
@@ -60,6 +70,16 @@ func TestConvertToRpcPkgs(t *testing.T) {
6070
SrcRelease: "1",
6171
SrcEpoch: 2,
6272
Licenses: []string{"MIT"},
73+
Locations: []*common.Location{
74+
{
75+
StartLine: 10,
76+
EndLine: 20,
77+
},
78+
{
79+
StartLine: 22,
80+
EndLine: 32,
81+
},
82+
},
6383
Layer: &common.Layer{
6484
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
6585
DiffId: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
@@ -101,6 +121,16 @@ func TestConvertFromRpcPkgs(t *testing.T) {
101121
SrcRelease: "1",
102122
SrcEpoch: 2,
103123
Licenses: []string{"MIT"},
124+
Locations: []*common.Location{
125+
{
126+
StartLine: 10,
127+
EndLine: 20,
128+
},
129+
{
130+
StartLine: 22,
131+
EndLine: 32,
132+
},
133+
},
104134
Layer: &common.Layer{
105135
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
106136
DiffId: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
@@ -122,6 +152,16 @@ func TestConvertFromRpcPkgs(t *testing.T) {
122152
SrcRelease: "1",
123153
SrcEpoch: 2,
124154
Licenses: []string{"MIT"},
155+
Locations: []ftypes.Location{
156+
{
157+
StartLine: 10,
158+
EndLine: 20,
159+
},
160+
{
161+
StartLine: 22,
162+
EndLine: 32,
163+
},
164+
},
125165
Layer: ftypes.Layer{
126166
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
127167
DiffID: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",

0 commit comments

Comments
 (0)