GitLabgitlab-org

cli

main
ply6 git calls186 ms98 KiB4 cachedprotocol v2
219 lines6.1 KiBRaw
1package config
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "os"
8 "os/exec"
9 "strings"
10 "sync"
11 "time"
13 "github.com/google/shlex"
14 "go.yaml.in/yaml/v3"
17const customHeaderCommandTimeout = 30 * time.Second
19// CustomHeader represents a single custom HTTP header configuration
20type CustomHeader struct {
21 Name string `yaml:"name"`
22 Value string `yaml:"value,omitempty"`
23 ValueFromEnv string `yaml:"valueFromEnv,omitempty"`
24 ValueFromCommand string `yaml:"valueFromCommand,omitempty"`
27type customHeaderCommandResult struct {
28 once sync.Once
29 value string
30 err error
33// ResolvedValue returns the actual header value, resolving environment variables or commands if needed.
34func (h *CustomHeader) ResolvedValue() (string, error) {
35 switch {
36 case h.ValueFromCommand != "":
37 return resolveCustomHeaderCommand(h.ValueFromCommand)
38 case h.ValueFromEnv != "":
39 value := os.Getenv(h.ValueFromEnv)
40 if value == "" {
41 return "", fmt.Errorf("environment variable %q for header %q is not set or empty", h.ValueFromEnv, h.Name)
42 }
43 return value, nil
44 case h.Value != "":
45 return h.Value, nil
46 default:
47 return "", errors.New("exactly one of value, valueFromEnv, or valueFromCommand must be specified for a custom header")
48 }
51func resolveCustomHeaderCommand(command string) (string, error) {
52 return resolveCustomHeaderCommandWithTimeout(command, customHeaderCommandTimeout)
55func resolveCustomHeaderCommandWithTimeout(command string, timeout time.Duration) (string, error) {
56 args, err := shlex.Split(command)
57 if err != nil {
58 return "", fmt.Errorf("parsing command: %w", err)
59 }
60 if len(args) == 0 {
61 return "", errors.New("command is empty")
62 }
64 cmd := exec.Command(args[0], args[1:]...)
65 var stdout bytes.Buffer
66 var stderr bytes.Buffer
67 cmd.Stdout = &stdout
68 cmd.Stderr = &stderr
69 setProcessGroup(cmd)
70 if err := cmd.Start(); err != nil {
71 return "", fmt.Errorf("running command: %w", err)
72 }
74 timedOut := false
75 timerDone := make(chan struct{})
76 timer := time.AfterFunc(timeout, func() {
77 defer close(timerDone)
78 // Kill the whole process group/tree, not just the directly-spawned
79 // process: shell wrappers such as `sh -c '...'` (the README's
80 // documented pattern for pipelines) leave their real work running in
81 // a child that would otherwise be orphaned when only the wrapper is
82 // killed.
83 if err := killProcessGroup(cmd); err == nil {
84 timedOut = true
85 }
86 })
87 err = cmd.Wait()
88 if !timer.Stop() {
89 <-timerDone
90 }
91 if timedOut {
92 return "", fmt.Errorf("command timed out after %s", timeout)
93 }
94 if err != nil {
95 if message := strings.TrimSpace(stderr.String()); message != "" {
96 return "", fmt.Errorf("running command: %w: %s", err, message)
97 }
98 return "", fmt.Errorf("running command: %w", err)
99 }
101 value := strings.TrimSpace(stdout.String())
102 if value == "" {
103 return "", errors.New("command returned an empty value")
104 }
105 if strings.ContainsAny(value, "\r\n\x00") {
106 return "", errors.New("command returned a value containing a newline or NUL byte")
107 }
109 return value, nil
112// GetCustomHeaders returns the custom headers for a specific host
113func (hc *HostConfig) GetCustomHeaders() ([]CustomHeader, error) {
114 entry, err := hc.FindEntry("custom_headers")
115 if err != nil {
116 if isNotFoundError(err) {
117 // No custom headers configured, return nil slice
118 return nil, nil
119 }
121 return nil, err
122 }
124 if entry.ValueNode.Kind != yaml.SequenceNode {
125 return nil, fmt.Errorf("custom_headers must be a list")
126 }
128 var headers []CustomHeader
129 for _, headerNode := range entry.ValueNode.Content {
130 if headerNode.Kind != yaml.MappingNode {
131 return nil, fmt.Errorf("each custom header must be a mapping with 'name' and exactly one of 'value', 'valueFromEnv', or 'valueFromCommand'")
132 }
134 var header CustomHeader
135 if err := headerNode.Decode(&header); err != nil {
136 return nil, fmt.Errorf("failed to decode custom header: %w", err)
137 }
139 // Validate header configuration
140 if header.Name == "" {
141 return nil, fmt.Errorf("custom header must have a 'name' field")
142 }
143 sourceCount := 0
144 for _, value := range []string{header.Value, header.ValueFromEnv, header.ValueFromCommand} {
145 if value != "" {
146 sourceCount++
147 }
148 }
149 if sourceCount != 1 {
150 return nil, fmt.Errorf("custom header %q must have exactly one of 'value', 'valueFromEnv', or 'valueFromCommand'", header.Name)
151 }
153 headers = append(headers, header)
154 }
156 return headers, nil
159// ResolveCustomHeaders returns a map of resolved custom headers for a host
160func (c *fileConfig) ResolveCustomHeaders(hostname string) (map[string]string, error) {
161 if hostname == "" {
162 return nil, nil
163 }
165 hostCfg, err := c.configForHost(hostname)
166 if err != nil {
167 if isNotFoundError(err) {
168 // Host not configured, return empty map
169 return nil, nil
170 }
172 return nil, err
173 }
175 headers, err := hostCfg.GetCustomHeaders()
176 if err != nil {
177 return nil, err
178 }
180 resolved := make(map[string]string, len(headers))
181 for _, header := range headers {
182 value, err := c.resolveCustomHeaderValue(hostname, &header)
183 if err != nil {
184 return nil, fmt.Errorf("failed to resolve header %q: %w", header.Name, err)
185 }
186 resolved[header.Name] = value
187 }
189 return resolved, nil
192func (c *fileConfig) resolveCustomHeaderValue(hostname string, header *CustomHeader) (string, error) {
193 if header.ValueFromCommand == "" {
194 return header.ResolvedValue()
195 }
197 key := strings.Join([]string{hostname, header.Name, header.ValueFromCommand}, "\x00")
198 resultValue, _ := c.customHeaderCommandValues.LoadOrStore(key, &customHeaderCommandResult{})
199 result, ok := resultValue.(*customHeaderCommandResult)
200 if !ok {
201 return "", errors.New("invalid cached custom header command result")
202 }
203 result.once.Do(func() {
204 result.value, result.err = header.ResolvedValue()
205 })
206 return result.value, result.err
209// ResolveCustomHeaders is a helper function that works with the Config interface
210func ResolveCustomHeaders(cfg Config, hostname string) (map[string]string, error) {
211 // Try to get the fileConfig implementation
212 fc, ok := cfg.(*fileConfig)
213 if !ok {
214 // Not a fileConfig, this is an unexpected condition
215 return nil, fmt.Errorf("unexpected config type: %T, expected *fileConfig", cfg)
216 }
218 return fc.ResolveCustomHeaders(hostname)
Diagnostics6 git calls · 186 ms
CallStatusSizeTime0186 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 · 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 · 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=32926
git.response.cache=HIT
git.response.duration_ms=4
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:02001.4 KiB · 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=1391
git.response.cache=HIT
git.response.duration_ms=3
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
fetch2002.1 KiB177 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=177
git.response.bytes=2154
git.response.cache=MISS
git.response.duration_ms=177
git.fetch.wants=d19ab3596e0df3a608aaf2a09fd4e5ea350272ff
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=2154
git.pkt.payload.max=2127
git.sideband.band1.bytes=2127
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
a5e210f1d9857f3b
Request
7d235220…
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