GitLabgitlab-org
cli
main
1package login
3import (
4 "errors"
5 "fmt"
6 "net/url"
7 "os"
8 "path/filepath"
9 "regexp"
10 "runtime"
11 "strings"
12)
14// sbtCredentialRealm is the realm sbt records the credentials under. It is
15// what the Artifact Registry sends on the maven route that both --gradle and
16// --sbt use: `Basic realm="artifact-registry"`, lower case and hyphenated.
17//
18// The realm has to match that header because Ivy keys its credentials store on
19// (host, realm), so `sbt publish` finds no entry when they disagree. The
20// ordinary resolve is looser: sbt's own lookup keys on the host alone
21// (Credentials.forHost), and coursier's DirectCredentials.matches checks the
22// host and user, with the realm gating only the credentials it resends when
23// following a redirect.
24const sbtCredentialRealm = "artifact-registry"
26// sbtCredentialLine renders the line loginSbt writes for host.
27func sbtCredentialLine(realm, host, token string) string {
28 return fmt.Sprintf(`credentials += Credentials(%q, %q, "__token__", %q)`, realm, host, token)
29}
31// sbtCredentialLineRegexp builds the pattern that finds a line previously
32// written by sbtCredentialLine for realm, capturing the host that appears as
33// the second quoted argument.
34//
35// realm goes through regexp.QuoteMeta because it is a constant interpolated
36// into a pattern rather than a literal. A realm carrying "(", ")" or "[" would
37// still compile without it, and would still be written into the file, but would
38// stop matching the line this file writes: every login would then append
39// another entry holding a live token instead of refreshing the existing one.
40//
41// The trailing \r? tolerates a CRLF credentials.sbt. upsertLines splits on "\n"
42// only, so on a CRLF file every line arrives with a trailing carriage return,
43// and an anchored pattern without this would never match a line it had written.
44//
45// Whitespace is tolerated wherever Scala ignores it, and so is a trailing
46// comment in either spelling, "//" or "/* */", which the second group captures
47// so the caller can keep it. All of these are the same credential to sbt, so
48// matching them strictly would append a second entry for the same host instead
49// of refreshing the first. Credentials.forHost then takes the first entry it
50// finds for the host, so sbt would keep using the stale token while this
51// command reported a fresh login.
52func sbtCredentialLineRegexp(realm string) *regexp.Regexp {
53 return regexp.MustCompile(`^[ \t]*credentials[ \t]*\+=[ \t]*Credentials\([ \t]*"` + regexp.QuoteMeta(realm) + `"[ \t]*,[ \t]*"([^"]*)"[ \t]*,[ \t]*"__token__"[ \t]*,[ \t]*"[^"]*"[ \t]*\)[ \t]*((?://|/\*)[^\r]*?)?[ \t]*\r?$`)
54}
56// sbtCommentedLines reports, for each line, whether it starts inside a /* */
57// block comment, and how deep in one the file ends.
58//
59// sbt compiles credentials.sbt as Scala, so a credentials line inside a
60// comment is not a credential. Refreshing one in place would write a live
61// token into a comment: sbt would still have nothing to authenticate with,
62// and this command would report a login that no build has. Appending after a
63// comment the file never closes has the same effect, which is what the
64// returned depth is for.
65//
66// Scala nests block comments, so the depth counts up and down rather than
67// toggling, and a "//" hides the rest of its own line, including a "/*" that
68// would otherwise open a block.
69//
70// The scan is string-literal-blind, and that cuts both ways. A "/*" inside a
71// string literal opens a comment here that Scala never opened, so live entries
72// below it look commented and the next login appends instead of refreshing. In
73// the other direction the "//" every URL carries hides the rest of its line,
74// including a real "/*" after it, so an entry inside that block comment is
75// refreshed in place: a live token written where sbt never reads it. Neither
76// case can arise from a line this command writes; both need a hand-written one
77// that puts a comment marker inside a string.
78func sbtCommentedLines(lines []string) ([]bool, int) {
79 commented := make([]bool, len(lines))
80 depth := 0
82 for i, line := range lines {
83 commented[i] = depth > 0
85 for j := 0; j < len(line); j++ {
86 switch {
87 case depth == 0 && strings.HasPrefix(line[j:], "//"):
88 j = len(line)
89 case strings.HasPrefix(line[j:], "/*"):
90 depth++
91 j++
92 case depth > 0 && strings.HasPrefix(line[j:], "*/"):
93 depth--
94 j++
95 }
96 }
97 }
99 return commented, depth
100}
102var sbtCredentialLineRe = sbtCredentialLineRegexp(sbtCredentialRealm)
104// sbtCredentialsFile is one credentials.sbt loginSbt writes, with the sbt
105// builds that read it.
106type sbtCredentialsFile struct {
107 readBy string
108 path string
109}
111// sbtCredentialsFilesFor returns the credentials.sbt files this command writes,
112// one in each sbt global base, and the name of the environment variable it
113// skipped while resolving the sbt 2.x base, or "" when it skipped none.
114//
115// sbt keeps one global base per binary version, and neither version reads the
116// other's. Which one a build reads follows the sbt.version the project declares
117// in project/build.properties, not the sbt installed, so no single file covers
118// every project on a machine. An unread file in the other base is inert, so
119// writing both is safe.
120//
121// sbt 1.x reads ~/.sbt/1.0 ("Settings that should be applied to all projects
122// can go in $HOME/.sbt/1.0/global.sbt, or any file in $HOME/.sbt/1.0 with a
123// .sbt extension"). sbt 2.x reads <base>/2, and SysProp.defaultGlobalBaseDirectory
124// in sbt 2.0.8 picks <base> from $SBT_CONFIG_HOME, then $XDG_CONFIG_HOME/sbt
125// (%LOCALAPPDATA%\sbt on Windows), then ~/.config/sbt. Those are environment
126// variables, so the 2.x base is resolved here the way sbt resolves it, trimmed
127// as sbt trims them.
128//
129// The -Dsbt.global.base override is a JVM property, so it is not visible here,
130// and BuildPaths.getGlobalBase uses it as is, with no version directory
131// appended. The one that matters is what the sbt.bat shipped with sbt 2.x
132// (2.0.8, lines 727-731) passes whenever LOCALAPPDATA is set:
133// %LOCALAPPDATA%\sbt. Every build that launcher starts, on either sbt version,
134// reads that base, so on Windows it gets a third file. With a 1.x launcher
135// nothing reads it. The property also outranks SBT_CONFIG_HOME, so such a
136// build reads neither of the other two files, and its readBy says so: without
137// that, a user who set SBT_CONFIG_HOME would take the sbt 2.x file for the one
138// in use.
139//
140// One departure: a relative value is skipped, as the XDG Base Directory spec
141// requires ("If an implementation encounters a relative path in any of these
142// variables it should consider the path invalid and ignore it"). sbt instead
143// resolves it against the directory the build runs in, which this command
144// cannot know, so no file written here would match it. A blank value is
145// skipped too: sbt drops only an empty one, so it resolves a blank one to the
146// build directory as well. Either way the sbt 2.x file reported is one sbt 2.x
147// does not read, which is why the skipped name is returned.
148func sbtCredentialsFilesFor(home, goos string) ([]sbtCredentialsFile, string) {
149 configVar := "XDG_CONFIG_HOME"
150 if goos == "windows" {
151 configVar = "LOCALAPPDATA"
152 }
154 base2 := filepath.Join(home, ".config", "sbt")
155 skipped := ""
156 for _, name := range []string{"SBT_CONFIG_HOME", configVar} {
157 if value, ok := absoluteEnv(name); ok {
158 base2 = value
159 if name == configVar {
160 base2 = filepath.Join(value, "sbt")
161 }
162 break
163 }
164 if skipped == "" && os.Getenv(name) != "" {
165 skipped = name
166 }
167 }
169 files := []sbtCredentialsFile{
170 {readBy: "sbt 1.x", path: filepath.Join(home, ".sbt", "1.0", "credentials.sbt")},
171 {readBy: "sbt 2.x", path: filepath.Join(base2, "2", "credentials.sbt")},
172 }
173 if goos == "windows" {
174 if localAppData, ok := absoluteEnv("LOCALAPPDATA"); ok {
175 files = append(files, sbtCredentialsFile{
176 readBy: "sbt 1.x and 2.x builds started by the sbt.bat of sbt 2.x, which read no other file, even with SBT_CONFIG_HOME set",
177 path: filepath.Join(localAppData, "sbt", "credentials.sbt"),
178 })
179 }
180 }
181 return files, skipped
182}
184// absoluteEnv returns the environment variable name with surrounding
185// whitespace trimmed, and false when it is unset, blank, or a relative path.
186func absoluteEnv(name string) (string, bool) {
187 value := strings.TrimSpace(os.Getenv(name))
188 return value, filepath.IsAbs(value)
189}
191// describeSbtCredentialsFiles renders files as "<path> for <readBy>", joined
192// with "and", for the success line and the partial-write error.
193func describeSbtCredentialsFiles(files []sbtCredentialsFile) string {
194 parts := make([]string, len(files))
195 for i, file := range files {
196 parts[i] = fmt.Sprintf("%s for %s", file.path, file.readBy)
197 }
198 return strings.Join(parts, " and ")
199}
201// loginSbt upserts a `credentials += Credentials("artifact-registry", host,
202// "__token__", token)` line in path, or in every file sbtCredentialsFilesFor
203// returns when path is empty, for registry's host, matched by the host appearing
204// as the second quoted argument, so a different host's line is left untouched.
205// It returns the files written and the variable sbtCredentialsFilesFor skipped.
206//
207// A failure after an earlier file was written still fails the login, and the
208// error names the files that do hold the token: a build that reads the file
209// that failed would go out unauthenticated after a login reported as done.
210func loginSbt(path string, registry *url.URL, token string) ([]sbtCredentialsFile, string, error) {
211 // Hostname(), not Host: sbt's IvyAuthenticator and coursier both look
212 // credentials up by the port-less requesting host
213 // (Authenticator.getRequestingHost / URI.getHost). Writing "host:port"
214 // here for a registry on a non-standard port would leave sbt finding no
215 // match at build time and sending the request unauthenticated, with no
216 // error at login time.
217 //
218 // Lower-cased for the same reason the match has to be reliable: host names
219 // are case-insensitive, so the same registry typed two ways must land on one
220 // entry rather than two that disagree about which token is current.
221 host := strings.ToLower(registry.Hostname())
223 line := sbtCredentialLine(sbtCredentialRealm, host, token)
225 files := []sbtCredentialsFile{{readBy: "--config-file", path: path}}
226 skipped := ""
227 if path == "" {
228 home, err := homeDir()
229 if err != nil {
230 return nil, "", err
231 }
232 files, skipped = sbtCredentialsFilesFor(home, runtime.GOOS)
233 }
235 var written []sbtCredentialsFile
236 for _, file := range files {
237 if err := upsertSbtCredentialLine(file.path, host, line); err != nil {
238 if len(written) == 0 {
239 return nil, skipped, err
240 }
241 return written, skipped, fmt.Errorf("wrote %s, but not the file for %s: %w", describeSbtCredentialsFiles(written), file.readBy, err)
242 }
243 written = append(written, file)
244 }
246 return written, skipped, nil
247}
249// upsertSbtCredentialLine refreshes host's entry in the credentials.sbt at
250// path with line, or appends line when the file has none.
251func upsertSbtCredentialLine(path, host, line string) error {
252 return upsertLines(path, func(lines []string) ([]string, error) {
253 commented, depth := sbtCommentedLines(lines)
254 if depth > 0 {
255 return nil, errors.New("found an unterminated /* comment: the credentials line would be appended inside it, where sbt never reads it; close the comment, or add the line by hand")
256 }
258 found := false
259 for i, l := range lines {
260 if commented[i] {
261 continue
262 }
263 m := sbtCredentialLineRe.FindStringSubmatch(l)
264 if len(m) < 3 || m[1] != host {
265 continue
266 }
267 // The entry keeps the indentation and the trailing comment it had:
268 // only the token is this command's to change, and a refresh that
269 // drops the note somebody left on the line is a silent edit.
270 indent := l[:len(l)-len(strings.TrimLeft(l, " \t"))]
271 lines[i] = indent + line
272 if m[2] != "" {
273 lines[i] += " " + m[2]
274 }
275 found = true
276 }
277 if !found {
278 lines = append(lines, line)
279 }
280 return lines, nil
281 })
282}
Diagnostics8 git calls · 364 ms
CallStatusSizeTime0364 ms
discover200185 B2 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 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 · 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=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=44Open pack map
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=4 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:02001.8 KiB · hit4 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=4 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=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200242 B · hit5 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=5 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=bd426fb1ae09d9fdcf755902c6903cd2eea819b2Open 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=348 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=363b27e4d94209a9331c11b44fe1ce6e5f4e6e81Open pack map
fetch2004.6 KiB348 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4757 git.response.cache=MISS git.response.duration_ms=348 git.fetch.wants=404313a59f9011eae2cb86813745fcdb7ee1efd6 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=4757 git.pkt.payload.max=4730 git.sideband.band1.bytes=4730 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
- a5e2f6a229e83a24
- Request
- b7b5d0c6…
- 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