GitLabgitlab-org
cli
main
1//go:build !integration
3package docker
5import (
6 "errors"
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "testing"
11 "time"
13 "github.com/docker/docker-credential-helpers/credentials"
14 "github.com/golang-jwt/jwt/v5"
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
18 "gitlab.com/gitlab-org/cli/internal/config"
19 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
20 "gitlab.com/gitlab-org/cli/internal/testing/tokenexchangetest"
21)
23func TestHelper(t *testing.T) {
24 // This avoids the oauth2 refresh from sending an http request.
25 futureDate := time.Now().Add(24 * time.Hour).Format(time.RFC822)
27 t.Run("Get", func(t *testing.T) {
28 // This ensures that we don't pull the wrong user or token
29 // if the config file search is incorrectly done with
30 // env variable search space included.
31 t.Setenv("USER", "wrong_user")
32 t.Setenv("GITLAB_TOKEN", "wrong_token")
34 t.Run("without error", func(t *testing.T) {
35 tests := map[string]struct {
36 cfg config.Config
37 registryURL string
38 expectUser string
39 expectPassword string
40 }{
41 "single host": {
42 cfg: config.NewFromString(`
43---
44hosts:
45 gitlab.example.com:
46 is_oauth2: "true"
47 client_id: abc
48 user: user1
49 token: token1
50 oauth2_expiry_date: ` + futureDate + `
51 container_registry_domains: registry.gitlab.example.com
52`),
53 registryURL: "registry.gitlab.example.com",
54 expectUser: "user1",
55 expectPassword: "token1",
56 },
57 "multi-host": {
58 cfg: config.NewFromString(`
59---
60hosts:
61 gitlab.example.com:
62 is_oauth2: "true"
63 client_id: abc
64 user: user1
65 token: token1
66 oauth2_expiry_date: ` + futureDate + `
67 container_registry_domains: registry.gitlab.example.com
68 gdk.example.com:
69 is_oauth2: "true"
70 client_id: abc
71 user: user2
72 token: token2
73 oauth2_expiry_date: ` + futureDate + `
74 container_registry_domains: registry.gdk.example.com
75`),
76 registryURL: "registry.gdk.example.com",
77 expectUser: "user2",
78 expectPassword: "token2",
79 },
80 // A host authenticated with a personal access token has no
81 // oauth2_expiry_date, so any attempt to refresh fails to parse it.
82 "personal access token host": {
83 cfg: config.NewFromString(`
84---
85hosts:
86 gitlab.com:
87 user: user1
88 token: token1
89 container_registry_domains: registry.gitlab.com
90`),
91 registryURL: "registry.gitlab.com",
92 expectUser: "user1",
93 expectPassword: "token1",
94 },
95 }
97 for name, tt := range tests {
98 t.Run(name, func(t *testing.T) {
99 helper := Helper{cfg: tt.cfg}
100 gotUser, gotPassword, err := helper.Get(tt.registryURL)
101 require.NoError(t, err)
102 assert.Equal(t, tt.expectUser, gotUser, "username does not match")
103 assert.Equal(t, tt.expectPassword, gotPassword, "password does not match")
104 })
105 }
106 })
108 t.Run("with error", func(t *testing.T) {
109 tests := map[string]struct {
110 cfg config.Config
111 registryURL string
112 expectErr string
113 }{
114 "no associated hostname": {
115 cfg: config.NewFromString(`
116---
117hosts:
118 gitlab.example.com:
119 is_oauth2: "true"
120 client_id: abc
121 user: user1
122 token: token1
123 oauth2_expiry_date: ` + futureDate + `
124`),
125 registryURL: "gitlab.example.com",
126 expectErr: "no hostname associated with",
127 },
128 "empty username": {
129 cfg: config.NewFromString(`
130---
131hosts:
132 gitlab.example.com:
133 is_oauth2: "true"
134 client_id: abc
135 user: ""
136 token: token1
137 oauth2_expiry_date: ` + futureDate + `
138 container_registry_domains: registry.gitlab.example.com
139`),
140 registryURL: "registry.gitlab.example.com",
141 expectErr: "glab user for this registryURL (hostname) is empty",
142 },
143 "empty token": {
144 cfg: config.NewFromString(`
145---
146hosts:
147 gitlab.example.com:
148 is_oauth2: "true"
149 client_id: abc
150 user: user1
151 token: ""
152 oauth2_expiry_date: ` + futureDate + `
153 container_registry_domains: registry.gitlab.example.com
154`),
155 registryURL: "registry.gitlab.example.com",
156 // With no per-host token and no refresh token, the
157 // upfront oauth2 refresh (kept out of the environment by
158 // searchEnvForIdentity=false) fails before the empty-token
159 // check downstream is even reached.
160 expectErr: "oauth2: token expired and refresh token is not set",
161 },
162 "no username": {
163 cfg: config.NewFromString(`
164---
165hosts:
166 gitlab.example.com:
167 is_oauth2: "true"
168 client_id: abc
169 token: token1
170 oauth2_expiry_date: ` + futureDate + `
171 container_registry_domains: registry.gitlab.example.com
172`),
173 registryURL: "registry.gitlab.example.com",
174 expectErr: "glab user for this registryURL (hostname) is empty",
175 },
176 "no token": {
177 cfg: config.NewFromString(`
178---
179hosts:
180 gitlab.example.com:
181 is_oauth2: "true"
182 client_id: abc
183 user: user1
184 oauth2_expiry_date: ` + futureDate + `
185 container_registry_domains: registry.gitlab.example.com
186`),
187 registryURL: "registry.gitlab.example.com",
188 // Same as "empty token": the upfront oauth2 refresh fails
189 // first since there is no per-host token or refresh token.
190 expectErr: "oauth2: token expired and refresh token is not set",
191 },
192 }
194 for name, tt := range tests {
195 t.Run(name, func(t *testing.T) {
196 helper := Helper{cfg: tt.cfg}
197 gotUser, gotPassword, err := helper.Get(tt.registryURL)
198 require.ErrorContains(t, err, tt.expectErr)
199 assert.Empty(t, gotUser, "username is not empty")
200 assert.Empty(t, gotPassword, "password is not empty")
201 })
202 }
203 })
205 t.Run("GLAB_IS_OAUTH2 does not force the refresh onto a PAT host", func(t *testing.T) {
206 t.Setenv("GLAB_IS_OAUTH2", "true")
208 helper := Helper{cfg: config.NewFromString(`
209---
210hosts:
211 gitlab.com:
212 user: user1
213 token: token1
214 container_registry_domains: registry.gitlab.com
215`)}
217 gotUser, gotPassword, err := helper.Get("registry.gitlab.com")
218 require.NoError(t, err)
219 assert.Equal(t, "user1", gotUser)
220 assert.Equal(t, "token1", gotPassword)
221 })
222 })
224 // A read failure must name itself rather than arrive as "no hostname
225 // associated with registryURL", which points at configuration that exists
226 // but couldn't be read.
227 t.Run("reports a config read failure instead of an unknown domain", func(t *testing.T) {
228 cfg := failingConfig{
229 Config: config.NewFromString(`
230---
231hosts:
232 gitlab.example.com:
233 is_oauth2: "true"
234 client_id: abc
235 user: user1
236 token: token1
237 oauth2_expiry_date: ` + futureDate + `
238`),
239 failKey: "container_registry_domains",
240 err: errors.New("keyring is locked"),
241 }
242 helper := Helper{cfg: cfg}
244 _, _, err := helper.Get("registry.gitlab.example.com")
245 require.ErrorContains(t, err, "keyring is locked")
246 assert.NotContains(t, err.Error(), "no hostname associated")
247 })
249 // The read error is collected, not returned on the spot, so one broken host
250 // entry cannot stop an unrelated host from resolving the domain.
251 t.Run("a broken host entry does not hide a matching host", func(t *testing.T) {
252 cfg := failingConfig{
253 Config: config.NewFromString(`
254---
255hosts:
256 broken.example.com:
257 token: token1
258 gdk.example.com:
259 is_oauth2: "true"
260 client_id: abc
261 user: user2
262 token: token2
263 oauth2_expiry_date: ` + futureDate + `
264 container_registry_domains: registry.gdk.example.com
265`),
266 failHost: "broken.example.com",
267 failKey: "container_registry_domains",
268 err: errors.New("keyring is locked"),
269 }
270 ios, _, _, errOut := cmdtest.TestIOStreams()
271 helper := Helper{cfg: cfg, io: ios}
273 gotUser, gotPassword, err := helper.Get("registry.gdk.example.com")
274 require.NoError(t, err)
275 assert.Equal(t, "user2", gotUser)
276 assert.Equal(t, "token2", gotPassword)
277 // broken.example.com could have been the intended host for this
278 // domain too; its config just couldn't be read, so the match on
279 // gdk.example.com must not look unconditionally trustworthy.
280 assert.Contains(t, errOut.String(), "keyring is locked")
281 })
283 t.Run("Add", func(t *testing.T) {
284 var helper Helper
285 err := helper.Add(&credentials.Credentials{})
286 assert.ErrorContains(t, err, "glab auth docker-helper does not")
287 })
289 t.Run("Delete", func(t *testing.T) {
290 var helper Helper
291 err := helper.Delete("registry.gitlab.example.com")
292 assert.ErrorContains(t, err, "glab auth docker-helper does not")
293 })
295 t.Run("List", func(t *testing.T) {
296 var helper Helper
297 got, err := helper.List()
298 require.ErrorContains(t, err, "glab auth docker-helper does not")
299 assert.Empty(t, got)
300 })
301}
303// TestHelper_Get_Precedence pins the dual-listed-domain precedence rule: the
304// artifact registry is tried first, the container registry is only a
305// fallback, and a domain configured solely for artifact registry gets its
306// real exchange error rather than "unknown domain".
307func TestHelper_Get_Precedence(t *testing.T) {
308 futureDate := time.Now().Add(24 * time.Hour).Format(time.RFC822)
310 t.Run("dual-listed domain, artifact registry exchange succeeds", func(t *testing.T) {
311 wantToken := tokenexchangetest.MakeJWT(t, jwt.RegisteredClaims{
312 Issuer: "https://gitlab.example.com",
313 Subject: "gid://gitlab/User/1",
314 ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)),
315 })
316 srv, count := tokenexchangetest.NewTokenExchangeServer(t, wantToken, &tokenexchangetest.WireRequest{})
317 host := apiHost(t, srv)
319 cfg := config.NewFromString(`
320---
321hosts:
322 gitlab.example.com:
323 is_oauth2: "true"
324 client_id: abc
325 user: user1
326 token: token1
327 oauth2_expiry_date: ` + futureDate + `
328 container_registry_domains: registry.gitlab.example.com
329 artifact_registry_domains: registry.gitlab.example.com
330 api_host: ` + host + `
331 api_protocol: http
332`)
333 helper := Helper{cfg: cfg}
335 gotUser, gotToken, err := helper.Get("registry.gitlab.example.com")
336 require.NoError(t, err)
337 assert.Equal(t, "__token__", gotUser)
338 assert.Equal(t, wantToken, gotToken)
339 assert.EqualValues(t, 1, count.Load())
340 })
342 t.Run("dual-listed domain, artifact registry exchange fails, container registry used", func(t *testing.T) {
343 srv, _ := tokenexchangetest.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
344 w.WriteHeader(http.StatusForbidden)
345 })
346 host := apiHost(t, srv)
348 cfg := config.NewFromString(`
349---
350hosts:
351 gitlab.example.com:
352 is_oauth2: "true"
353 client_id: abc
354 user: user1
355 token: token1
356 oauth2_expiry_date: ` + futureDate + `
357 container_registry_domains: registry.gitlab.example.com
358 artifact_registry_domains: registry.gitlab.example.com
359 api_host: ` + host + `
360 api_protocol: http
361`)
362 ios, _, _, errOut := cmdtest.TestIOStreams()
363 helper := Helper{cfg: cfg, io: ios}
365 gotUser, gotToken, err := helper.Get("registry.gitlab.example.com")
366 require.NoError(t, err)
367 assert.Equal(t, "user1", gotUser)
368 assert.Equal(t, "token1", gotToken)
369 // The fall-through must be visible on stderr without DEBUG=1, since
370 // Docker forwards the helper's stderr but not its debug logging.
371 assert.Contains(t, errOut.String(), "artifact registry exchange failed")
372 })
374 t.Run("artifact-registry-only domain, exchange fails, hard error", func(t *testing.T) {
375 srv, _ := tokenexchangetest.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
376 w.Header().Set("Content-Type", "application/json")
377 w.WriteHeader(http.StatusForbidden)
378 _, _ = w.Write([]byte(`{"message":"artifact registry is disabled for this project"}`))
379 })
380 host := apiHost(t, srv)
382 cfg := config.NewFromString(`
383---
384hosts:
385 gitlab.example.com:
386 is_oauth2: "true"
387 client_id: abc
388 user: user1
389 token: token1
390 oauth2_expiry_date: ` + futureDate + `
391 artifact_registry_domains: registry.gitlab.example.com
392 api_host: ` + host + `
393 api_protocol: http
394`)
395 helper := Helper{cfg: cfg}
397 _, _, err := helper.Get("registry.gitlab.example.com")
398 require.Error(t, err)
399 assert.Contains(t, err.Error(), "artifact registry is disabled for this project")
400 assert.NotContains(t, err.Error(), "no hostname associated")
401 })
403 // Dual-listed domain, both exchanges fail: the container-registry error
404 // names a real failure (an empty stored token), not just an unassociated
405 // domain, so it must not be swallowed by the artifact-registry error alone.
406 t.Run("dual-listed domain, both exchanges fail, container registry error is not swallowed", func(t *testing.T) {
407 srv, _ := tokenexchangetest.NewTestServer(t, func(w http.ResponseWriter, r *http.Request) {
408 w.Header().Set("Content-Type", "application/json")
409 w.WriteHeader(http.StatusForbidden)
410 _, _ = w.Write([]byte(`{"message":"artifact registry is disabled for this project"}`))
411 })
412 host := apiHost(t, srv)
414 cfg := config.NewFromString(`
415---
416hosts:
417 gitlab.example.com:
418 is_oauth2: "true"
419 client_id: abc
420 user: ""
421 token: token1
422 oauth2_expiry_date: ` + futureDate + `
423 container_registry_domains: registry.gitlab.example.com
424 artifact_registry_domains: registry.gitlab.example.com
425 api_host: ` + host + `
426 api_protocol: http
427`)
428 helper := Helper{cfg: cfg}
430 _, _, err := helper.Get("registry.gitlab.example.com")
431 require.Error(t, err)
432 assert.Contains(t, err.Error(), "artifact registry is disabled for this project")
433 assert.Contains(t, err.Error(), "glab user for this registryURL (hostname) is empty")
434 })
436 t.Run("a config read failure surfaces as itself, not as an unknown domain", func(t *testing.T) {
437 cfg := failingConfig{
438 Config: config.NewFromString(`
439---
440hosts:
441 gitlab.example.com:
442 is_oauth2: "true"
443 client_id: abc
444 user: user1
445 token: token1
446 oauth2_expiry_date: ` + futureDate + `
447`),
448 failKey: "artifact_registry_domains",
449 err: errors.New("keyring is locked"),
450 }
451 helper := Helper{cfg: cfg}
453 _, _, err := helper.Get("registry.gitlab.example.com")
454 require.ErrorContains(t, err, "keyring is locked")
455 assert.NotContains(t, err.Error(), "no hostname associated")
456 })
457}
459// apiHost returns srv's host:port, since the api_host config key takes a
460// bare host:port rather than a full URL.
461func apiHost(t *testing.T, srv *httptest.Server) string {
462 t.Helper()
463 u, err := url.Parse(srv.URL)
464 require.NoError(t, err)
465 return u.Host
466}
Diagnostics8 git calls · 239 ms
CallStatusSizeTime0239 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · hit2 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=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
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 · hit4 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=4 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=44Open pack map
fetchtree:02001.8 KiB · hit2 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=1857 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 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=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200358 B · hit1 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=358 git.response.cache=HIT git.response.duration_ms=1 git.fetch.filter=tree:0 git.fetch.wants=5d783fac547a70b19ca138c47d067b44febb52da 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=358 git.pkt.payload.max=331 git.sideband.band1.bytes=331 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=331 git.pack.trailer=22a484e5ecad6d6e62b224e8a7f90cf5ff085938Open pack map
fetchtree:0200352 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=228 git.response.bytes=352 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=078b54e79fa23822972abf99b4c28a7b8a3e2806 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=352 git.pkt.payload.max=325 git.sideband.band1.bytes=325 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=325 git.pack.trailer=756e6cebb5c15b5cd4bd8e9270eb683dbb54a67cOpen pack map
fetch2002.9 KiB228 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2977 git.response.cache=MISS git.response.duration_ms=228 git.fetch.wants=e34e1876c84b6299799ffa018c0ed775eeaf5323 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=2977 git.pkt.payload.max=2950 git.sideband.band1.bytes=2950 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
- a5e2c3b0efacaedd
- Request
- c10b13c5…
- 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