-
Notifications
You must be signed in to change notification settings - Fork 530
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
Introduce indexed embedded CPE dictionary #1897
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9987e6a
Introduce indexed embedded CPE dictionary
luhring d7ab39d
Don't generate cpe-index on make snapshot
luhring 1a2b4ae
Add unit tests for individual addEntry funcs
luhring 8b0ed81
migrate CPE index build to go generate and add periodic workflow
wagoodman 5d2dfd5
add test to ensure generated cpe index is wired up to function that u…
wagoodman 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
Large diffs are not rendered by default.
Oops, something went wrong.
230 changes: 230 additions & 0 deletions
230
syft/pkg/cataloger/common/cpe/dictionary/index-generator/generate.go
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,230 @@ | ||
package main | ||
|
||
import ( | ||
"compress/gzip" | ||
"encoding/json" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"log" | ||
"strings" | ||
|
||
"github.com/facebookincubator/nvdtools/wfn" | ||
"golang.org/x/exp/slices" | ||
|
||
"github.com/anchore/syft/syft/pkg/cataloger/common/cpe/dictionary" | ||
) | ||
|
||
func generateIndexedDictionaryJSON(rawGzipData io.Reader) ([]byte, error) { | ||
gzipReader, err := gzip.NewReader(rawGzipData) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to decompress CPE dictionary: %w", err) | ||
} | ||
defer gzipReader.Close() | ||
|
||
// Read XML data | ||
data, err := io.ReadAll(gzipReader) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read CPE dictionary: %w", err) | ||
} | ||
|
||
// Unmarshal XML | ||
var cpeList CpeList | ||
if err := xml.Unmarshal(data, &cpeList); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshal CPE dictionary XML: %w", err) | ||
} | ||
|
||
// Filter out data that's not applicable here | ||
cpeList = filterCpeList(cpeList) | ||
|
||
// Create indexed dictionary to help with looking up CPEs | ||
indexedDictionary := indexCPEList(cpeList) | ||
|
||
// Convert to JSON | ||
jsonData, err := json.MarshalIndent(indexedDictionary, "", " ") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to marshal CPE dictionary to JSON: %w", err) | ||
} | ||
return jsonData, nil | ||
} | ||
|
||
// filterCpeList removes CPE items that are not applicable to software packages. | ||
func filterCpeList(cpeList CpeList) CpeList { | ||
var processedCpeList CpeList | ||
|
||
seen := make(map[string]struct{}) | ||
|
||
for _, cpeItem := range cpeList.CpeItems { | ||
// Skip CPE items that don't have any references. | ||
if len(cpeItem.References) == 0 { | ||
continue | ||
} | ||
|
||
// Skip CPE items where the CPE URI doesn't meet our criteria. | ||
parsedName, err := wfn.Parse(cpeItem.Name) | ||
if err != nil { | ||
log.Printf("unable to parse CPE URI %q: %s", cpeItem.Name, err) | ||
} | ||
|
||
if slices.Contains([]string{"h", "o"}, parsedName.Part) { | ||
continue | ||
} | ||
|
||
normalizedName := normalizeCPE(parsedName).BindToURI() | ||
if _, ok := seen[normalizedName]; ok { | ||
continue | ||
} | ||
seen[normalizedName] = struct{}{} | ||
cpeItem.Name = normalizedName | ||
|
||
parsedCPE, err := wfn.Parse(cpeItem.Cpe23Item.Name) | ||
if err != nil { | ||
log.Printf("unable to parse CPE value %q: %s", cpeItem.Cpe23Item.Name, err) | ||
} | ||
|
||
cpeItem.Cpe23Item.Name = normalizeCPE(parsedCPE).BindToFmtString() | ||
|
||
processedCpeList.CpeItems = append(processedCpeList.CpeItems, cpeItem) | ||
} | ||
|
||
return processedCpeList | ||
} | ||
|
||
// normalizeCPE removes the version and update parts of a CPE. | ||
func normalizeCPE(cpe *wfn.Attributes) *wfn.Attributes { | ||
cpeCopy := *cpe | ||
|
||
cpeCopy.Version = "" | ||
cpeCopy.Update = "" | ||
|
||
return &cpeCopy | ||
} | ||
|
||
const ( | ||
prefixForNPMPackages = "https://www.npmjs.com/package/" | ||
prefixForRubyGems = "https://rubygems.org/gems/" | ||
prefixForRubyGemsHTTP = "http://rubygems.org/gems/" | ||
prefixForNativeRubyGems = "https://github.com/ruby/" | ||
prefixForPyPIPackages = "https://pypi.org/project/" | ||
prefixForJenkinsPlugins = "https://github.com/jenkinsci/" | ||
prefixForRustCrates = "https://crates.io/crates/" | ||
) | ||
|
||
// indexCPEList creates an index of CPEs by ecosystem. | ||
func indexCPEList(list CpeList) *dictionary.Indexed { | ||
wagoodman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
indexed := &dictionary.Indexed{ | ||
EcosystemPackages: make(map[string]dictionary.Packages), | ||
} | ||
|
||
for _, cpeItem := range list.CpeItems { | ||
cpeItemName := cpeItem.Cpe23Item.Name | ||
|
||
for _, reference := range cpeItem.References { | ||
ref := reference.Reference.Href | ||
|
||
switch { | ||
case strings.HasPrefix(ref, prefixForNPMPackages): | ||
addEntryForNPMPackage(indexed, ref, cpeItemName) | ||
|
||
case strings.HasPrefix(ref, prefixForRubyGems), strings.HasPrefix(ref, prefixForRubyGemsHTTP): | ||
addEntryForRubyGem(indexed, ref, cpeItemName) | ||
|
||
case strings.HasPrefix(ref, prefixForNativeRubyGems): | ||
addEntryForNativeRubyGem(indexed, ref, cpeItemName) | ||
|
||
case strings.HasPrefix(ref, prefixForPyPIPackages): | ||
addEntryForPyPIPackage(indexed, ref, cpeItemName) | ||
|
||
case strings.HasPrefix(ref, prefixForJenkinsPlugins): | ||
// It _might_ be a jenkins plugin! | ||
addEntryForJenkinsPlugin(indexed, ref, cpeItemName) | ||
|
||
case strings.HasPrefix(ref, prefixForRustCrates): | ||
addEntryForRustCrate(indexed, ref, cpeItemName) | ||
} | ||
} | ||
} | ||
|
||
return indexed | ||
} | ||
|
||
func addEntryForRustCrate(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.TrimPrefix(ref, prefixForRustCrates) | ||
ref = strings.Split(ref, "/")[0] | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemRustCrates]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemRustCrates] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemRustCrates][ref] = cpeItemName | ||
} | ||
|
||
func addEntryForJenkinsPlugin(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.TrimPrefix(ref, prefixForJenkinsPlugins) | ||
ref = strings.Split(ref, "/")[0] | ||
|
||
if !strings.HasSuffix(ref, "-plugin") { | ||
// It's not a jenkins plugin! | ||
return | ||
} | ||
|
||
ref = strings.TrimSuffix(ref, "-plugin") | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemJenkinsPlugins][ref] = cpeItemName | ||
} | ||
|
||
func addEntryForPyPIPackage(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.TrimPrefix(ref, prefixForPyPIPackages) | ||
ref = strings.Split(ref, "/")[0] | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemPyPI]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemPyPI] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemPyPI][ref] = cpeItemName | ||
} | ||
|
||
func addEntryForNativeRubyGem(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.TrimPrefix(ref, prefixForNativeRubyGems) | ||
ref = strings.Split(ref, "/")[0] | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemRubyGems]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemRubyGems] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItemName | ||
} | ||
|
||
func addEntryForRubyGem(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.TrimPrefix(ref, prefixForRubyGems) | ||
ref = strings.TrimPrefix(ref, prefixForRubyGemsHTTP) | ||
ref = strings.Split(ref, "/")[0] | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemRubyGems]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemRubyGems] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemRubyGems][ref] = cpeItemName | ||
} | ||
|
||
func addEntryForNPMPackage(indexed *dictionary.Indexed, ref string, cpeItemName string) { | ||
// Prune off the non-package-name parts of the URL | ||
ref = strings.Split(ref, "/v/")[0] | ||
ref = strings.Split(ref, "?")[0] | ||
ref = strings.TrimPrefix(ref, prefixForNPMPackages) | ||
|
||
if _, ok := indexed.EcosystemPackages[dictionary.EcosystemNPM]; !ok { | ||
indexed.EcosystemPackages[dictionary.EcosystemNPM] = make(dictionary.Packages) | ||
} | ||
|
||
indexed.EcosystemPackages[dictionary.EcosystemNPM][ref] = cpeItemName | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
actually, generating new data during the release step isn't the right place for this. Though it answers the question of how this gets refreshed, it introduces potentially breaking changes right at the release, bypassing testing.
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.
Yeah, I think we can just take that step out, lean on the index that's checked into the repo, and periodically regenerate what's in the repo. I've seen other projects take a similar approach with generated/embedded data. How does that sound to you?