GitLabgitlab-org

cli

main
ply7 git calls280 ms100 KiB5 cachedprotocol v2
374 lines12 KiBRaw
1//go:build !integration
3package pm
5import (
6 "bytes"
7 "crypto/rand"
8 "encoding/pem"
9 "log"
10 "maps"
11 "net"
12 "net/http"
13 "net/http/httptest"
14 "os"
15 "path/filepath"
16 "slices"
17 "strings"
18 "sync"
19 "testing"
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 "golang.org/x/mod/sumdb"
24 "golang.org/x/mod/sumdb/note"
26 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/cilog"
27 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
28 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
31const (
32 upstreamUser = "alice"
33 upstreamPassword = "s3cret-upstream-password"
34 upstreamToken = "t0ken-as-username"
37type upstream struct {
38 *httptest.Server
39 mu sync.Mutex
40 paths []string
41 status map[string]int
44type statusWriter struct {
45 http.ResponseWriter
46 code int
49func (w *statusWriter) WriteHeader(code int) {
50 w.code = code
51 w.ResponseWriter.WriteHeader(code)
54func newUpstream(t *testing.T, secure bool, next http.Handler) *upstream {
55 t.Helper()
56 u := &upstream{status: map[string]int{}}
57 h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 sw := &statusWriter{ResponseWriter: w, code: http.StatusOK}
59 next.ServeHTTP(sw, r)
60 u.mu.Lock()
61 defer u.mu.Unlock()
62 u.paths = append(u.paths, r.URL.Path)
63 u.status[r.URL.Path] = sw.code
64 })
65 if !secure {
66 u.Server = httptest.NewServer(h)
67 t.Cleanup(u.Close)
68 return u
69 }
70 u.Server = httptest.NewTLSServer(h)
71 t.Cleanup(u.Close)
72 certFile := filepath.Join(t.TempDir(), "upstream.pem")
73 cert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: u.Certificate().Raw})
74 require.NoError(t, os.WriteFile(certFile, cert, 0o600))
75 t.Setenv("SSL_CERT_FILE", certFile)
76 return u
79func (u *upstream) seen() ([]string, map[string]int) {
80 u.mu.Lock()
81 defer u.mu.Unlock()
82 return slices.Clone(u.paths), maps.Clone(u.status)
85func (u *upstream) host() string {
86 return strings.TrimPrefix(strings.TrimPrefix(u.URL, "https://"), "http://")
89func moduleFiles(t *testing.T) http.Handler {
90 t.Helper()
91 root, _ := writeModuleFixture(t)
92 return http.FileServer(http.Dir(root))
95func requireBasicAuth(next http.Handler) http.Handler {
96 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
97 if user, pass, ok := r.BasicAuth(); !ok || user != upstreamUser || pass != upstreamPassword {
98 w.Header().Set("WWW-Authenticate", `Basic realm="upstream"`)
99 http.Error(w, "unauthorized", http.StatusUnauthorized)
100 return
101 }
102 next.ServeHTTP(w, r)
103 })
106func closedAddr(t *testing.T) string {
107 t.Helper()
108 l, err := net.Listen("tcp", "127.0.0.1:0")
109 require.NoError(t, err)
110 addr := l.Addr().String()
111 require.NoError(t, l.Close())
112 return addr
115func countFetcherEgress(t *testing.T, addr string) {
116 t.Helper()
117 t.Setenv("HTTPS_PROXY", addr)
118 t.Setenv("HTTP_PROXY", addr)
121func runUpstreamGo(t *testing.T, newBackend func(string, []string, map[string]string, []byte) (http.Handler, func(), error), args ...string) goRun {
122 t.Helper()
123 ios, _, out, errOut := cmdtest.TestIOStreams()
124 r := goRun{dir: t.TempDir(), exec: newRealGoExecutor(t), m: &golangManager{newBackend: newBackend}}
125 countFetcherEgress(t, r.exec.proxy)
126 r.err = Run(t.Context(), r.m, RunOptions{
127 IO: ios, Executor: r.exec, BaseDir: r.dir, ProjectID: "group/proj", Args: args,
128 })
129 r.stdout, r.stderr = out.String(), errOut.String()
130 return r
133func downloadFixture(t *testing.T) goRun {
134 t.Helper()
135 return runUpstreamGo(t, newGoBackend, "mod", "download", "-json", fixtureModule+"@"+fixtureVersion)
138func TestRunUpstreamEnvFileGOPROXY(t *testing.T) {
139 up := newUpstream(t, false, moduleFiles(t))
140 isolateGo(t, "GOPROXY="+up.URL+"\n")
141 t.Setenv("GOSUMDB", "off")
142 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
144 r := downloadFixture(t)
145 require.NoError(t, r.err, r.stdout+r.stderr)
147 paths, _ := up.seen()
148 assert.Contains(t, paths, "/"+fixtureModule+"/@v/"+fixtureVersion+".zip", "the go env file's GOPROXY must be the upstream")
149 assert.Zero(t, r.exec.proxied.Load(), "nothing may reach proxy.golang.org or any other host")
150 assert.Equal(t, 1, r.m.checkedZips())
153func TestRunUpstreamEmptyGOSUMDBFailsLikeGo(t *testing.T) {
154 up := newUpstream(t, false, moduleFiles(t))
155 isolateGo(t, "GOPROXY="+up.URL+"\nGOSUMDB=\n")
156 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
158 r := downloadFixture(t)
159 var ee *ExitError
160 require.ErrorAs(t, r.err, &ee)
161 assert.True(t, ee.ChildExited, "go itself must fail")
162 assert.Contains(t, r.stdout+r.stderr, "missing GOSUMDB", "the user must see the error plain go gives for this setting")
163 assert.NotContains(t, r.stdout+r.stderr, "invalid GOSUMDB")
166func TestRunUpstreamListFallsThroughOnlyOnNotFound(t *testing.T) {
167 zip := "/" + fixtureModule + "/@v/" + fixtureVersion + ".zip"
168 files := moduleFiles(t)
169 a := newUpstream(t, false, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
170 if r.URL.Path == zip {
171 http.NotFound(w, r)
172 return
173 }
174 files.ServeHTTP(w, r)
175 }))
176 b := newUpstream(t, false, files)
177 isolateGo(t, "")
178 t.Setenv("GOPROXY", a.URL+","+b.URL+",direct")
179 t.Setenv("GOSUMDB", "off")
180 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
182 r := downloadFixture(t)
183 require.NoError(t, r.err, r.stdout+r.stderr)
185 aPaths, aStatus := a.seen()
186 bPaths, _ := b.seen()
187 assert.Contains(t, aPaths, zip)
188 assert.Equal(t, []string{zip}, bPaths, "b sees only what a answered 404 for")
189 assert.Equal(t, http.StatusNotFound, aStatus[zip])
190 assert.Zero(t, r.exec.proxied.Load(), "direct must not be reached once b served the module")
193func TestRunUpstreamSchemelessEntryUsesHTTPS(t *testing.T) {
194 up := newUpstream(t, true, moduleFiles(t))
195 isolateGo(t, "")
196 t.Setenv("GOPROXY", up.host())
197 t.Setenv("GOSUMDB", "off")
198 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
200 r := downloadFixture(t)
201 require.NoError(t, r.err, r.stdout+r.stderr)
202 paths, _ := up.seen()
203 assert.Contains(t, paths, "/"+fixtureModule+"/@v/"+fixtureVersion+".zip", "a scheme-less entry is fetched over https")
206func TestRunUpstreamDown(t *testing.T) {
207 cases := []struct {
208 sep string
209 directTried bool
210 wantInOutput string
211 }{
212 {",", false, "connection refused"},
213 {"|", true, ""},
214 }
215 for _, c := range cases {
216 t.Run(c.sep, func(t *testing.T) {
217 isolateGo(t, "")
218 t.Setenv("GOPROXY", "http://"+closedAddr(t)+c.sep+"direct")
219 t.Setenv("GOSUMDB", "off")
220 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
222 r := downloadFixture(t)
223 var ee *ExitError
224 require.ErrorAs(t, r.err, &ee)
225 assert.True(t, ee.ChildExited, "go itself must fail")
226 assert.Contains(t, r.stdout+r.stderr, c.wantInOutput, "the child's error must say why the upstream failed")
227 assert.NotContains(t, r.stdout+r.stderr, "internal server error")
228 assert.Equal(t, c.directTried, r.exec.proxied.Load() > 0, "direct is tried only when the list falls back on errors")
229 assert.Zero(t, r.m.checkedZips(), "no zip reached the gate")
230 })
231 }
234func TestRunUpstreamNetrcCredentials(t *testing.T) {
235 up := newUpstream(t, true, requireBasicAuth(moduleFiles(t)))
236 isolateGo(t, "")
237 netrc := filepath.Join(t.TempDir(), "netrc")
238 require.NoError(t, os.WriteFile(netrc, []byte("machine "+up.host()+" login "+upstreamUser+" password "+upstreamPassword+"\n"), 0o600))
239 t.Setenv("NETRC", netrc)
240 t.Setenv("GOPROXY", up.URL)
241 t.Setenv("GOSUMDB", "off")
242 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
244 r := downloadFixture(t)
245 require.NoError(t, r.err, r.stdout+r.stderr)
246 _, status := up.seen()
247 assert.Equal(t, http.StatusOK, status["/"+fixtureModule+"/@v/"+fixtureVersion+".zip"], "the .netrc credentials must reach the upstream")
248 assert.NotContains(t, r.stdout+r.stderr, upstreamPassword)
251func TestRunUpstreamURLCredentials(t *testing.T) {
252 up := newUpstream(t, true, requireBasicAuth(moduleFiles(t)))
253 isolateGo(t, "")
254 t.Setenv("GOPROXY", "https://"+upstreamUser+":"+upstreamPassword+"@"+up.host())
255 t.Setenv("GOSUMDB", "off")
256 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
258 r := downloadFixture(t)
259 require.NoError(t, r.err, r.stdout+r.stderr)
260 _, status := up.seen()
261 assert.Equal(t, http.StatusOK, status["/"+fixtureModule+"/@v/"+fixtureVersion+".zip"], "the URL's credentials must reach the upstream")
262 assert.NotContains(t, r.stdout+r.stderr, upstreamPassword)
265func captureDebugLog(t *testing.T) *bytes.Buffer {
266 t.Helper()
267 t.Setenv("GLAB_DEBUG", "true")
268 var buf bytes.Buffer
269 log.SetOutput(&buf)
270 t.Cleanup(func() { log.SetOutput(os.Stderr) })
271 return &buf
274func TestRunUpstreamBlockedModuleWithCredentials(t *testing.T) {
275 up := newUpstream(t, true, requireBasicAuth(moduleFiles(t)))
276 isolateGo(t, "")
277 debugLog := captureDebugLog(t)
278 t.Setenv("GOPROXY", "https://"+upstreamUser+":"+upstreamPassword+"@"+up.host())
279 t.Setenv("GOSUMDB", "off")
280 t.Setenv("GLAB_DF_FAKE_BLOCK", "golang:"+fixtureModule+"@"+fixtureVersion)
282 r := downloadFixture(t)
283 var ee *ExitError
284 require.ErrorAs(t, r.err, &ee)
285 assert.True(t, ee.ChildExited, "go itself must fail")
286 assert.Contains(t, r.stdout+r.stderr, "blocked by GLAB_DF_FAKE_BLOCK", "the gate refuses the module although the upstream serves it")
288 got, err := cilog.Load(r.dir)
289 require.NoError(t, err)
290 require.Len(t, got.Entries, 1)
291 assert.Equal(t, verdict.Blocked, got.Entries[0].Verdict)
292 raw, err := os.ReadFile(filepath.Join(r.dir, ".gitlab", "df", "ci-log.json"))
293 require.NoError(t, err)
294 for name, out := range map[string]string{
295 "verdict": got.Entries[0].Reason,
296 "ci log": string(raw),
297 "go output": r.stdout + r.stderr,
298 "debug log": debugLog.String(),
299 } {
300 assert.NotContains(t, out, upstreamPassword, name)
301 }
304func TestRunUpstreamFailureHidesURLCredentials(t *testing.T) {
305 for name, userinfo := range map[string]string{
306 "user and password": upstreamUser + ":" + upstreamPassword,
307 "token as username": upstreamToken,
308 } {
309 t.Run(name, func(t *testing.T) {
310 isolateGo(t, "")
311 debugLog := captureDebugLog(t)
312 t.Setenv("GOPROXY", "https://"+userinfo+"@"+closedAddr(t))
313 t.Setenv("GOSUMDB", "off")
314 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
316 r := downloadFixture(t)
317 require.Error(t, r.err)
318 assert.Contains(t, r.stdout+r.stderr, "connection refused")
319 for name, out := range map[string]string{
320 "error": r.err.Error(),
321 "go output": r.stdout + r.stderr,
322 "debug log": debugLog.String(),
323 } {
324 assert.NotContains(t, out, upstreamPassword, name)
325 assert.NotContains(t, out, upstreamToken, name)
326 }
327 })
328 }
331type recordingBackend struct {
332 mu sync.Mutex
333 paths []string
336func (b *recordingBackend) newBackend(goBin string, proxiedSumDBs []string, overrides map[string]string, upstreamCAs []byte) (http.Handler, func(), error) {
337 h, closeBackend, err := newGoBackend(goBin, proxiedSumDBs, overrides, upstreamCAs)
338 if err != nil {
339 return nil, nil, err
340 }
341 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
342 b.mu.Lock()
343 b.paths = append(b.paths, r.URL.Path)
344 b.mu.Unlock()
345 h.ServeHTTP(w, r)
346 }), closeBackend, nil
349func TestRunUpstreamCustomSumDBURLStaysOnLoopback(t *testing.T) {
350 const name = "sumdb.example"
351 _, sum := writeModuleFixture(t)
352 skey, vkey, err := note.GenerateKey(rand.Reader, name)
353 require.NoError(t, err)
354 db := newUpstream(t, false, sumdb.NewServer(sumdb.NewTestServer(skey, func(string, string) ([]byte, error) { return sum, nil })))
355 up := newUpstream(t, false, moduleFiles(t))
356 isolateGo(t, "")
357 t.Setenv("GOPROXY", up.URL)
358 t.Setenv("GOSUMDB", vkey+" "+db.URL)
359 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
360 backend := &recordingBackend{}
362 r := runUpstreamGo(t, backend.newBackend, "mod", "download", "-json", fixtureModule+"@"+fixtureVersion)
363 require.NoError(t, r.err, r.stdout+r.stderr)
365 assert.Equal(t, vkey, lookupEnv(r.exec.env, "GOSUMDB"), "the child's GOSUMDB must not carry the URL")
366 var lookups int
367 for _, p := range backend.paths {
368 if strings.HasPrefix(p, "/sumdb/"+name+"/lookup/") {
369 lookups++
370 }
371 }
372 assert.Positive(t, lookups, "the child's checksum lookups must go through the loopback server")
373 assert.Zero(t, r.exec.proxied.Load(), "no checksum lookup may reach the MITM proxy")
Diagnostics7 git calls · 280 ms
CallStatusSizeTime0280 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0200361 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=361
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=361
git.pkt.payload.max=334
git.sideband.band1.bytes=334
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=334
git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.3 KiB · hit6 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=6
git.response.bytes=1327
git.response.cache=HIT
git.response.duration_ms=6
git.fetch.filter=tree:0
git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1327
git.pkt.payload.max=1300
git.sideband.band1.bytes=1300
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1300
git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4
Open pack map
fetch2003.5 KiB263 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=263
git.response.bytes=3621
git.response.cache=MISS
git.response.duration_ms=263
git.fetch.wants=34c38d6c2608de839436f0d180e82cde25fa606c
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3621
git.pkt.payload.max=3594
git.sideband.band1.bytes=3594
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e1be8bea126c75
Request
07930e62…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format