internal/worker: remove unused module_proxy.go
Follows [internal/worker: remove scan-modules code](https://go-review.googlesource.com/c/vulndb/+/459836).
Change-Id: I2e25e083b7b2b4d3128146b533d88796b83828ea
GitHub-Last-Rev: c6b5b29f48c1283af936d8b50ad87b42d5e91091
GitHub-Pull-Request: golang/vulndb#1510
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/463102
Reviewed-by: Tatiana Bradley <tatianabradley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Tatiana Bradley <tatianabradley@google.com>
Run-TryBot: Tatiana Bradley <tatianabradley@google.com>
diff --git a/internal/worker/module_proxy.go b/internal/worker/module_proxy.go
deleted file mode 100644
index ca7ec25..0000000
--- a/internal/worker/module_proxy.go
+++ /dev/null
@@ -1,93 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package worker
-
-import (
- "archive/zip"
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "sort"
- "strings"
- "time"
-
- "golang.org/x/mod/module"
- "golang.org/x/mod/semver"
- "golang.org/x/net/context/ctxhttp"
-)
-
-// Convenience functions for accessing the Go module proxy.
-
-const proxyURL = "https://proxy.golang.org"
-
-// latestVersion returns the version of modulePath provided by the proxy's "@latest"
-// endpoint.
-func latestVersion(ctx context.Context, proxyURL, modulePath string) (string, error) {
- body, err := proxyRequest(ctx, proxyURL, modulePath, "/@latest")
- if err != nil {
- return "", err
- }
-
- var info struct {
- Version string
- Time time.Time
- }
- if err := json.Unmarshal(body, &info); err != nil {
- return "", err
- }
- return info.Version, nil
-}
-
-// latestTaggedVersion returns the latest (largest in the semver sense) tagged
-// version of modulePath, as determined by the module proxy's "list" endpoint.
-// It returns ("", nil) if there are no tagged versions.
-func latestTaggedVersion(ctx context.Context, proxyURL, modulePath string) (string, error) {
- body, err := proxyRequest(ctx, proxyURL, modulePath, "/@v/list")
- if err != nil {
- return "", err
- }
- vs := strings.Split(string(bytes.TrimSpace(body)), "\n")
- if len(vs) == 0 {
- return "", nil
- }
- sort.Slice(vs, func(i, j int) bool { return semver.Compare(vs[i], vs[j]) > 0 })
- return vs[0], nil
-}
-
-func moduleZip(ctx context.Context, proxyURL, modulePath, version string) (*zip.Reader, error) {
- ev, err := module.EscapeVersion(version)
- if err != nil {
- return nil, err
- }
- body, err := proxyRequest(ctx, proxyURL, modulePath, fmt.Sprintf("/@v/%s.zip", ev))
- if err != nil {
- return nil, err
- }
- return zip.NewReader(bytes.NewReader(body), int64(len(body)))
-}
-
-func proxyRequest(ctx context.Context, proxyURL, modulePath, suffix string) ([]byte, error) {
- ep, err := module.EscapePath(modulePath)
- if err != nil {
- return nil, fmt.Errorf("module path %v: %w", modulePath, err)
- }
- url := fmt.Sprintf("%s/%s%s", proxyURL, ep, suffix)
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return nil, err
- }
- res, err := ctxhttp.Do(ctx, http.DefaultClient, req)
- if err != nil {
- return nil, err
- }
- defer res.Body.Close()
- if res.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("%s returned status %d", url, res.StatusCode)
- }
- return io.ReadAll(res.Body)
-}
diff --git a/internal/worker/module_proxy_test.go b/internal/worker/module_proxy_test.go
deleted file mode 100644
index eaa3aaf..0000000
--- a/internal/worker/module_proxy_test.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package worker
-
-import (
- "archive/zip"
- "context"
- "net/http"
- "net/http/httptest"
- "testing"
-
- "golang.org/x/mod/semver"
-)
-
-func TestLatestVersion(t *testing.T) {
- pt := newProxyTest()
- defer pt.Close()
-
- got, err := latestVersion(context.Background(), pt.URL, "golang.org/x/build")
- if err != nil {
- t.Fatal(err)
- }
- if !semver.IsValid(got) {
- t.Errorf("got invalid version %q", got)
- }
-}
-
-func TestLatestTaggedVersion(t *testing.T) {
- pt := newProxyTest()
- defer pt.Close()
-
- got, err := latestTaggedVersion(context.Background(), pt.URL, "golang.org/x/build")
- if err != nil {
- t.Fatal(err)
- }
- if got != "" {
- t.Errorf(`got %q, wanted ""`, got)
- }
-
- got, err = latestTaggedVersion(context.Background(), pt.URL, "golang.org/x/tools")
- if err != nil {
- t.Fatal(err)
- }
- if !semver.IsValid(got) {
- t.Errorf("got invalid version %q", got)
- }
-
-}
-
-func TestModuleZip(t *testing.T) {
- pt := newProxyTest()
- defer pt.Close()
-
- ctx := context.Background()
- const m = "golang.org/x/time"
- v, err := latestVersion(ctx, pt.URL, m)
- if err != nil {
- t.Fatal(err)
- }
- _, err = moduleZip(ctx, pt.URL, m, v)
- if err != nil {
- t.Fatal(err)
- }
-}
-
-type proxyTest struct {
- *httptest.Server
-}
-
-func (*proxyTest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- switch r.URL.Path {
- case "/golang.org/x/build/@latest":
- w.Write([]byte(`{"Version":"v0.0.0-20220722180300-9ed544e84dd1","Time":"2022-07-22T18:03:00Z"}`))
- case "/golang.org/x/build/@v/list":
- case "/golang.org/x/tools/@v/list":
- w.Write([]byte("v0.1.1\nv0.1.2\n"))
- case "/golang.org/x/time/@latest":
- w.Write([]byte(`{"Version":"v0.0.0-20220722155302-e5dcc9cfc0b9","Time":"2022-07-22T15:53:02Z"}`))
- case "/golang.org/x/time/@v/v0.0.0-20220722155302-e5dcc9cfc0b9.zip":
- zw := zip.NewWriter(w)
- fw, _ := zw.Create("golang.org/x/time@v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod")
- fw.Write([]byte(`module golang.org/x/time`))
- zw.Close()
- case "/golang.org/x/mod/@v/v0.5.1.zip":
- zw := zip.NewWriter(w)
- fw, _ := zw.Create("golang.org/x/mod@v0.5.1/go.mod")
- fw.Write([]byte(`module golang.org/x/mod`))
- zw.Close()
- default:
- w.WriteHeader(404)
- }
-}
-
-func newProxyTest() *proxyTest {
- pt := &proxyTest{}
- pt.Server = httptest.NewServer(pt)
- return pt
-}