GitLabgitlab-org

cli

main
ply8 git calls41 ms101 KiB7 cachedprotocol v2
291 lines9.3 KiBRaw
1package login
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "net/url"
9 "os"
10 "path/filepath"
11 "regexp"
12 "strings"
14 "go.yaml.in/yaml/v3"
17// errYarnAnchorOrAlias refuses edits through anchors or aliases, because a
18// shared value would change entries this command was not asked to touch.
19var errYarnAnchorOrAlias = errors.New("found a YAML anchor or alias on the path to the npmRegistries entry; this command cannot edit it safely, so add the entry by hand")
21// loginYarn upserts an npmRegistries entry with npmAuthToken and
22// npmAlwaysAuth in ~/.yarnrc.yml, which Yarn 2 and later read. It returns
23// the npmScopes that keep sending their own credential instead.
24//
25// YARN_RC_FILENAME is ignored on purpose. Yarn reads the home file through
26// findFolderRcFile(getHomeFolder()), which always uses ".yarnrc.yml".
27func loginYarn(registry *url.URL, token string) ([]string, error) {
28 home, err := homeDir()
29 if err != nil {
30 return nil, err
31 }
32 path := filepath.Join(home, ".yarnrc.yml")
34 content, err := os.ReadFile(path)
35 if err != nil && !os.IsNotExist(err) {
36 return nil, fmt.Errorf("reading %s: %w", path, err)
37 }
39 updated, shadowed, err := upsertYarnRegistry(content, registry, token)
40 if err != nil {
41 return nil, fmt.Errorf("updating %s: %w", path, err)
42 }
44 if err := writeCredentialFile(path, updated); err != nil {
45 return nil, err
46 }
47 return shadowed, nil
50// upsertYarnRegistry refreshes every npmRegistries key Yarn resolves to
51// registry, and adds one only when there is none. Re-encoding can change the
52// formatting in other places too: indentation becomes two spaces, blank lines
53// may be dropped, CRLF line endings become LF, and an entry added to a flow
54// mapping such as {} stays on that one line.
55func upsertYarnRegistry(content []byte, registry *url.URL, token string) ([]byte, []string, error) {
56 var out bytes.Buffer
58 decoder := yaml.NewDecoder(bytes.NewReader(content))
59 var doc yaml.Node
60 err := decoder.Decode(&doc)
61 switch {
62 case errors.Is(err, io.EOF):
63 // A comment-only file decodes to no document, so its comments are kept
64 // by writing the new document after them.
65 if len(bytes.TrimSpace(content)) > 0 {
66 out.Write(content)
67 if !bytes.HasSuffix(content, []byte("\n")) {
68 out.WriteByte('\n')
69 }
70 }
71 doc = yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{{Kind: yaml.MappingNode}}}
72 case err != nil:
73 return nil, nil, fmt.Errorf("parsing YAML: %w", err)
74 default:
75 // Encoding would drop every document after the first. Yarn rejects
76 // such a file anyway (js-yaml single-document load).
77 var next yaml.Node
78 if err := decoder.Decode(&next); !errors.Is(err, io.EOF) {
79 return nil, nil, errors.New("found more than one YAML document, which Yarn does not load; keep a single document, or add the entry by hand")
80 }
81 }
83 root, err := yarnMapping(doc.Content[0], "the top level")
84 if err != nil {
85 return nil, nil, err
86 }
88 registries, err := yarnUpsertMapping(root, "npmRegistries")
89 if err != nil {
90 return nil, nil, err
91 }
93 // Yarn strips one trailing "/" from keys, and getRegistryConfiguration
94 // tries the scheme-qualified key before the "//" one. Refreshing only the
95 // "//" key would leave a stale "https://" token winning.
96 key := yarnRegistryKey(registry)
97 entries, err := yarnPairs(registries, func(k string) bool {
98 k = strings.TrimSuffix(k, "/")
99 return k == key || k == registry.Scheme+":"+key
100 })
101 if err != nil {
102 return nil, nil, err
103 }
104 if len(entries) == 0 {
105 // Quoted the way Yarn's own documentation spells registry keys.
106 pair := yarnPair{
107 key: &yaml.Node{Kind: yaml.ScalarNode, Style: yaml.DoubleQuotedStyle, Value: key},
108 value: &yaml.Node{Kind: yaml.MappingNode},
109 }
110 registries.Content = append(registries.Content, pair.key, pair.value)
111 entries = append(entries, pair)
112 }
114 for _, pair := range entries {
115 entry, err := yarnMapping(pair.value, "the npmRegistries entry for "+pair.key.Value)
116 if err != nil {
117 return nil, nil, err
118 }
119 // Yarn sends npmAuthToken on unscoped fetches only when npmAlwaysAuth
120 // is set (shouldAuthenticate in plugin-npm).
121 if err := yarnSetScalar(entry, "npmAlwaysAuth", "!!bool", "true"); err != nil {
122 return nil, nil, err
123 }
124 if err := yarnSetScalar(entry, "npmAuthToken", "!!str", token); err != nil {
125 return nil, nil, err
126 }
127 }
129 encoder := yaml.NewEncoder(&out)
130 encoder.SetIndent(2)
131 if err := encoder.Encode(&doc); err != nil {
132 return nil, nil, fmt.Errorf("encoding YAML: %w", err)
133 }
134 if err := encoder.Close(); err != nil {
135 return nil, nil, fmt.Errorf("encoding YAML: %w", err)
136 }
138 return out.Bytes(), yarnScopesWithOwnAuth(root, registry), nil
141// yarnRegistryKey returns "//" plus the host and escaped path as typed, minus
142// one trailing "/". Unlike loginNpm, it does not normalize the host, because
143// Yarn matches keys against npmRegistryServer literally.
144func yarnRegistryKey(registry *url.URL) string {
145 return "//" + registry.Host + strings.TrimSuffix(registry.EscapedPath(), "/")
148// yarnMapping turns a null value into an empty mapping. For Yarn only an
149// empty value means no entries: it parses with the js-yaml FAILSAFE_SCHEMA, so
150// "~" and "null" are strings there.
151func yarnMapping(n *yaml.Node, what string) (*yaml.Node, error) {
152 if n.Kind == yaml.AliasNode || n.Anchor != "" {
153 return nil, errYarnAnchorOrAlias
154 }
155 if n.Kind == yaml.ScalarNode && n.Tag == "!!null" {
156 n.Kind = yaml.MappingNode
157 n.Tag = ""
158 n.Value = ""
159 n.Style = 0
160 }
161 if n.Kind != yaml.MappingNode {
162 return nil, fmt.Errorf("found %s holding something other than a mapping, which this command cannot add an entry to; fix it, or add the entry by hand", what)
163 }
164 return n, nil
167type yarnPair struct {
168 key, value *yaml.Node
171// yarnPairs refuses an alias key, since what it stands for is not visible here
172// and it could be a duplicate of the key being refreshed.
173func yarnPairs(m *yaml.Node, match func(key string) bool) ([]yarnPair, error) {
174 var pairs []yarnPair
175 for i := 0; i+1 < len(m.Content); i += 2 {
176 key := m.Content[i]
177 if key.Kind == yaml.AliasNode {
178 return nil, errYarnAnchorOrAlias
179 }
180 if key.Kind == yaml.ScalarNode && match(key.Value) {
181 pairs = append(pairs, yarnPair{key: key, value: m.Content[i+1]})
182 }
183 }
184 return pairs, nil
187// yarnUpsertMapping refuses a key that appears more than once. Yarn reads the
188// last occurrence, so editing any one of them could leave the token where Yarn
189// never looks.
190func yarnUpsertMapping(m *yaml.Node, key string) (*yaml.Node, error) {
191 pairs, err := yarnPairs(m, func(k string) bool { return k == key })
192 if err != nil {
193 return nil, err
194 }
195 switch len(pairs) {
196 case 0:
197 value := &yaml.Node{Kind: yaml.MappingNode}
198 m.Content = append(m.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: key}, value)
199 return value, nil
200 case 1:
201 return yarnMapping(pairs[0].value, key)
202 default:
203 return nil, fmt.Errorf("found %d %s keys, and Yarn reads only the last one; merge them into one, or add the entry by hand", len(pairs), key)
204 }
207// yarnSetScalar rewrites existing values in place so their comments survive.
208func yarnSetScalar(m *yaml.Node, key, tag, value string) error {
209 pairs, err := yarnPairs(m, func(k string) bool { return k == key })
210 if err != nil {
211 return err
212 }
213 if len(pairs) == 0 {
214 m.Content = append(m.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: key}, &yaml.Node{Kind: yaml.ScalarNode, Tag: tag, Value: value})
215 return nil
216 }
217 for _, pair := range pairs {
218 v := pair.value
219 if v.Kind == yaml.AliasNode || v.Anchor != "" {
220 return errYarnAnchorOrAlias
221 }
222 v.Kind = yaml.ScalarNode
223 v.Tag = tag
224 v.Value = value
225 v.Style = 0
226 v.Content = nil
227 }
228 return nil
231var yarnSchemeRe = regexp.MustCompile(`^[a-z]+:`)
233// yarnScopesWithOwnAuth returns the npmScopes in root that Yarn fetches from
234// registry with their own npmAuthToken or npmAuthIdent. getAuthConfiguration
235// checks the scope before npmRegistries, so the npmRegistries token is never
236// sent for them. A scope without npmRegistryServer uses the top-level one
237// (getScopeRegistry).
238func yarnScopesWithOwnAuth(root *yaml.Node, registry *url.URL) []string {
239 scopes := yarnLastValue(root, "npmScopes")
240 if scopes == nil || scopes.Kind != yaml.MappingNode {
241 return nil
242 }
244 key := yarnRegistryKey(registry)
245 var names []string
246 for i := 0; i+1 < len(scopes.Content); i += 2 {
247 scope := yarnDeref(scopes.Content[i+1])
248 if scope.Kind != yaml.MappingNode {
249 continue
250 }
251 if yarnScalarValue(scope, "npmAuthToken") == "" && yarnScalarValue(scope, "npmAuthIdent") == "" {
252 continue
253 }
254 server := yarnScalarValue(scope, "npmRegistryServer")
255 if server == "" {
256 server = yarnScalarValue(root, "npmRegistryServer")
257 }
258 if yarnSchemeRe.ReplaceAllString(strings.TrimSuffix(server, "/"), "") == key {
259 names = append(names, scopes.Content[i].Value)
260 }
261 }
262 return names
265// yarnLastValue returns the last value for key, the one Yarn reads.
266func yarnLastValue(m *yaml.Node, key string) *yaml.Node {
267 var value *yaml.Node
268 for i := 0; i+1 < len(m.Content); i += 2 {
269 if m.Content[i].Value == key {
270 value = yarnDeref(m.Content[i+1])
271 }
272 }
273 return value
276// yarnScalarValue keeps "~" and "null" as set values, because Yarn's
277// FAILSAFE_SCHEMA parse reads them as strings, and only "" as unset.
278func yarnScalarValue(m *yaml.Node, key string) string {
279 value := yarnLastValue(m, key)
280 if value == nil || value.Kind != yaml.ScalarNode {
281 return ""
282 }
283 return value.Value
286func yarnDeref(n *yaml.Node) *yaml.Node {
287 if n.Kind == yaml.AliasNode && n.Alias != nil {
288 return n.Alias
289 }
290 return n
Diagnostics8 git calls · 41 ms
CallStatusSizeTime041 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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=3
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.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:0200242 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=242
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=5da85e254ab77f5f6e7e1419be57d524a9f8208e
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=242
git.pkt.payload.max=215
git.sideband.band1.bytes=215
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=215
git.pack.trailer=bd426fb1ae09d9fdcf755902c6903cd2eea819b2
Open pack map
fetchtree:0200494 B · 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=494
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=400358373377bb8e4922d8b821520ebdf8130c4d
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=494
git.pkt.payload.max=467
git.sideband.band1.bytes=467
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=467
git.pack.trailer=363b27e4d94209a9331c11b44fe1ce6e5f4e6e81
Open pack map
fetch2003.3 KiB · hit30 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=30
git.response.bytes=3393
git.response.cache=HIT
git.response.duration_ms=30
git.fetch.wants=3b02bc4fefc8f20bdfb4639d339034917fc21410
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=3393
git.pkt.payload.max=3366
git.sideband.band1.bytes=3366
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
a5e2f9dd0c6190c0
Request
174dd2ba…
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