GitLabgitlab-org

cli

main
ply6 git calls28 ms101 KiB4 cachedprotocol v2
660 lines25 KiBRaw
1//go:build !integration
3package oauth2
5import (
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "net/url"
10 "os"
11 "path/filepath"
12 "strings"
13 "sync/atomic"
14 "testing"
15 "time"
17 "github.com/stretchr/testify/assert"
18 "github.com/stretchr/testify/require"
19 "github.com/zalando/go-keyring"
20 "golang.org/x/oauth2"
22 "gitlab.com/gitlab-org/cli/internal/config"
25const testHost = "gitlab.com"
27// newKeyringConfig writes a keyring-backed OAuth config for testHost to a temp
28// directory and returns a config re-read from that directory, so its backing
29// dir is set and reload() works. The keyring is mocked per-test.
30func newKeyringConfig(t *testing.T, accessToken, refreshToken string, expiry time.Time) config.Config {
31 t.Helper()
32 return newKeyringConfigInDir(t, t.TempDir(), accessToken, refreshToken, expiry)
35// tokenSourceForServer builds a configTokenSource whose token endpoint points at
36// srv, bypassing NewConfigTokenSource so the test controls the refresh response.
37func tokenSourceForServer(cfg config.Config, srv *httptest.Server) *configTokenSource {
38 return &configTokenSource{
39 cfg: cfg,
40 httpClient: srv.Client(),
41 hostname: testHost,
42 oauth2Config: &oauth2.Config{
43 ClientID: "test-client",
44 Endpoint: oauth2.Endpoint{TokenURL: srv.URL + "/oauth/token"},
45 Scopes: scopes,
46 },
47 }
50// withoutAdoptBackoff removes the inter-attempt sleep in adopt() so a test that
51// exhausts the retries does not pause for adoptAttempts*adoptBackoff.
52func withoutAdoptBackoff(t *testing.T) {
53 t.Helper()
54 old := adoptBackoff
55 adoptBackoff = 0
56 t.Cleanup(func() { adoptBackoff = old })
59func writeTokenResponse(t *testing.T, w http.ResponseWriter, accessToken, refreshToken string, expiresIn int) {
60 t.Helper()
61 w.Header().Set("Content-Type", "application/json")
62 // assert, not require: this runs in the server's handler goroutine, where
63 // require's FailNow (runtime.Goexit) would exit the wrong goroutine.
64 assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{
65 "access_token": accessToken,
66 "refresh_token": refreshToken,
67 "token_type": "bearer",
68 "expires_in": expiresIn,
69 }))
72func writeInvalidGrant(t *testing.T, w http.ResponseWriter) {
73 t.Helper()
74 w.Header().Set("Content-Type", "application/json")
75 w.WriteHeader(http.StatusBadRequest)
76 // assert, not require: this runs in the server's handler goroutine.
77 assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{
78 "error": "invalid_grant",
79 "error_description": "The provided authorization grant is invalid, expired, or revoked.",
80 }))
83func TestToken_SkipsRefreshWhenFreshestTokenIsValid(t *testing.T) {
84 cfg := newKeyringConfig(t, "access-current", "refresh-current", time.Now().Add(time.Hour))
86 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87 t.Errorf("token endpoint should not be called when the token is still valid; got %s", r.URL.Path)
88 writeInvalidGrant(t, w)
89 }))
90 defer srv.Close()
92 ts := tokenSourceForServer(cfg, srv)
93 token, err := ts.Token()
94 require.NoError(t, err)
95 assert.Equal(t, "access-current", token.AccessToken)
98func TestToken_RefreshRotatesAndPersists(t *testing.T) {
99 cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour))
101 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
102 if !assert.NoError(t, r.ParseForm()) {
103 return
104 }
105 assert.Equal(t, "refresh_token", r.Form.Get("grant_type"))
106 assert.Equal(t, "refresh-old", r.Form.Get("refresh_token"))
107 writeTokenResponse(t, w, "access-new", "refresh-new", 7200)
108 }))
109 defer srv.Close()
111 ts := tokenSourceForServer(cfg, srv)
112 token, err := ts.Token()
113 require.NoError(t, err)
114 assert.Equal(t, "access-new", token.AccessToken)
115 assert.Equal(t, "refresh-new", token.RefreshToken)
117 // The rotated credentials are persisted to the keyring, not just returned.
118 storedAccess, err := keyring.Get("glab:gitlab.com:token", "")
119 require.NoError(t, err)
120 assert.Equal(t, "access-new", storedAccess)
121 storedRefresh, err := keyring.Get("glab:gitlab.com:oauth2_refresh_token", "")
122 require.NoError(t, err)
123 assert.Equal(t, "refresh-new", storedRefresh)
126func TestToken_AdoptsConcurrentlyRotatedTokenOnInvalidGrant(t *testing.T) {
127 dir := t.TempDir()
128 cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour))
130 // The handler models a concurrent process that won the refresh race: it
131 // commits a fresh, valid token to the keyring and config file, then returns
132 // invalid_grant to us because our refresh token is now consumed.
133 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
134 // assert, not require: this runs in the server's handler goroutine. Return
135 // early if the config cannot be re-read so we do not deref a nil winner.
136 winner, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
137 if !assert.NoError(t, err) {
138 writeInvalidGrant(t, w)
139 return
140 }
141 assert.NoError(t, winner.Set(testHost, "token", "access-winner"))
142 assert.NoError(t, winner.Set(testHost, "oauth2_refresh_token", "refresh-winner"))
143 assert.NoError(t, winner.Set(testHost, "oauth2_expiry_date", time.Now().Add(time.Hour).Format(time.RFC3339)))
144 assert.NoError(t, winner.Write())
146 writeInvalidGrant(t, w)
147 }))
148 defer srv.Close()
150 ts := tokenSourceForServer(cfg, srv)
151 token, err := ts.Token()
152 require.NoError(t, err)
153 assert.Equal(t, "access-winner", token.AccessToken, "should adopt the token the winning process rotated")
156func TestToken_RefreshesInsideGracePeriod(t *testing.T) {
157 // 4 minutes left: valid by x/oauth2's 10s default, stale under the grace.
158 cfg := newKeyringConfig(t, "access-4min", "refresh-old", time.Now().Add(4*time.Minute))
160 var refreshes atomic.Int32
161 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162 refreshes.Add(1)
163 if !assert.NoError(t, r.ParseForm()) {
164 return
165 }
166 assert.Equal(t, "refresh_token", r.Form.Get("grant_type"))
167 assert.Equal(t, "refresh-old", r.Form.Get("refresh_token"))
168 writeTokenResponse(t, w, "access-refreshed", "refresh-refreshed", 7200)
169 }))
170 defer srv.Close()
172 ts := tokenSourceForServer(cfg, srv)
173 token, err := ts.Token()
174 require.NoError(t, err)
175 assert.Equal(t, "access-refreshed", token.AccessToken, "a token inside the grace period must be refreshed, not returned as-is")
176 assert.EqualValues(t, 1, refreshes.Load(), "the token endpoint must be called exactly once")
179func TestToken_ReturnsCurrentTokenWhenProbeFailsInsideGracePeriod(t *testing.T) {
180 // File-mode config (no keyring) whose directory cannot be written, so
181 // CredentialWriteProbe fails on the directory check. Set in memory only:
182 // nothing can be written here, and freshest() falls back to the in-memory
183 // copy when the re-read fails.
184 cfg := config.NewBlankConfigInDir(unwritableDir(t))
185 require.NoError(t, cfg.Set(testHost, "is_oauth2", "true"))
186 require.NoError(t, cfg.Set(testHost, "oauth2_refresh_token", "refresh-old"))
187 require.NoError(t, cfg.Set(testHost, "token", "access-4min"))
188 require.NoError(t, cfg.Set(testHost, "oauth2_expiry_date", time.Now().Add(4*time.Minute).Format(time.RFC3339)))
190 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
191 t.Errorf("token endpoint must not be called when the refreshed credentials could not be saved; got %s", r.URL.Path)
192 writeInvalidGrant(t, w)
193 }))
194 defer srv.Close()
196 ts := tokenSourceForServer(cfg, srv)
197 token, err := ts.Token()
198 require.NoError(t, err, "a token that still passes the plain check must be returned, not turned into a failure")
199 assert.Equal(t, "access-4min", token.AccessToken)
202func TestToken_ReturnsProbeErrorWhenTokenIsExpired(t *testing.T) {
203 // Same unwritable setup as the fallback test, but the token is already
204 // dead. The fallback only applies to a token that still passes the plain
205 // check; an expired one must surface the probe error, not be handed back.
206 cfg := config.NewBlankConfigInDir(unwritableDir(t))
207 require.NoError(t, cfg.Set(testHost, "is_oauth2", "true"))
208 require.NoError(t, cfg.Set(testHost, "oauth2_refresh_token", "refresh-old"))
209 require.NoError(t, cfg.Set(testHost, "token", "access-expired"))
210 require.NoError(t, cfg.Set(testHost, "oauth2_expiry_date", time.Now().Add(-time.Hour).Format(time.RFC3339)))
212 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
213 t.Errorf("token endpoint must not be called when the refreshed credentials could not be saved; got %s", r.URL.Path)
214 writeInvalidGrant(t, w)
215 }))
216 defer srv.Close()
218 ts := tokenSourceForServer(cfg, srv)
219 _, err := ts.Token()
220 require.Error(t, err)
221 assert.ErrorContains(t, err, "could not be saved")
224func TestToken_ReturnsAccessTokenOnlyTokenInsideGracePeriod(t *testing.T) {
225 // is_oauth2 with an access token and no refresh token, as the Docker
226 // credential helper builds it. Nothing can renew it, so inside the grace
227 // window it must be returned while it is still usable, not refreshed.
228 cfg := newKeyringConfig(t, "access-4min", "", time.Now().Add(4*time.Minute))
230 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
231 t.Errorf("token endpoint must not be called with no refresh token; got %s", r.URL.Path)
232 writeInvalidGrant(t, w)
233 }))
234 defer srv.Close()
236 ts := tokenSourceForServer(cfg, srv)
237 token, err := ts.Token()
238 require.NoError(t, err)
239 assert.Equal(t, "access-4min", token.AccessToken)
242func TestToken_AdoptsWinnerTokenInsideGracePeriod(t *testing.T) {
243 withoutAdoptBackoff(t)
244 dir := t.TempDir()
245 cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour))
247 // The winning process committed a token with 4 minutes left, then our
248 // refresh fails with invalid_grant. adopt must accept that token: the
249 // refresh token is spent, so there is nothing to renew with, and a
250 // usable token in the config must not force a re-login.
251 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
252 winner, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
253 if !assert.NoError(t, err) {
254 writeInvalidGrant(t, w)
255 return
256 }
257 assert.NoError(t, winner.Set(testHost, "token", "access-winner-4min"))
258 assert.NoError(t, winner.Set(testHost, "oauth2_refresh_token", "refresh-winner"))
259 assert.NoError(t, winner.Set(testHost, "oauth2_expiry_date", time.Now().Add(4*time.Minute).Format(time.RFC3339)))
260 assert.NoError(t, winner.Write())
262 writeInvalidGrant(t, w)
263 }))
264 defer srv.Close()
266 ts := tokenSourceForServer(cfg, srv)
267 token, err := ts.Token()
268 require.NoError(t, err)
269 assert.Equal(t, "access-winner-4min", token.AccessToken, "adopt must keep the plain 10s check and accept the winner's token")
272func TestToken_ReturnsErrorWhenInvalidGrantWithoutRecovery(t *testing.T) {
273 withoutAdoptBackoff(t)
274 cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour))
276 // A genuinely revoked/expired session: invalid_grant with no valid token ever
277 // appearing on disk. We must surface the error rather than loop forever.
278 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
279 writeInvalidGrant(t, w)
280 }))
281 defer srv.Close()
283 ts := tokenSourceForServer(cfg, srv)
284 _, err := ts.Token()
285 require.Error(t, err)
286 assert.True(t, isInvalidGrant(err), "expected the original invalid_grant error to be surfaced")
289func TestToken_ForgetsRejectedRefreshToken(t *testing.T) {
290 withoutAdoptBackoff(t)
291 cfg := newKeyringConfig(t, "access-old", "refresh-old", time.Now().Add(-time.Hour))
293 var refreshes atomic.Int32
294 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
295 refreshes.Add(1)
296 writeInvalidGrant(t, w)
297 }))
298 defer srv.Close()
300 ts := tokenSourceForServer(cfg, srv)
301 _, err := ts.Token()
302 require.Error(t, err)
303 assert.True(t, isInvalidGrant(err))
305 _, err = keyring.Get("glab:gitlab.com:oauth2_refresh_token", "")
306 require.ErrorIs(t, err, keyring.ErrNotFound, "the rejected refresh token must be deleted")
308 sent := refreshes.Load()
309 _, err = ts.Token()
310 require.Error(t, err)
311 assert.Equal(t, sent, refreshes.Load(), "a forgotten refresh token must not be sent again")
314func TestToken_ForgetsRejectedRefreshToken_FileBacked(t *testing.T) {
315 withoutAdoptBackoff(t)
316 keyring.MockInit()
317 t.Cleanup(keyring.MockInit)
318 dir := t.TempDir()
319 writeOAuthConfig(t, dir, false, "access-old", "refresh-old", time.Now().Add(-time.Hour))
320 cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
321 require.NoError(t, err)
323 var refreshes atomic.Int32
324 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
325 refreshes.Add(1)
326 writeInvalidGrant(t, w)
327 }))
328 defer srv.Close()
330 ts := tokenSourceForServer(cfg, srv)
331 _, err = ts.Token()
332 require.Error(t, err)
333 assert.True(t, isInvalidGrant(err))
335 onDisk, err := os.ReadFile(filepath.Join(dir, "config.yml"))
336 require.NoError(t, err)
337 assert.NotContains(t, string(onDisk), "refresh-old", "the rejected refresh token must be removed from config.yml")
339 sent := refreshes.Load()
340 _, err = tokenSourceForServer(cfg, srv).Token()
341 require.Error(t, err)
342 assert.Equal(t, sent, refreshes.Load(), "a forgotten refresh token must not be sent again")
345func TestToken_KeepsRefreshTokenRotatedAfterRejection(t *testing.T) {
346 withoutAdoptBackoff(t)
347 dir := t.TempDir()
348 cfg := newKeyringConfigInDir(t, dir, "access-old", "refresh-old", time.Now().Add(-time.Hour))
350 // Another process stores a different refresh token, with an access token
351 // adopt won't take. forget must not delete a token it never presented.
352 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
353 other, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
354 if !assert.NoError(t, err) {
355 writeInvalidGrant(t, w)
356 return
357 }
358 assert.NoError(t, other.Set(testHost, "oauth2_refresh_token", "refresh-other"))
359 assert.NoError(t, other.Write())
361 writeInvalidGrant(t, w)
362 }))
363 defer srv.Close()
365 ts := tokenSourceForServer(cfg, srv)
366 _, err := ts.Token()
367 require.Error(t, err)
369 stored, err := keyring.Get("glab:gitlab.com:oauth2_refresh_token", "")
370 require.NoError(t, err)
371 assert.Equal(t, "refresh-other", stored)
374func TestIsInvalidGrant(t *testing.T) {
375 assert.True(t, isInvalidGrant(&oauth2.RetrieveError{ErrorCode: "invalid_grant"}))
376 assert.False(t, isInvalidGrant(&oauth2.RetrieveError{ErrorCode: "invalid_client"}))
377 assert.False(t, isInvalidGrant(&url.Error{Op: "Post", Err: assert.AnError}))
378 assert.False(t, isInvalidGrant(nil))
381func TestValidWithGrace(t *testing.T) {
382 tests := []struct {
383 name string
384 token *oauth2.Token
385 want bool
386 }{
387 {name: "nil token", token: nil, want: false},
388 {name: "empty access token", token: &oauth2.Token{Expiry: time.Now().Add(time.Hour)}, want: false},
389 {name: "no expiry recorded", token: &oauth2.Token{AccessToken: "a"}, want: true},
390 {name: "one hour left", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(time.Hour)}, want: true},
391 {name: "four minutes left", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(4 * time.Minute)}, want: false},
392 {name: "expired", token: &oauth2.Token{AccessToken: "a", Expiry: time.Now().Add(-time.Minute)}, want: false},
393 }
395 for _, tt := range tests {
396 t.Run(tt.name, func(t *testing.T) {
397 assert.Equal(t, tt.want, validWithGrace(tt.token))
398 })
399 }
402// TestToken_ActsOnFreshestStateNotStartupCopy verifies that a token source
403// constructed with a stale startup config acts on the freshest persisted state:
404// when another process has rotated the credentials to a valid token, Token()
405// returns that token and never contacts the endpoint. Covered for both storage
406// modes because a keyring host reads its secrets live while a file-backed host
407// relies on the config re-read.
408func TestToken_ActsOnFreshestStateNotStartupCopy(t *testing.T) {
409 for _, tc := range []struct {
410 name string
411 useKeyring bool
412 }{
413 {"keyring-backed", true},
414 {"file-backed", false},
415 } {
416 t.Run(tc.name, func(t *testing.T) {
417 keyring.MockInit()
418 t.Cleanup(keyring.MockInit)
419 dir := t.TempDir()
421 // Startup state persisted to disk: an already-expired token, then loaded
422 // into a config that stands in for a long-lived process's in-memory copy.
423 writeOAuthConfig(t, dir, tc.useKeyring, "access-startup", "refresh-startup", time.Now().Add(-time.Hour))
424 startup, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
425 require.NoError(t, err)
427 // Another process rotates the credentials to a fresh, valid token.
428 writeOAuthConfig(t, dir, tc.useKeyring, "access-fresh", "refresh-fresh", time.Now().Add(time.Hour))
430 // The freshest token is valid, so the endpoint must not be contacted.
431 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
432 t.Errorf("token endpoint should not be called; the freshest token is valid")
433 writeInvalidGrant(t, w)
434 }))
435 defer srv.Close()
437 ts := tokenSourceForServer(startup, srv)
438 token, err := ts.Token()
439 require.NoError(t, err)
440 assert.Equal(t, "access-fresh", token.AccessToken,
441 "Token must act on the freshest persisted state, not the startup in-memory copy")
442 })
443 }
446// TestToken_RefreshDoesNotOverwriteConcurrentConfigWrites verifies that a
447// refresh persists through a freshly re-read config, so a write another process
448// made after this process loaded its config (here, rotating a second host's
449// token) is not rolled back when the refresh writes. The narrow window of a
450// write landing *during* the network refresh is closed by the cross-process
451// lock added in !3679 (mechanism A).
452func TestToken_RefreshDoesNotOverwriteConcurrentConfigWrites(t *testing.T) {
453 keyring.MockInit()
454 t.Cleanup(keyring.MockInit)
455 dir := t.TempDir()
456 const otherHost = "other.example.com"
458 // Our host starts with an expired token (file-backed so credentials live in
459 // the document being serialized).
460 writeOAuthConfig(t, dir, false, "access-old", "refresh-old", time.Now().Add(-time.Hour))
461 startup, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
462 require.NoError(t, err)
464 // Another process adds/rotates a second host's token on disk after we loaded.
465 other, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
466 require.NoError(t, err)
467 require.NoError(t, other.Set(otherHost, "token", "other-rotated"))
468 require.NoError(t, other.Write())
470 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
471 writeTokenResponse(t, w, "access-new", "refresh-new", 7200)
472 }))
473 defer srv.Close()
475 ts := tokenSourceForServer(startup, srv)
476 _, err = ts.Token()
477 require.NoError(t, err)
479 onDisk, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
480 require.NoError(t, err)
482 ourToken, _, err := onDisk.GetWithSource(testHost, "token", false)
483 require.NoError(t, err)
484 assert.Equal(t, "access-new", ourToken, "our refreshed token should be persisted")
486 otherToken, _, err := onDisk.GetWithSource(otherHost, "token", false)
487 require.NoError(t, err)
488 assert.Equal(t, "other-rotated", otherToken,
489 "a config write from another process must survive the refresh")
492// TestNewConfigTokenSource_IgnoresEnvironmentToken pins the fix for the gap
493// Timo flagged on !3704: api.WithoutTokenFromEnvironment set
494// searchEnvForIdentity=false for the token and is_oauth2 lookups in
495// api.NewClientFromConfig, but NewConfigTokenSource still read "token" through
496// cfg.Get, which always searches GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN
497// regardless of that option. A caller like the Docker credential helper,
498// which runs as a subprocess inheriting the user's shell environment, could
499// therefore mint a request using a stray environment token even though it
500// asked not to.
501func TestNewConfigTokenSource_IgnoresEnvironmentToken(t *testing.T) {
502 for _, tc := range []struct {
503 name string
504 searchEnvForIdentity bool
505 want string
506 }{
507 {"searches env when asked to", true, "env-token"},
508 {"ignores env when told not to", false, "config-token"},
509 } {
510 t.Run(tc.name, func(t *testing.T) {
511 t.Setenv("GITLAB_TOKEN", "env-token")
513 expiry := time.Now().Add(time.Hour).Format(time.RFC3339)
514 cfg := config.NewFromString(`
515---
516hosts:
517 ` + testHost + `:
518 is_oauth2: "true"
519 client_id: abc
520 token: config-token
521 oauth2_refresh_token: refresh-token
522 oauth2_expiry_date: ` + expiry + `
523`)
525 ts, err := NewConfigTokenSource(cfg, &http.Client{}, "https", testHost, tc.searchEnvForIdentity)
526 require.NoError(t, err)
528 token, err := ts.Token()
529 require.NoError(t, err)
530 assert.Equal(t, tc.want, token.AccessToken)
531 })
532 }
535// TestNewConfigTokenSource_RefreshesInsideGracePeriod goes through
536// NewConfigTokenSource; the other grace tests build configTokenSource directly.
537func TestNewConfigTokenSource_RefreshesInsideGracePeriod(t *testing.T) {
538 var refreshes atomic.Int32
539 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
540 refreshes.Add(1)
541 writeTokenResponse(t, w, "access-renewed", "refresh-renewed", 3600)
542 }))
543 defer srv.Close()
544 host := strings.TrimPrefix(srv.URL, "http://")
546 cfg := config.NewFromString(`
547---
548hosts:
549 ` + host + `:
550 is_oauth2: "true"
551 client_id: abc
552 token: access-4min
553 oauth2_refresh_token: refresh-old
554 oauth2_expiry_date: ` + time.Now().Add(4*time.Minute).Format(time.RFC3339) + `
555`)
557 ts, err := NewConfigTokenSource(cfg, srv.Client(), "http", host, false)
558 require.NoError(t, err)
560 token, err := ts.Token()
561 require.NoError(t, err)
562 assert.Equal(t, "access-renewed", token.AccessToken)
563 assert.EqualValues(t, 1, refreshes.Load())
566// newKeyringConfigInDir is newKeyringConfig with a caller-provided directory, so
567// a test can also open a second config pointing at the same files.
568func newKeyringConfigInDir(t *testing.T, dir, accessToken, refreshToken string, expiry time.Time) config.Config {
569 t.Helper()
571 keyring.MockInit()
572 t.Cleanup(keyring.MockInit)
574 seed := config.NewBlankConfigInDir(dir)
575 require.NoError(t, seed.Set(testHost, "use_keyring", "true"))
576 require.NoError(t, seed.Set(testHost, "is_oauth2", "true"))
577 require.NoError(t, seed.Set(testHost, "oauth2_refresh_token", refreshToken))
578 require.NoError(t, seed.Set(testHost, "token", accessToken))
579 require.NoError(t, seed.Set(testHost, "oauth2_expiry_date", expiry.Format(time.RFC3339)))
580 require.NoError(t, seed.Write())
582 cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
583 require.NoError(t, err)
584 return cfg
587// writeOAuthConfig persists an OAuth config for testHost to dir in either
588// keyring or plaintext-file storage mode. Callers re-read it with
589// config.ParseConfig. The keyring must already be mocked by the caller.
590func writeOAuthConfig(t *testing.T, dir string, useKeyring bool, accessToken, refreshToken string, expiry time.Time) {
591 t.Helper()
593 seed := config.NewBlankConfigInDir(dir)
594 if useKeyring {
595 require.NoError(t, seed.Set(testHost, "use_keyring", "true"))
596 }
597 require.NoError(t, seed.Set(testHost, "is_oauth2", "true"))
598 require.NoError(t, seed.Set(testHost, "oauth2_refresh_token", refreshToken))
599 require.NoError(t, seed.Set(testHost, "token", accessToken))
600 require.NoError(t, seed.Set(testHost, "oauth2_expiry_date", expiry.Format(time.RFC3339)))
601 require.NoError(t, seed.Write())
604// The refresh endpoint is built from the host config, so a self-managed instance
605// installed under a subfolder must have that subfolder in the token URL. #8399
606// fixed this for the authorization URL; the refresh path still went to the host
607// root, so refreshing on such an instance hit the wrong endpoint and failed.
608func TestNewConfigTokenSource_RefreshUsesConfiguredSubfolder(t *testing.T) {
609 for _, tc := range []struct {
610 name string
611 subfolder string
612 wantPath string
613 }{
614 {"subfolder configured", "gitlab", "/gitlab/oauth/token"},
615 {"surrounding slashes are trimmed", "/gitlab/", "/gitlab/oauth/token"},
616 {"no subfolder", "", "/oauth/token"},
617 } {
618 t.Run(tc.name, func(t *testing.T) {
619 var gotPath string
620 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
621 gotPath = r.URL.Path
622 writeTokenResponse(t, w, "new-access-token", "new-refresh-token", 3600)
623 }))
624 defer srv.Close()
626 host := strings.TrimPrefix(srv.URL, "http://")
627 expired := time.Now().Add(-time.Hour).Format(time.RFC3339)
628 cfg := config.NewFromString(`
629---
630hosts:
631 "` + host + `":
632 is_oauth2: "true"
633 client_id: abc
634 token: expired-token
635 oauth2_refresh_token: refresh-token
636 oauth2_expiry_date: ` + expired + `
637 subfolder: ` + `"` + tc.subfolder + `"` + `
638`)
640 ts, err := NewConfigTokenSource(cfg, srv.Client(), "http", host, false)
641 require.NoError(t, err)
643 token, err := ts.Token()
644 require.NoError(t, err)
645 assert.Equal(t, "new-access-token", token.AccessToken)
646 assert.Equal(t, tc.wantPath, gotPath, "refresh must go to the configured subfolder")
647 })
648 }
651// unwritableDir returns a path whose parent is a regular file, so MkdirAll
652// fails with ENOTDIR. Chmod would not do: root ignores the permission bits,
653// and CI runs as root.
654func unwritableDir(t *testing.T) string {
655 t.Helper()
657 blocker := filepath.Join(t.TempDir(), "blocker")
658 require.NoError(t, os.WriteFile(blocker, nil, 0o600))
659 return filepath.Join(blocker, "glab-cli")
Diagnostics6 git calls · 28 ms
CallStatusSizeTime028 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 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 · hit2 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=2
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 · hit3 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=3
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:0200468 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=18
git.response.bytes=468
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=c498f09dbd0cebbc59f9eff3b3ef8be66186fac6
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=468
git.pkt.payload.max=441
git.sideband.band1.bytes=441
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=441
git.pack.trailer=6420a98b3b2e923a6b61c550c00de326d620e479
Open pack map
fetch2005.7 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5809
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=ae8b79ebfeced62efd0a6ed1eec9841ad21731d2
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=5809
git.pkt.payload.max=5782
git.sideband.band1.bytes=5782
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
a5e21a9f6842ecd4
Request
cd712318…
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