GitLabgitlab-org

cli

main
ply8 git calls242 ms100 KiB6 cachedprotocol v2
186 lines6.7 KiBRaw
1package login
3import (
4 "os"
5 "path/filepath"
6 "strings"
7)
9// gradlePropertyKey returns the key that line assigns to, or "" when line is
10// not a "key=value" assignment at all.
11//
12// Whitespace around the key and the "=" is tolerated because
13// java.util.Properties tolerates it: a plain "key=" prefix test misses
14// "myAliasPassword = old", so a refresh would append a second assignment and
15// leave the superseded token sitting in the file forever. Later assignments win
16// in a properties file, so the login would still work, which is exactly what
17// makes the stale credential easy to miss.
18//
19// A ":" or bare-space separator, which Properties also accepts, is still not
20// recognized. Those are rare in a hand-written gradle.properties, and the cost
21// of missing one is the duplicate-plus-stale-line case above rather than a
22// broken login.
23func gradlePropertyKey(line string) string {
24 key, _, ok := strings.Cut(line, "=")
25 if !ok {
26 return ""
27 }
28 return strings.TrimSpace(key)
31// gradlePropertiesValue escapes value for the right-hand side of a
32// java.util.Properties assignment, where "\" is the escape character rather
33// than a literal.
34//
35// Properties reads an unescaped "\" before an ordinary character as that
36// character ("\b" is read back as "b"), and one at the end of a line as a join
37// with the line below. So a --registry ending in "\" would swallow the
38// assignment written after it: the URL property would absorb
39// "{alias}Username=__token__", which would then never be defined, and Gradle
40// would authenticate with no username after a login that printed a green
41// check.
42//
43// Only "\" is escaped. The other characters Properties treats specially, "=",
44// ":" and leading whitespace, are only special in a key, and every key here is
45// an alias that registryAliasRe or defaultRegistryAlias has already restricted
46// to letters, digits, ".", "_" and "-".
47func gradlePropertiesValue(value string) string {
48 return strings.ReplaceAll(value, `\`, `\\`)
51// gradleIsComment reports whether line is a comment, which
52// java.util.Properties recognizes by its first non-blank character being "#"
53// or "!".
54func gradleIsComment(line string) bool {
55 trimmed := strings.TrimLeft(line, " \t\f")
56 return strings.HasPrefix(trimmed, "#") || strings.HasPrefix(trimmed, "!")
59// gradleContinuedLines reports, for each line, whether it continues the
60// previous line's value rather than starting an assignment of its own, and
61// whether the file's last line leaves a continuation open with nothing after
62// it.
63//
64// java.util.Properties joins a line ending in an odd number of backslashes
65// with the line after it, so a "myAliasPassword=x" arriving as such a
66// follow-on line is part of somebody else's value. Rewriting one would put the
67// token inside that unrelated value and leave the alias's own property unset:
68// Gradle would authenticate with nothing, and this command would report a
69// login no build has.
70//
71// A comment never joins, which the spec is explicit about: "a comment line
72// cannot be extended in this manner; every natural line that is a comment must
73// have its own comment indicator". Reading "# see docs \" as a join would hide
74// the real assignment below it, so a refresh would append a second one and
75// leave the superseded token in the file. A line that is itself a continuation
76// is not a comment however much its first character looks like one, so the
77// carve-out only applies where a logical line starts.
78func gradleContinuedLines(lines []string) ([]bool, bool) {
79 continued := make([]bool, len(lines))
81 joins := false
82 for i, line := range lines {
83 continued[i] = joins
85 trimmed := strings.TrimSuffix(line, "\r")
86 if !joins && gradleIsComment(trimmed) {
87 joins = false
88 continue
89 }
90 joins = (len(trimmed)-len(strings.TrimRight(trimmed, `\`)))%2 == 1
91 }
93 return continued, joins
96// gradlePropertiesPathFor returns the gradle.properties this command writes,
97// under the Gradle User Home.
98//
99// GRADLE_USER_HOME wins over ~/.gradle, because that is where Gradle then
100// reads it: "By default, the Gradle User Home (~/.gradle ...) stores global
101// configuration properties ... It can be set with the environment variable
102// GRADLE_USER_HOME". Ignoring it on a machine that sets it, which is common in
103// CI, writes a gradle.properties no build ever opens, and the command still
104// prints a green check.
105func gradlePropertiesPathFor(home string) string {
106 dir := os.Getenv("GRADLE_USER_HOME")
107 if dir == "" {
108 dir = filepath.Join(home, ".gradle")
109 }
110 return filepath.Join(dir, "gradle.properties")
113// loginGradle upserts three properties in the Gradle User Home's
114// gradle.properties: "{alias}Url", "{alias}Username" (always "__token__"), and
115// "{alias}Password" (token). Matching keys are updated in place; unrelated
116// lines and their order are preserved.
117func loginGradle(registry, alias, token string) error {
118 home, err := homeDir()
119 if err != nil {
120 return err
121 }
123 path := gradlePropertiesPathFor(home)
125 props := []struct{ key, value string }{
126 {alias + "Url", gradlePropertiesValue(registry)},
127 {alias + "Username", "__token__"},
128 {alias + "Password", gradlePropertiesValue(token)},
129 }
131 return upsertLines(path, func(lines []string) ([]string, error) {
132 continued, _ := gradleContinuedLines(lines)
134 found := make(map[string]struct{}, len(props))
135 // A replaced property's own continuation lines go with it. The new
136 // value is one line, so leaving them would strand the tail of the
137 // superseded token in the file as a stray property of its own.
138 drop := make([]bool, len(lines))
139 for i, line := range lines {
140 if continued[i] {
141 continue
142 }
143 key := gradlePropertyKey(line)
144 for _, p := range props {
145 if key != p.key {
146 continue
147 }
148 lines[i] = p.key + "=" + p.value
149 found[p.key] = struct{}{}
150 for j := i + 1; j < len(lines) && continued[j]; j++ {
151 drop[j] = true
152 }
153 }
154 }
156 kept := lines[:0]
157 for i, line := range lines {
158 if !drop[i] {
159 kept = append(kept, line)
160 }
161 }
162 lines = kept
164 // Recomputed, since dropping a continuation line can have taken the
165 // open continuation with it.
166 _, dangling := gradleContinuedLines(lines)
168 if len(found) < len(props) && dangling {
169 // The last line leaves a continuation open, so the first property
170 // appended below would be read as the rest of that property's
171 // value: the assignment would never be defined and the unrelated
172 // property would be corrupted. An empty line closes the
173 // continuation without changing the value it belongs to, since
174 // Properties strips a continuation line's leading whitespace and an
175 // empty one contributes nothing.
176 lines = append(lines, "")
177 }
179 for _, p := range props {
180 if _, ok := found[p.key]; !ok {
181 lines = append(lines, p.key+"="+p.value)
182 }
183 }
184 return lines, nil
185 })
Diagnostics8 git calls · 242 ms
CallStatusSizeTime0242 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 KiB196 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=192
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=196
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 · 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=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: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
fetchtree:02001.8 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=5
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 · 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=242
git.response.cache=HIT
git.response.duration_ms=2
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 · 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=494
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.7 KiB · hit33 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=33
git.response.bytes=2787
git.response.cache=HIT
git.response.duration_ms=33
git.fetch.wants=7b9abb020ab18e35376b9eb932489f3b1f5c8100
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=2787
git.pkt.payload.max=2760
git.sideband.band1.bytes=2760
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
a5e30393ff017929
Request
0f2f037a…
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