-
Notifications
You must be signed in to change notification settings - Fork 856
feat: Add PURL list input/output format #3853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2bf8bba
feat: purl-list format
kzantow 3b6d088
Merge remote-tracking branch 'upstream/main' into feat/purl-list
kzantow 98a76c5
chore: update tests and fix read seeker usage
kzantow c167a40
Merge remote-tracking branch 'upstream/main' into feat/purl-list
kzantow 5f4a672
chore: fix non-empty purl namespace import
kzantow 2383b31
chore: fix test
kzantow d2642ac
chore: port remaining grype PURL conversion
kzantow 7af5c1d
Merge remote-tracking branch 'upstream/main' into feat/purl-list
kzantow a8bbb12
chore: rename purl-list purls
kzantow 9bf1245
chore: fixes and update tests
kzantow f0d469f
chore: backfill syft format
kzantow 9ddd3e6
chore: correct backfill epoch handling
kzantow 46c235a
chore: cleanup
kzantow 5eb6f88
chore: address PR feedback, add more purl name logic
kzantow 52a00a8
Merge remote-tracking branch 'upstream/main' into feat/purl-list
kzantow 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,137 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/anchore/packageurl-go" | ||
| "github.com/anchore/syft/internal/log" | ||
| "github.com/anchore/syft/syft/cpe" | ||
| "github.com/anchore/syft/syft/pkg" | ||
| ) | ||
|
|
||
| // Backfill takes all information present in the package and attempts to fill in any missing information | ||
| // from any available sources, such as the Metadata and PURL. | ||
| // | ||
| // Backfill does not call p.SetID(), but this needs to be called later to ensure it's up to date | ||
| func Backfill(p *pkg.Package) { | ||
| if p.PURL == "" { | ||
| return | ||
| } | ||
|
|
||
| purl, err := packageurl.FromString(p.PURL) | ||
| if err != nil { | ||
| log.Debug("unable to parse purl: %s: %w", p.PURL, err) | ||
| return | ||
| } | ||
|
|
||
| var cpes []cpe.CPE | ||
| epoch := "" | ||
|
|
||
| for _, qualifier := range purl.Qualifiers { | ||
| switch qualifier.Key { | ||
| case pkg.PURLQualifierCPES: | ||
| rawCpes := strings.Split(qualifier.Value, ",") | ||
| for _, rawCpe := range rawCpes { | ||
| c, err := cpe.New(rawCpe, cpe.DeclaredSource) | ||
| if err != nil { | ||
| log.Debugf("unable to decode cpe %s in purl %s: %w", rawCpe, p.PURL, err) | ||
| continue | ||
| } | ||
| cpes = append(cpes, c) | ||
| } | ||
| case pkg.PURLQualifierEpoch: | ||
| epoch = qualifier.Value | ||
| } | ||
| } | ||
|
|
||
| if p.Type == "" { | ||
| p.Type = pkg.TypeFromPURL(p.PURL) | ||
| } | ||
| if p.Language == "" { | ||
| p.Language = pkg.LanguageFromPURL(p.PURL) | ||
| } | ||
| if p.Name == "" { | ||
| p.Name = nameFromPurl(purl) | ||
| } | ||
|
|
||
| setVersionFromPurl(p, purl, epoch) | ||
|
|
||
| if p.Language == pkg.Java { | ||
| setJavaMetadataFromPurl(p, purl) | ||
| } | ||
|
|
||
| for _, c := range cpes { | ||
| if slices.Contains(p.CPEs, c) { | ||
| continue | ||
| } | ||
| p.CPEs = append(p.CPEs, c) | ||
| } | ||
| } | ||
|
|
||
| func setJavaMetadataFromPurl(p *pkg.Package, purl packageurl.PackageURL) { | ||
| if p.Type != pkg.JavaPkg { | ||
| return | ||
| } | ||
| if purl.Namespace != "" { | ||
| if p.Metadata == nil { | ||
| p.Metadata = pkg.JavaArchive{} | ||
| } | ||
| meta, got := p.Metadata.(pkg.JavaArchive) | ||
| if got && meta.PomProperties == nil { | ||
| meta.PomProperties = &pkg.JavaPomProperties{} | ||
| p.Metadata = meta | ||
| } | ||
| if meta.PomProperties != nil { | ||
| // capture the group id from the purl if it is not already set | ||
| if meta.PomProperties.ArtifactID == "" { | ||
| meta.PomProperties.ArtifactID = purl.Name | ||
| } | ||
| if meta.PomProperties.GroupID == "" { | ||
| meta.PomProperties.GroupID = purl.Namespace | ||
| } | ||
| if meta.PomProperties.Version == "" { | ||
| meta.PomProperties.Version = purl.Version | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func setVersionFromPurl(p *pkg.Package, purl packageurl.PackageURL, epoch string) { | ||
| if p.Version == "" { | ||
| p.Version = purl.Version | ||
| } | ||
|
|
||
| if epoch != "" && p.Type == pkg.RpmPkg && !epochPrefix.MatchString(p.Version) { | ||
| p.Version = fmt.Sprintf("%s:%s", epoch, p.Version) | ||
| } | ||
| } | ||
|
|
||
| var epochPrefix = regexp.MustCompile(`^\d+:`) | ||
|
|
||
| // nameFromPurl returns the syft package name of the package from the purl. If the purl includes a namespace, | ||
| // the name is prefixed as appropriate based on the PURL type | ||
| func nameFromPurl(purl packageurl.PackageURL) string { | ||
| if !nameExcludesPurlNamespace(purl.Type) && purl.Namespace != "" { | ||
| return fmt.Sprintf("%s/%s", purl.Namespace, purl.Name) | ||
| } | ||
| return purl.Name | ||
| } | ||
|
|
||
| func nameExcludesPurlNamespace(purlType string) bool { | ||
| switch purlType { | ||
| case packageurl.TypeAlpine, | ||
| packageurl.TypeAlpm, | ||
| packageurl.TypeConan, | ||
| packageurl.TypeCpan, | ||
| packageurl.TypeDebian, | ||
| packageurl.TypeMaven, | ||
| packageurl.TypeQpkg, | ||
| packageurl.TypeRPM, | ||
| packageurl.TypeSWID: | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.