fix: stop some log.Warn spam due parsing an empty string as a CPE (#3330)

* chore: don't try to parse empty string as CPE

Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com>

* chore: improve OS name and version extraction from ELF metadata

Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com>

---------

Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com>
This commit is contained in:
William Murphy 2024-10-15 08:50:47 -04:00 committed by GitHub
parent 138c6e3420
commit 754cebee64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,19 +27,7 @@ func newELFPackage(metadata elfBinaryPackageNotes, locations file.LocationSet) p
func packageURL(metadata elfBinaryPackageNotes) string { func packageURL(metadata elfBinaryPackageNotes) string {
var qualifiers []packageurl.Qualifier var qualifiers []packageurl.Qualifier
os := metadata.OS os, osVersion := osNameAndVersionFromMetadata(metadata)
osVersion := metadata.OSVersion
var atts cpe.Attributes
atts, err := cpe.NewAttributes(metadata.OSCPE)
if err != nil {
log.WithFields("error", err).Warn("unable to parse cpe attributes for elf binary package")
}
// only "upgrade" the OS information if there is something more specific to use in it's place
if os == "" && osVersion == "" || os == "" && atts.Version != "" || atts.Product != "" && osVersion == "" {
os = atts.Product
osVersion = atts.Version
}
if os != "" { if os != "" {
osQualifier := os osQualifier := os
@ -70,6 +58,26 @@ func packageURL(metadata elfBinaryPackageNotes) string {
).ToString() ).ToString()
} }
func osNameAndVersionFromMetadata(metadata elfBinaryPackageNotes) (string, string) {
os := metadata.OS
osVersion := metadata.OSVersion
if os != "" && osVersion != "" {
return os, osVersion
}
if metadata.OSCPE == "" {
return "", ""
}
attrs, err := cpe.NewAttributes(metadata.OSCPE)
if err != nil {
log.WithFields("error", err).Trace("unable to parse cpe attributes for elf binary package")
return "", ""
}
return attrs.Product, attrs.Version
}
const alpmType = "alpm" const alpmType = "alpm"
func purlDistroType(ty string) string { func purlDistroType(ty string) string {