Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract language and package type from pURLs on SBOM decode (#777)
* add language detection from pURLs Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add package type detection from pURLs Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add cargo and npm pURL support Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix npm tests and linting Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
- Loading branch information
Showing
10 changed files
with
287 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/scylladb/go-set/strset" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestLanguageFromPURL(t *testing.T) { | ||
|
||
tests := []struct { | ||
purl string | ||
want Language | ||
}{ | ||
|
||
{ | ||
purl: "pkg:npm/util@2.32", | ||
want: JavaScript, | ||
}, | ||
{ | ||
purl: "pkg:pypi/util-linux@2.32.1-27.el8", | ||
want: Python, | ||
}, | ||
{ | ||
purl: "pkg:gem/ruby-advisory-db-check@0.12.4", | ||
want: Ruby, | ||
}, | ||
{ | ||
purl: "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c", | ||
want: Go, | ||
}, | ||
{ | ||
purl: "pkg:cargo/clap@2.33.0", | ||
want: Rust, | ||
}, | ||
{ | ||
purl: "pkg:composer/laravel/laravel@5.5.0", | ||
want: PHP, | ||
}, | ||
{ | ||
purl: "pkg:maven/org.apache.xmlgraphics/batik-anim@1.9.1?type=zip&classifier=dist", | ||
want: Java, | ||
}, | ||
} | ||
|
||
var languages []string | ||
var expectedLanguages = strset.New() | ||
for _, ty := range AllLanguages { | ||
expectedLanguages.Add(string(ty)) | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.purl, func(t *testing.T) { | ||
actual := LanguageFromPURL(tt.purl) | ||
|
||
if actual != "" { | ||
languages = append(languages, string(actual)) | ||
} | ||
|
||
assert.Equalf(t, tt.want, actual, "LanguageFromPURL(%v)", tt.purl) | ||
}) | ||
} | ||
|
||
assert.ElementsMatch(t, expectedLanguages.List(), languages, "missing one or more languages to test against (maybe a package type was added?)") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/anchore/packageurl-go" | ||
"github.com/anchore/syft/syft/linux" | ||
) | ||
|
||
var _ urlIdentifier = (*NpmPackageJSONMetadata)(nil) | ||
|
||
// NpmPackageJSONMetadata holds extra information that is used in pkg.Package | ||
type NpmPackageJSONMetadata struct { | ||
Name string `mapstructure:"name" json:"name"` | ||
Version string `mapstructure:"version" json:"version"` | ||
Files []string `mapstructure:"files" json:"files,omitempty"` | ||
Author string `mapstructure:"author" json:"author"` | ||
Licenses []string `mapstructure:"licenses" json:"licenses"` | ||
Homepage string `mapstructure:"homepage" json:"homepage"` | ||
Description string `mapstructure:"description" json:"description"` | ||
URL string `mapstructure:"url" json:"url"` | ||
} | ||
|
||
// PackageURL returns the PURL for the specific NPM package (see https://github.com/package-url/purl-spec) | ||
func (p NpmPackageJSONMetadata) PackageURL(_ *linux.Release) string { | ||
return packageurl.NewPackageURL( | ||
packageurl.TypeNPM, | ||
"", | ||
p.Name, | ||
p.Version, | ||
nil, | ||
"", | ||
).ToString() | ||
} |
Oops, something went wrong.