Skip to content

Commit

Permalink
feat: Update local matcher to the new requirements for resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
another-rex committed Jan 7, 2025
1 parent 729bb95 commit b846e3d
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions internal/clients/clientimpl/localmatcher/localmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"os"
"path"
"slices"
"strings"

"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scanner/internal/imodels"
Expand All @@ -26,6 +24,8 @@ type LocalMatcher struct {
dbBasePath string
dbs map[osvschema.Ecosystem]*ZipDB
downloadDB bool
// failedDBs keeps track of the errors when getting databases for each ecosystem
failedDBs map[osvschema.Ecosystem]error
// TODO(v2 logging): Remove this reporter
r reporter.Reporter
}
Expand All @@ -47,9 +47,6 @@ func NewLocalMatcher(r reporter.Reporter, localDBPath string, downloadDB bool) (
func (matcher *LocalMatcher) Match(ctx context.Context, invs []*extractor.Inventory) ([][]*models.Vulnerability, error) {
results := make([][]*models.Vulnerability, 0, len(invs))

// slice to track ecosystems that did not have an offline database available
var missingDBs []string

for _, inv := range invs {
if ctx.Err() != nil {
return nil, ctx.Err()
Expand All @@ -73,41 +70,37 @@ func (matcher *LocalMatcher) Match(ctx context.Context, invs []*extractor.Invent
db, err := matcher.loadDBFromCache(ctx, pkg.Ecosystem)

if err != nil {
if errors.Is(err, ErrOfflineDatabaseNotFound) {
missingDBs = append(missingDBs, string(pkg.Ecosystem.Ecosystem))
} else {
// TODO(V2 logging):
// the most likely error at this point is that the PURL could not be parsed
matcher.r.Errorf("could not load db for %s ecosystem: %v\n", pkg.Ecosystem, err)
}

results = append(results, []*models.Vulnerability{})

continue
}

results = append(results, db.VulnerabilitiesAffectingPackage(pkg))
}

if len(missingDBs) > 0 {
missingDBs = slices.Compact(missingDBs)
slices.Sort(missingDBs)
return results, nil
}

// TODO(v2 logging):
matcher.r.Errorf("could not find local databases for ecosystems: %s\n", strings.Join(missingDBs, ", "))
}
// LoadEcosystem tries to preload the ecosystem into the cache, and returns an error if the ecosystem
// cannot be loaded.
func (matcher *LocalMatcher) LoadEcosystem(ctx context.Context, ecosystem ecosystem.Parsed) error {
_, err := matcher.loadDBFromCache(ctx, ecosystem)

return results, nil
return err
}

func (matcher *LocalMatcher) loadDBFromCache(ctx context.Context, ecosystem ecosystem.Parsed) (*ZipDB, error) {
if db, ok := matcher.dbs[ecosystem.Ecosystem]; ok {
return db, nil
}

if matcher.failedDBs[ecosystem.Ecosystem] != nil {
return nil, matcher.failedDBs[ecosystem.Ecosystem]
}

db, err := NewZippedDB(ctx, matcher.dbBasePath, string(ecosystem.Ecosystem), fmt.Sprintf("%s/%s/all.zip", zippedDBRemoteHost, ecosystem.Ecosystem), !matcher.downloadDB)

if err != nil {
matcher.failedDBs[ecosystem.Ecosystem] = err
matcher.r.Errorf("could not load db for %s ecosystem: %v\n", ecosystem.Ecosystem, err)
return nil, err

Check failure on line 104 in internal/clients/clientimpl/localmatcher/localmatcher.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

Expand Down

0 comments on commit b846e3d

Please sign in to comment.