GitLabgitlab-org

cli

main
ply7 git calls308 ms100 KiB5 cachedprotocol v2
254 lines7.9 KiBRaw
1package pm
3import (
4 "encoding/xml"
5 "errors"
6 "fmt"
7 "net"
8 "net/url"
9 "path/filepath"
10 "regexp"
11 "slices"
12 "strconv"
13 "strings"
15 "gitlab.com/gitlab-org/cli/internal/dbg"
18// mavenUpstreamProxy follows Maven 3.9's proxy selection (verified against
19// Maven 3.9.16 and maven-resolver 1.9.27). Maven reads the <proxy> entries of
20// the user settings file, then those of the global settings file whose id the
21// user file does not use, and drops the inactive ones. For an https
22// repository it takes the first https proxy whose nonProxyHosts do not match
23// the host, else the first such http proxy. Other protocols and HTTPS_PROXY
24// are ignored, and Maven always reaches the proxy itself over plain HTTP.
25// It doesn't read .mvn/maven.config, or a settings file inside the project.
26func mavenUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error) {
27 // mvn's launcher puts MAVEN_ARGS ahead of the command line, and Maven
28 // takes the first -s or -gs.
29 cli := append(strings.Fields(lookupEnv(environ, "MAVEN_ARGS")), args...)
30 props := mavenProperties(environ, cli)
32 userFile, userSet := mavenOption(cli, "-s", "--settings", true)
33 if !userSet {
34 userFile = jvmJoin(javaUserHome(environ, props), ".m2", "settings.xml")
35 }
36 globalFile, globalSet := mavenOption(cli, "-gs", "--global-settings", false)
37 if !globalSet {
38 globalFile = mavenGlobalSettings(environ, props)
39 }
41 files := newConfigFiles(dir)
42 var proxies []mavenProxy
43 ids := map[string]struct{}{}
44 for _, f := range []struct {
45 path string
46 explicit bool
47 }{{userFile, userSet}, {globalFile, globalSet}} {
48 if f.path == "" {
49 continue
50 }
51 fileProxies, err := readMavenSettings(files, f.path, f.explicit, environ, props)
52 if err != nil {
53 return upstreamProxy{}, false, err
54 }
55 var added []string
56 for _, p := range fileProxies {
57 if _, dup := ids[p.ID]; !dup {
58 proxies = append(proxies, p)
59 added = append(added, p.ID)
60 }
61 }
62 for _, id := range added {
63 ids[id] = struct{}{}
64 }
65 }
67 var https, http []upstreamProxy
68 for _, p := range proxies {
69 if !p.active() {
70 continue
71 }
72 protocol := strings.ToLower(p.Protocol)
73 if protocol != "https" && protocol != "http" {
74 continue
75 }
76 upstream, err := p.upstream()
77 if err != nil {
78 return upstreamProxy{}, false, fmt.Errorf("%s: %w", p.source, err)
79 }
80 if protocol == "https" {
81 https = append(https, upstream)
82 } else {
83 http = append(http, upstream)
84 }
85 }
86 candidates := slices.Concat(https, http)
87 if len(candidates) == 0 {
88 return upstreamProxy{}, false, nil
89 }
90 first := candidates[0]
91 first.Fallbacks = candidates[1:]
92 if len(first.Fallbacks) == 0 {
93 first.Fallbacks = nil
94 }
95 return first, true, nil
98// mavenProxy is a <proxy> entry of a Maven settings file, its values trimmed
99// and interpolated as Maven does.
100type mavenProxy struct {
101 ID string `xml:"id"`
102 Active *string `xml:"active"`
103 Protocol string `xml:"protocol"`
104 Username string `xml:"username"`
105 Password string `xml:"password"`
106 Host string `xml:"host"`
107 Port *string `xml:"port"`
108 NonProxyHosts string `xml:"nonProxyHosts"`
110 source string
113// mavenEncryptedRe matches a value Maven passes to its settings decrypter,
114// the pattern plexus-cipher 2.0 uses to recognise "{...}".
115var mavenEncryptedRe = regexp.MustCompile(`\{.*?[^\\]\}`)
117func (p mavenProxy) active() bool {
118 return p.Active == nil || strings.EqualFold(*p.Active, "true")
121func (p mavenProxy) upstream() (upstreamProxy, error) {
122 port := "8080"
123 if p.Port != nil {
124 if _, err := strconv.ParseUint(*p.Port, 10, 16); err != nil {
125 return upstreamProxy{}, errors.New("the proxy port is not a number")
126 }
127 port = *p.Port
128 }
129 if p.Host == "" {
130 return upstreamProxy{}, errors.New("the proxy has no host")
131 }
132 u := url.URL{Scheme: "http", Host: net.JoinHostPort(p.Host, port)}
133 if p.Username != "" || p.Password != "" {
134 if mavenEncryptedRe.MatchString(p.Password) {
135 return upstreamProxy{}, errors.New("the proxy password is encrypted, which glab does not support. Use the plain-text password, for example from an environment variable with ${env.NAME}")
136 }
137 u.User = url.UserPassword(p.Username, p.Password)
138 }
139 var nonProxyHosts []string
140 for h := range strings.SplitSeq(p.NonProxyHosts, "|") {
141 if h != "" {
142 nonProxyHosts = append(nonProxyHosts, h)
143 }
144 }
145 return upstreamProxy{Source: p.source, URL: u.String(), NonProxyHosts: nonProxyHosts}, nil
148// readMavenSettings returns the <proxy> entries of the settings file at path,
149// or none when it is missing or inside the project. A file named with -s or
150// -gs must exist, as Maven requires.
151func readMavenSettings(files configFiles, path string, explicit bool, environ []string, props map[string]string) ([]mavenProxy, error) {
152 if explicit && !exists(path) {
153 return nil, fmt.Errorf("the settings file %s does not exist", path)
154 }
155 data, ok, err := files.read(path)
156 if !ok {
157 return nil, err
158 }
159 return mavenProxiesIn(path, data, environ, props)
162func mavenProxiesIn(path string, data []byte, environ []string, props map[string]string) ([]mavenProxy, error) {
163 var settings struct {
164 Proxies []struct {
165 Proxy []mavenProxy `xml:"proxy"`
166 } `xml:"proxies"`
167 }
168 if err := xml.Unmarshal(data, &settings); err != nil {
169 return nil, parseError(path, err)
170 }
171 if len(settings.Proxies) > 1 {
172 return nil, fmt.Errorf("failed to parse %s: duplicate <proxies> element", path)
173 }
174 if len(settings.Proxies) == 0 {
175 return nil, nil
176 }
177 proxies := settings.Proxies[0].Proxy
178 for i := range proxies {
179 p := &proxies[i]
180 // Maven reads a numeric or boolean value before it interpolates the
181 // file, so only the string values expand ${...}.
182 expand := func(s string) string { return mavenInterpolate(strings.TrimSpace(s), environ, props) }
183 p.ID = strings.TrimSpace(p.ID)
184 if p.ID == "" {
185 p.ID = "default"
186 }
187 if p.Active != nil {
188 *p.Active = strings.TrimSpace(*p.Active)
189 }
190 if p.Port != nil {
191 *p.Port = strings.TrimSpace(*p.Port)
192 }
193 p.Protocol = expand(p.Protocol)
194 if p.Protocol == "" {
195 p.Protocol = "http"
196 }
197 p.Username = expand(p.Username)
198 p.Password = expand(p.Password)
199 p.Host = expand(p.Host)
200 p.NonProxyHosts = expand(p.NonProxyHosts)
201 p.source = fileSource(path, "proxy "+p.ID)
202 }
203 return proxies, nil
206var mavenExprRe = regexp.MustCompile(`\$\{([^}]+)\}`)
208// mavenInterpolate expands ${env.NAME}, ${user.home} and the -D properties in
209// a settings value. Maven leaves an expression it cannot resolve as it is.
210func mavenInterpolate(s string, environ []string, props map[string]string) string {
211 return mavenExprRe.ReplaceAllStringFunc(s, func(expr string) string {
212 name := expr[2 : len(expr)-1]
213 if env, ok := strings.CutPrefix(name, "env."); ok {
214 if v, ok := envValue(environ, env); ok {
215 return v
216 }
217 return expr
218 }
219 if name == "user.home" {
220 return javaUserHome(environ, props)
221 }
222 if v, ok := props[name]; ok {
223 return v
224 }
225 return expr
226 })
229// jvmJoin joins names to dir as java.io.File does, without cleaning, so that
230// configFiles.read applies a ".." after a symlink in dir as the OS does.
231func jvmJoin(dir string, names ...string) string {
232 return strings.TrimRight(dir, string(filepath.Separator)) + string(filepath.Separator) + filepath.Join(names...)
235// mavenGlobalSettings returns conf/settings.xml of the Maven installation
236// that runs: -Dmaven.conf, else the installation holding the mvn found on
237// PATH. The mvn launcher derives its home from its own location and ignores
238// MAVEN_HOME.
239func mavenGlobalSettings(environ []string, props map[string]string) string {
240 if conf, ok := props["maven.conf"]; ok {
241 return jvmJoin(conf, "settings.xml")
242 }
243 bin, ok := lookPathIn(environ, "mvn")
244 if !ok {
245 dbg.Debugf("dependency firewall: not reading the maven global settings, as no mvn is on PATH")
246 return ""
247 }
248 home, _, err := mavenHome(bin)
249 if err != nil {
250 dbg.Debugf("dependency firewall: not reading the maven global settings, as the installation of %s is unknown: %v", bin, err)
251 return ""
252 }
253 return filepath.Join(home, "conf", "settings.xml")
Diagnostics7 git calls · 308 ms
CallStatusSizeTime0308 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 KiB · hit4 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=HIT
git.response.duration_ms=4
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 · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=1327
git.response.cache=HIT
git.response.duration_ms=1
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.0 KiB297 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=297
git.response.bytes=3119
git.response.cache=MISS
git.response.duration_ms=297
git.fetch.wants=04e601ea74aeaf6ba3e4058ce9a92646c63c1153
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=3119
git.pkt.payload.max=3092
git.sideband.band1.bytes=3092
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
a5e1ba651bcb6c75
Request
1fd3d2ba…
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