-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change root of JSON presenter to a mapping (instead of a sequence) (#163
) * update root of json presenter document Signed-off-by: Alex Goodman <[email protected]> * change vulnerabilities to matches in json output Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
10 changed files
with
488 additions
and
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package json | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/anchore/grype/grype/match" | ||
"github.com/anchore/grype/grype/vulnerability" | ||
"github.com/anchore/syft/syft/pkg" | ||
syftJson "github.com/anchore/syft/syft/presenter/json" | ||
"github.com/anchore/syft/syft/scope" | ||
) | ||
|
||
// Document represents the JSON document to be presented | ||
type Document struct { | ||
Matches []Match `json:"matches"` | ||
Image *syftJson.Image `json:"image,omitempty"` | ||
Directory *string `json:"directory,omitempty"` | ||
} | ||
|
||
// Match is a single item for the JSON array reported | ||
type Match struct { | ||
Vulnerability Vulnerability `json:"vulnerability"` | ||
MatchDetails MatchDetails `json:"matchDetails"` | ||
Artifact syftJson.Artifact `json:"artifact"` | ||
} | ||
|
||
// MatchDetails contains all data that indicates how the result match was found | ||
type MatchDetails struct { | ||
Matcher string `json:"matcher"` | ||
SearchKey map[string]interface{} `json:"searchKey"` | ||
MatchInfo map[string]interface{} `json:"matchedOn"` | ||
} | ||
|
||
// NewDocument creates and populates a new Document struct, representing the populated JSON document. | ||
func NewDocument(catalog *pkg.Catalog, s scope.Scope, matches match.Matches, metadataProvider vulnerability.MetadataProvider) (Document, error) { | ||
doc := Document{} | ||
|
||
srcObj := s.Source() | ||
switch src := srcObj.(type) { | ||
case scope.ImageSource: | ||
doc.Image = syftJson.NewImage(src) | ||
case scope.DirSource: | ||
doc.Directory = &s.DirSrc.Path | ||
default: | ||
return Document{}, fmt.Errorf("unsupported source: %T", src) | ||
} | ||
|
||
// we must preallocate the findings to ensure the JSON document does not show "null" when no matches are found | ||
var findings = make([]Match, 0) | ||
for m := range matches.Enumerate() { | ||
p := catalog.Package(m.Package.ID()) | ||
art, err := syftJson.NewArtifact(p, s) | ||
if err != nil { | ||
return Document{}, err | ||
} | ||
|
||
metadata, err := metadataProvider.GetMetadata(m.Vulnerability.ID, m.Vulnerability.RecordSource) | ||
if err != nil { | ||
return Document{}, fmt.Errorf("unable to fetch vuln=%q metadata: %+v", m.Vulnerability.ID, err) | ||
} | ||
|
||
findings = append( | ||
findings, | ||
Match{ | ||
Vulnerability: NewVulnerability(m, metadata), | ||
Artifact: art, | ||
MatchDetails: MatchDetails{ | ||
Matcher: m.Matcher.String(), | ||
SearchKey: m.SearchKey, | ||
MatchInfo: m.SearchMatches, | ||
}, | ||
}, | ||
) | ||
} | ||
doc.Matches = findings | ||
|
||
return doc, nil | ||
} |
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
Oops, something went wrong.