GitLabgitlab-org

cli

main
ply6 git calls28 ms99 KiB5 cachedprotocol v2
266 lines9.3 KiBRaw
1package oauth2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "strings"
8 "time"
10 "github.com/MakeNowJust/heredoc/v2"
11 "golang.org/x/oauth2"
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"
19const (
20 redirectURL = "http://localhost:7171/auth/redirect"
21 callbackServerListenAddr = ":7171"
24var scopes = []string{"openid", "profile", "read_user", "write_repository", "api"}
26const (
27 clientIDChoicePaste = "I have an Application ID to paste"
28 clientIDChoiceShow = "Show me what an admin needs to create"
29 clientIDChoiceCancel = "Cancel"
32func nonInteractiveClientIDErr(hostname string) error {
33 return fmt.Errorf("set 'client_id' first with `glab config set client_id <client_id> -g --host %s`", hostname)
36// oauthClientID looks up the OAuth client_id already configured for
37// hostname, without prompting for one. A self-managed/Dedicated hostname
38// with nothing configured yet resolves to ("", nil), leaving the decision
39// of what to do about that to the caller.
40func oauthClientID(cfg config.Config, hostname string) (string, error) {
41 if !glinstance.IsSelfHosted(hostname) {
42 return glinstance.DefaultClientID, nil
43 }
44 return cfg.Get(hostname, "client_id")
47// resolveClientID resolves the OAuth client_id to use for hostname,
48// prompting to register (or paste) one on a self-managed/Dedicated instance
49// with nothing configured yet, if io is interactive. io must be non-nil:
50// IsInteractive dereferences it unconditionally.
51//
52// The second return value persists clientID; the caller must invoke it only
53// after actually completing an OAuth round trip with clientID, never before.
54// A pasted value is unproven: a typo, an ID from another instance, or the
55// application Secret pasted in place of the Application ID would otherwise
56// be written immediately, permanently hiding this prompt behind a value that
57// silently hangs the next login's OAuth callback wait instead of failing
58// visibly. For an already-configured or GITLAB_CLIENT_ID-supplied value,
59// persist is a no-op: there's nothing new to write.
60func resolveClientID(ctx context.Context, cfg config.Config, io *iostreams.IOStreams, hostname string) (string, func() error, error) {
61 clientID, err := oauthClientID(cfg, hostname)
62 if err != nil {
63 return "", nil, err
64 }
65 clientID = strings.TrimSpace(clientID)
66 if clientID != "" {
67 if err := ValidateClientID(clientID); err != nil {
68 return "", nil, fmt.Errorf("invalid 'client_id' for %s: %w; update it with `glab config set client_id <client_id> -g --host %s`, or update or unset GITLAB_CLIENT_ID", hostname, err, hostname)
69 }
70 return clientID, func() error { return nil }, nil
71 }
73 if !io.IsInteractive() {
74 return "", nil, nonInteractiveClientIDErr(hostname)
75 }
77 return promptClientIDChoice(ctx, cfg, io, hostname)
80// promptClientIDChoice asks how to proceed once a self-managed hostname
81// turns out to have no client_id configured locally. An absent config value
82// says nothing about whether an admin already registered an application on
83// the instance, so the missing-value case and the need-to-create-one case
84// get their own paths instead of one prompt that assumes the latter.
85func promptClientIDChoice(ctx context.Context, cfg config.Config, io *iostreams.IOStreams, hostname string) (string, func() error, error) {
86 var choice string
87 title := fmt.Sprintf("No OAuth application ID is configured for %s.\n\nHow do you want to continue?", hostname)
88 if err := io.Select(ctx, &choice, title, []string{clientIDChoicePaste, clientIDChoiceShow, clientIDChoiceCancel}); err != nil {
89 if errors.Is(err, iostreams.ErrUserCancelled) {
90 return "", nil, nonInteractiveClientIDErr(hostname)
91 }
92 return "", nil, err
93 }
95 switch choice {
96 case clientIDChoicePaste:
97 return promptClientID(ctx, cfg, io, hostname)
98 case clientIDChoiceShow:
99 printClientIDRegistrationInstructions(io)
100 return "", nil, nonInteractiveClientIDErr(hostname)
101 default: // clientIDChoiceCancel
102 return "", nil, nonInteractiveClientIDErr(hostname)
103 }
106// printClientIDRegistrationInstructions prints the values an instance or
107// group admin needs to register an OAuth application for glab. It never
108// persists anything: the caller still returns nonInteractiveClientIDErr
109// afterwards, so the exact `config set` command to run once an ID exists has
110// one source of truth regardless of which exit path got here.
111func printClientIDRegistrationInstructions(io *iostreams.IOStreams) {
112 io.LogError()
113 io.LogError(heredoc.Docf(`
114 Ask an instance or group admin to create one (Admin Area > Applications,
115 or Group > Settings > Applications):
116 Redirect URI: %[1]s
117 Scopes: %[2]s
118 Confidential: off (glab is a public client and cannot hold a secret)
120 See also: https://docs.gitlab.com/cli/authentication/#oauth-gitlab-self-managed-gitlab-dedicated
121 `, redirectURL, strings.Join(scopes, " ")))
124// promptClientID asks for the Application ID to paste. It does not persist
125// it: the returned func does, exactly the way
126// `glab config set client_id <id> -g --host <hostname>` would, once the
127// caller has proven the value by completing an OAuth round trip with it.
128func promptClientID(ctx context.Context, cfg config.Config, io *iostreams.IOStreams, hostname string) (string, func() error, error) {
129 var clientID string
130 err := io.Input(ctx, &clientID, "Paste the Application ID here:", "", ValidateClientID)
131 if err != nil {
132 if errors.Is(err, iostreams.ErrUserCancelled) {
133 return "", nil, nonInteractiveClientIDErr(hostname)
134 }
135 return "", nil, err
136 }
137 clientID = strings.TrimSpace(clientID)
139 persist := func() error {
140 if err := cfg.Set(hostname, "client_id", clientID); err != nil {
141 return fmt.Errorf("failed to set 'client_id': %w", err)
142 }
143 if err := cfg.Write(); err != nil {
144 return fmt.Errorf("failed to write configuration to disk: %w", err)
145 }
146 return nil
147 }
149 return clientID, persist, nil
152// unmarshal reads the OAuth2 token for hostname out of cfg. The "token" key
153// is also resolvable from GITLAB_TOKEN/GITLAB_ACCESS_TOKEN/OAUTH_TOKEN, so
154// searchEnvForIdentity gates that lookup the same way it does in
155// api.NewClientFromConfig: callers that act on behalf of another process
156// inheriting the environment (the Docker credential helper) must pass false
157// so a stray environment variable cannot substitute a different identity's
158// access token here.
159// ParseExpiryDate reads a stored oauth2_expiry_date, accepting the RFC822 forms
160// older versions of glab wrote.
161func ParseExpiryDate(s string) (time.Time, error) {
162 for _, layout := range []string{time.RFC3339, time.RFC822, time.RFC822Z} {
163 if expiry, err := time.Parse(layout, s); err == nil {
164 return expiry, nil
165 }
166 }
168 return time.Time{}, fmt.Errorf("could not parse %q as an expiry date", s)
171func unmarshal(hostname string, cfg config.Config, searchEnvForIdentity bool) (*oauth2.Token, error) {
172 result := &oauth2.Token{}
173 var err error
175 expiryDateString, err := cfg.Get(hostname, "oauth2_expiry_date")
176 if err != nil {
177 return nil, err
178 }
180 result.Expiry, err = ParseExpiryDate(expiryDateString)
181 if err != nil {
182 return nil, err
183 }
185 result.RefreshToken, err = cfg.Get(hostname, "oauth2_refresh_token")
186 if err != nil {
187 return nil, err
188 }
190 result.AccessToken, _, err = cfg.GetWithSource(hostname, "token", searchEnvForIdentity)
191 if err != nil {
192 return nil, err
193 }
195 return result, nil
198func marshal(hostname string, cfg config.Config, token *oauth2.Token) error {
199 err := cfg.Set(hostname, "is_oauth2", "true")
200 if err != nil {
201 return err
202 }
204 if token.RefreshToken != "" {
205 err = cfg.Set(hostname, "oauth2_refresh_token", token.RefreshToken)
206 if err != nil {
207 return err
208 }
209 }
211 err = cfg.Set(hostname, "oauth2_expiry_date", token.Expiry.Format(time.RFC3339))
212 if err != nil {
213 return err
214 }
216 err = cfg.Set(hostname, "token", token.AccessToken)
217 if err != nil {
218 return err
219 }
221 return nil
224func init() {
225 config.SetCredentialMerger(mergeFresherCredentialsFromDisk)
228// oauthCredentialKeys rotate together on every token refresh.
229var oauthCredentialKeys = []string{"token", "oauth2_refresh_token", "oauth2_expiry_date"}
231// mergeFresherCredentialsFromDisk keeps credentials another process rotated, so
232// a stale write can't roll back a single-use refresh token. Empty disk values
233// are skipped, so keyring-backed hosts take only the newer expiry.
234func mergeFresherCredentialsFromDisk(mem, disk *config.HostConfig) {
235 diskExpiry, _ := disk.GetStringValue("oauth2_expiry_date")
236 memExpiry, _ := mem.GetStringValue("oauth2_expiry_date")
237 if !expiryNewer(diskExpiry, memExpiry) {
238 return
239 }
241 for _, key := range oauthCredentialKeys {
242 v, err := disk.GetStringValue(key)
243 if err != nil || v == "" {
244 continue
245 }
246 if setErr := mem.SetStringValue(key, v); setErr != nil {
247 dbg.Debugf("oauth2: could not copy %q for %q from config.yml: %v", key, mem.Host, setErr)
248 }
249 }
250 dbg.Debugf("oauth2: %q has newer credentials in config.yml, keeping them", mem.Host)
253// expiryNewer reports whether disk is strictly newer than mem.
254func expiryNewer(disk, mem string) bool {
255 dt, err := ParseExpiryDate(disk)
256 if err != nil {
257 return false
258 }
259 mt, err := ParseExpiryDate(mem)
260 if err != nil {
261 // An empty in-memory expiry usually means `glab auth logout` cleared the
262 // credentials. Adopting the disk copy would undo it.
263 return false
264 }
265 return dt.After(mt)
Diagnostics6 git calls · 28 ms
CallStatusSizeTime028 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=4
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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · 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=4273
git.response.cache=HIT
git.response.duration_ms=4
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:0200468 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=17
git.response.bytes=468
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=c498f09dbd0cebbc59f9eff3b3ef8be66186fac6
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=468
git.pkt.payload.max=441
git.sideband.band1.bytes=441
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=441
git.pack.trailer=6420a98b3b2e923a6b61c550c00de326d620e479
Open pack map
fetch2003.3 KiB · hit17 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3340
git.response.cache=HIT
git.response.duration_ms=17
git.fetch.wants=3f255ff7eeb1c338f38fafe86d36804b3bc114f2
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=3340
git.pkt.payload.max=3313
git.sideband.band1.bytes=3313
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
a5e289efc8edbd4a
Request
38310063…
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