GitLabgitlab-org

cli

main
ply7 git calls225 ms103 KiB4 cachedprotocol v2
672 lines28 KiBRaw
1//go:build !integration
3package pm
5import (
6 "context"
7 "crypto/tls"
8 "crypto/x509"
9 "errors"
10 "fmt"
11 "io"
12 "net/http"
13 "net/http/httptest"
14 "net/url"
15 "os"
16 "os/user"
17 "path/filepath"
18 "strings"
19 "testing"
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
24 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
27const mavenSettingsFmt = "<settings xmlns=\"http://maven.apache.org/SETTINGS/1.0.0\"><proxies>%s</proxies></settings>"
29func mavenSettingsXML(proxies ...string) string {
30 return fmt.Sprintf(mavenSettingsFmt, strings.Join(proxies, ""))
33// mavenInstall adds the files of a Maven 3.9 installation at home, which
34// mavenHome accepts, to files.
35func mavenInstall(home string, files map[string]string) map[string]string {
36 files[home+"/bin/mvn"] = ""
37 files[home+"/bin/m2.conf"] = stockM2Conf
38 files[home+"/lib/maven-core-3.9.12.jar"] = ""
39 return files
42// withUserHome points Java's user.home at the temp root, as the JVM reads the
43// account's home directory rather than HOME.
44func withUserHome(cases []configCase, env string) []configCase {
45 for i := range cases {
46 cases[i].env = append([]string{env}, cases[i].env...)
47 }
48 return cases
51func TestMavenUpstreamProxyFromSettings(t *testing.T) {
52 t.Parallel()
53 corp := "<proxy><id>corp</id><protocol>https</protocol><host>corp</host><port>3128</port></proxy>"
54 other := "<proxy><id>other</id><protocol>https</protocol><host>other</host><port>1</port></proxy>"
55 runConfigCases(t, Maven(), withUserHome([]configCase{
56 {
57 name: "an active proxy in ~/.m2/settings.xml",
58 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML(corp)},
59 want: upstreamProxy{Source: "$ROOT/home/.m2/settings.xml (proxy corp)", URL: "http://corp:3128"},
60 wantOK: true,
61 },
62 {
63 name: "an http proxy without a port serves https on 8080",
64 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><host> corp </host></proxy>")},
65 want: upstreamProxy{Source: "$ROOT/home/.m2/settings.xml (proxy default)", URL: "http://corp:8080"},
66 wantOK: true,
67 },
68 {
69 name: "an https proxy wins over an earlier http one, which serves its non-proxy hosts",
70 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><id>plain</id><host>plain</host><port>2</port></proxy>", "<proxy><id>corp</id><protocol>HTTPS</protocol><host>corp</host><port>3128</port><nonProxyHosts>*.internal.example|repo.maven.apache.org||</nonProxyHosts></proxy>")},
71 want: upstreamProxy{
72 Source: "$ROOT/home/.m2/settings.xml (proxy corp)", URL: "http://corp:3128",
73 NonProxyHosts: []string{"*.internal.example", "repo.maven.apache.org"},
74 Fallbacks: []upstreamProxy{{Source: "$ROOT/home/.m2/settings.xml (proxy plain)", URL: "http://plain:2"}},
75 },
76 wantOK: true,
77 },
78 {
79 name: "inactive and non-http proxies are skipped",
80 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><id>off</id><active>yes</active><host>off</host></proxy>", "<proxy><id>socks</id><protocol>socks5</protocol><host>socks</host></proxy>")},
81 env: []string{"HTTPS_PROXY=http://env:1"},
82 wantOK: false,
83 },
84 {
85 name: "-s replaces ~/.m2/settings.xml",
86 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML(other), "ci/settings.xml": mavenSettingsXML(corp)},
87 args: []string{"-B", "-s", "$ROOT/ci/settings.xml", "package"},
88 want: upstreamProxy{Source: "$ROOT/ci/settings.xml (proxy corp)", URL: "http://corp:3128"},
89 wantOK: true,
90 },
91 {
92 name: "--settings=file and the first settings option in MAVEN_ARGS win",
93 files: map[string]string{"a.xml": mavenSettingsXML(corp), "b.xml": mavenSettingsXML(other)},
94 env: []string{"MAVEN_ARGS=--settings=$ROOT/a.xml"},
95 args: []string{"-s$ROOT/b.xml"},
96 want: upstreamProxy{Source: "$ROOT/a.xml (proxy corp)", URL: "http://corp:3128"},
97 wantOK: true,
98 },
99 {
100 name: "the project's .mvn/maven.config is not read",
101 files: map[string]string{"proj/.mvn/maven.config": "-s\n$ROOT/a.xml\n", "a.xml": mavenSettingsXML(corp), "home/.m2/settings.xml": mavenSettingsXML(other)},
102 want: upstreamProxy{Source: "$ROOT/home/.m2/settings.xml (proxy other)", URL: "http://other:1"},
103 wantOK: true,
104 },
105 {
106 name: "an ancestor's .mvn/maven.config is not read",
107 files: map[string]string{".mvn/maven.config": "-s\n$ROOT/a.xml\n", "a.xml": mavenSettingsXML(corp), "home/.m2/settings.xml": mavenSettingsXML(other)},
108 dir: "proj/module",
109 want: upstreamProxy{Source: "$ROOT/home/.m2/settings.xml (proxy other)", URL: "http://other:1"},
110 wantOK: true,
111 },
112 {
113 name: "the global settings of the mvn on PATH apply",
114 files: mavenInstall("maven", map[string]string{"maven/conf/settings.xml": mavenSettingsXML(corp)}),
115 env: []string{"PATH=$ROOT/maven/bin"},
116 want: upstreamProxy{Source: "$ROOT/maven/conf/settings.xml (proxy corp)", URL: "http://corp:3128"},
117 wantOK: true,
118 },
119 {
120 name: "Homebrew's mvn takes the global settings from libexec",
121 files: mavenInstall("brew/Cellar/maven/3.9.12/libexec", map[string]string{
122 "brew/Cellar/maven/3.9.12/bin/mvn": "",
123 "brew/Cellar/maven/3.9.12/libexec/conf/settings.xml": mavenSettingsXML(corp),
124 }),
125 env: []string{"PATH=$ROOT/brew/Cellar/maven/3.9.12/bin"},
126 want: upstreamProxy{Source: "$ROOT/brew/Cellar/maven/3.9.12/libexec/conf/settings.xml (proxy corp)", URL: "http://corp:3128"},
127 wantOK: true,
128 },
129 {
130 name: "user proxies come first, and replace global ones with the same id",
131 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML(other, "<proxy><id>corp</id><active>false</active><host>off</host></proxy>"), "global.xml": mavenSettingsXML(corp, "<proxy><id>third</id><protocol>https</protocol><host>third</host><port>3</port></proxy>")},
132 args: []string{"-gs", "$ROOT/global.xml"},
133 want: upstreamProxy{
134 Source: "$ROOT/home/.m2/settings.xml (proxy other)", URL: "http://other:1",
135 Fallbacks: []upstreamProxy{{Source: "$ROOT/global.xml (proxy third)", URL: "http://third:3"}},
136 },
137 wantOK: true,
138 },
139 {
140 name: "-Dmaven.conf names the global settings directory",
141 files: map[string]string{"conf/settings.xml": mavenSettingsXML(corp)},
142 args: []string{"-Dmaven.conf=$ROOT/conf"},
143 want: upstreamProxy{Source: "$ROOT/conf/settings.xml (proxy corp)", URL: "http://corp:3128"},
144 wantOK: true,
145 },
146 {
147 name: "-Duser.home on the command line wins over MAVEN_OPTS",
148 files: map[string]string{"other/.m2/settings.xml": mavenSettingsXML(corp)},
149 args: []string{"-D", "user.home=$ROOT/other"},
150 want: upstreamProxy{Source: "$ROOT/other/.m2/settings.xml (proxy corp)", URL: "http://corp:3128"},
151 wantOK: true,
152 },
153 {
154 name: "values expand environment variables and properties, and carry credentials",
155 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><host>${env.PROXY_HOST}</host><port>3128</port><username>${proxy.user}</username><password>${env.PROXY_PASSWORD}</password></proxy>")},
156 env: []string{"PROXY_HOST=corp", "PROXY_PASSWORD=p@ss"},
157 args: []string{"-Dproxy.user=alice"},
158 want: upstreamProxy{Source: "$ROOT/home/.m2/settings.xml (proxy default)", URL: "http://alice:p%40ss@corp:3128"},
159 wantOK: true,
160 },
161 {
162 name: "an encrypted password fails",
163 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><host>corp</host><username>alice</username><password>{COQLCE6DU6GtcS5P=}</password></proxy>")},
164 wantErr: "the proxy password is encrypted, which glab does not support",
165 },
166 {
167 name: "a settings file named with -s must exist",
168 args: []string{"-s", "$ROOT/missing.xml"},
169 wantErr: "missing.xml does not exist",
170 },
171 {
172 name: "malformed XML fails as in Maven",
173 files: map[string]string{"home/.m2/settings.xml": "<settings><proxies><proxy>"},
174 wantErr: "failed to parse",
175 },
176 {
177 name: "a port that is not a number fails",
178 files: map[string]string{"home/.m2/settings.xml": mavenSettingsXML("<proxy><host>corp</host><port>${env.PORT}</port></proxy>")},
179 wantErr: "the proxy port is not a number",
180 },
181 {
182 name: "an empty -Dmaven.conf puts the global settings at the filesystem root, not the installation",
183 files: mavenInstall("maven", map[string]string{"maven/conf/settings.xml": mavenSettingsXML(corp)}),
184 env: []string{"PATH=$ROOT/maven/bin"},
185 args: []string{"-Dmaven.conf="},
186 },
187 {
188 name: "-s inside the project is not read, and the global settings still apply",
189 files: map[string]string{"proj/ci/settings.xml": mavenSettingsXML(corp), "home/.m2/settings.xml": mavenSettingsXML(corp), "global.xml": mavenSettingsXML(other)},
190 args: []string{"-s", "$ROOT/proj/ci/settings.xml", "-gs", "$ROOT/global.xml"},
191 want: upstreamProxy{Source: "$ROOT/global.xml (proxy other)", URL: "http://other:1"},
192 wantOK: true,
193 },
194 {
195 name: "-gs inside the project is not read",
196 files: map[string]string{"proj/global.xml": mavenSettingsXML(corp)},
197 args: []string{"-gs", "$ROOT/proj/global.xml"},
198 },
199 {
200 name: "a user.home inside the project is not read",
201 files: map[string]string{"proj/home/.m2/settings.xml": mavenSettingsXML(corp)},
202 args: []string{"-Duser.home=$ROOT/proj/home"},
203 },
204 {
205 name: "a maven.conf inside the project is not read",
206 files: map[string]string{"proj/conf/settings.xml": mavenSettingsXML(corp)},
207 args: []string{"-Dmaven.conf=$ROOT/proj/conf"},
208 },
209 {
210 name: "the global settings of an mvn inside the project are not read",
211 files: mavenInstall("proj/maven", map[string]string{"proj/maven/conf/settings.xml": mavenSettingsXML(corp)}),
212 env: []string{"PATH=$ROOT/proj/maven/bin"},
213 },
214 {
215 name: "a settings file in the git work tree is not read from a subdirectory",
216 files: map[string]string{"proj/.git/HEAD": "", "proj/ci.xml": mavenSettingsXML(corp)},
217 dir: "proj/sub",
218 env: []string{"MAVEN_ARGS=-s $ROOT/proj/ci.xml"},
219 },
220 }, "MAVEN_OPTS=-Duser.home=$ROOT/home"))
223func TestGradleUpstreamProxyFromProperties(t *testing.T) {
224 t.Parallel()
225 runConfigCases(t, Gradle(), withUserHome([]configCase{
226 {
227 name: "the Gradle user home's gradle.properties, with credentials and non-proxy hosts",
228 files: map[string]string{
229 "guh/gradle.properties": "systemProp.https.proxyHost=corp\nsystemProp.https.proxyPort : 3128\nsystemProp.https.proxyUser alice\nsystemProp.https.proxyPassword=s3cr\\\n et\nsystemProp.http.nonProxyHosts=*.internal.example|repo.*.org|localhost\n",
230 },
231 want: upstreamProxy{
232 Source: "$ROOT/guh/gradle.properties (systemProp.https.proxyHost)", URL: "http://alice:s3cret@corp:3128",
233 NonProxyHosts: []string{"*.internal.example", "localhost"},
234 },
235 wantOK: true,
236 },
237 {
238 name: "the Gradle user home wins over the installation, key by key",
239 files: map[string]string{
240 "guh/gradle.properties": "systemProp.https.proxyHost=user\n",
241 "gradle/bin/gradle": "",
242 "gradle/gradle.properties": "systemProp.https.proxyHost=installation\nsystemProp.https.proxyPort=3128\n",
243 },
244 env: []string{"PATH=$ROOT/gradle/bin"},
245 want: upstreamProxy{Source: "$ROOT/guh/gradle.properties (systemProp.https.proxyHost)", URL: "http://user:3128"},
246 wantOK: true,
247 },
248 {
249 name: "-D on the command line wins, and the port defaults to 443",
250 files: map[string]string{"guh/gradle.properties": "systemProp.https.proxyHost=user\nsystemProp.https.proxyPort=3128\n"},
251 args: []string{"build", "--system-prop", "https.proxyPort=1", "-Dhttps.proxyHost=cli", "-Dhttps.proxyPort=443"},
252 want: upstreamProxy{Source: "-Dhttps.proxyHost", URL: "http://cli:443"},
253 wantOK: true,
254 },
255 {
256 name: "an empty https.proxyHost connects directly",
257 files: map[string]string{"guh/gradle.properties": "systemProp.https.proxyHost=\n"},
258 env: []string{"HTTPS_PROXY=http://env:1"},
259 want: upstreamProxy{Source: "$ROOT/guh/gradle.properties (systemProp.https.proxyHost)"},
260 wantOK: true,
261 },
262 {
263 name: "http.proxyHost alone does not serve https",
264 files: map[string]string{"guh/gradle.properties": "systemProp.http.proxyHost=plain\nsystemProp.https.proxyPort=3128\n"},
265 wantOK: false,
266 },
267 {
268 name: "-g chooses the Gradle user home",
269 files: map[string]string{"other/gradle.properties": "systemProp.https.proxyHost=\\u0063orp\nsystemProp.https.proxyPort=3128\n", "guh/gradle.properties": "systemProp.https.proxyHost=user\n"},
270 args: []string{"-g", "$ROOT/other"},
271 want: upstreamProxy{Source: "$ROOT/other/gradle.properties (systemProp.https.proxyHost)", URL: "http://corp:3128"},
272 wantOK: true,
273 },
274 {
275 name: "an empty -g counts as unset",
276 files: map[string]string{"guh/gradle.properties": "systemProp.https.proxyHost=user\n"},
277 args: []string{"-g", ""},
278 want: upstreamProxy{Source: "$ROOT/guh/gradle.properties (systemProp.https.proxyHost)", URL: "http://user:443"},
279 wantOK: true,
280 },
281 {
282 name: "the installation's gradle.properties applies",
283 files: map[string]string{"gradle/bin/gradle": "", "gradle/gradle.properties": "systemProp.https.proxyHost=corp\n# comment\n! comment\n"},
284 env: []string{"PATH=$ROOT/gradle/bin"},
285 want: upstreamProxy{Source: "$ROOT/gradle/gradle.properties (systemProp.https.proxyHost)", URL: "http://corp:443"},
286 wantOK: true,
287 },
288 {
289 name: "a port that is not a number fails",
290 files: map[string]string{"guh/gradle.properties": "systemProp.https.proxyHost=corp\nsystemProp.https.proxyPort=31x8\n"},
291 wantErr: "https.proxyPort is not a number",
292 },
293 {
294 name: "the project's gradle.properties is not read",
295 files: map[string]string{"proj/gradle.properties": "systemProp.https.proxyHost=project\n"},
296 },
297 {
298 name: "the root project's gradle.properties is not read from a subproject",
299 files: map[string]string{"proj/settings.gradle.kts": "", "proj/gradle.properties": "systemProp.https.proxyHost=project\n"},
300 dir: "proj/app",
301 },
302 {
303 name: "a GRADLE_USER_HOME inside the project is not read",
304 files: map[string]string{"proj/.gradle/gradle.properties": "systemProp.https.proxyHost=project\n"},
305 env: []string{"GRADLE_USER_HOME=$ROOT/proj/.gradle"},
306 },
307 {
308 name: "-g inside the project is not read",
309 files: map[string]string{"proj/.gradle/gradle.properties": "systemProp.https.proxyHost=project\n"},
310 args: []string{"--gradle-user-home=$ROOT/proj/.gradle"},
311 },
312 {
313 name: "-Dgradle.user.home inside the project is not read",
314 files: map[string]string{"proj/.gradle/gradle.properties": "systemProp.https.proxyHost=project\n"},
315 args: []string{"-Dgradle.user.home=$ROOT/proj/.gradle"},
316 },
317 {
318 name: "an empty -Dgradle.user.home does not fall back to GRADLE_USER_HOME",
319 files: map[string]string{"proj/gradle.properties": "systemProp.https.proxyHost=project\n", "guh/gradle.properties": "systemProp.https.proxyHost=user\n"},
320 args: []string{"-Dgradle.user.home="},
321 },
322 {
323 name: "the gradle.properties of a gradle inside the project is not read",
324 files: map[string]string{"proj/gradle/bin/gradle": "", "proj/gradle/gradle.properties": "systemProp.https.proxyHost=project\n"},
325 env: []string{"PATH=$ROOT/proj/gradle/bin"},
326 },
327 }, "GRADLE_USER_HOME=$ROOT/guh"))
330func TestJVMConfigPathsThatResolveInsideTheProjectAreNotRead(t *testing.T) {
331 root, err := filepath.EvalSymlinks(t.TempDir())
332 require.NoError(t, err)
333 project := mavenSettingsXML("<proxy><id>project</id><host>project</host><port>1</port></proxy>")
334 outside := mavenSettingsXML("<proxy><id>outside</id><host>outside</host><port>2</port></proxy>")
335 writeConfigFiles(t, root, map[string]string{
336 "proj/.git/HEAD": "",
337 "proj/sub/ci.xml": project,
338 "proj/sub/home/.m2/settings.xml": project,
339 "proj/sub/conf/settings.xml": project,
340 "proj/sub/.gradle/gradle.properties": "systemProp.https.proxyHost=project\n",
341 "proj/d/e/.keep": "",
342 "proj/decoy.xml": project,
343 "decoy.xml": outside,
344 "proj/.m2/settings.xml": project,
345 ".m2/settings.xml": outside,
346 "proj/gradle.properties": "systemProp.https.proxyHost=project\n",
347 "gradle.properties": "systemProp.https.proxyHost=outside\n",
348 "proj/x.xml": project,
349 "outside/x.xml": outside,
350 "outside.xml": outside,
351 "guh/gradle.properties": "systemProp.https.proxyHost=outside\n",
352 }, func(s string) string { return s })
353 require.NoError(t, os.Symlink(filepath.Join("d", "e"), filepath.Join(root, "proj", "l")))
354 require.NoError(t, os.Symlink(filepath.Join(root, "proj", "sub"), filepath.Join(root, "outside", "link")))
355 // Not filepath.Join, which would drop the ".." that the OS applies after
356 // the symlink: this names root/proj, not root.
357 throughLink := filepath.Join(root, "proj", "l") + filepath.FromSlash("/../..")
358 userHome := "MAVEN_OPTS=-Duser.home=" + filepath.Join(root, "home")
359 gradleHome := "GRADLE_USER_HOME=" + filepath.Join(root, "home", ".gradle")
361 tests := []struct {
362 name string
363 manager PackageManager
364 cwd string
365 env []string
366 args []string
367 want upstreamProxy
368 wantOK bool
369 }{
370 {name: "Maven -s", manager: Maven(), env: []string{userHome}, args: []string{"-s", "ci.xml"}},
371 {name: "Maven -gs", manager: Maven(), env: []string{userHome}, args: []string{"-gs", "ci.xml"}},
372 {name: "Maven user.home", manager: Maven(), env: []string{"MAVEN_OPTS=-Duser.home=home"}},
373 {name: "Maven maven.conf", manager: Maven(), env: []string{userHome}, args: []string{"-Dmaven.conf=conf"}},
374 {name: "Maven -s with .. after a symlink", manager: Maven(), env: []string{userHome}, args: []string{"-s", throughLink + filepath.FromSlash("/decoy.xml")}},
375 {name: "Maven user.home with .. after a symlink", manager: Maven(), env: []string{"MAVEN_OPTS=-Duser.home=" + throughLink}},
376 {name: "Maven -s with .. from a symlinked working directory", manager: Maven(), cwd: "outside/link", env: []string{userHome}, args: []string{"-s", filepath.FromSlash("../x.xml")}},
377 {name: "Gradle GRADLE_USER_HOME", manager: Gradle(), env: []string{"GRADLE_USER_HOME=.gradle"}},
378 {name: "Gradle -g", manager: Gradle(), env: []string{gradleHome}, args: []string{"-g", ".gradle"}},
379 {name: "Gradle gradle.user.home", manager: Gradle(), env: []string{gradleHome}, args: []string{"-Dgradle.user.home=.gradle"}},
380 {name: "Gradle GRADLE_USER_HOME with .. after a symlink", manager: Gradle(), env: []string{"GRADLE_USER_HOME=" + throughLink}},
381 {
382 name: "Maven -s outside the project",
383 manager: Maven(),
384 env: []string{userHome},
385 args: []string{"-s", filepath.FromSlash("../../outside.xml")},
386 want: upstreamProxy{Source: filepath.FromSlash("../../outside.xml") + " (proxy outside)", URL: "http://outside:2"},
387 wantOK: true,
388 },
389 {
390 name: "Gradle GRADLE_USER_HOME outside the project",
391 manager: Gradle(),
392 env: []string{"GRADLE_USER_HOME=" + filepath.FromSlash("../../guh")},
393 want: upstreamProxy{Source: filepath.FromSlash("../../guh/gradle.properties") + " (systemProp.https.proxyHost)", URL: "http://outside:443"},
394 wantOK: true,
395 },
396 }
397 for _, tt := range tests {
398 t.Run(tt.name, func(t *testing.T) {
399 dir := filepath.Join(root, "proj", "sub")
400 if tt.cwd != "" {
401 dir = filepath.Join(root, filepath.FromSlash(tt.cwd))
402 }
403 t.Chdir(dir)
404 resolver, ok := tt.manager.(upstreamProxyResolver)
405 require.True(t, ok)
407 got, gotOK, err := resolver.resolveUpstreamProxy(tt.env, dir, tt.args)
408 require.NoError(t, err)
409 assert.Equal(t, tt.wantOK, gotOK)
410 assert.Equal(t, tt.want, got)
411 })
412 }
415// Not parallel: it swaps the account home that TestMain sets.
416func TestEmptyJVMUserHomeIsNotTheAccountHome(t *testing.T) {
417 home := t.TempDir()
418 writeConfigFiles(t, home, map[string]string{
419 ".m2/settings.xml": mavenSettingsXML("<proxy><id>account</id><host>account</host><port>1</port></proxy>"),
420 ".gradle/gradle.properties": "systemProp.https.proxyHost=account\n",
421 }, func(s string) string { return s })
422 saved := currentUser
423 currentUser = func() (*user.User, error) { return &user.User{HomeDir: home}, nil }
424 t.Cleanup(func() { currentUser = saved })
426 tests := []struct {
427 name string
428 manager PackageManager
429 env []string
430 args []string
431 }{
432 {name: "Maven, empty -Duser.home", manager: Maven(), args: []string{"-Duser.home=", "verify"}},
433 {name: "Gradle, empty GRADLE_USER_HOME", manager: Gradle(), env: []string{"GRADLE_USER_HOME="}},
434 {name: "Gradle, empty -Dgradle.user.home", manager: Gradle(), args: []string{"-Dgradle.user.home="}},
435 }
436 for _, tt := range tests {
437 t.Run(tt.name, func(t *testing.T) {
438 resolver, ok := tt.manager.(upstreamProxyResolver)
439 require.True(t, ok)
441 got, gotOK, err := resolver.resolveUpstreamProxy(tt.env, t.TempDir(), tt.args)
442 require.NoError(t, err)
443 assert.False(t, gotOK)
444 assert.Equal(t, upstreamProxy{}, got)
445 })
446 }
449func TestJVMConfigErrorsNameTheFileWithoutQuotingIt(t *testing.T) {
450 t.Parallel()
451 tests := []struct {
452 name string
453 manager PackageManager
454 files map[string]string
455 env string
456 // want is the whole error, with $ROOT for the temp root.
457 want string
458 }{
459 {
460 name: "an entity in settings.xml",
461 manager: Maven(),
462 files: map[string]string{"home/.m2/settings.xml": "<settings>\n<proxies><proxy><password>a&s3cret;</password></proxy></proxies></settings>\n"},
463 env: "MAVEN_OPTS=-Duser.home=$ROOT/home",
464 want: "failed to parse $ROOT/home/.m2/settings.xml line 2",
465 },
466 {
467 name: "a bad escape in gradle.properties",
468 manager: Gradle(),
469 files: map[string]string{"guh/gradle.properties": "systemProp.https.proxyPassword=s3cret\\u00zz\n"},
470 env: "GRADLE_USER_HOME=$ROOT/guh",
471 want: "failed to parse $ROOT/guh/gradle.properties: line 1: malformed \\uxxxx escape",
472 },
473 }
474 for _, tt := range tests {
475 t.Run(tt.name, func(t *testing.T) {
476 t.Parallel()
477 root, err := filepath.EvalSymlinks(t.TempDir())
478 require.NoError(t, err)
479 writeConfigFiles(t, root, tt.files, func(s string) string { return s })
480 dir := filepath.Join(root, "proj")
481 require.NoError(t, os.MkdirAll(dir, 0o755))
482 resolver, ok := tt.manager.(upstreamProxyResolver)
483 require.True(t, ok)
485 _, _, err = resolver.resolveUpstreamProxy([]string{strings.ReplaceAll(tt.env, "$ROOT", root)}, dir, nil)
486 require.EqualError(t, err, strings.ReplaceAll(tt.want, "$ROOT", root))
487 })
488 }
491func TestMatchesHostPattern(t *testing.T) {
492 t.Parallel()
493 tests := []struct {
494 pattern, host string
495 want bool
496 }{
497 {"repo.maven.apache.org", "REPO.maven.apache.org", true},
498 {"*.apache.org", "repo.maven.apache.org", true},
499 {"*.apache.org", "apache.org", false},
500 {".apache.org", "repo.apache.org", false},
501 {"repo.*.org", "repo.maven.apache.org", true},
502 {"*maven*", "repo.maven.apache.org", true},
503 {"10.*", "10.1.2.3", true},
504 {"*", "anything", true},
505 {"repo.maven.apache.org:443", "repo.maven.apache.org", false},
506 }
507 for _, tt := range tests {
508 t.Run(tt.pattern+" vs "+tt.host, func(t *testing.T) {
509 t.Parallel()
510 assert.Equal(t, tt.want, matchesHostPattern([]string{tt.pattern}, tt.host))
511 })
512 }
515func TestUpstreamProxyFuncFallsBackForNonProxyHosts(t *testing.T) {
516 t.Parallel()
517 pick, err := upstreamProxy{
518 URL: "http://corp:3128",
519 NonProxyHosts: []string{"*.internal.example", "direct.example"},
520 Fallbacks: []upstreamProxy{{URL: "http://plain:8080", NonProxyHosts: []string{"direct.example"}}},
521 }.proxyFunc()
522 require.NoError(t, err)
524 for host, want := range map[string]string{
525 "repo.maven.apache.org": "http://corp:3128",
526 "nexus.internal.example": "http://plain:8080",
527 "direct.example": "",
528 "DIRECT.example": "",
529 "internal.example.evil.x": "http://corp:3128",
530 } {
531 got, err := pick(httptest.NewRequest(http.MethodGet, "https://"+host+"/x", http.NoBody))
532 require.NoError(t, err)
533 if want == "" {
534 assert.Nil(t, got, host)
535 } else {
536 assert.Equal(t, want, got.String(), host)
537 }
538 }
541func TestUpstreamProxyFuncRejectsBadFallback(t *testing.T) {
542 t.Parallel()
543 _, err := upstreamProxy{Source: "a", URL: "http://corp:3128", Fallbacks: []upstreamProxy{{Source: "b", URL: "ftp://u:s3cret@x:21"}}}.proxyFunc()
544 require.ErrorContains(t, err, "invalid outbound proxy in b")
545 assert.NotContains(t, err.Error(), "s3cret")
548// isolateMaven keeps the developer's own config and proxy variables out of a
549// Maven Run test, so Run reads only the settings files the test names.
550func isolateMaven(t *testing.T) {
551 t.Helper()
552 t.Setenv("GITLAB_CI", "") // honor fake mode even when the suite runs in CI
553 t.Setenv("GLAB_DF_FAKE_DEFAULT", "allow")
554 isolateUserConfig(t, t.TempDir())
555 clearProxyEnv(t)
558func TestRunSendsMavenTrafficThroughProxyFromSettings(t *testing.T) {
559 isolateMaven(t)
560 corporate := &refusingProxy{}
561 server := httptest.NewServer(corporate)
562 t.Cleanup(server.Close)
563 corporateURL, err := url.Parse(server.URL)
564 require.NoError(t, err)
566 project := t.TempDir()
567 home := t.TempDir()
568 userSettings := filepath.Join(home, "settings.xml")
569 require.NoError(t, os.WriteFile(userSettings, []byte(mavenSettingsXML(
570 "<proxy><id>corp</id><host>"+corporateURL.Hostname()+"</host><port>"+corporateURL.Port()+"</port><nonProxyHosts>*.internal.example</nonProxyHosts></proxy>",
571 )), 0o600))
572 globalSettings := filepath.Join(home, "global.xml")
573 require.NoError(t, os.WriteFile(globalSettings, []byte("<settings/>"), 0o600))
575 ios, _, _, _ := cmdtest.TestIOStreams()
576 exec := &mavenFetchExecutor{url: "https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0.jar"}
577 // The fetch fails because the corporate proxy refuses the tunnel; the test
578 // only checks where the inspection proxy sent it.
579 _ = Run(t.Context(), Maven(), RunOptions{
580 IO: ios,
581 Executor: exec,
582 BaseDir: project,
583 ProjectID: "group/proj",
584 Args: []string{"-s", userSettings, "-gs", globalSettings, "dependency:resolve"},
585 })
587 require.True(t, exec.ran, "mvn must run")
588 assert.Equal(t, []string{"repo.maven.apache.org:443"}, corporate.Targets())
591func TestRunRejectsEncryptedMavenProxyPassword(t *testing.T) {
592 isolateMaven(t)
593 project := t.TempDir()
594 userSettings := filepath.Join(t.TempDir(), "settings.xml")
595 require.NoError(t, os.WriteFile(userSettings, []byte(mavenSettingsXML(
596 "<proxy><id>corp</id><host>corp</host><username>alice</username><password>{COQLCE6DU6GtcS5P=}</password></proxy>",
597 )), 0o600))
598 ios, _, _, _ := cmdtest.TestIOStreams()
599 exec := &fakeExecutor{}
601 err := Run(t.Context(), Maven(), RunOptions{
602 IO: ios,
603 Executor: exec,
604 BaseDir: project,
605 ProjectID: "group/proj",
606 Args: []string{"-s", userSettings, "-gs", userSettings, "package"},
607 })
609 require.ErrorContains(t, err, "failed to read the maven proxy settings: "+userSettings+" (proxy corp): the proxy password is encrypted")
610 assert.NotContains(t, err.Error(), "COQLCE6DU6GtcS5P")
611 assert.Nil(t, exec.env, "the package manager must not run")
614// mavenFetchExecutor stands in for mvn: it reads the proxy from the settings
615// file glab passes in MAVEN_ARGS and fetches url through it, trusting the CA
616// bundle that file is written next to.
617type mavenFetchExecutor struct {
618 url string
619 ran bool
622func (*mavenFetchExecutor) LookPath(file string) (string, error) { return "/bin/" + file, nil }
624func (m *mavenFetchExecutor) ExecWithIO(ctx context.Context, _ string, _ []string, env []string, _ io.Reader, _, _ io.Writer) error {
625 m.ran = true
626 settingsPath, ok := mavenOption(strings.Fields(lookupEnv(env, "MAVEN_ARGS")), "-s", "--settings", true)
627 if !ok {
628 return errors.New("no settings file in MAVEN_ARGS")
629 }
630 data, err := os.ReadFile(settingsPath)
631 if err != nil {
632 return err
633 }
634 proxies, err := mavenProxiesIn(settingsPath, data, nil, nil)
635 if err != nil || len(proxies) == 0 {
636 return fmt.Errorf("no proxy in %s: %w", settingsPath, err)
637 }
638 upstream, err := proxies[0].upstream()
639 if err != nil {
640 return err
641 }
642 proxyURL, err := url.Parse(upstream.URL)
643 if err != nil {
644 return err
645 }
646 pemBytes, err := os.ReadFile(strings.TrimSuffix(settingsPath, ".settings.xml"))
647 if err != nil {
648 return err
649 }
650 pool := x509.NewCertPool()
651 if !pool.AppendCertsFromPEM(pemBytes) {
652 return errors.New("failed to load the proxy CA")
653 }
654 client := &http.Client{Transport: &http.Transport{
655 Proxy: http.ProxyURL(proxyURL),
656 TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12},
657 }}
658 req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.url, http.NoBody)
659 if err != nil {
660 return err
661 }
662 resp, err := client.Do(req)
663 if err != nil {
664 return err
665 }
666 defer resp.Body.Close()
667 _, _ = io.Copy(io.Discard, resp.Body)
668 if resp.StatusCode != http.StatusOK {
669 return fmt.Errorf("registry returned %d", resp.StatusCode)
670 }
671 return nil
Diagnostics7 git calls · 225 ms
CallStatusSizeTime0225 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 KiB2 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=UPDATING
git.response.duration_ms=2
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 · 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=1327
git.response.cache=HIT
git.response.duration_ms=2
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
fetch2006.4 KiB215 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=215
git.response.bytes=6576
git.response.cache=MISS
git.response.duration_ms=215
git.fetch.wants=b09800e2ec97e54bf4cf5b43d46a2b65d830705c
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=6576
git.pkt.payload.max=6549
git.sideband.band1.bytes=6549
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
a5e1ba845ae66c75
Request
5faa201e…
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