GitLabgitlab-org

cli

main
ply6 git calls31 ms98 KiB5 cachedprotocol v2
299 lines10 KiBRaw
1//go:build !integration
3package oauth2
5import (
6 "os"
7 "path/filepath"
8 "testing"
9 "time"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "github.com/zalando/go-keyring"
14 "go.yaml.in/yaml/v3"
16 "gitlab.com/gitlab-org/cli/internal/config"
19func reloadHostValue(t *testing.T, dir, host, key string) string {
20 t.Helper()
21 cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
22 require.NoError(t, err)
23 v, _ := cfg.Get(host, key)
24 return v
27// rawHostEntry reads a host's entry straight from config.yml. Unlike Get, it
28// never resolves a value from the keyring or the environment.
29func rawHostEntry(t *testing.T, dir, host string) (map[string]any, bool) {
30 t.Helper()
31 data, err := os.ReadFile(filepath.Join(dir, "config.yml"))
32 require.NoError(t, err)
33 var doc struct {
34 Hosts map[string]map[string]any `yaml:"hosts"`
35 }
36 require.NoError(t, yaml.Unmarshal(data, &doc))
37 entry, ok := doc.Hosts[host]
38 return entry, ok
41// TestWrite_AdoptsFresherCredentialsFromDisk reproduces the #8390 clobber: a
42// process holding a stale in-memory copy writes for an unrelated reason and must
43// not roll back credentials another process rotated on disk in the meantime.
44func TestWrite_AdoptsFresherCredentialsFromDisk(t *testing.T) {
45 keyring.MockInit()
46 t.Cleanup(keyring.MockInit)
48 dir := t.TempDir()
49 past := time.Now().Add(-time.Hour).Format(time.RFC3339)
50 future := time.Now().Add(time.Hour).Format(time.RFC3339)
52 // Seed plaintext credentials on disk (no use_keyring => stored in the file).
53 seed := config.NewBlankConfigInDir(dir)
54 require.NoError(t, seed.Set("gitlab.com", "token", "access-old"))
55 require.NoError(t, seed.Set("gitlab.com", "oauth2_refresh_token", "refresh-old"))
56 require.NoError(t, seed.Set("gitlab.com", "oauth2_expiry_date", past))
57 require.NoError(t, seed.Write())
59 // A stale reader loads the config...
60 stale, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
61 require.NoError(t, err)
63 // ...then another process rotates the token on disk.
64 winner, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
65 require.NoError(t, err)
66 require.NoError(t, winner.Set("gitlab.com", "token", "access-new"))
67 require.NoError(t, winner.Set("gitlab.com", "oauth2_refresh_token", "refresh-new"))
68 require.NoError(t, winner.Set("gitlab.com", "oauth2_expiry_date", future))
69 require.NoError(t, winner.Write())
71 // The stale reader now writes for an unrelated reason.
72 require.NoError(t, stale.Set("", "last_update_check_timestamp", future))
73 require.NoError(t, stale.Write())
75 // The rotated credentials survive, and the unrelated change is persisted.
76 assert.Equal(t, "access-new", reloadHostValue(t, dir, "gitlab.com", "token"))
77 assert.Equal(t, "refresh-new", reloadHostValue(t, dir, "gitlab.com", "oauth2_refresh_token"))
78 assert.Equal(t, future, reloadHostValue(t, dir, "gitlab.com", "oauth2_expiry_date"))
79 assert.Equal(t, future, reloadHostValue(t, dir, "", "last_update_check_timestamp"))
82// TestWrite_KeepsNewerInMemoryCredentials is the converse: when this process
83// holds the fresher credentials (it just rotated), Write must not regress to
84// older on-disk values.
85func TestWrite_KeepsNewerInMemoryCredentials(t *testing.T) {
86 keyring.MockInit()
87 t.Cleanup(keyring.MockInit)
89 dir := t.TempDir()
90 past := time.Now().Add(-time.Hour).Format(time.RFC3339)
91 future := time.Now().Add(time.Hour).Format(time.RFC3339)
93 seed := config.NewBlankConfigInDir(dir)
94 require.NoError(t, seed.Set("gitlab.com", "token", "access-old"))
95 require.NoError(t, seed.Set("gitlab.com", "oauth2_expiry_date", past))
96 require.NoError(t, seed.Write())
98 fresh, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
99 require.NoError(t, err)
100 require.NoError(t, fresh.Set("gitlab.com", "token", "access-new"))
101 require.NoError(t, fresh.Set("gitlab.com", "oauth2_expiry_date", future))
102 require.NoError(t, fresh.Write())
104 assert.Equal(t, "access-new", reloadHostValue(t, dir, "gitlab.com", "token"))
105 assert.Equal(t, future, reloadHostValue(t, dir, "gitlab.com", "oauth2_expiry_date"))
108// TestWrite_KeyringHostAdoptsFresherExpiryWithoutPlaintextToken checks that for
109// a keyring-backed host (token/refresh live in the keyring, absent from the
110// file) the merge adopts only the fresher expiry and never writes a plaintext
111// token into config.yml.
112func TestWrite_KeyringHostAdoptsFresherExpiryWithoutPlaintextToken(t *testing.T) {
113 keyring.MockInit()
114 t.Cleanup(keyring.MockInit)
116 dir := t.TempDir()
117 past := time.Now().Add(-time.Hour).Format(time.RFC3339)
118 future := time.Now().Add(time.Hour).Format(time.RFC3339)
120 seed := config.NewBlankConfigInDir(dir)
121 require.NoError(t, seed.Set("gitlab.com", "use_keyring", "true"))
122 require.NoError(t, seed.Set("gitlab.com", "token", "access-old")) // -> keyring
123 require.NoError(t, seed.Set("gitlab.com", "oauth2_expiry_date", past))
124 require.NoError(t, seed.Write())
126 stale, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
127 require.NoError(t, err)
129 winner, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
130 require.NoError(t, err)
131 require.NoError(t, winner.Set("gitlab.com", "token", "access-new")) // -> keyring
132 require.NoError(t, winner.Set("gitlab.com", "oauth2_expiry_date", future))
133 require.NoError(t, winner.Write())
135 require.NoError(t, stale.Set("", "last_update_check_timestamp", future))
136 require.NoError(t, stale.Write())
138 // Fresher expiry adopted; the keyring token is current; the file never gains a
139 // plaintext token node (Get resolves it from the keyring, so assert the raw
140 // document instead).
141 assert.Equal(t, future, reloadHostValue(t, dir, "gitlab.com", "oauth2_expiry_date"))
142 storedToken, err := keyring.Get("glab:gitlab.com:token", "")
143 require.NoError(t, err)
144 assert.Equal(t, "access-new", storedToken)
146 hostEntry, ok := rawHostEntry(t, dir, "gitlab.com")
147 require.True(t, ok)
148 assert.NotContains(t, hostEntry, "token", "config.yml should not contain a plaintext token node for a keyring host")
151// TestWrite_DoesNotReadoptClearedCredentials guards the logout / credential-clear
152// path: when a process clears the OAuth fields in memory but keeps the host
153// entry (as `glab auth logout` does), the merge must not treat the now-empty
154// in-memory expiry as "stale" and re-adopt the still-present on-disk credentials,
155// which would silently undo the clear.
156func TestWrite_DoesNotReadoptClearedCredentials(t *testing.T) {
157 keyring.MockInit()
158 t.Cleanup(keyring.MockInit)
160 dir := t.TempDir()
161 future := time.Now().Add(time.Hour).Format(time.RFC3339)
163 // A file-backed host with credentials on disk.
164 seed := config.NewBlankConfigInDir(dir)
165 require.NoError(t, seed.Set("gitlab.com", "is_oauth2", "true"))
166 require.NoError(t, seed.Set("gitlab.com", "token", "access-old"))
167 require.NoError(t, seed.Set("gitlab.com", "oauth2_refresh_token", "refresh-old"))
168 require.NoError(t, seed.Set("gitlab.com", "oauth2_expiry_date", future))
169 require.NoError(t, seed.Write())
171 // Clear the credential fields in memory (mirrors authutils.ClearAuthFields),
172 // keeping the host entry, then persist.
173 cfg, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
174 require.NoError(t, err)
175 for _, key := range []string{"token", "oauth2_refresh_token", "oauth2_expiry_date"} {
176 require.NoError(t, cfg.Set("gitlab.com", key, ""))
177 }
178 require.NoError(t, cfg.Write())
180 // The credentials must be gone from disk, not re-adopted by the merge.
181 hostEntry, ok := rawHostEntry(t, dir, "gitlab.com")
182 require.True(t, ok, "the host entry itself should be preserved")
183 for _, key := range []string{"token", "oauth2_refresh_token", "oauth2_expiry_date"} {
184 assert.NotContains(t, hostEntry, key, "%s should stay cleared, not be re-adopted from disk", key)
185 }
188// TestWrite_AdoptsFresherCredentialsOverLegacyExpiry covers a stale reader whose
189// in-memory expiry is still in the RFC822 format older glab versions wrote.
190func TestWrite_AdoptsFresherCredentialsOverLegacyExpiry(t *testing.T) {
191 keyring.MockInit()
192 t.Cleanup(keyring.MockInit)
194 dir := t.TempDir()
195 past := time.Now().Add(-time.Hour).Format(time.RFC822)
196 future := time.Now().Add(time.Hour).Format(time.RFC3339)
198 seed := config.NewBlankConfigInDir(dir)
199 require.NoError(t, seed.Set("gitlab.com", "token", "access-old"))
200 require.NoError(t, seed.Set("gitlab.com", "oauth2_refresh_token", "refresh-old"))
201 require.NoError(t, seed.Set("gitlab.com", "oauth2_expiry_date", past))
202 require.NoError(t, seed.Write())
204 stale, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
205 require.NoError(t, err)
207 winner, err := config.ParseConfig(filepath.Join(dir, "config.yml"))
208 require.NoError(t, err)
209 require.NoError(t, winner.Set("gitlab.com", "token", "access-new"))
210 require.NoError(t, winner.Set("gitlab.com", "oauth2_refresh_token", "refresh-new"))
211 require.NoError(t, winner.Set("gitlab.com", "oauth2_expiry_date", future))
212 require.NoError(t, winner.Write())
214 require.NoError(t, stale.Set("", "last_update_check_timestamp", future))
215 require.NoError(t, stale.Write())
217 assert.Equal(t, "access-new", reloadHostValue(t, dir, "gitlab.com", "token"))
218 assert.Equal(t, "refresh-new", reloadHostValue(t, dir, "gitlab.com", "oauth2_refresh_token"))
219 assert.Equal(t, future, reloadHostValue(t, dir, "gitlab.com", "oauth2_expiry_date"))
222func TestExpiryNewer(t *testing.T) {
223 t.Parallel()
225 tests := []struct {
226 name string
227 disk string
228 mem string
229 want bool
230 }{
231 {
232 name: "disk newer",
233 disk: "2026-01-02T10:00:00Z",
234 mem: "2026-01-02T09:00:00Z",
235 want: true,
236 },
237 {
238 name: "equal",
239 disk: "2026-01-02T10:00:00Z",
240 mem: "2026-01-02T10:00:00Z",
241 want: false,
242 },
243 {
244 name: "memory newer",
245 disk: "2026-01-02T09:00:00Z",
246 mem: "2026-01-02T10:00:00Z",
247 want: false,
248 },
249 {
250 name: "unparseable disk",
251 disk: "not-a-date",
252 mem: "2026-01-02T09:00:00Z",
253 want: false,
254 },
255 {
256 name: "empty disk",
257 disk: "",
258 mem: "2026-01-02T09:00:00Z",
259 want: false,
260 },
261 {
262 name: "empty memory",
263 disk: "2026-01-02T10:00:00Z",
264 mem: "",
265 want: false,
266 },
267 {
268 name: "unparseable memory",
269 disk: "2026-01-02T10:00:00Z",
270 mem: "not-a-date",
271 want: false,
272 },
273 {
274 name: "RFC3339 disk newer than RFC822 memory",
275 disk: "2026-01-02T10:00:00Z",
276 mem: "02 Jan 26 09:00 UTC",
277 want: true,
278 },
279 {
280 name: "RFC822Z disk newer than RFC3339 memory",
281 disk: "02 Jan 26 10:00 +0000",
282 mem: "2026-01-02T09:00:00Z",
283 want: true,
284 },
285 {
286 name: "RFC3339 disk older than RFC822 memory",
287 disk: "2026-01-02T09:00:00Z",
288 mem: "02 Jan 26 10:00 UTC",
289 want: false,
290 },
291 }
293 for _, tc := range tests {
294 t.Run(tc.name, func(t *testing.T) {
295 t.Parallel()
296 assert.Equal(t, tc.want, expiryNewer(tc.disk, tc.mem))
297 })
298 }
Diagnostics6 git calls · 31 ms
CallStatusSizeTime031 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 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
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:0200468 B · 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=468
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.3 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=2361
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=a6e3c590e6b4b3693c725f554258af16dba55d95
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=2361
git.pkt.payload.max=2334
git.sideband.band1.bytes=2334
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
a5e277cfdf24d806
Request
088f25ab…
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