-
Notifications
You must be signed in to change notification settings - Fork 496
/
go_get_fetcher.go
197 lines (175 loc) · 5.85 KB
/
go_get_fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package module
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/gomods/athens/pkg/auth"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/observ"
"github.com/gomods/athens/pkg/paths"
"github.com/gomods/athens/pkg/storage"
"github.com/spf13/afero"
)
type goGetFetcher struct {
fs afero.Fs
goBinaryName string
envVars []string
gogetDir string
propagateAuth bool
propagateAuthPatterns []string
}
type goModule struct {
Path string `json:"path"` // module path
Version string `json:"version"` // module version
Error string `json:"error"` // error loading module
Info string `json:"info"` // absolute path to cached .info file
GoMod string `json:"goMod"` // absolute path to cached .mod file
Zip string `json:"zip"` // absolute path to cached .zip file
Dir string `json:"dir"` // absolute path to cached source root directory
Sum string `json:"sum"` // checksum for path, version (as in go.sum)
GoModSum string `json:"goModSum"` // checksum for go.mod (as in go.sum)
}
// NewGoGetFetcher creates fetcher which uses go get tool to fetch modules
func NewGoGetFetcher(goBinaryName, gogetDir string, envVars []string, fs afero.Fs, propagateAuth bool, propagateAuthPatterns []string) (Fetcher, error) {
const op errors.Op = "module.NewGoGetFetcher"
if err := validGoBinary(goBinaryName); err != nil {
return nil, errors.E(op, err)
}
return &goGetFetcher{
fs: fs,
goBinaryName: goBinaryName,
envVars: envVars,
gogetDir: gogetDir,
propagateAuth: propagateAuth,
propagateAuthPatterns: propagateAuthPatterns,
}, nil
}
// Fetch downloads the sources from the go binary and returns the corresponding
// .info, .mod, and .zip files.
func (g *goGetFetcher) Fetch(ctx context.Context, mod, ver string) (*storage.Version, error) {
const op errors.Op = "goGetFetcher.Fetch"
ctx, span := observ.StartSpan(ctx, op.String())
defer span.End()
// setup the GOPATH
goPathRoot, err := afero.TempDir(g.fs, g.gogetDir, "athens")
if err != nil {
return nil, errors.E(op, err)
}
sourcePath := filepath.Join(goPathRoot, "src")
modPath := filepath.Join(sourcePath, getRepoDirName(mod, ver))
if err := g.fs.MkdirAll(modPath, os.ModeDir|os.ModePerm); err != nil {
clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
m, err := g.downloadModule(ctx, goPathRoot, modPath, mod, ver)
if err != nil {
clearFiles(g.fs, goPathRoot)
return nil, errors.E(op, err)
}
var storageVer storage.Version
storageVer.Semver = m.Version
info, err := afero.ReadFile(g.fs, m.Info)
if err != nil {
return nil, errors.E(op, err)
}
storageVer.Info = info
gomod, err := afero.ReadFile(g.fs, m.GoMod)
if err != nil {
return nil, errors.E(op, err)
}
storageVer.Mod = gomod
zip, err := g.fs.Open(m.Zip)
if err != nil {
return nil, errors.E(op, err)
}
// note: don't close zip here so that the caller can read directly from disk.
//
// if we close, then the caller will panic, and the alternative to make this work is
// that we read into memory and return an io.ReadCloser that reads out of memory
storageVer.Zip = &zipReadCloser{zip, g.fs, goPathRoot}
return &storageVer, nil
}
// given a filesystem, gopath, repository root, module and version, runs 'go mod download -json'
// on module@version from the repoRoot with GOPATH=gopath, and returns a non-nil error if anything went wrong.
func (g *goGetFetcher) downloadModule(ctx context.Context, gopath, repoRoot, module, version string) (goModule, error) {
const op errors.Op = "module.downloadModule"
var (
netrcDir string
err error
)
creds, ok := auth.FromContext(ctx)
if ok && g.shouldPropAuth(module) {
if ok {
host := strings.Split(module, "/")[0]
netrcDir, err = auth.WriteTemporaryNETRC(host, creds.User, creds.Password)
if err != nil {
return goModule{}, errors.E(op, err)
}
defer os.RemoveAll(netrcDir)
}
}
uri := strings.TrimSuffix(module, "/")
fullURI := fmt.Sprintf("%s@%s", uri, version)
cmd := exec.CommandContext(ctx, g.goBinaryName, "mod", "download", "-json", fullURI)
cmd.Env = prepareEnv(gopath, netrcDir, g.envVars)
cmd.Dir = repoRoot
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd.Stdout = stdout
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
err = fmt.Errorf("%v: %s", err, stderr)
var m goModule
if jsonErr := json.NewDecoder(stdout).Decode(&m); jsonErr != nil {
return goModule{}, errors.E(op, err)
}
// github quota exceeded
if isLimitHit(m.Error) {
return goModule{}, errors.E(op, m.Error, errors.KindRateLimit)
}
return goModule{}, errors.E(op, m.Error, errors.KindNotFound)
}
var m goModule
if err = json.NewDecoder(stdout).Decode(&m); err != nil {
return goModule{}, errors.E(op, err)
}
if m.Error != "" {
return goModule{}, errors.E(op, m.Error)
}
return m, nil
}
func (g *goGetFetcher) shouldPropAuth(module string) bool {
return g.propagateAuth && matchesAuthPattern(g.propagateAuthPatterns, module)
}
func matchesAuthPattern(patterns []string, module string) bool {
for _, p := range patterns {
if paths.MatchesPattern(p, module) {
return true
}
}
return false
}
func isLimitHit(o string) bool {
return strings.Contains(o, "403 response from api.github.com")
}
// getRepoDirName takes a raw repository URI and a version and creates a directory name that the
// repository contents can be put into
func getRepoDirName(repoURI, version string) string {
escapedURI := strings.Replace(repoURI, "/", "-", -1)
return fmt.Sprintf("%s-%s", escapedURI, version)
}
func validGoBinary(name string) error {
const op errors.Op = "module.validGoBinary"
err := exec.Command(name).Run()
_, ok := err.(*exec.ExitError)
if err != nil && !ok {
return errors.E(op, err)
}
return nil
}