GitLabgitlab-org

cli

main
ply6 git calls328 ms106 KiB4 cachedprotocol v2
1020 lines30 KiBRaw
1package config
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "net/url"
8 "os"
9 "path/filepath"
10 "slices"
11 "sort"
12 "strconv"
13 "strings"
14 "sync"
15 "sync/atomic"
17 "github.com/zalando/go-keyring"
18 "go.yaml.in/yaml/v3"
20 "gitlab.com/gitlab-org/cli/internal/dbg"
21 "gitlab.com/gitlab-org/cli/internal/glinstance"
24var keyringEligibleKeys = func() map[string]struct{} {
25 out := map[string]struct{}{}
26 for i := range KeySchema {
27 if KeySchema[i].Keyring {
28 out[KeySchema[i].Name] = struct{}{}
29 }
30 }
31 return out
32}()
34// A Config reads and writes persistent configuration for glab.
35type Config interface {
36 Get(string, string) (string, error)
37 GetWithSource(string, string, bool) (string, string, error)
38 Set(string, string, string) error
39 Hosts() ([]string, error)
40 Aliases() (*AliasConfig, error)
41 Local() (*LocalConfig, error)
42 // Write writes to the config.yml file
43 Write() error
44 // WriteAll saves all the available configuration file types
45 WriteAll() error
46 // Reload re-reads the configuration from its backing files and returns the
47 // refreshed Config, reflecting changes written by other processes since this
48 // Config was parsed. It does not mutate the receiver. Configs with no backing
49 // files (in-memory configs) return themselves unchanged.
50 Reload() (Config, error)
53// ErrKeyringUnavailable marks a failed read or write of the operating system keyring.
54var ErrKeyringUnavailable = errors.New("operating system keyring is unavailable")
56// keyringError matches ErrKeyringUnavailable without adding its text to the
57// message, which commands like auth login show to the user as is.
58type keyringError struct {
59 err error
62func (e keyringError) Error() string {
63 return e.err.Error()
66func (e keyringError) Unwrap() []error {
67 return []error{e.err, ErrKeyringUnavailable}
70// NotFoundError is returned when a config entry is not found.
71type NotFoundError struct {
72 error
75func isNotFoundError(err error) bool {
76 var nfe *NotFoundError
77 return errors.As(err, &nfe)
80// HostConfig represents the configuration for a single host.
81type HostConfig struct {
82 ConfigMap
83 Host string
86// ConfigMap type implements a low-level get/set config that is backed by an in-memory tree of YAML
87// nodes. It allows us to interact with a YAML-based config programmatically, preserving any
88// comments that were present when the YAML was parsed.
89type ConfigMap struct {
90 Root *yaml.Node
93func (cm *ConfigMap) Empty() bool {
94 return cm.Root == nil || len(cm.Root.Content) == 0
97func (cm *ConfigMap) GetStringValue(key string) (string, error) {
98 entry, err := cm.FindEntry(key)
99 if err != nil {
100 return "", err
101 }
102 return entry.ValueNode.Value, nil
105func (cm *ConfigMap) SetStringValue(key, value string) error {
106 entry, err := cm.FindEntry(key)
108 valueNode := entry.ValueNode
110 if err != nil && isNotFoundError(err) {
111 keyNode := &yaml.Node{
112 Kind: yaml.ScalarNode,
113 Value: key,
114 }
115 valueNode = &yaml.Node{
116 Kind: yaml.ScalarNode,
117 Tag: "!!str",
118 Value: "",
119 }
121 cm.Root.Content = append(cm.Root.Content, keyNode, valueNode)
122 } else if err != nil {
123 return err
124 }
126 valueNode.Value = value
128 // A blank entry (a key written with no value, e.g. `token:`) parses as a
129 // null-tagged scalar. Assigning a value without clearing that tag produces
130 // `token: !!null <value>`, which leaves the value in the file yet makes it
131 // decode as null. Reset the tag so string values round-trip correctly.
132 if valueNode.Tag == "!!null" {
133 valueNode.Tag = "!!str"
134 }
136 return nil
139type ConfigEntry struct {
140 KeyNode *yaml.Node
141 ValueNode *yaml.Node
142 Index int
145func (cm *ConfigMap) FindEntry(key string) (*ConfigEntry, error) {
146 ce := &ConfigEntry{}
148 topLevelKeys := cm.Root.Content
149 for i, v := range topLevelKeys {
150 if v.Value == key {
151 ce.KeyNode = v
152 ce.Index = i
153 if i+1 < len(topLevelKeys) {
154 ce.ValueNode = topLevelKeys[i+1]
155 }
156 return ce, nil
157 }
158 }
160 return ce, &NotFoundError{errors.New("not found")}
163func (cm *ConfigMap) RemoveEntry(key string) {
164 var newContent []*yaml.Node
166 content := cm.Root.Content
167 for i := 0; i < len(content); i++ {
168 if content[i].Value == key {
169 i++ // skip the next node which is this key's value
170 } else {
171 newContent = append(newContent, content[i])
172 }
173 }
175 cm.Root.Content = newContent
178func NewConfig(root *yaml.Node) Config {
179 return newConfig(root, "", "")
182// newConfig builds a fileConfig that persists to dir. An empty dir means the
183// config is in-memory only and Write()/WriteAll() are no-ops. localPath is the
184// per-repository config file merged in at parse time, if any; it is retained so
185// Reload() re-merges the same local overrides.
186func newConfig(root *yaml.Node, dir, localPath string) Config {
187 return &fileConfig{
188 ConfigMap: ConfigMap{Root: root.Content[0]},
189 documentRoot: root,
190 dir: dir,
191 localPath: localPath,
192 }
195// NewFromString initializes an in-memory Config from a yaml string. It has no
196// directory behind it, so Write()/WriteAll() are no-ops.
197func NewFromString(str string) Config {
198 return newConfigFromString(str, "")
201// NewFromStringInDir initializes a Config from a yaml string that persists to
202// dir. Intended for tests that need to inspect what would be written to disk.
203func NewFromStringInDir(str, dir string) Config {
204 return newConfigFromString(str, dir)
207func newConfigFromString(str, dir string) Config {
208 root, err := parseConfigData([]byte(str))
209 if err != nil {
210 panic(err)
211 }
212 return newConfig(root, dir, "")
215// NewBlankConfig initializes an in-memory config pre-populated with comments and
216// default values. It has no directory behind it, so Write()/WriteAll() are no-ops.
217func NewBlankConfig() Config {
218 return newConfig(NewBlankRoot(), "", "")
221// NewBlankConfigInDir initializes a blank config that persists to dir.
222func NewBlankConfigInDir(dir string) Config {
223 return newConfig(NewBlankRoot(), dir, "")
226func NewBlankRoot() *yaml.Node {
227 return rootConfig()
230// A fileConfig reads and writes glab configuration to a file on disk. An empty
231// dir means the config is in-memory only and its writers are no-ops.
232type fileConfig struct {
233 ConfigMap
234 documentRoot *yaml.Node
235 dir string
236 // localPath is the per-repository config file merged in at parse time, if
237 // any. Retained so Reload() re-merges the same local overrides.
238 localPath string
239 customHeaderCommandValues sync.Map
242func (c *fileConfig) Root() *yaml.Node {
243 return c.ConfigMap.Root
246// Reload re-parses the config from the files it was read from, so a caller that
247// must not act on a stale in-memory copy (for example, the OAuth token source in
248// a long-lived process) can pick up changes written by another process. It does
249// not mutate the receiver: an in-memory config (no backing directory) returns
250// itself unchanged. The keyring is always read live, so a reloaded config also
251// reflects the freshest keyring secrets.
252func (c *fileConfig) Reload() (Config, error) {
253 if c.dir == "" {
254 return c, nil
255 }
256 return parseConfig(filepath.Join(c.dir, "config.yml"), c.localPath)
259func (c *fileConfig) Get(hostname, key string) (string, error) {
260 val, _, err := c.GetWithSource(hostname, key, true)
261 return val, err
264// envValueForHost returns key's environment value, withholding credential keys
265// from hosts that are neither the default nor configured.
266func (c *fileConfig) envValueForHost(hostname, key string) (string, string) {
267 value, source := GetFromEnvWithSource(key)
268 if value == "" {
269 return "", ""
270 }
271 if hostname == "" || !envScopedToTrustedHosts(key) || IsTrustedHost(c, hostname) {
272 return value, source
273 }
274 dbg.Debugf("config: ignoring environment variable %s for host %q: it is neither the default host nor a configured host", source, hostname)
275 return "", ""
278func (c *fileConfig) GetWithSource(hostname, key string, searchENVVars bool) (string, string, error) {
279 if searchENVVars {
280 if value, source := c.envValueForHost(hostname, key); value != "" {
281 return value, source, nil
282 }
283 }
285 key = ConfigKeyEquivalence(key)
287 var cfgError error
289 if hostname != "" {
290 hostCfg, err := c.configForHost(hostname)
291 if err != nil && !isNotFoundError(err) {
292 return "", "", err
293 }
295 var hostValue string
296 if hostCfg != nil {
297 // Check if use_keyring field is enabled for token keys
298 if isKeyringEligibleKey(key) {
299 useKeyring, _ := hostCfg.GetStringValue("use_keyring")
301 if useKeyring == "true" {
302 // Keyring enabled - retrieve from platform-native secure storage
303 token, err := getFromKeyring(hostname, key)
304 if err == nil {
305 return token, "keyring", nil
306 }
307 // A missing entry is not a failure: treat it like an unset
308 // value and fall through to normal resolution. Any other
309 // error (locked keyring, denied access, unavailable backend)
310 // is surfaced so callers do not silently proceed with an
311 // empty credential.
312 if !errors.Is(err, keyring.ErrNotFound) {
313 return "", "keyring", keyringError{fmt.Errorf("failed to read %q from the operating system keyring for host %q: %w", key, hostname, err)}
314 }
315 }
316 }
318 hostValue, err = hostCfg.GetStringValue(key)
319 if err != nil && !isNotFoundError(err) {
320 return "", "", err
321 }
323 // Fallback: check keyring if token not in config (backward compat for existing users)
324 // Only check for PAT (token) and OAuth2 refresh tokens, not job_token, since job tokens
325 // were not commonly stored in keyring historically and the legacy format is ambiguous.
326 if (err != nil || hostValue == "") && (key == "token" || key == "oauth2_refresh_token") {
327 token, err := getFromKeyring(hostname, key)
328 if err == nil {
329 return token, "keyring", nil
330 }
331 }
333 }
335 if hostValue != "" {
336 return hostValue, ConfigFile(), nil
337 }
338 }
340 source := ConfigFile()
342 // State glab maintains for itself is written to the global file only, so a
343 // value sitting in a repository's local file would win every read while
344 // every write went to a file that is never read. Skip the local file for
345 // those keys rather than let a stale copy shadow the real one.
346 skipLocal := IsGlobalOnlyKey(key)
348 var (
349 value string
350 err error
351 )
353 if !skipLocal {
354 l, _ := c.Local()
355 value, err = l.GetStringValue(key)
356 }
358 if skipLocal || (err != nil && isNotFoundError(err)) || value == "" {
359 value, err = c.GetStringValue(key)
360 if err != nil && isNotFoundError(err) {
361 return defaultFor(key), source, cfgError
362 } else if err != nil {
363 if hostname != "" {
364 err = cfgError
365 }
366 return "", LocalConfigFile(), err
367 }
368 } else if value != "" {
369 source = LocalConfigFile()
370 }
372 if value == "" {
373 return defaultFor(key), source, cfgError
374 }
376 return value, source, cfgError
379// isKeyringEligibleKey returns true if the key can be stored in keyring
380func isKeyringEligibleKey(key string) bool {
381 _, eligible := keyringEligibleKeys[key]
382 return eligible
385// envCredentialKeys are the keys whose environment variables resolve only for
386// trusted hosts. Other per-host keys (api_host, client_id, ...) are not secrets
387// and keep resolving globally, because `glab auth login` reads them for a host
388// that is not configured yet.
389var envCredentialKeys = map[string]struct{}{
390 "token": {},
391 "job_token": {},
392 "is_oauth2": {},
395// envScopedToTrustedHosts reports whether key (or one of its aliases) is a
396// credential whose environment variables only resolve for trusted hosts.
397func envScopedToTrustedHosts(key string) bool {
398 _, ok := envCredentialKeys[resolveAlias(key)]
399 return ok
402// IsTrustedHost reports whether environment-provided credentials may be used
403// for hostname: either it is the default host (the global host key, which may
404// itself come from GITLAB_HOST/GITLAB_URI/GL_HOST, or from CI_SERVER_FQDN
405// under CI auto-login, falling back to gitlab.com), the host of a GITLAB_REPO
406// URL, or a host present in the configuration's hosts map. Environment
407// variables are process-global, so credentials must not resolve for an
408// arbitrary host taken from a command argument (for example
409// `glab repo view https://attacker.example.com/acme/awesome-tool`).
410func IsTrustedHost(c Config, hostname string) bool {
411 hostname = trustedHostKey(hostname)
413 defaultHost, _ := c.Get("", "host")
414 if defaultHost == "" {
415 defaultHost = glinstance.DefaultHostname
416 }
417 if hostname == trustedHostKey(defaultHost) {
418 return true
419 }
420 if repoHost := gitlabRepoHost(); repoHost != "" && hostname == trustedHostKey(repoHost) {
421 return true
422 }
424 hosts, err := c.Hosts()
425 if err != nil {
426 return false
427 }
428 return slices.ContainsFunc(hosts, func(h string) bool {
429 return trustedHostKey(h) == hostname
430 })
433// gitlabRepoHost returns the host of a GITLAB_REPO given as a URL or an
434// scp-like Git URL. GITLAB_REPO is user-set like GITLAB_HOST, so its host is
435// trusted too. The HOST/OWNER/REPO form is not parsed here: glrepo only treats
436// its first segment as a host when that host is already in the hosts map.
437// internal/git cannot be imported here (it imports config), so this mirrors
438// git.ParseURL, which drops the port of an SSH URL.
439func gitlabRepoHost() string {
440 repo := strings.TrimSpace(os.Getenv("GITLAB_REPO"))
441 if strings.Contains(repo, "://") {
442 u, err := url.Parse(repo)
443 if err != nil {
444 return ""
445 }
446 if u.Scheme == "ssh" || u.Scheme == "git+ssh" {
447 return u.Hostname()
448 }
449 return u.Host
450 }
451 if rest, ok := strings.CutPrefix(repo, "git@"); ok {
452 host, _, _ := strings.Cut(rest, ":")
453 return host
454 }
455 return ""
458// trustedHostKey reduces a host as spelled by GITLAB_HOST, a hosts key, or a
459// command argument to the lowercase host[:port] that IsTrustedHost compares.
460// The scheme is cut only at "://" because StripHostProtocol also trims a bare
461// leading "http" or "https", which would turn "httpsgit.corp.test" into
462// "git.corp.test".
463func trustedHostKey(h string) string {
464 h = glinstance.NormalizeHostname(h)
465 if _, rest, ok := strings.Cut(h, "://"); ok {
466 h = rest
467 }
468 h, _ = glinstance.ExtractSubfolder(h)
469 return h
472// keyringProbePrefix names the sentinel entries used to check whether a working
473// OS keyring backend is present.
474const keyringProbePrefix = "glab:__keyring_probe__"
476// keyringProbeSeq disambiguates concurrent probes within one process, since the
477// PID alone cannot.
478var keyringProbeSeq atomic.Uint64
480// keyringProbeService returns a sentinel service name unique to this probe, so
481// concurrent probes never contend for one entry. Backends vary in how badly they
482// take that: the macOS shell-out searches then creates as two steps, so the loser
483// of the race gets a duplicate-item error on a healthy keyring and the command
484// refuses to refresh, while the Secret Service and Windows backends replace
485// atomically. A sentinel leaked by a process that died mid-probe is harmless,
486// since every backend's write replaces an existing entry.
487func keyringProbeService() string {
488 return fmt.Sprintf("%s:%d:%d", keyringProbePrefix, os.Getpid(), keyringProbeSeq.Add(1))
491// KeyringAvailable reports whether a usable OS keyring backend is present by
492// performing a lightweight write/delete of a sentinel entry. It returns false
493// on platforms without a keyring (for example, headless Linux without a Secret
494// Service, or CI runners), which lets callers fall back to file storage.
495func KeyringAvailable() bool {
496 return keyringWriteError() == nil
499// keyringWriteError probes the keyring the same way KeyringAvailable does, but
500// returns the underlying failure so callers can report why the keyring rejected
501// the write instead of only that it did.
502func keyringWriteError() error {
503 service := keyringProbeService()
504 if err := keyring.Set(service, "", "1"); err != nil {
505 return err
506 }
507 // Best effort cleanup; a lingering sentinel is harmless.
508 _ = keyring.Delete(service, "")
509 return nil
512// buildKeyringKey constructs the keyring key for a given hostname and config key
513func buildKeyringKey(hostname, key string) string {
514 // Always suffix with the key name to avoid collisions
515 // e.g., "glab:gitlab.com:token", "glab:gitlab.com:oauth2_refresh_token"
516 return "glab:" + hostname + ":" + key
519// deleteFromKeyring best-effort removes a credential from the keyring, trying
520// both the current and legacy key formats. Missing entries are ignored.
521func deleteFromKeyring(hostname, key string) {
522 _ = keyring.Delete(buildKeyringKey(hostname, key), "")
523 if legacyKey := buildLegacyKeyringKey(hostname, key); legacyKey != "" {
524 _ = keyring.Delete(legacyKey, "")
525 }
528// InCI reports whether glab is running in a CI environment, based on the
529// conventional GITLAB_CI and CI variables. A value that parses as boolean true
530// (for example "true" or "1") counts; empty, "false", "0", and unparseable
531// values do not. GitLab and GitHub both set these to "true".
532func InCI() bool {
533 return ciEnvEnabled("GITLAB_CI") || ciEnvEnabled("CI")
536func ciEnvEnabled(name string) bool {
537 enabled, err := strconv.ParseBool(os.Getenv(name))
538 return err == nil && enabled
541// getFromKeyring attempts to retrieve a value from the keyring, trying both
542// new and legacy key formats for backward compatibility.
543func getFromKeyring(hostname, key string) (string, error) {
544 // Try new format first
545 keyringKey := buildKeyringKey(hostname, key)
546 token, err := keyring.Get(keyringKey, "")
547 if err == nil {
548 return token, nil
549 }
551 // Fallback to legacy key format for backward compatibility (if one exists)
552 legacyKey := buildLegacyKeyringKey(hostname, key)
553 if legacyKey != "" {
554 return keyring.Get(legacyKey, "")
555 }
557 // No legacy format exists for this key type
558 return "", err
561// buildLegacyKeyringKey constructs the old keyring key format for backward compatibility.
562// Legacy format used "glab:hostname" for token (PAT only)
563// and "glab:hostname:refresh_token" for oauth2_refresh_token.
564// Returns empty string for keys that did not have a legacy format (e.g., job_token).
565// Note: The old code used "glab:hostname" ambiguously for both token and job_token,
566// but we only check it for token (PAT) since that was the common use case for keyring.
567func buildLegacyKeyringKey(hostname, key string) string {
568 if key == "oauth2_refresh_token" {
569 return "glab:" + hostname + ":refresh_token"
570 }
571 if key == "token" {
572 // Only PATs were commonly stored in keyring
573 return "glab:" + hostname
574 }
575 // No legacy format for job_token and other keys
576 return ""
579// lockedSyncKeyring runs syncKeyring while holding the config-directory lock so
580// a keyring write is serialized against other processes' writes. In-memory
581// configs (no backing dir) run without a lock.
582func (c *fileConfig) lockedSyncKeyring(hostname, key, value string) (string, error) {
583 if c.dir == "" {
584 return c.syncKeyring(hostname, key, value)
585 }
587 var out string
588 err := withLock(c.dir, func() error {
589 var e error
590 out, e = c.syncKeyring(hostname, key, value)
591 return e
592 })
593 return out, err
596// syncKeyring reconciles the keyring copy of a credential with the value being
597// written and returns the value to persist in the config file. When storing to
598// the keyring, the plaintext copy is dropped from the file (empty return). When
599// storing to the file or clearing the value, any stale keyring copy is removed
600// so that switching keyring->file, or logging out, never orphans a secret,
601// regardless of the order in which use_keyring and the credential are written.
602// The keyring cleanup is skipped in CI, where the keyring is left untouched.
603func (c *fileConfig) syncKeyring(hostname, key, value string) (string, error) {
604 useKeyring, _ := c.Get(hostname, "use_keyring")
605 switch {
606 case useKeyring == "true" && value != "":
607 // Keyring mode: store in the keyring and drop the plaintext copy from the
608 // config file by returning an empty value.
609 if err := keyring.Set(buildKeyringKey(hostname, key), "", value); err != nil {
610 // Name the keyring and the host, mirroring the read path in
611 // GetWithSource. On macOS the backend shells out to
612 // /usr/bin/security, so the bare error is an unexplained
613 // "exit status <n>" with nothing tying it to the keyring.
614 return value, keyringError{fmt.Errorf("failed to store %q in the operating system's keyring for host %q: %w", key, hostname, err)}
615 }
616 return "", nil
617 case useKeyring == "true" || !InCI():
618 // Keyring mode clearing the value, or file mode storing/clearing: remove any
619 // keyring copy so switching keyring->file, or logging out, does not orphan a
620 // secret. Skipped in CI, where the keyring is left untouched (and file mode
621 // is the default there anyway).
622 deleteFromKeyring(hostname, key)
623 }
624 return value, nil
627func (c *fileConfig) Set(hostname, key, value string) error {
628 key = ConfigKeyEquivalence(key)
630 // Keep the keyring-backed and file-backed copies of a credential from
631 // diverging, serialized against concurrent writers via the config-directory
632 // lock so a keyring write cannot interleave with another process's refresh.
633 if isKeyringEligibleKey(key) && hostname != "" {
634 var err error
635 value, err = c.lockedSyncKeyring(hostname, key, value)
636 if err != nil {
637 return err
638 }
639 }
641 var cfg interface {
642 SetStringValue(string, string) error
643 RemoveEntry(string)
644 }
646 switch hostname {
647 case "":
648 cfg = c
649 default:
650 var err error
651 cfg, err = c.configForHost(hostname)
652 if err != nil {
653 if isNotFoundError(err) {
654 cfg = c.makeConfigForHost(hostname)
655 break
656 }
657 return err
658 }
659 }
661 switch value {
662 case "":
663 cfg.RemoveEntry(key)
664 return nil
665 default:
666 return cfg.SetStringValue(key, value)
667 }
670func (c *fileConfig) Write() error {
671 if c.dir == "" {
672 return nil
673 }
675 // Serialize the read-merge-write against other processes. renameio makes the
676 // write itself atomic, but not the merge that precedes it. If the lock cannot
677 // be taken (wedged peer), withLock degrades to writing unlocked.
678 return withLock(c.dir, func() error {
679 // A long-lived process writing its whole
680 // in-memory document for an unrelated reason (an update-check timestamp
681 // bump) could otherwise roll back OAuth credentials another process just
682 // rotated. Adopt any fresher on-disk credentials before serializing.
683 c.mergeCredentialsFromDisk()
684 return c.writeLocked()
685 })
688// writeLocked serializes the in-memory document to config.yml. Callers hold the
689// config-directory lock.
690func (c *fileConfig) writeLocked() error {
691 mainData := yaml.Node{Kind: yaml.MappingNode}
693 nodes := c.documentRoot.Content[0].Content
694 for i := 0; i < len(nodes)-1; i += 2 {
695 if nodes[i].Value == "aliases" || nodes[i].Value == "local" {
696 continue
697 } else {
698 mainData.Content = append(mainData.Content, nodes[i], nodes[i+1])
699 }
700 }
702 mainBytes, err := yaml.Marshal(&mainData)
703 if err != nil {
704 return err
705 }
707 return writeConfigFile(filepath.Join(c.dir, "config.yml"), yamlNormalize(mainBytes))
710// CredentialMerger copies newer credentials from a host's on-disk entry into its
711// in-memory entry. It must only touch the YAML document, never the keyring.
712type CredentialMerger func(mem, disk *HostConfig)
714var credentialMerger atomic.Pointer[CredentialMerger]
716// SetCredentialMerger registers the merger Write runs before saving.
717func SetCredentialMerger(m CredentialMerger) {
718 credentialMerger.Store(&m)
721// mergeCredentialsFromDisk runs the registered merger on each host. Callers must
722// hold the config-directory lock.
723func (c *fileConfig) mergeCredentialsFromDisk() {
724 merge := credentialMerger.Load()
725 if merge == nil || *merge == nil {
726 return
727 }
729 onDisk, err := ParseConfig(filepath.Join(c.dir, "config.yml"))
730 if err != nil {
731 // A missing or unreadable file has nothing fresher to adopt.
732 dbg.Debugf("config: could not re-read config.yml before write, skipping credential merge: %v", err)
733 return
734 }
735 diskCfg, ok := onDisk.(*fileConfig)
736 if !ok {
737 return
738 }
740 hosts, err := c.Hosts()
741 if err != nil {
742 return
743 }
745 for _, host := range hosts {
746 diskHost, err := diskCfg.configForHost(host)
747 if err != nil {
748 continue
749 }
750 memHost, err := c.configForHost(host)
751 if err != nil {
752 continue
753 }
754 (*merge)(memHost, diskHost)
755 }
758func (c *fileConfig) WriteAll() error {
759 err := c.Write()
760 if err != nil {
761 return err
762 }
764 aliases, err := c.Aliases()
765 if err != nil {
766 return err
767 }
768 return aliases.Write()
771func yamlNormalize(b []byte) []byte {
772 if bytes.Equal(b, []byte("{}\n")) {
773 return []byte{}
774 }
775 return b
778func (c *fileConfig) Local() (*LocalConfig, error) {
779 entry, err := c.FindEntry("local")
780 notFound := isNotFoundError(err)
781 if err != nil && !notFound {
782 return nil, err
783 }
785 var toInsert []*yaml.Node
787 keyNode := entry.KeyNode
788 valueNode := entry.ValueNode
790 if keyNode == nil {
791 keyNode = &yaml.Node{
792 Kind: yaml.ScalarNode,
793 Value: "local",
794 }
795 toInsert = append(toInsert, keyNode)
796 }
798 if valueNode == nil || valueNode.Kind != yaml.MappingNode {
799 valueNode = &yaml.Node{
800 Kind: yaml.MappingNode,
801 Value: "",
802 }
803 toInsert = append(toInsert, valueNode)
804 }
806 if len(toInsert) > 0 {
807 var newContent []*yaml.Node
808 if notFound {
809 newContent = slices.Concat(c.Root().Content, []*yaml.Node{keyNode, valueNode})
810 } else {
811 for i := 0; i < len(c.Root().Content); i++ {
812 if i == entry.Index {
813 newContent = append(newContent, keyNode, valueNode)
814 i++
815 } else {
816 newContent = append(newContent, c.Root().Content[i])
817 }
818 }
819 }
820 c.Root().Content = newContent
821 }
822 return &LocalConfig{
823 Parent: c,
824 ConfigMap: ConfigMap{Root: valueNode},
825 dir: c.dir,
826 }, nil
829func (c *fileConfig) Aliases() (*AliasConfig, error) {
830 // The complexity here is for dealing with either a missing or empty aliases key. It's something
831 // we'll likely want for other config sections at some point.
832 entry, err := c.FindEntry("aliases")
833 notFound := isNotFoundError(err)
834 if err != nil && !notFound {
835 return nil, err
836 }
838 var toInsert []*yaml.Node
840 keyNode := entry.KeyNode
841 valueNode := entry.ValueNode
843 if keyNode == nil {
844 keyNode = &yaml.Node{
845 Kind: yaml.ScalarNode,
846 Value: "aliases",
847 }
848 toInsert = append(toInsert, keyNode)
849 }
851 if valueNode == nil || valueNode.Kind != yaml.MappingNode {
852 valueNode = &yaml.Node{
853 Kind: yaml.MappingNode,
854 Value: "",
855 }
856 toInsert = append(toInsert, valueNode)
857 }
859 if len(toInsert) > 0 {
860 var newContent []*yaml.Node
861 if notFound {
862 newContent = slices.Concat(c.Root().Content, []*yaml.Node{keyNode, valueNode})
863 } else {
864 for i := 0; i < len(c.Root().Content); i++ {
865 if i == entry.Index {
866 newContent = append(newContent, keyNode, valueNode)
867 i++
868 } else {
869 newContent = append(newContent, c.Root().Content[i])
870 }
871 }
872 }
873 c.Root().Content = newContent
874 }
876 return &AliasConfig{
877 Parent: c,
878 ConfigMap: ConfigMap{Root: valueNode},
879 dir: c.dir,
880 }, nil
883func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
884 entry, err := c.FindEntry("hosts")
885 if err != nil {
886 return nil, fmt.Errorf("could not find hosts config: %w", err)
887 }
889 hostConfigs, err := c.parseHosts(entry.ValueNode)
890 if err != nil {
891 return nil, fmt.Errorf("could not parse hosts config: %w", err)
892 }
894 return hostConfigs, nil
897// Hosts returns a list of all known hostnames configured in hosts.yml
898func (c *fileConfig) Hosts() ([]string, error) {
899 entries, err := c.hostEntries()
900 if err != nil {
901 return nil, err
902 }
904 var hostnames []string
905 for _, entry := range entries {
906 hostnames = append(hostnames, entry.Host)
907 }
909 sort.SliceStable(hostnames, func(i, j int) bool { return hostnames[i] == glinstance.DefaultHostname })
911 return hostnames, nil
914func (c *fileConfig) makeConfigForHost(hostname string) *HostConfig {
915 hostRoot := &yaml.Node{Kind: yaml.MappingNode}
916 hostCfg := &HostConfig{
917 Host: hostname,
918 ConfigMap: ConfigMap{Root: hostRoot},
919 }
920 hostsEntry, err := c.FindEntry("hosts")
921 if isNotFoundError(err) {
922 hostsEntry.KeyNode = &yaml.Node{
923 Kind: yaml.ScalarNode,
924 Value: "hosts",
925 }
926 hostsEntry.ValueNode = &yaml.Node{Kind: yaml.MappingNode}
927 root := c.Root()
928 root.Content = append(root.Content, hostsEntry.KeyNode, hostsEntry.ValueNode)
929 } else if err != nil {
930 panic(err)
931 }
933 hostsEntry.ValueNode.Content = append(hostsEntry.ValueNode.Content,
934 &yaml.Node{
935 Kind: yaml.ScalarNode,
936 Value: hostname,
937 }, hostRoot)
939 return hostCfg
942func (c *fileConfig) parseHosts(hostsEntry *yaml.Node) ([]*HostConfig, error) {
943 var hostConfigs []*HostConfig
945 for i := 0; i < len(hostsEntry.Content)-1; i += 2 {
946 hostname := hostsEntry.Content[i].Value
947 hostRoot := hostsEntry.Content[i+1]
949 // Resolve YAML alias nodes (e.g. "*gitlab") to their anchored target.
950 // yaml.Unmarshal into yaml.Node preserves aliases as AliasNode with an
951 // Alias pointer; the node's own Content is empty until dereferenced.
952 if hostRoot.Kind == yaml.AliasNode {
953 hostRoot = hostRoot.Alias
954 }
955 hostConfig := HostConfig{
956 ConfigMap: ConfigMap{Root: hostRoot},
957 Host: hostname,
958 }
959 hostConfigs = append(hostConfigs, &hostConfig)
960 }
962 if len(hostConfigs) == 0 {
963 return nil, &NotFoundError{errors.New("could not find any host configurations")}
964 }
966 return hostConfigs, nil
969// GetFromEnv is just a wrapper for os.GetEnv but checks for matching names used in previous glab versions and
970// retrieves the value of the environment if any of the matching names have been set.
971// It returns the value, which will be empty if the variable is not present.
972func GetFromEnv(key string) string {
973 value, _ := GetFromEnvWithSource(key)
974 return value
977// GetFromEnvWithSource works like GetFromEnv but also returns the name of the environment variable that was
978// set as the source.
979func GetFromEnvWithSource(key string) (string, string) {
980 envEq := EnvKeyEquivalence(key)
981 for _, e := range envEq {
982 if val := os.Getenv(e); val != "" {
983 // Special handling for CI autologin variables that need parsing
984 switch {
985 case key == "subfolder" && e == "CI_SERVER_URL":
986 // Extract subfolder from CI_SERVER_URL
987 // is like "https://example.com/gitlab" or "https://example.com"
988 if subfolder := extractSubfolderFromURL(val); subfolder != "" {
989 return subfolder, e
990 }
991 // If no subfolder in URL, continue checking other env vars
992 continue
993 case key == "ssh_host" && e == "CI_SERVER_SHELL_SSH_HOST":
994 // Combine SSH host and port if port is set
995 sshHost := val
996 if sshPort := os.Getenv("CI_SERVER_SHELL_SSH_PORT"); sshPort != "" && sshPort != "22" {
997 // Only include port if it's non-default (not 22)
998 sshHost = sshHost + ":" + sshPort
999 }
1000 return sshHost, e
1001 default:
1002 return val, e
1006 return "", ""
1009// extractSubfolderFromURL parses a URL and extracts the path component (subfolder).
1010// Returns empty string if URL has no path or only "/".
1011func extractSubfolderFromURL(urlStr string) string {
1012 u, err := url.Parse(urlStr)
1013 if err != nil {
1014 return ""
1017 // Extract and clean the path
1018 subfolder := strings.Trim(u.Path, "/")
1019 return subfolder
Diagnostics6 git calls · 328 ms
CallStatusSizeTime0328 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=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=3
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
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=2
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:02001.4 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=320
git.response.bytes=1391
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=e9da9edfdcf44933ee5fa121d411de326220161c
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=1391
git.pkt.payload.max=1364
git.sideband.band1.bytes=1364
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1364
git.pack.trailer=bf750bd385ed98d15e1fea1289d3b00caa61d64c
Open pack map
fetch2009.5 KiB320 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=9731
git.response.cache=MISS
git.response.duration_ms=320
git.fetch.wants=4682b4b00e6bde32acc35753d6b1ac0e79e82325
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=9731
git.pkt.payload.max=9704
git.sideband.band1.bytes=9704
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
a5e22c0f5d652a83
Request
0849d41a…
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