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

move RequestUserAgent to internal/useragent #981

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cmd/osv-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/google/osv-scanner/cmd/osv-scanner/fix"
"github.com/google/osv-scanner/cmd/osv-scanner/scan"
"github.com/google/osv-scanner/cmd/osv-scanner/update"
"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/internal/version"
"github.com/google/osv-scanner/pkg/osv"
"github.com/google/osv-scanner/pkg/osvscanner"
"github.com/google/osv-scanner/pkg/reporter"

Expand All @@ -30,7 +30,7 @@ func run(args []string, stdout, stderr io.Writer) int {
r.Infof("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
}

osv.RequestUserAgent = "osv-scanner/" + version.OSVVersion
useragent.RequestUserAgent = "osv-scanner/" + version.OSVVersion

app := &cli.App{
Name: "osv-scanner",
Expand Down
6 changes: 3 additions & 3 deletions internal/local/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"path"
"strings"

"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/internal/utility/vulns"
"github.com/google/osv-scanner/pkg/lockfile"
"github.com/google/osv-scanner/pkg/models"
"github.com/google/osv-scanner/pkg/osv"
)

type ZipDB struct {
Expand Down Expand Up @@ -104,8 +104,8 @@ func (db *ZipDB) fetchZip() ([]byte, error) {
return nil, fmt.Errorf("could not retrieve OSV database archive: %w", err)
}

if osv.RequestUserAgent != "" {
req.Header.Set("User-Agent", osv.RequestUserAgent)
if useragent.RequestUserAgent != "" {
req.Header.Set("User-Agent", useragent.RequestUserAgent)
}

resp, err := http.DefaultClient.Do(req)
Expand Down
6 changes: 3 additions & 3 deletions internal/resolution/client/npm_registry_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"deps.dev/util/resolve/dep"
"deps.dev/util/semver"
"github.com/google/osv-scanner/internal/resolution/datasource"
"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/pkg/depsdev"
"github.com/google/osv-scanner/pkg/osv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -43,8 +43,8 @@ func NewNpmRegistryClient(workdir string) (*NpmRegistryClient, error) {
creds := credentials.NewClientTLSFromCert(certPool, "")
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}

if osv.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(osv.RequestUserAgent))
if useragent.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(useragent.RequestUserAgent))
}

conn, err := grpc.Dial(depsdev.DepsdevAPI, dialOpts...)
Expand Down
6 changes: 3 additions & 3 deletions internal/resolution/datasource/depsdev_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

pb "deps.dev/api/v3"
"github.com/google/osv-scanner/pkg/osv"
"github.com/google/osv-scanner/internal/useragent"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand Down Expand Up @@ -60,8 +60,8 @@ func NewDepsDevAPIClient(addr string) (*DepsDevAPIClient, error) {
creds := credentials.NewClientTLSFromCert(certPool, "")
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}

if osv.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(osv.RequestUserAgent))
if useragent.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(useragent.RequestUserAgent))
}

conn, err := grpc.Dial(addr, dialOpts...)
Expand Down
3 changes: 3 additions & 0 deletions internal/useragent/useragent.go
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know how you feel about this new location.

Copy link
Contributor

@spencerschrock spencerschrock May 15, 2024

Choose a reason for hiding this comment

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

From an external perspective, this would prevent Scorecard from setting a user agent, which I thought Rex had mentioned a bit back. If that's the direction the team wants to go, I'll close out our corresponding issue.
ossf/scorecard#4029

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes and I also noticed that this won't solve our problem of import cycle so I marked this PR to draft

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package useragent

var RequestUserAgent = ""
6 changes: 3 additions & 3 deletions pkg/depsdev/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"crypto/x509"
"fmt"

"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/pkg/lockfile"
"github.com/google/osv-scanner/pkg/models"
"github.com/google/osv-scanner/pkg/osv"

depsdevpb "deps.dev/api/v3"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -63,8 +63,8 @@ func MakeVersionRequestsWithContext(ctx context.Context, queries []*depsdevpb.Ge
creds := credentials.NewClientTLSFromCert(certPool, "")
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}

if osv.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(osv.RequestUserAgent))
if useragent.RequestUserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(useragent.RequestUserAgent))
}

conn, err := grpc.Dial(DepsdevAPI, dialOpts...)
Expand Down
15 changes: 7 additions & 8 deletions pkg/osv/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"time"

"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/pkg/lockfile"
"github.com/google/osv-scanner/pkg/models"
"golang.org/x/sync/semaphore"
Expand All @@ -34,8 +35,6 @@ const (
jitterMultiplier = 2
)

var RequestUserAgent = ""

// Package represents a package identifier for OSV.
type Package struct {
PURL string `json:"purl,omitempty"`
Expand Down Expand Up @@ -198,8 +197,8 @@ func MakeRequestWithClient(request BatchedQuery, client *http.Client) (*BatchedR
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if RequestUserAgent != "" {
req.Header.Set("User-Agent", RequestUserAgent)
if useragent.RequestUserAgent != "" {
req.Header.Set("User-Agent", useragent.RequestUserAgent)
}

return client.Do(req)
Expand Down Expand Up @@ -237,8 +236,8 @@ func GetWithClient(id string, client *http.Client) (*models.Vulnerability, error
if err != nil {
return nil, err
}
if RequestUserAgent != "" {
req.Header.Set("User-Agent", RequestUserAgent)
if useragent.RequestUserAgent != "" {
req.Header.Set("User-Agent", useragent.RequestUserAgent)
}

return client.Do(req)
Expand Down Expand Up @@ -364,8 +363,8 @@ func MakeDetermineVersionRequest(name string, hashes []DetermineVersionHash) (*D
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if RequestUserAgent != "" {
req.Header.Set("User-Agent", RequestUserAgent)
if useragent.RequestUserAgent != "" {
req.Header.Set("User-Agent", useragent.RequestUserAgent)
}

return http.DefaultClient.Do(req)
Expand Down
5 changes: 3 additions & 2 deletions pkg/osvscanner/osvscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/google/osv-scanner/internal/output"
"github.com/google/osv-scanner/internal/sbom"
"github.com/google/osv-scanner/internal/semantic"
"github.com/google/osv-scanner/internal/useragent"
"github.com/google/osv-scanner/internal/version"
"github.com/google/osv-scanner/pkg/config"
"github.com/google/osv-scanner/pkg/depsdev"
Expand Down Expand Up @@ -970,8 +971,8 @@ func makeRequest(
return hydratedResp, nil
}

if osv.RequestUserAgent == "" {
osv.RequestUserAgent = "osv-scanner-api_v" + version.OSVVersion
if useragent.RequestUserAgent == "" {
useragent.RequestUserAgent = "osv-scanner-api_v" + version.OSVVersion
}

resp, err := osv.MakeRequest(query)
Expand Down
Loading