-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also partially removes the global user agent variable to be specificly passed in for every client. Majority of the code in licencematcher.go is migrated from the deleted license.go file.
- Loading branch information
1 parent
6cd1233
commit 2e9d96a
Showing
15 changed files
with
173 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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 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
109 changes: 109 additions & 0 deletions
109
internal/clients/clientimpl/licensematcher/licensematcher.go
This file contains 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,109 @@ | ||
package licensematcher | ||
|
||
import ( | ||
"context" | ||
|
||
depsdevpb "deps.dev/api/v3" | ||
"github.com/google/osv-scanner/internal/depsdev" | ||
"github.com/google/osv-scanner/internal/imodels" | ||
"github.com/google/osv-scanner/internal/resolution/datasource" | ||
"github.com/google/osv-scanner/pkg/models" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
const ( | ||
maxConcurrentRequests = 1000 | ||
) | ||
|
||
// DepsDevLicenseMatcher implements the LicenseMatcher interface with a deps.dev client. | ||
// It sends out requests for every package version and does not perform caching. | ||
type DepsDevLicenseMatcher struct { | ||
Client *datasource.DepsDevAPIClient | ||
} | ||
|
||
func (matcher *DepsDevLicenseMatcher) MatchLicenses(ctx context.Context, packages []imodels.PackageScanResult) error { | ||
queries := make([]*depsdevpb.GetVersionRequest, len(packages)) | ||
|
||
for i, psr := range packages { | ||
pkg := psr.PackageInfo | ||
system, ok := depsdev.System[psr.PackageInfo.Ecosystem().Ecosystem] | ||
if !ok || pkg.Name() == "" || pkg.Version() == "" { | ||
continue | ||
} | ||
queries[i] = versionQuery(system, pkg.Name(), pkg.Version()) | ||
} | ||
|
||
licenses, err := matcher.makeVersionRequest(ctx, queries) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i, license := range licenses { | ||
packages[i].Licenses = license | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// makeVersionRequest calls the deps.dev GetVersion gRPC API endpoint for each | ||
// query. It makes these requests concurrently, sharing the single HTTP/2 | ||
// connection. The order in which the requests are specified should correspond | ||
// to the order of licenses returned by this function. | ||
func (matcher *DepsDevLicenseMatcher) makeVersionRequest(ctx context.Context, queries []*depsdevpb.GetVersionRequest) ([][]models.License, error) { | ||
licenses := make([][]models.License, len(queries)) | ||
g, ctx := errgroup.WithContext(ctx) | ||
g.SetLimit(maxConcurrentRequests) | ||
|
||
for i := range queries { | ||
if queries[i] == nil { | ||
// This may be a private package. | ||
licenses[i] = []models.License{models.License("UNKNOWN")} | ||
continue | ||
} | ||
g.Go(func() error { | ||
resp, err := matcher.Client.GetVersion(ctx, queries[i]) | ||
if err != nil { | ||
if status.Code(err) == codes.NotFound { | ||
licenses[i] = append(licenses[i], "UNKNOWN") | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
ls := make([]models.License, len(resp.GetLicenses())) | ||
for j, license := range resp.GetLicenses() { | ||
ls[j] = models.License(license) | ||
} | ||
if len(ls) == 0 { | ||
// The deps.dev API will return an | ||
// empty slice if the license is | ||
// unknown. | ||
ls = []models.License{models.License("UNKNOWN")} | ||
} | ||
licenses[i] = ls | ||
|
||
return nil | ||
}) | ||
} | ||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return licenses, nil | ||
} | ||
|
||
func versionQuery(system depsdevpb.System, name string, version string) *depsdevpb.GetVersionRequest { | ||
if system == depsdevpb.System_GO { | ||
version = "v" + version | ||
} | ||
|
||
return &depsdevpb.GetVersionRequest{ | ||
VersionKey: &depsdevpb.VersionKey{ | ||
System: system, | ||
Name: name, | ||
Version: version, | ||
}, | ||
} | ||
} |
This file contains 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,11 @@ | ||
package clientinterfaces | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/osv-scanner/internal/imodels" | ||
) | ||
|
||
type LicenseMatcher interface { | ||
MatchLicenses(ctx context.Context, psr []imodels.PackageScanResult) error | ||
} |
This file contains 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,21 @@ | ||
package depsdev | ||
|
||
import ( | ||
"github.com/ossf/osv-schema/bindings/go/osvschema" | ||
|
||
depsdevpb "deps.dev/api/v3" | ||
) | ||
|
||
// DepsdevAPI is the URL to the deps.dev API. It is documented at | ||
// docs.deps.dev/api. | ||
const DepsdevAPI = "api.deps.dev:443" | ||
|
||
// System maps from a lockfile system to the depsdev API system. | ||
var System = map[osvschema.Ecosystem]depsdevpb.System{ | ||
osvschema.EcosystemNPM: depsdevpb.System_NPM, | ||
osvschema.EcosystemNuGet: depsdevpb.System_NUGET, | ||
osvschema.EcosystemCratesIO: depsdevpb.System_CARGO, | ||
osvschema.EcosystemGo: depsdevpb.System_GO, | ||
osvschema.EcosystemMaven: depsdevpb.System_MAVEN, | ||
osvschema.EcosystemPyPI: depsdevpb.System_PYPI, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package version | ||
|
||
// OSVVersion is the current release version, you should update this variable when doing a release | ||
var OSVVersion = "1.9.1" | ||
const OSVVersion = "1.9.1" |
Oops, something went wrong.