Skip to content
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

fix(sbom): use PURL or Group and Name in case of Java #5154

Merged
merged 7 commits into from
Oct 3, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pkg/sbom/cyclonedx/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"sort"
"strconv"
"strings"

"github.com/aquasecurity/trivy/pkg/sbom/cyclonedx/core"

Expand Down Expand Up @@ -409,9 +410,31 @@ func toTrivyCdxComponent(component cdx.Component) ftypes.Component {
}

func getPackageName(typ string, component cdx.Component) string {
// Jar uses `Group` field for `GroupID`
if typ == packageurl.TypeMaven && component.Group != "" {
return fmt.Sprintf("%s:%s", component.Group, component.Name)
if typ == packageurl.TypeMaven {
return convertMavenPackage(component)
}
return component.Name
}

func convertMavenPackage(component cdx.Component) string {
// Check if both Group and Name are present
if component.Group != "" {
return fmt.Sprintf("%s:%s", component.Group, component.Name)
} else if component.PackageURL != "" {
// Split the package into its parts
parts := strings.Split(component.PackageURL, "/")

group := parts[1]

// Get FullName with Version
nameWithVersion := parts[len(parts)-1]

// Remove the Version from the package
nameWOVersion := strings.Split(nameWithVersion, "@")[0]

return fmt.Sprintf("%s:%s", group, nameWOVersion)
// In case we don't have Group or PURL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already parsed purl here -

pkg := p.Package()

We can use this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already parsed purl here -

pkg := p.Package()

We can use this.

Great idea, thanks! Made some changes

} else {
return component.Name
}
}