-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add osv output lockfile + refactor #505
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
037e8e9
Add osv output lockfile
another-rex 689e231
Show test name upon output failure
another-rex a5f390b
Specify source field
another-rex ff96cc4
FIx lints
another-rex 90cfaf1
Remove unused ecosystem
another-rex fea0038
Merge branch 'main' into osv-package-lockfile
another-rex 53c1c91
Remove old modelsutil
another-rex 36a2e4a
Add comment on why source for requirement.txt is being filled out
another-rex a5fc1b5
Avoid %% shortcut, generate abs path when specifying tests
another-rex 491f6e3
Keep helper func private
another-rex 5b163ad
change to a much clearer name
another-rex 22133b2
Removed source for now
another-rex 7039392
Remove absPathOrPanic
another-rex 4d6c590
Remove running test print
another-rex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"results": [] | ||
} |
504 changes: 504 additions & 0 deletions
504
pkg/lockfile/fixtures/osvscannerresults/multi-packages-with-vulns.json
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
this is not valid json! (I think) |
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,21 @@ | ||
{ | ||
"results": [ | ||
{ | ||
"source": { | ||
"path": "/path/to/Gemfile.lock", | ||
"type": "lockfile" | ||
}, | ||
"packages": [ | ||
{ | ||
"package": { | ||
"name": "activesupport", | ||
"version": "7.0.7", | ||
"ecosystem": "RubyGems" | ||
}, | ||
"vulnerabilities": [], | ||
"groups": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
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,87 @@ | ||
package lockfile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/pkg/lockfile" | ||
) | ||
|
||
func TestParseOSVScannerResults_FileDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/does-not-exist") | ||
|
||
expectErrContaining(t, err, "no such file or directory") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_InvalidJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/not-json.txt") | ||
|
||
expectErrContaining(t, err, "could not extract from") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_NoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/empty.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_OnePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/one-package.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "activesupport", | ||
Version: "7.0.7", | ||
Ecosystem: lockfile.BundlerEcosystem, | ||
CompareAs: lockfile.BundlerEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseOSVScannerResults_MultiPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/multi-packages-with-vulns.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "crossbeam-utils", | ||
Version: "0.6.6", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
{ | ||
Name: "memoffset", | ||
Version: "0.5.6", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
{ | ||
Name: "smallvec", | ||
Version: "1.6.0", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
}) | ||
} |
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,55 @@ | ||
package lockfile | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/google/osv-scanner/pkg/models" | ||
) | ||
|
||
func ParseOSVScannerResults(pathToLockfile string) ([]PackageDetails, error) { | ||
return extractFromFile(pathToLockfile, OSVScannerResultsExtractor{}) | ||
} | ||
|
||
type OSVScannerResultsExtractor struct{} | ||
|
||
func (e OSVScannerResultsExtractor) ShouldExtract(path string) bool { | ||
// The output will always be a custom json file, so don't return a default should extract | ||
return false | ||
} | ||
|
||
func (e OSVScannerResultsExtractor) Extract(f DepFile) ([]PackageDetails, error) { | ||
parsedResults := models.VulnerabilityResults{} | ||
err := json.NewDecoder(f).Decode(&parsedResults) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not extract from %s: %w", f.Path(), err) | ||
} | ||
|
||
packages := []PackageDetails{} | ||
for _, res := range parsedResults.Results { | ||
for _, pkg := range res.Packages { | ||
packages = append(packages, PackageDetails{ | ||
Name: pkg.Package.Name, | ||
Ecosystem: Ecosystem(pkg.Package.Ecosystem), | ||
Version: pkg.Package.Version, | ||
CompareAs: Ecosystem(pkg.Package.Ecosystem), | ||
}) | ||
} | ||
} | ||
|
||
return packages, nil | ||
} | ||
|
||
var _ Extractor = OSVScannerResultsExtractor{} | ||
|
||
// FromOSVScannerResults attempts to extract packages stored in the OSVScannerResults format | ||
func FromOSVScannerResults(pathToInstalled string) (Lockfile, error) { | ||
packages, err := extractFromFile(pathToInstalled, OSVScannerResultsExtractor{}) | ||
|
||
return Lockfile{ | ||
FilePath: pathToInstalled, | ||
ParsedAs: "osv-scanner-results", | ||
Packages: packages, | ||
}, err | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is good by itself along with the
hasPackage
one, so if you can be bothered I'd favor landing it in its own PR so that this diff is reduced rather than have it wait around for the rest of the changes to get reviewed