GitLabgitlab-org

cli

main
ply9 git calls273 ms99 KiB7 cachedprotocol v2
198 lines4.5 KiBRaw
1package get_token
3import (
4 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "time"
12 "github.com/zalando/go-keyring"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17const keyringService = "glab"
19var (
20 errNotFound = errors.New("not found")
21 errTokenExpired = errors.New("token expired")
22 errTokenRevoked = errors.New("token revoked")
23 errUnsupportedPlatform = errors.New("unsupported platform")
26// storage defines the interface for token storage backends
27type storage interface {
28 get(id string) ([]byte, error)
29 set(id string, data []byte) error
32// keyringStorage implements storage using the system keyring
33type keyringStorage struct{}
35func (k *keyringStorage) get(id string) ([]byte, error) {
36 data, err := keyring.Get(keyringService, id)
37 switch {
38 case err == nil:
39 return []byte(data), nil
40 case errors.Is(err, keyring.ErrNotFound):
41 return nil, errNotFound
42 case errors.Is(err, keyring.ErrUnsupportedPlatform):
43 return nil, errUnsupportedPlatform
44 default:
45 return nil, err
46 }
49func (k *keyringStorage) set(id string, data []byte) error {
50 if err := keyring.Set(keyringService, id, string(data)); err != nil {
51 if errors.Is(err, keyring.ErrUnsupportedPlatform) {
52 return errUnsupportedPlatform
53 }
54 return err
55 }
56 return nil
59// fileStorage implements storage using the filesystem
60type fileStorage struct {
61 root *os.Root
64func newFileStorage() (*fileStorage, error) {
65 cacheDir, err := os.UserCacheDir()
66 if err != nil {
67 return nil, err
68 }
70 gitlabCacheDir := filepath.Join(cacheDir, "gitlab")
71 err = os.MkdirAll(gitlabCacheDir, 0o700)
72 if err != nil {
73 return nil, err
74 }
76 root, err := os.OpenRoot(gitlabCacheDir)
77 if err != nil {
78 return nil, err
79 }
81 return &fileStorage{root: root}, nil
84func (f *fileStorage) get(id string) ([]byte, error) {
85 file, err := f.root.Open(id)
86 if err != nil {
87 if errors.Is(err, os.ErrNotExist) {
88 return nil, errNotFound
89 }
90 return nil, err
91 }
92 defer file.Close()
94 return io.ReadAll(file)
97func (f *fileStorage) set(id string, data []byte) (err error) {
98 var file *os.File
99 file, err = f.root.OpenFile(id, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
100 if err != nil {
101 return err
102 }
103 defer func() {
104 cerr := file.Close()
105 if err == nil {
106 err = cerr
107 }
108 }()
110 if _, err := file.Write(data); err != nil {
111 return err
112 }
114 if err := file.Sync(); err != nil {
115 return err
116 }
118 return nil
121func (f *fileStorage) close() error {
122 return f.root.Close()
125type cache struct {
126 id string
127 createFunc func() (*gitlab.PersonalAccessToken, error)
128 isTokenRevoked func(t *gitlab.PersonalAccessToken) (bool, error)
129 storage storage
132func (c *cache) isTokenExpired(token *gitlab.PersonalAccessToken) bool {
133 return time.Time(*token.ExpiresAt).Before(time.Now().UTC())
136func (c *cache) get() (*gitlab.PersonalAccessToken, error) {
137 token, err := c.getCachedToken()
138 switch {
139 case err == nil:
140 return token, nil
141 case errors.Is(err, errNotFound), errors.Is(err, errTokenExpired), errors.Is(err, errTokenRevoked):
142 return c.createAndCacheToken()
143 default:
144 return nil, err
145 }
148func (c *cache) getCachedToken() (*gitlab.PersonalAccessToken, error) {
149 data, err := c.storage.get(c.id)
150 if err != nil {
151 return nil, err
152 }
154 var token gitlab.PersonalAccessToken
155 if err := json.Unmarshal(data, &token); err != nil {
156 return nil, err
157 }
159 if c.isTokenExpired(&token) {
160 return nil, errTokenExpired
161 }
163 if token.Revoked {
164 fmt.Fprintln(os.Stderr, "Cached token has been revoked, creating new one") //nolint:forbidigo // cache has no IOStreams reference, writes directly to os.Stderr
165 return nil, errTokenRevoked
166 }
168 isRevoked, err := c.isTokenRevoked(&token)
169 if err != nil {
170 fmt.Fprintf(os.Stderr, "Warning: Failed to check if token is revoked: %v. Using cached token anyway.\n", err) //nolint:forbidigo // cache has no IOStreams reference, writes directly to os.Stderr
171 return &token, nil
172 }
174 if isRevoked {
175 fmt.Fprintln(os.Stderr, "Cached token has been revoked, creating new one") //nolint:forbidigo // cache has no IOStreams reference, writes directly to os.Stderr
176 return nil, errTokenRevoked
177 }
179 return &token, nil
182func (c *cache) createAndCacheToken() (*gitlab.PersonalAccessToken, error) {
183 token, err := c.createFunc()
184 if err != nil {
185 return nil, err
186 }
188 data, err := json.Marshal(token) //nolint:forbidigo // cached token is stored in keyring, not stdout
189 if err != nil {
190 return nil, err
191 }
193 if err := c.storage.set(c.id, data); err != nil {
194 return nil, err
195 }
197 return token, nil
Diagnostics9 git calls · 273 ms
CallStatusSizeTime0273 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=11
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 · hit11 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=11
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=11
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 · hit11 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=32926
git.response.cache=HIT
git.response.duration_ms=11
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.8 KiB · hit1 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=1
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200200 B · hit1 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=200
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=tree:0
git.fetch.wants=9988d47506dcbe3004b8b72cbdb9d66d6d75cda4
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=200
git.pkt.payload.max=173
git.sideband.band1.bytes=173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=173
git.pack.trailer=948ee807a1ee817af67f2df94421a202e82325f9
Open pack map
fetchtree:0200405 B · 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=405
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=d271680e941992259de0ff84d4a04bcbba55f531
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=405
git.pkt.payload.max=378
git.sideband.band1.bytes=378
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=378
git.pack.trailer=adfacba5ff1f4fcf28dcaac12e2874f6d48a7934
Open pack map
fetchtree:0200263 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=250
git.response.bytes=263
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=2c1df054e09d18899b4c74c207905511ed57f752
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=263
git.pkt.payload.max=236
git.sideband.band1.bytes=236
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=236
git.pack.trailer=6ae3f3b63c9b4fb1e4060b9efa24a84240bbb33e
Open pack map
fetch2001.4 KiB250 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1409
git.response.cache=MISS
git.response.duration_ms=250
git.fetch.wants=1349ee9432522b95e4a9a7fc966e7b8f8944c301
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=1409
git.pkt.payload.max=1382
git.sideband.band1.bytes=1382
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
a5eb08b39ef221ca
Request
ac96a822…
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