Skip to content

Commit

Permalink
fix(server): add Locations for Packages in client/server mode (#6366)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen authored Mar 24, 2024
1 parent e866bd5 commit a2482c1
Show file tree
Hide file tree
Showing 5 changed files with 655 additions and 487 deletions.
14 changes: 14 additions & 0 deletions integration/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ func TestClientServer(t *testing.T) {
},
golden: "testdata/pom.json.golden",
},
{
name: "scan package-lock.json with repo command in client/server mode",
args: csArgs{
Command: "repo",
RemoteAddrOption: "--server",
Target: "testdata/fixtures/repo/npm/",
ListAllPackages: true,
},
golden: "testdata/npm.json.golden",
},
{
name: "scan sample.pem with repo command in client/server mode",
args: csArgs{
Expand Down Expand Up @@ -588,6 +598,10 @@ func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden st
osArgs = append(osArgs, "--format", "json")
}

if c.ListAllPackages {
osArgs = append(osArgs, "--list-all-pkgs")
}

if c.IgnoreUnfixed {
osArgs = append(osArgs, "--ignore-unfixed")
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/rpc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func ConvertToRPCPkgs(pkgs []ftypes.Package) []*common.Package {
SrcRelease: pkg.SrcRelease,
SrcEpoch: int32(pkg.SrcEpoch),
Licenses: pkg.Licenses,
Locations: ConvertToRPCLocations(pkg.Locations),
Layer: ConvertToRPCLayer(pkg.Layer),
FilePath: pkg.FilePath,
DependsOn: pkg.DependsOn,
Expand All @@ -90,6 +91,17 @@ func ConvertToRPCPkgIdentifier(pkg ftypes.PkgIdentifier) *common.PkgIdentifier {
}
}

func ConvertToRPCLocations(pkgLocs []ftypes.Location) []*common.Location {
var locations []*common.Location
for _, pkgLoc := range pkgLocs {
locations = append(locations, &common.Location{
StartLine: int32(pkgLoc.StartLine),
EndLine: int32(pkgLoc.EndLine),
})
}
return locations
}

func ConvertToRPCCustomResources(resources []ftypes.CustomResource) []*common.CustomResource {
var rpcResources []*common.CustomResource
for _, r := range resources {
Expand Down Expand Up @@ -207,6 +219,7 @@ func ConvertFromRPCPkgs(rpcPkgs []*common.Package) []ftypes.Package {
SrcRelease: pkg.SrcRelease,
SrcEpoch: int(pkg.SrcEpoch),
Licenses: pkg.Licenses,
Locations: ConvertFromRPCLocation(pkg.Locations),
Layer: ConvertFromRPCLayer(pkg.Layer),
FilePath: pkg.FilePath,
DependsOn: pkg.DependsOn,
Expand Down Expand Up @@ -237,6 +250,17 @@ func ConvertFromRPCPkgIdentifier(pkg *common.PkgIdentifier) ftypes.PkgIdentifier
return pkgID
}

func ConvertFromRPCLocation(locs []*common.Location) []ftypes.Location {
var pkgLocs []ftypes.Location
for _, loc := range locs {
pkgLocs = append(pkgLocs, ftypes.Location{
StartLine: int(loc.StartLine),
EndLine: int(loc.EndLine),
})
}
return pkgLocs
}

// ConvertToRPCVulns returns common.Vulnerability
func ConvertToRPCVulns(vulns []types.DetectedVulnerability) []*common.Vulnerability {
var rpcVulns []*common.Vulnerability
Expand Down
40 changes: 40 additions & 0 deletions pkg/rpc/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func TestConvertToRpcPkgs(t *testing.T) {
SrcRelease: "1",
SrcEpoch: 2,
Licenses: []string{"MIT"},
Locations: []ftypes.Location{
{
StartLine: 10,
EndLine: 20,
},
{
StartLine: 22,
EndLine: 32,
},
},
Layer: ftypes.Layer{
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
DiffID: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
Expand All @@ -60,6 +70,16 @@ func TestConvertToRpcPkgs(t *testing.T) {
SrcRelease: "1",
SrcEpoch: 2,
Licenses: []string{"MIT"},
Locations: []*common.Location{
{
StartLine: 10,
EndLine: 20,
},
{
StartLine: 22,
EndLine: 32,
},
},
Layer: &common.Layer{
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
DiffId: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
Expand Down Expand Up @@ -101,6 +121,16 @@ func TestConvertFromRpcPkgs(t *testing.T) {
SrcRelease: "1",
SrcEpoch: 2,
Licenses: []string{"MIT"},
Locations: []*common.Location{
{
StartLine: 10,
EndLine: 20,
},
{
StartLine: 22,
EndLine: 32,
},
},
Layer: &common.Layer{
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
DiffId: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
Expand All @@ -122,6 +152,16 @@ func TestConvertFromRpcPkgs(t *testing.T) {
SrcRelease: "1",
SrcEpoch: 2,
Licenses: []string{"MIT"},
Locations: []ftypes.Location{
{
StartLine: 10,
EndLine: 20,
},
{
StartLine: 22,
EndLine: 32,
},
},
Layer: ftypes.Layer{
Digest: "sha256:6a428f9f83b0a29f1fdd2ccccca19a9bab805a925b8eddf432a5a3d3da04afbc",
DiffID: "sha256:39982b2a789afc156fff00c707d0ff1c6ab4af8f1666a8df4787714059ce24e7",
Expand Down
Loading

0 comments on commit a2482c1

Please sign in to comment.