Skip to content

Commit

Permalink
Implement golang Purl subpath (#2547)
Browse files Browse the repository at this point in the history
* Added test for golang package that include subpath into the module

Signed-off-by: Laurent Goderre <[email protected]>

* Implement golang purl subpath

Signed-off-by: Laurent Goderre <[email protected]>

---------

Signed-off-by: Laurent Goderre <[email protected]>
  • Loading branch information
LaurentGoderre authored Jan 25, 2024
1 parent 414fb2f commit d7c51e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
28 changes: 16 additions & 12 deletions syft/pkg/cataloger/golang/package.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package golang

import (
"regexp"
"runtime/debug"
"strings"

Expand Down Expand Up @@ -48,22 +47,27 @@ func packageURL(moduleName, moduleVersion string) string {
// source: https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#golang
// note: "The version is often empty when a commit is not specified and should be the commit in most cases when available."

re := regexp.MustCompile(`(/)[^/]*$`)
fields := re.Split(moduleName, -1)
fields := strings.Split(moduleName, "/")
if len(fields) == 0 {
return ""
}
namespace := fields[0]
name := strings.TrimPrefix(strings.TrimPrefix(moduleName, namespace), "/")

if name == "" {
// this is a "short" url (with no namespace)
name = namespace
namespace = ""
}

namespace := ""
name := ""
// The subpath is used to point to a subpath inside a package (e.g. pkg:golang/google.golang.org/genproto#googleapis/api/annotations)
subpath := "" // TODO: not implemented
subpath := ""

switch len(fields) {
case 1:
name = fields[0]
case 2:
name = fields[1]
namespace = fields[0]
default:
name = fields[2]
namespace = strings.Join(fields[0:2], "/")
subpath = strings.Join(fields[3:], "/")
}

return packageurl.NewPackageURL(
packageurl.TypeGolang,
Expand Down
15 changes: 15 additions & 0 deletions syft/pkg/cataloger/golang/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ func Test_packageURL(t *testing.T) {
},
expected: "pkg:golang/[email protected]",
},
{
name: "golang with subpath",
pkg: pkg.Package{
Name: "github.com/coreos/go-systemd/v22",
Version: "v22.1.0",
},
expected: "pkg:golang/github.com/coreos/[email protected]#v22",
},
{
name: "golang with subpath deep",
pkg: pkg.Package{
Name: "google.golang.org/genproto/googleapis/api/annotations",
},
expected: "pkg:golang/google.golang.org/genproto/googleapis#api/annotations",
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/golang/parse_go_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestBuildGoPkgInfo(t *testing.T) {
{
Name: "github.com/a/b/c",
Version: "(devel)",
PURL: "pkg:golang/github.com/a/b/c@(devel)",
PURL: "pkg:golang/github.com/a/b@(devel)#c",
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: file.NewLocationSet(
Expand Down

0 comments on commit d7c51e5

Please sign in to comment.