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: merging of binary packages #1583

Merged
merged 9 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 33 additions & 20 deletions syft/pkg/cataloger/binary/cataloger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package binary

import (
"fmt"
"regexp"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
Expand Down Expand Up @@ -33,19 +36,42 @@ func (c Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []artif

for _, cls := range defaultClassifiers {
log.WithFields("classifier", cls.Class).Trace("cataloging binaries")
pkgs, err := catalog(resolver, cls)
newPkgs, err := catalog(resolver, cls)
if err != nil {
log.WithFields("error", err, "classifier", cls.Class).Warn("unable to catalog binary package: %w", err)
continue
}
packages = append(packages, pkgs...)
newPackages:
for i := range newPkgs {
newPkg := &newPkgs[i]
for j := range packages {
p := &packages[j]
// consolidate identical packages found in different locations,
// but continue to track each location
if packagesMatch(p, newPkg) {
// add the locations
p.Locations.Add(newPkg.Locations.ToSlice()...)
kzantow marked this conversation as resolved.
Show resolved Hide resolved
// update the metadata.Classifier to indicate multiple classifiers were used
pMeta, pOk := p.Metadata.(pkg.BinaryMetadata)
newMeta, newOk := newPkg.Metadata.(pkg.BinaryMetadata)
if pOk && newOk {
matcher := regexp.MustCompile(fmt.Sprintf(`(^|\+)%s($|\+)`, newMeta.Classifier))
if !matcher.MatchString(pMeta.Classifier) {
pMeta.Classifier = fmt.Sprintf("%s+%s", pMeta.Classifier, newMeta.Classifier)
p.Metadata = pMeta
}
}
continue newPackages
}
}
packages = append(packages, *newPkg)
}
}

return packages, relationships, nil
}

func catalog(resolver source.FileResolver, cls classifier) ([]pkg.Package, error) {
var pkgs []pkg.Package
func catalog(resolver source.FileResolver, cls classifier) (packages []pkg.Package, err error) {
locations, err := resolver.FilesByGlob(cls.FileGlob)
if err != nil {
return nil, err
Expand All @@ -56,26 +82,13 @@ func catalog(resolver source.FileResolver, cls classifier) ([]pkg.Package, error
return nil, err
}
locationReader := source.NewLocationReadCloser(location, reader)
newPkgs, err := cls.EvidenceMatcher(cls, locationReader)
pkgs, err := cls.EvidenceMatcher(cls, locationReader)
if err != nil {
return nil, err
}
newPackages:
for i := range newPkgs {
newPkg := &newPkgs[i]
for j := range pkgs {
p := &pkgs[j]
// consolidate identical packages found in different locations,
// but continue to track each location
if packagesMatch(p, newPkg) {
p.Locations.Add(newPkg.Locations.ToSlice()...)
continue newPackages
}
}
pkgs = append(pkgs, *newPkg)
}
packages = append(packages, pkgs...)
}
return pkgs, nil
return packages, nil
}

// packagesMatch returns true if the binary packages "match" based on basic criteria
Expand Down
Loading