GitLabgitlab-org

cli

main
ply6 git calls26 ms99 KiB5 cachedprotocol v2
387 lines11 KiBRaw
1//go:build !integration
3package cmdutils
5import (
6 "bytes"
7 "fmt"
8 "io"
9 "net"
10 "net/http"
11 "net/http/httptest"
12 "net/url"
13 "os/exec"
14 "strings"
15 "sync/atomic"
16 "testing"
18 "github.com/MakeNowJust/heredoc/v2"
19 "github.com/stretchr/testify/assert"
20 "github.com/stretchr/testify/require"
22 "gitlab.com/gitlab-org/cli/internal/api"
23 "gitlab.com/gitlab-org/cli/internal/config"
24 "gitlab.com/gitlab-org/cli/internal/git"
25 "gitlab.com/gitlab-org/cli/internal/iostreams"
28func TestFactory_ResolveHostNameFromConfig(t *testing.T) {
29 // GIVEN
30 isolateHost(t)
31 cfg := config.NewFromString(heredoc.Doc(`
32 host: gitlab.example.com
33 `))
35 // WHEN
36 f := NewFactory(nil, false, cfg, api.BuildInfo{})
38 // THEN
39 assert.Equal(t, "gitlab.example.com", f.defaultHostname)
42func TestFactory_ResolveHostNameFromEnv(t *testing.T) {
43 // GIVEN
44 isolateHost(t)
45 t.Setenv("GITLAB_HOST", "gitlab.example.com")
46 cfg := config.NewFromString(heredoc.Doc(`
47 host: another.gitlab.example.com
48 `))
50 // WHEN
51 f := NewFactory(nil, false, cfg, api.BuildInfo{})
53 // THEN
54 assert.Equal(t, "gitlab.example.com", f.defaultHostname)
57func TestFactory_ResolveToGitLabComByDefault(t *testing.T) {
58 // GIVEN
59 isolateHost(t)
60 cfg := config.NewBlankConfig()
62 // WHEN
63 f := NewFactory(nil, false, cfg, api.BuildInfo{})
65 // THEN
66 assert.Equal(t, "gitlab.com", f.defaultHostname)
69func TestFactory_GitLabClientUsesCorrectHost(t *testing.T) {
70 // GIVEN
71 tests := []struct {
72 name string
73 cfg config.Config
74 env map[string]string
75 expectedGitLabClientHostname *url.URL
76 }{
77 {
78 name: "default",
79 cfg: config.NewBlankConfig(),
80 expectedGitLabClientHostname: mustURL(t, "https://gitlab.com/api/v4/"),
81 },
82 {
83 name: "host from config",
84 cfg: config.NewFromString(heredoc.Doc(`
85 host: gitlab.example.com
86 `)),
87 expectedGitLabClientHostname: mustURL(t, "https://gitlab.example.com/api/v4/"),
88 },
89 {
90 name: "host from env",
91 env: map[string]string{"GITLAB_HOST": "gitlab.example.com"},
92 cfg: config.NewFromString(heredoc.Doc(`
93 host: another.gitlab.example.com
94 `)),
95 expectedGitLabClientHostname: mustURL(t, "https://gitlab.example.com/api/v4/"),
96 },
97 }
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 // GIVEN
102 isolateHost(t)
103 if tt.env != nil {
104 for k, v := range tt.env {
105 t.Setenv(k, v)
106 }
107 }
109 // WHEN
110 f := NewFactory(nil, false, tt.cfg, api.BuildInfo{})
111 c, err := f.GitLabClient()
112 require.NoError(t, err)
114 // THEN
115 assert.Equal(t, tt.expectedGitLabClientHostname, c.BaseURL())
116 })
117 }
120func TestFactory_ApiClientsReuseConnection(t *testing.T) {
121 // GIVEN
122 var conns atomic.Int64
123 srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
124 w.WriteHeader(http.StatusOK)
125 }))
126 srv.Config.ConnState = func(_ net.Conn, state http.ConnState) {
127 if state == http.StateNew {
128 conns.Add(1)
129 }
130 }
131 srv.StartTLS()
132 defer srv.Close()
134 host := strings.TrimPrefix(srv.URL, "https://")
135 cfg := config.NewFromString(fmt.Sprintf(`hosts:
136 %q:
137 api_host: %q
138 token: glpat-test
139 skip_tls_verify: true
140`, host, host))
141 f := NewFactory(nil, false, cfg, api.BuildInfo{})
143 // WHEN
144 for range 2 {
145 c, err := f.ApiClient(host)
146 require.NoError(t, err)
147 req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL, http.NoBody)
148 require.NoError(t, err)
149 resp, err := c.HTTPClient().Do(req)
150 require.NoError(t, err)
151 _, err = io.Copy(io.Discard, resp.Body)
152 require.NoError(t, err)
153 require.NoError(t, resp.Body.Close())
154 }
156 // THEN
157 assert.Equal(t, int64(1), conns.Load())
160func TestFactory_ConfigHostDoesNotOverrideRemote(t *testing.T) {
161 // GIVEN
162 isolateHost(t)
163 require.NoError(t, exec.Command("git", "remote", "add", "origin", "https://gitlab.com/owner/repo.git").Run())
164 cfg := config.NewFromString(heredoc.Doc(`
165 host: gitlab.example.com
166 `))
168 // WHEN
169 f := NewFactory(nil, false, cfg, api.BuildInfo{})
171 // THEN
172 assert.Equal(t, "gitlab.com", f.DefaultHostname())
173 repo, err := f.BaseRepo()
174 require.NoError(t, err)
175 assert.Equal(t, "gitlab.com", repo.RepoHost())
177 require.NoError(t, f.RepoOverride("group/proj"))
178 repo, err = f.BaseRepo()
179 require.NoError(t, err)
180 assert.Equal(t, "gitlab.com", repo.RepoHost())
181 assert.Equal(t, "group/proj", repo.FullName())
184func TestFactory_EnvHostOverridesRemote(t *testing.T) {
185 // GIVEN
186 isolateHost(t)
187 require.NoError(t, exec.Command("git", "remote", "add", "origin", "https://gitlab.com/owner/repo.git").Run())
188 t.Setenv("GITLAB_HOST", "gitlab.example.com")
189 cfg := config.NewBlankConfig()
191 // WHEN
192 f := NewFactory(nil, false, cfg, api.BuildInfo{})
194 // THEN
195 assert.Equal(t, "gitlab.example.com", f.DefaultHostname())
196 _, err := f.BaseRepo()
197 require.ErrorContains(t, err, "GITLAB_HOST is currently set to gitlab.example.com")
199 require.NoError(t, f.RepoOverride("group/proj"))
200 repo, err := f.BaseRepo()
201 require.NoError(t, err)
202 assert.Equal(t, "gitlab.example.com", repo.RepoHost())
203 assert.Equal(t, "group/proj", repo.FullName())
206func TestFactory_CIHostOverridesRemote(t *testing.T) {
207 // GIVEN
208 isolateHost(t)
209 require.NoError(t, exec.Command("git", "remote", "add", "origin", "https://gitlab.com/owner/repo.git").Run())
210 t.Setenv("GLAB_ENABLE_CI_AUTOLOGIN", "true")
211 t.Setenv("GITLAB_CI", "true")
212 t.Setenv("CI_SERVER_FQDN", "gitlab.example.com")
213 cfg := config.NewBlankConfig()
215 // WHEN
216 f := NewFactory(nil, false, cfg, api.BuildInfo{})
218 // THEN
219 assert.Equal(t, "gitlab.example.com", f.DefaultHostname())
220 _, err := f.BaseRepo()
221 require.ErrorContains(t, err, "correspond to the CI_SERVER_FQDN environment variable")
222 require.ErrorContains(t, err, "CI_SERVER_FQDN is currently set to gitlab.example.com")
225// isolateHost keeps the developer's environment and the glab checkout's own
226// remotes out of host resolution.
227func isolateHost(t *testing.T) {
228 t.Helper()
230 git.InitGitRepo(t)
231 t.Setenv("GLAB_ENABLE_CI_AUTOLOGIN", "")
232 for _, key := range config.EnvKeyEquivalence("host") {
233 t.Setenv(key, "")
234 }
237func mustURL(t *testing.T, s string) *url.URL {
238 t.Helper()
240 u, err := url.Parse(s)
241 require.NoError(t, err)
243 return u
246// TestFactory_ApiClientEnvTokenWarning pins that a token from the environment
247// is only sent to trusted hosts (see config.IsTrustedHost), and that withholding
248// it is reported once per host on stderr.
249func TestFactory_ApiClientEnvTokenWarning(t *testing.T) {
250 for _, e := range []string{"GITLAB_TOKEN", "GITLAB_ACCESS_TOKEN", "OAUTH_TOKEN", "JOB_TOKEN", "GITLAB_HOST", "GITLAB_URI", "GL_HOST", "GITLAB_REPO"} {
251 t.Setenv(e, "")
252 }
254 newFactoryWithStderr := func(cfg config.Config) (*DefaultFactory, *bytes.Buffer) {
255 stderr := &bytes.Buffer{}
256 ios := iostreams.New(
257 iostreams.WithStdin(io.NopCloser(&bytes.Buffer{}), false),
258 iostreams.WithStdout(&bytes.Buffer{}, false),
259 iostreams.WithStderr(stderr, false),
260 )
261 return NewFactory(ios, false, cfg, api.BuildInfo{}), stderr
262 }
264 t.Run("warns when the environment token does not apply to the host", func(t *testing.T) {
265 t.Setenv("GITLAB_TOKEN", "env-token")
266 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
268 client, err := f.ApiClient("attacker.example.com")
269 require.NoError(t, err)
271 assert.Contains(t, stderr.String(), "GITLAB_TOKEN is set")
272 assert.Contains(t, stderr.String(), `"attacker.example.com"`)
273 assert.Contains(t, stderr.String(), "did not send the credential")
274 assert.Contains(t, stderr.String(), "glab auth login --hostname attacker.example.com")
276 // And the client carries no credential.
277 _, _, err = client.AuthSource().Header(t.Context())
278 require.ErrorContains(t, err, "unauthenticated")
279 })
281 t.Run("warns when JOB_TOKEN alone does not apply to the host", func(t *testing.T) {
282 t.Setenv("JOB_TOKEN", "env-job-token")
283 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
285 _, err := f.ApiClient("attacker.example.com")
286 require.NoError(t, err)
288 assert.Contains(t, stderr.String(), "JOB_TOKEN is set")
289 assert.Contains(t, stderr.String(), "did not send the credential")
290 })
292 t.Run("lists every credential env var that was withheld", func(t *testing.T) {
293 t.Setenv("GITLAB_TOKEN", "env-token")
294 t.Setenv("JOB_TOKEN", "env-job-token")
295 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
297 _, err := f.ApiClient("attacker.example.com")
298 require.NoError(t, err)
300 assert.Contains(t, stderr.String(), "GITLAB_TOKEN, JOB_TOKEN is set")
301 })
303 t.Run("stays quiet for the default host", func(t *testing.T) {
304 t.Setenv("GITLAB_TOKEN", "env-token")
305 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
307 _, err := f.ApiClient("gitlab.com")
308 require.NoError(t, err)
309 assert.Empty(t, stderr.String())
310 })
312 t.Run("stays quiet for a configured host", func(t *testing.T) {
313 t.Setenv("GITLAB_TOKEN", "env-token")
314 f, stderr := newFactoryWithStderr(config.NewFromString("hosts:\n gitlab.example.com:\n user: someone\n"))
316 _, err := f.ApiClient("gitlab.example.com")
317 require.NoError(t, err)
318 assert.Empty(t, stderr.String())
319 })
321 t.Run("stays quiet without a token environment variable", func(t *testing.T) {
322 t.Setenv("GITLAB_TOKEN", "")
323 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
325 _, err := f.ApiClient("attacker.example.com")
326 require.NoError(t, err)
327 assert.Empty(t, stderr.String())
328 })
330 t.Run("sends the token to a GITLAB_HOST with a subfolder", func(t *testing.T) {
331 isolateHost(t)
332 t.Setenv("GITLAB_TOKEN", "env-token")
333 t.Setenv("GITLAB_HOST", "http://127.0.0.1:8099/gitlab")
334 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
336 client, err := f.ApiClient("")
337 require.NoError(t, err)
338 assert.Empty(t, stderr.String())
340 _, value, err := client.AuthSource().Header(t.Context())
341 require.NoError(t, err)
342 assert.Equal(t, "env-token", value)
343 })
345 t.Run("warns once per host when several clients are built", func(t *testing.T) {
346 isolateHost(t)
347 t.Setenv("GITLAB_TOKEN", "env-token")
348 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
349 require.NoError(t, f.RepoOverride("https://attacker.example.com/group/proj"))
351 _, err := f.GitLabClient()
352 require.NoError(t, err)
353 _, err = f.ApiClient("attacker.example.com")
354 require.NoError(t, err)
355 assert.Equal(t, 1, strings.Count(stderr.String(), "is neither the default host"))
356 })
358 t.Run("sends the token to the host of a GITLAB_REPO URL", func(t *testing.T) {
359 isolateHost(t)
360 t.Setenv("GITLAB_TOKEN", "env-token")
361 t.Setenv("GITLAB_REPO", "https://gitlab.example.com/group/proj")
362 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
363 require.NoError(t, f.RepoOverride("https://gitlab.example.com/group/proj"))
365 repo, err := f.BaseRepo()
366 require.NoError(t, err)
367 client, err := f.ApiClient(repo.RepoHost())
368 require.NoError(t, err)
369 assert.Empty(t, stderr.String())
371 _, value, err := client.AuthSource().Header(t.Context())
372 require.NoError(t, err)
373 assert.Equal(t, "env-token", value)
374 })
376 t.Run("warns from GitLabClient when a repository override names an unconfigured host", func(t *testing.T) {
377 isolateHost(t)
378 t.Setenv("GITLAB_TOKEN", "env-token")
379 f, stderr := newFactoryWithStderr(config.NewBlankConfig())
380 require.NoError(t, f.RepoOverride("https://attacker.example.com/group/proj"))
382 _, err := f.GitLabClient()
383 require.NoError(t, err)
384 assert.Contains(t, stderr.String(), "GITLAB_TOKEN is set")
385 assert.Contains(t, stderr.String(), `"attacker.example.com"`)
386 })
Diagnostics6 git calls · 26 ms
CallStatusSizeTime026 ms
discover200185 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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=3
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 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=4273
git.response.cache=HIT
git.response.duration_ms=2
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: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=2
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
fetchtree:02001.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=17
git.response.bytes=1238
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17
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=1238
git.pkt.payload.max=1211
git.sideband.band1.bytes=1211
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1211
git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27ca
Open pack map
fetch2002.6 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2633
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=6334a0a607a8dad14aa0612f1aa7d3130ee7aa93
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=2633
git.pkt.payload.max=2606
git.sideband.band1.bytes=2606
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
a5e18d03db595e37
Request
585f9868…
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