GitLabgitlab-org

cli

main
ply7 git calls353 ms99 KiB4 cachedprotocol v2
171 lines5.9 KiBRaw
1package pm
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "os"
10 "slices"
11 "strings"
13 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/gomodproxy"
14 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
17var goFetcherSettings = []string{"GONOPROXY", "GONOSUMDB", "GOPRIVATE", "GOSUMDB", "GOINSECURE"}
19var goSettings = append([]string{"GOPROXY"}, goFetcherSettings...)
21const goNothingCheckedNotice = "No Go module downloads were checked in this run. Modules already in the module cache, vendored code, and local replace targets are not checked."
23type golangManager struct {
24 newBackend func(goBin string, proxiedSumDBs []string, envOverrides map[string]string, upstreamCAs []byte) (http.Handler, func(), error)
25 server *gomodproxy.Server
28// Golang returns the go package manager. Module zips are checked by a
29// loopback Go module proxy rather than the MITM proxy: go and git on macOS
30// trust only the system store, so they reject the MITM CA.
31func Golang() PackageManager { return &golangManager{newBackend: newGoBackend} }
33func newGoBackend(goBin string, proxiedSumDBs []string, envOverrides map[string]string, upstreamCAs []byte) (http.Handler, func(), error) {
34 b, err := gomodproxy.NewBackend(goBin, proxiedSumDBs, envOverrides, upstreamCAs)
35 if err != nil {
36 return nil, nil, err
37 }
38 return b, b.Close, nil
41func (*golangManager) Name() string { return "go" }
42func (*golangManager) Binary() string { return "go" }
44// - GONOPROXY uses the sentinel "none" because go reads the go env file
45// for an empty value.
46// - GO111MODULE=on closes GOPATH-mode go get, which clones over git.
47// - go skips toolchain checksum verification when GIT_HTTP_USER_AGENT
48// mentions proxy.golang.org.
49func (*golangManager) Environment(string) []string {
50 return []string{
51 "GOPROXY=off",
52 "GONOPROXY=none",
53 "GO111MODULE=on",
54 "GIT_HTTP_USER_AGENT=",
55 }
58func (m *golangManager) Setup(ctx context.Context, in setupInput) (setupResult, error) {
59 settings, err := goPreflight(ctx, in.exec, in.bin, in.parent)
60 if err != nil {
61 return setupResult{}, err
62 }
63 if settings["GOPROXY"] == "off" {
64 return setupResult{env: []string{"GOPROXY=off"}, stop: func() { in.io.LogError(goNothingCheckedNotice) }}, nil
65 }
67 overrides := map[string]string{"GOPROXY": settings["GOPROXY"]}
68 // "none" is a pattern that matches nothing, which keeps an empty list
69 // from falling back to GOPRIVATE. GOSUMDB names a database, not a
70 // pattern, so an empty one stays empty and go reports it as missing.
71 for _, k := range goFetcherSettings {
72 v := settings[k]
73 if v == "" && k != "GOSUMDB" {
74 v = "none"
75 }
76 overrides[k] = v
77 }
78 childSumDB, proxied := checksumDB(settings["GOSUMDB"])
79 handler, closeBackend, err := m.newBackend(in.bin, proxied, overrides, in.upstreamCAs)
80 if err != nil {
81 return setupResult{}, fmt.Errorf("failed to create the Go module proxy: %w", err)
82 }
83 server := gomodproxy.New(in.checker, in.projectID, handler)
84 if err := server.Start(); err != nil {
85 closeBackend()
86 return setupResult{}, fmt.Errorf("failed to start the Go module proxy: %w", err)
87 }
88 m.server = server
89 env := []string{"GOPROXY=" + server.BaseURL()}
90 if childSumDB != settings["GOSUMDB"] {
91 env = append(env, "GOSUMDB="+childSumDB)
92 }
93 return setupResult{
94 env: env,
95 stop: func() { //nolint:contextcheck // server shutdown manages its own context internally
96 server.Stop()
97 closeBackend()
98 if server.CheckedZips() == 0 {
99 in.io.LogError(goNothingCheckedNotice)
100 }
101 },
102 verdicts: server.Verdicts,
103 }, nil
106// goPreflight runs go env from an empty directory so no go.mod or go.work
107// applies, with GOTOOLCHAIN=local so it cannot download a toolchain. GOFLAGS
108// is pinned because the user's could carry -changed, which drops every
109// setting still at its default from the output.
110func goPreflight(ctx context.Context, exec Executor, bin string, parent []string) (map[string]string, error) {
111 dir, err := os.MkdirTemp("", "glab-df-go-env-*")
112 if err != nil {
113 return nil, err
114 }
115 defer os.Remove(dir)
117 var stdout, stderr bytes.Buffer
118 args := append([]string{"env", "-C", dir, "-json"}, goSettings...)
119 env := dedupEnv(append(slices.Clip(parent), "GOTOOLCHAIN=local", "GOFLAGS=-changed=false"))
120 if err := exec.ExecWithIO(ctx, bin, args, env, nil, &stdout, &stderr); err != nil {
121 return nil, fmt.Errorf("failed to read the Go settings with go env: %w: %s", err, strings.TrimSpace(stderr.String()))
122 }
123 var settings map[string]string
124 if err := json.Unmarshal(stdout.Bytes(), &settings); err != nil {
125 return nil, fmt.Errorf("failed to parse go env output: %w", err)
126 }
127 return settings, nil
130// checksumDB returns the GOSUMDB the child uses and the server's
131// ProxiedSumDBs entry for it. go looks a database up through the module
132// proxy only when GOSUMDB names no URL; an explicit URL it contacts directly,
133// through the MITM proxy. So the child gets the key alone and the server
134// fetches from the URL. sum.golang.google.cn is go's alias for sum.golang.org
135// served from that host.
136func checksumDB(gosumdb string) (string, []string) {
137 resolved := gosumdb
138 if resolved == "sum.golang.google.cn" {
139 resolved = "sum.golang.org https://sum.golang.google.cn"
140 }
141 fields := strings.Fields(resolved)
142 if len(fields) == 0 || resolved == "off" {
143 return gosumdb, nil
144 }
145 name, _, _ := strings.Cut(fields[0], "+")
146 if len(fields) != 2 {
147 return gosumdb, []string{name}
148 }
149 return fields[0], []string{name + " " + fields[1]}
152func (m *golangManager) checkedZips() int {
153 if m.server == nil {
154 return 0
155 }
156 return m.server.CheckedZips()
159func (*golangManager) CATrustEnviron(caPath string) []string {
160 return []string{"SSL_CERT_FILE=" + caPath}
163func (*golangManager) ExistingBundleVars() []string { return []string{"SSL_CERT_FILE"} }
165func (*golangManager) CleanupCAFiles(string) {}
167func (*golangManager) Matcher() proxy.Matcher { return passThroughMatcher{} }
169type passThroughMatcher struct{}
171func (passThroughMatcher) Match(*http.Request) proxy.Match { return proxy.Match{} }
Diagnostics7 git calls · 353 ms
CallStatusSizeTime0353 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=59849
git.response.cache=UPDATING
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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=4
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=361
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=1327
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.4 KiB341 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=341
git.response.bytes=2470
git.response.cache=MISS
git.response.duration_ms=341
git.fetch.wants=c3252dc431f742383a4c38398f821629362bce0c
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=2470
git.pkt.payload.max=2443
git.sideband.band1.bytes=2443
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
a5e1bac2dbd26c75
Request
a6585ba7…
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