GitLabgitlab-org
cli
main
1package login
3import (
4 "fmt"
5 "slices"
6 "strings"
8 "gitlab.com/gitlab-org/cli/internal/config"
9 "gitlab.com/gitlab-org/cli/internal/dockercredhelper"
10 "gitlab.com/gitlab-org/cli/internal/iostreams"
11)
13// loginDocker configures Docker to authenticate against registry using
14// glab's Docker credential helper: it installs the docker-credential-glab
15// shim, wires it into Docker's credHelpers for registry, and records
16// registry in hostname's artifact_registry_domains config key so the
17// credential helper (internal/commands/auth/docker.Helper.Get) can find it
18// later.
19func loginDocker(io *iostreams.IOStreams, cfg config.Config, hostname, registry string) error {
20 installation, err := dockercredhelper.Install()
21 if err != nil {
22 return err
23 }
24 if !installation.OnPath {
25 io.LogErrorf("%s %s\n", io.Color().WarnIcon(), installation.PathWarning())
26 }
28 dir, err := dockercredhelper.ConfigDir()
29 if err != nil {
30 return err
31 }
33 registrations, err := dockercredhelper.Register(dir, registry)
34 if err != nil {
35 return err
36 }
38 // Read-modify-write, not compare-and-swap: two concurrent `glab
39 // artifact-registry login` runs against the same $HOME still race when they
40 // add different domains, and the last writer's update wins. Acceptable for
41 // an interactively-invoked CLI.
42 domains, _, err := cfg.GetWithSource(hostname, "artifact_registry_domains", false)
43 if err != nil {
44 return unrecordedRegistryError(registry, fmt.Errorf("reading artifact_registry_domains: %w", err))
45 }
47 // The Set and the Write are skipped when the domain is already recorded, not
48 // just the append: a re-login has nothing to add, and writing anyway would
49 // rewrite the file from this snapshot, dropping a domain a concurrent login
50 // added after the read. That narrows the race above to logins that really
51 // change something.
52 if parsed := config.ParseDomains(domains); !slices.Contains(parsed, registry) {
53 parsed = append(parsed, registry)
55 if err := cfg.Set(hostname, "artifact_registry_domains", strings.Join(parsed, ",")); err != nil {
56 return unrecordedRegistryError(registry, fmt.Errorf("setting artifact_registry_domains: %w", err))
57 }
58 if err := cfg.Write(); err != nil {
59 return unrecordedRegistryError(registry, fmt.Errorf("writing config: %w", err))
60 }
61 }
63 io.LogInfof("%s Configured Docker credential helper for %s.\n", io.Color().GreenCheck(), registry)
64 for _, reg := range registrations {
65 if reg.ShadowedLogin {
66 io.LogErrorf("%s %s\n", io.Color().WarnIcon(), reg.ShadowedLoginWarning())
67 }
68 }
70 warnIfAnotherHostResolvesRegistry(io, cfg, hostname, registry)
71 warnIfContainerRegistryDomain(io, cfg, hostname, registry)
73 return nil
74}
76// unrecordedRegistryError annotates any configuration failure between
77// Register and the config write landing, by which point Docker's credHelpers
78// already points at glab for registry.
79// Docker now calls the credential helper, which resolves a registry through
80// artifact_registry_domains and will not find this one, so `docker pull` fails
81// until the value is recorded.
82//
83// Register deliberately runs first, so a registry another helper already owns
84// is refused without touching the configuration file. That ordering makes this
85// window the price, so name it in the error rather than reporting a bare write
86// failure.
87func unrecordedRegistryError(registry string, err error) error {
88 return fmt.Errorf("%w; Docker is already pointed at glab for %s but the registry was not recorded, so docker pull fails against it until this command is rerun", err, registry)
89}
91// warnIfAnotherHostResolvesRegistry warns when a host other than hostname is
92// the one the Docker credential helper will resolve registry through.
93// artifact_registry_domains is per-host, but the helper takes the first host
94// that lists the domain (internal/commands/auth/docker.Helper's
95// findHostnameByDomain, over cfg.Hosts(), which puts gitlab.com first and then
96// keeps configuration order), so registering a domain a second time under a
97// later host writes an entry nothing will ever read: this login reports
98// success while `docker pull` keeps authenticating against the earlier host.
99//
100// Called after the write, so the host being configured is already in the list
101// and the loop below sees exactly the state the helper will see.
102//
103// A read failure is reported and skipped, as in warnIfContainerRegistryDomain.
104// That can make an unreadable earlier host look like it does not list the
105// domain, but the failure is on stderr either way.
106func warnIfAnotherHostResolvesRegistry(io *iostreams.IOStreams, cfg config.Config, hostname, registry string) {
107 hostnames, err := cfg.Hosts()
108 if err != nil {
109 io.LogErrorf("%s Could not check whether another host also lists %s: %v\n", io.Color().WarnIcon(), registry, err)
110 return
111 }
113 for _, candidate := range hostnames {
114 domains, _, err := cfg.GetWithSource(candidate, "artifact_registry_domains", false)
115 if err != nil {
116 io.LogErrorf("%s Could not read artifact_registry_domains for %s: %v\n", io.Color().WarnIcon(), candidate, err)
117 continue
118 }
119 if !slices.Contains(config.ParseDomains(domains), registry) {
120 continue
121 }
123 if candidate == hostname {
124 return
125 }
127 io.LogErrorf("%s %s is also listed in artifact_registry_domains for %s. The Docker credential helper takes the first host that lists a domain, so docker pull authenticates against %s, not %s. To use %s, drop %s from the other host with `glab config set artifact_registry_domains <remaining domains> --host %s`.\n",
128 io.Color().WarnIcon(), registry, candidate, candidate, hostname, hostname, registry, candidate)
129 return
130 }
131}
133// warnIfContainerRegistryDomain warns when registry is also listed in some
134// host's container_registry_domains. Both keys feed the same credential
135// helper, which prefers the artifact-registry token exchange and falls back to
136// the container-registry credentials only when that exchange fails
137// (internal/commands/auth/docker.Helper.Get).
138//
139// The warning says the fallback will not fire, because for this domain it
140// cannot: run only reaches loginDocker once the exchange has succeeded, so
141// Helper.Get takes the artifact-registry branch and returns. The dangerous
142// case is a container registry listed here by mistake, which
143// `glab auth login` puts in container_registry_domains for gitlab.com,
144// registry.gitlab.com, and gitlab.com:443 by default: `docker pull` worked
145// before this login and now hard-fails on an artifact-registry token the
146// container registry rejects. So the message leads with undoing this login,
147// and keeps dropping the container_registry_domains entry as the remedy for a
148// domain the Artifact Registry really backs.
149//
150// A read failure here is reported, not returned: the login itself already
151// succeeded, and failing it now would misreport work that is on disk.
152func warnIfContainerRegistryDomain(io *iostreams.IOStreams, cfg config.Config, arHostname, registry string) {
153 hostnames, err := cfg.Hosts()
154 if err != nil {
155 io.LogErrorf("%s Could not check whether %s is also a container registry domain: %v\n", io.Color().WarnIcon(), registry, err)
156 return
157 }
159 for _, hostname := range hostnames {
160 domains, _, err := cfg.GetWithSource(hostname, "container_registry_domains", false)
161 if err != nil {
162 io.LogErrorf("%s Could not read container_registry_domains for %s: %v\n", io.Color().WarnIcon(), hostname, err)
163 continue
164 }
165 if slices.Contains(config.ParseDomains(domains), registry) {
166 io.LogErrorf("%s %s is also listed in container_registry_domains for %s. The Docker credential helper falls back to those credentials only when the artifact registry exchange fails, and this login just verified the exchange succeeds, so the fallback will not fire: docker pull now authenticates with an artifact registry token. If %s is a container registry, it rejects that token; undo this login with `glab config set artifact_registry_domains <remaining domains> --host %s`. If the Artifact Registry backs %s, drop the container_registry_domains entry instead with `glab config set container_registry_domains <remaining domains> --host %s`.\n",
167 io.Color().WarnIcon(), registry, hostname, registry, arHostname, registry, hostname)
168 }
169 }
170}
Diagnostics9 git calls · 42 ms
CallStatusSizeTime042 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · 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=5 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=44Open pack map
fetchtree:0 · deepen 1200706 B · 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=706 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 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=706 git.pkt.payload.max=606 git.sideband.band1.bytes=606 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=606 git.pack.trailer=5b8940d8a309b9f86f08953120610ba63124325eOpen pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273 git.response.cache=HIT git.response.duration_ms=5 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 · hit3 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=3 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=4 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 · hit4 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=494 git.response.cache=HIT git.response.duration_ms=4 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
fetch2002.5 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2601 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=a7e2b2a6c25cb93655a2ccb9cbd388f757984cad 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=2601 git.pkt.payload.max=2574 git.sideband.band1.bytes=2574 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
- a5e2d41c8b70689d
- Request
- 79363101…
- 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