GitLabgitlab-org

cli

main
ply8 git calls199 ms101 KiB6 cachedprotocol v2
273 lines11 KiBRaw
1package docker
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "slices"
9 "time"
11 "github.com/docker/docker-credential-helpers/credentials"
13 "gitlab.com/gitlab-org/cli/internal/config"
14 "gitlab.com/gitlab-org/cli/internal/dbg"
15 "gitlab.com/gitlab-org/cli/internal/glinstance"
16 "gitlab.com/gitlab-org/cli/internal/iostreams"
17 "gitlab.com/gitlab-org/cli/internal/oauth2"
20var _ credentials.Helper = (*Helper)(nil)
22// errNoHostnameForDomain is the shared "no host lists this domain" signal
23// findHostnameByDomain returns. Callers translate it into their own
24// domain-specific error or sentinel.
25var errNoHostnameForDomain = errors.New("no hostname associated with domain")
27// artifactRegistryTimeout bounds the artifact-registry token exchange Get
28// attempts before falling back to (or failing with) the container-registry
29// path. An instance without Artifact Registry answers 403/404 in
30// milliseconds; this is only spent when the server accepts the connection
31// and never answers.
32const artifactRegistryTimeout = 30 * time.Second
34// Helper implements a docker-credential-* helper interface.
35// It assists in logging into a registry.
36type Helper struct {
37 client *http.Client
38 cfg config.Config
39 io *iostreams.IOStreams
40 // userAgent is used only when building the artifact-registry API client,
41 // since that client is built fresh per-host rather than reused from the
42 // caller's default-host client.
43 userAgent string
44 // ctx is the command's context, so a cancellation or deadline set by the
45 // caller (rather than a fresh, unrelated context.Background()) governs the
46 // artifact-registry exchange. Get is a fixed third-party interface method
47 // with no context parameter of its own, so this is the only way to thread
48 // one through. A zero-value Helper (as constructed by tests that only
49 // exercise config-driven paths) falls back to context.Background().
50 ctx context.Context
53// Get fetches the glab auth token for registryURL. It tries the Artifact
54// Registry first; on any failure, it falls back to the container-registry
55// path, using that credential and logging the fall-through if the fallback
56// succeeds. If neither produces a credential, it prefers the artifact
57// registry error whenever the domain was configured for it, since that
58// names the actual failure, where the container-registry path can only say
59// the domain is unassociated.
60func (h *Helper) Get(registryURL string) (string, string, error) {
61 base := h.ctx
62 if base == nil {
63 base = context.Background()
64 }
65 ctx, cancel := context.WithTimeout(base, artifactRegistryTimeout)
66 defer cancel()
68 arUser, arToken, arErr := h.getArtifactRegistryToken(ctx, registryURL)
69 if arErr == nil {
70 return arUser, arToken, nil
71 }
73 crUser, crToken, crErr := h.getContainerRegistryToken(registryURL)
74 if crErr == nil {
75 // LogErrorf, not dbg.Debugf: Docker forwards the helper's stderr, so
76 // this is the only way a broken artifact-registry config (silently
77 // degrading to the container-registry credential on every pull) is
78 // visible without DEBUG=1.
79 if !errors.Is(arErr, errNotArtifactRegistry) {
80 h.io.LogErrorf("%s artifact registry exchange failed for %s, falling back to container registry credentials: %v\n",
81 h.io.Color().WarnIcon(), registryURL, arErr)
82 }
83 return crUser, crToken, nil
84 }
86 if !errors.Is(arErr, errNotArtifactRegistry) {
87 // crErr is joined in when it names a real container-registry failure
88 // (for example, a dual-listed domain whose stored credentials are
89 // empty), so that failure is not silently swallowed by arErr alone.
90 // A domain simply unassociated with container_registry_domains carries
91 // no such signal, so it is left out: arErr already names the actual
92 // exchange failure, and "no hostname associated" would read as if the
93 // domain were unknown rather than misconfigured for artifact registry.
94 if !errors.Is(crErr, errNoHostnameForDomain) {
95 return "", "", errors.Join(arErr, crErr)
96 }
97 return "", "", arErr
98 }
99 return "", "", crErr
102// getContainerRegistryToken fetches the glab auth token for registryURL from
103// the container-registry path: it reads the stored user/token for the
104// associated host directly, without minting anything new.
105func (h *Helper) getContainerRegistryToken(registryURL string) (string, string, error) {
106 hostname, err := h.findAssociatedHostname(registryURL)
107 if err != nil {
108 return "", "", err
109 }
111 // searchEnvForIdentity: false, for the same reason as the token read below: a
112 // stray GLAB_IS_OAUTH2 inherited from the user's shell must not decide how the
113 // stored per-host credentials are interpreted.
114 isOAuth2, _, err := h.cfg.GetWithSource(hostname, "is_oauth2", false)
115 if err != nil {
116 return "", "", fmt.Errorf("failed to read is_oauth2 for %q: %w", hostname, err)
117 }
119 cfg := h.cfg
121 // Only an OAuth2 access token expires, so only an OAuth2 host has anything to
122 // refresh. A host authenticated with a personal access token has no
123 // oauth2_expiry_date, and the token source fails parsing that empty value
124 // before it ever reads the user and token this needs.
125 if isOAuth2 == "true" {
126 // searchEnvForIdentity: false. This helper runs as a Docker subprocess
127 // that inherits the user's shell environment, so a stray GITLAB_TOKEN must
128 // not decide which identity's token gets refreshed here.
129 ts, err := oauth2.NewConfigTokenSource(h.cfg, h.client, glinstance.DefaultProtocol, hostname, false)
130 if err != nil {
131 return "", "", fmt.Errorf("failed to get OAuth2 token source to potentially refresh token: %w", err)
132 }
133 if _, err := ts.Token(); err != nil {
134 return "", "", fmt.Errorf("refreshing oauth2 token: %w", err)
135 }
137 // ts.Token() persists a refreshed token but does not mutate our in-memory
138 // config, so re-read it to pick up a rotated token. For a file-backed host the
139 // in-memory copy would otherwise still hold the pre-refresh (now expired)
140 // token; a keyring-backed host reads live, but reloading is correct for both.
141 // We deliberately do not use the token ts.Token() returns: it is derived with
142 // env-var lookup enabled, and GITLAB_TOKEN must not override the per-host
143 // credential here.
144 //
145 // A config that cannot be re-read is not fatal (for example, the config lives
146 // in an XDG_CONFIG_DIRS entry and no user-level file exists yet, so no refresh
147 // ever created one); fall back to the in-memory copy, matching freshest().
148 if fresh, err := h.cfg.Reload(); err == nil {
149 cfg = fresh
150 } else {
151 dbg.Debugf("docker helper: could not re-read config for %q, using in-memory copy: %v", hostname, err)
152 }
153 }
155 user, err := cfg.Get(hostname, "user")
156 if err != nil {
157 return "", "", err
158 }
160 // Skip env var lookup: GITLAB_TOKEN should not override per-host credentials
161 // stored in config when acting as a Docker credential helper.
162 token, _, err := cfg.GetWithSource(hostname, "token", false)
163 if err != nil {
164 return "", "", err
165 }
167 if user == "" {
168 return "", "", fmt.Errorf("glab user for this registryURL (hostname) is empty")
169 }
171 if token == "" {
172 return "", "", fmt.Errorf("glab token for this registryURL (hostname) is empty")
173 }
175 return user, token, nil
178// findAssociatedHostname takes a GitLab container registry URL
179// and finds its associated GitLab instance's hostname.
180//
181// The returned error wraps errNoHostnameForDomain when the domain is simply
182// unassociated, so Get can tell that case apart from a real container-registry
183// failure worth surfacing alongside an artifact-registry error.
184func (h *Helper) findAssociatedHostname(registryURL string) (string, error) {
185 hostname, err := h.findHostnameByDomain("container_registry_domains", registryURL)
186 if errors.Is(err, errNoHostnameForDomain) {
187 return "", fmt.Errorf("no hostname associated with registryURL: %s: %w", registryURL, errNoHostnameForDomain)
188 }
189 return hostname, err
192// findHostnameByDomain scans every host's domainsConfigKey for registryURL,
193// returning the first host that lists it.
194//
195// A key that is simply not set reads back as "" with no error, so an error
196// from GetWithSource is a real read failure: structurally broken YAML for
197// that host, or a locked or denied keyring. Such an error must not be
198// allowed to look like "this host doesn't list the domain", which would
199// report configuration that exists but couldn't be read as missing.
200//
201// The errors are collected rather than returned on the spot: a broken entry
202// for one host must not stop a different host from legitimately resolving
203// the domain. A read failure can only have changed the answer when no host
204// matched, so that is where it is reported.
205func (h *Helper) findHostnameByDomain(domainsConfigKey, registryURL string) (string, error) {
206 hostnames, err := h.cfg.Hosts()
207 if err != nil {
208 return "", err
209 }
211 var readErr error
213 for _, hostname := range hostnames {
214 domains, err := readDomains(h.cfg, hostname, domainsConfigKey)
215 if err != nil {
216 readErr = errors.Join(readErr, err)
217 continue
218 }
219 if slices.Contains(domains, registryURL) {
220 if readErr != nil {
221 h.io.LogErrorf("%s another host's configuration could not be read; if it also handles %s, glab may be using the wrong GitLab instance's credentials: %v\n",
222 h.io.Color().WarnIcon(), registryURL, readErr)
223 }
224 return hostname, nil
225 }
226 }
228 if readErr != nil {
229 return "", readErr
230 }
232 return "", errNoHostnameForDomain
235type helperError struct {
236 action string
237 serverURL string
240func (e helperError) Error() string {
241 msg := "glab auth docker-helper does not support manual registry " + e.action + "s. "
242 msg += "Remove the glab credential helper for " + e.serverURL + " "
243 msg += "if you'd like to manually handle credentials for this registry."
244 return msg
247func (h *Helper) Add(creds *credentials.Credentials) error {
248 return helperError{action: "login", serverURL: creds.ServerURL}
251func (h *Helper) Delete(serverURL string) error {
252 return helperError{action: "logout", serverURL: serverURL}
255func (h *Helper) List() (map[string]string, error) {
256 return nil, errors.New("glab auth docker-helper does not support listing registries")
259// readDomains reads domainsConfigKey for hostname and parses it into a
260// domain slice.
261//
262// A key that is simply not set reads back as "" with no error, so an error
263// here is a real read failure: structurally broken YAML for the host, or a
264// locked or denied keyring. Callers must not let that look like "this host
265// doesn't list the domain," which would report configuration that exists
266// but couldn't be read as missing.
267func readDomains(cfg config.Config, hostname, domainsConfigKey string) ([]string, error) {
268 domains, _, err := cfg.GetWithSource(hostname, domainsConfigKey, false)
269 if err != nil {
270 return nil, fmt.Errorf("failed to read %s for %q: %w", domainsConfigKey, hostname, err)
271 }
272 return config.ParseDomains(domains), nil
Diagnostics8 git calls · 199 ms
CallStatusSizeTime0199 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 · 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=2
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 · 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=44
Open pack map
fetchtree:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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:0200358 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=358
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=5d783fac547a70b19ca138c47d067b44febb52da
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=358
git.pkt.payload.max=331
git.sideband.band1.bytes=331
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=331
git.pack.trailer=22a484e5ecad6d6e62b224e8a7f90cf5ff085938
Open pack map
fetchtree:0200352 B · hit4 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=352
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=078b54e79fa23822972abf99b4c28a7b8a3e2806
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=352
git.pkt.payload.max=325
git.sideband.band1.bytes=325
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=325
git.pack.trailer=756e6cebb5c15b5cd4bd8e9270eb683dbb54a67c
Open pack map
fetch2003.7 KiB185 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=185
git.response.bytes=3830
git.response.cache=MISS
git.response.duration_ms=185
git.fetch.wants=a82189122fbef0a2cc700194378ad2773e9af1dd
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=3830
git.pkt.payload.max=3803
git.sideband.band1.bytes=3803
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
a5e2ba714968cf46
Request
7f674146…
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