Skip to content

Commit

Permalink
internal,proxy, all: replace proxy.DefaultClient with NewDefaultClient()
Browse files Browse the repository at this point in the history
Instead of exposing a global public variable, require that users of the
proxy package instantiate their own proxy client. This makes the package
harder to misuse, and makes it clearer where we are using a real client
vs. a test client.

Change-Id: I2f0f5895065e6efec1d50b3ac34cb04847e3b002
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/524457
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
tatianab committed Sep 11, 2023
1 parent a435f71 commit 44ab8d2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestLintReports(t *testing.T) {
return r.LintOffline()
}
} else {
pc := proxy.DefaultClient
pc := proxy.NewDefaultClient()
lint = func(r *report.Report) []string {
return r.Lint(pc)
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/issue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func main() {
}
c := issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: *githubToken})
ghsaClient := ghsa.NewClient(ctx, *githubToken)
pc := proxy.NewDefaultClient()
switch cmd {
case "triage":
err = createIssueToTriage(ctx, c, ghsaClient, filename)
err = createIssueToTriage(ctx, c, ghsaClient, pc, filename)
case "excluded":
err = createExcluded(ctx, c, ghsaClient, filename)
err = createExcluded(ctx, c, ghsaClient, pc, filename)
default:
err = fmt.Errorf("unsupported command: %q", cmd)
}
Expand All @@ -66,33 +67,33 @@ func main() {
}
}

func createIssueToTriage(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, filename string) (err error) {
func createIssueToTriage(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, pc *proxy.Client, filename string) (err error) {
aliases, err := parseAliases(filename)
if err != nil {
return err
}
for _, alias := range aliases {
if err := constructIssue(ctx, c, ghsaClient, alias, []string{"NeedsTriage"}); err != nil {
if err := constructIssue(ctx, c, ghsaClient, pc, alias, []string{"NeedsTriage"}); err != nil {
return err
}
}
return nil
}

func createExcluded(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, filename string) (err error) {
func createExcluded(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, pc *proxy.Client, filename string) (err error) {
records, err := parseExcluded(filename)
if err != nil {
return err
}
for _, r := range records {
if err := constructIssue(ctx, c, ghsaClient, r.identifier, []string{fmt.Sprintf("excluded: %s", r.category)}); err != nil {
if err := constructIssue(ctx, c, ghsaClient, pc, r.identifier, []string{fmt.Sprintf("excluded: %s", r.category)}); err != nil {
return err
}
}
return nil
}

func constructIssue(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, alias string, labels []string) (err error) {
func constructIssue(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Client, pc *proxy.Client, alias string, labels []string) (err error) {
var ghsas []*ghsa.SecurityAdvisory
if strings.HasPrefix(alias, "GHSA") {
sa, err := ghsaClient.FetchGHSA(ctx, alias)
Expand Down Expand Up @@ -132,7 +133,6 @@ func constructIssue(ctx context.Context, c *issues.Client, ghsaClient *ghsa.Clie
if err != nil {
return err
}
pc := proxy.DefaultClient
for _, sa := range ghsas {
for _, id := range sa.Identifiers {
ids = append(ids, id.Value)
Expand Down
10 changes: 5 additions & 5 deletions cmd/vulnreport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ func main() {
}

ghsaClient := ghsa.NewClient(ctx, *githubToken)
pc := proxy.DefaultClient
pc := proxy.NewDefaultClient()
var cmdFunc func(context.Context, string) error
switch cmd {
case "lint":
cmdFunc = lint
cmdFunc = func(ctx context.Context, name string) error { return lint(ctx, name, pc) }
case "commit":
cmdFunc = func(ctx context.Context, name string) error { return commit(ctx, name, ghsaClient, pc, *force) }
case "cve":
Expand Down Expand Up @@ -321,7 +321,7 @@ func setupCreate(ctx context.Context, args []string) ([]int, *createCfg, error)
return githubIDs, &createCfg{
issuesClient: issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: *githubToken}),
ghsaClient: ghsa.NewClient(ctx, *githubToken),
proxyClient: proxy.DefaultClient,
proxyClient: proxy.NewDefaultClient(),
existingByFile: existingByFile,
existingByIssue: existingByIssue,
allowClosed: *closedOk,
Expand Down Expand Up @@ -716,11 +716,11 @@ func addReferenceTODOs(r *report.Report) {
}
}

func lint(_ context.Context, filename string) (err error) {
func lint(_ context.Context, filename string, pc *proxy.Client) (err error) {
defer derrors.Wrap(&err, "lint(%q)", filename)
infolog.Printf("lint %s\n", filename)

_, err = report.ReadAndLint(filename, proxy.DefaultClient)
_, err = report.ReadAndLint(filename, pc)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func createIssuesCommand(ctx context.Context) error {
if err != nil {
return err
}
pc := proxy.DefaultClient
pc := proxy.NewDefaultClient()
return worker.CreateIssues(ctx, cfg.Store, client, pc, allReports, *limit)
}

Expand Down
24 changes: 10 additions & 14 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
"golang.org/x/vulndb/internal/version"
)

// TODO(https://go.dev/issues/60275): Replace this with a function that
// returns a new instance of a default client.
var DefaultClient *Client

const ProxyURL = "https://proxy.golang.org"

// Client is a client for reading from the proxy.
//
// It uses a simple in-memory cache that does not expire,
Expand All @@ -41,14 +35,6 @@ type Client struct {
errLog *errLog // for testing
}

func init() {
proxyURL := ProxyURL
if proxy, ok := os.LookupEnv("GOPROXY"); ok {
proxyURL = proxy
}
DefaultClient = NewClient(http.DefaultClient, proxyURL)
}

func NewClient(c *http.Client, url string) *Client {
return &Client{
Client: c,
Expand All @@ -58,6 +44,16 @@ func NewClient(c *http.Client, url string) *Client {
}
}

const ProxyURL = "https://proxy.golang.org"

func NewDefaultClient() *Client {
proxyURL := ProxyURL
if proxy, ok := os.LookupEnv("GOPROXY"); ok {
proxyURL = proxy
}
return NewClient(http.DefaultClient, proxyURL)
}

func (c *Client) lookup(urlSuffix string) ([]byte, error) {
url := fmt.Sprintf("%s/%s", c.url, urlSuffix)
if b, found := c.cache.get(urlSuffix); found {
Expand Down
2 changes: 1 addition & 1 deletion internal/worker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewServer(ctx context.Context, cfg Config) (_ *Server, err error) {
log.Infof(ctx, "issue creation disabled")
}

s.proxyClient = proxy.DefaultClient
s.proxyClient = proxy.NewDefaultClient()

s.indexTemplate, err = parseTemplate(staticPath, template.TrustedSourceFromConstant("index.tmpl"))
if err != nil {
Expand Down

0 comments on commit 44ab8d2

Please sign in to comment.