GitLabgitlab-org
cli
main
1//go:build !integration
3package api
5import (
6 "bytes"
7 "fmt"
8 "io"
9 "net/http"
10 "net/http/httptest"
11 "net/url"
12 "strings"
13 "testing"
14 "time"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
19 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
21 "gitlab.com/gitlab-org/cli/internal/config"
22)
24func TestCustomHeadersTransport(t *testing.T) {
25 tests := []struct {
26 name string
27 customHeaders map[string]string
28 allowedHosts map[string]struct{}
29 requestURL string
30 requestHeader string
31 expected map[string]string
32 }{
33 {
34 name: "adds one header",
35 customHeaders: map[string]string{
36 "X-Custom-Header": "custom-value",
37 },
38 expected: map[string]string{"X-Custom-Header": "custom-value"},
39 },
40 {
41 name: "adds proxy authorization header",
42 customHeaders: map[string]string{
43 "Proxy-Authorization": "Bearer token123",
44 },
45 expected: map[string]string{"Proxy-Authorization": "Bearer token123"},
46 },
47 {
48 name: "adds authorization header",
49 customHeaders: map[string]string{
50 "Authorization": "Bearer token456",
51 },
52 expected: map[string]string{"Authorization": "Bearer token456"},
53 },
54 {
55 name: "adds cloudflare access headers",
56 customHeaders: map[string]string{
57 "Cf-Access-Client-Id": "client-123",
58 "Cf-Access-Client-Secret": "secret-456",
59 },
60 expected: map[string]string{
61 "Cf-Access-Client-Id": "client-123",
62 "Cf-Access-Client-Secret": "secret-456",
63 },
64 },
65 {
66 name: "adds multiple headers",
67 customHeaders: map[string]string{
68 "X-Custom-Header": "value1",
69 "Proxy-Authorization": "Bearer proxy-token",
70 },
71 expected: map[string]string{
72 "X-Custom-Header": "value1",
73 "Proxy-Authorization": "Bearer proxy-token",
74 },
75 },
76 {
77 name: "configured value replaces a request value",
78 customHeaders: map[string]string{
79 "X-Custom-Header": "configured-value",
80 },
81 requestHeader: "request-value",
82 expected: map[string]string{"X-Custom-Header": "configured-value"},
83 },
84 {
85 name: "does not add headers without an allowed host",
86 customHeaders: map[string]string{
87 "Proxy-Authorization": "Bearer proxy-token",
88 },
89 allowedHosts: map[string]struct{}{},
90 expected: map[string]string{"Proxy-Authorization": ""},
91 },
92 {
93 name: "does not add headers to another host",
94 customHeaders: map[string]string{
95 "Proxy-Authorization": "Bearer proxy-token",
96 },
97 allowedHosts: map[string]struct{}{"example.com": {}},
98 requestURL: "https://redirect.example.com",
99 expected: map[string]string{"Proxy-Authorization": ""},
100 },
101 }
103 for _, tc := range tests {
104 t.Run(tc.name, func(t *testing.T) {
105 var transportedRequest *http.Request
106 allowedHosts := tc.allowedHosts
107 if allowedHosts == nil {
108 allowedHosts = map[string]struct{}{"example.com": {}}
109 }
110 transport := &customHeadersTransport{
111 headers: tc.customHeaders,
112 allowedHosts: allowedHosts,
113 rt: roundTripFunc(func(req *http.Request) (*http.Response, error) {
114 transportedRequest = req
115 return &http.Response{
116 StatusCode: http.StatusOK,
117 Body: io.NopCloser(strings.NewReader("")),
118 Request: req,
119 }, nil
120 }),
121 }
123 requestURL := tc.requestURL
124 if requestURL == "" {
125 requestURL = "https://example.com"
126 }
127 req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, requestURL, http.NoBody)
128 require.NoError(t, err)
129 if tc.requestHeader != "" {
130 req.Header.Set("X-Custom-Header", tc.requestHeader)
131 }
133 resp, err := transport.RoundTrip(req)
134 require.NoError(t, err)
135 require.NoError(t, resp.Body.Close())
137 for name, value := range tc.expected {
138 assert.Equal(t, value, transportedRequest.Header.Get(name))
139 }
140 if tc.requestHeader != "" {
141 assert.Equal(t, []string{"configured-value"}, transportedRequest.Header.Values("X-Custom-Header"))
142 }
143 if tc.requestHeader == "" {
144 assert.Empty(t, req.Header.Get("X-Custom-Header"), "the caller request must not be mutated")
145 } else {
146 assert.Equal(t, tc.requestHeader, req.Header.Get("X-Custom-Header"), "the caller request must not be mutated")
147 }
148 })
149 }
150}
152func TestDebugTransportIncludesCustomHeaders(t *testing.T) {
153 t.Setenv("GLAB_DEBUG_HTTP", "true")
155 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
156 w.WriteHeader(http.StatusNoContent)
157 }))
158 t.Cleanup(server.Close)
160 client, err := NewClient(
161 func(*http.Client) (gitlab.AuthSource, error) {
162 return gitlab.Unauthenticated{}, nil
163 },
164 WithBaseURL(server.URL+"/api/v4/"),
165 WithCustomHeaders(map[string]string{
166 "X-Debug-Header": "visible-value",
167 "Proxy-Authorization": "Bearer secret-value",
168 }),
169 )
170 require.NoError(t, err)
172 customTransport, ok := client.HTTPClient().Transport.(*customHeadersTransport)
173 require.True(t, ok, "custom header transport should be the outer transport")
174 debug, ok := customTransport.rt.(*debugTransport)
175 require.True(t, ok, "debug transport should receive the injected custom headers")
176 var output bytes.Buffer
177 debug.w = &output
179 resp, err := client.HTTPClient().Get(server.URL)
180 require.NoError(t, err)
181 require.NoError(t, resp.Body.Close())
183 assert.Contains(t, output.String(), "X-Debug-Header: visible-value")
184 assert.Contains(t, output.String(), "Proxy-Authorization: [REDACTED]")
185 assert.NotContains(t, output.String(), "secret-value")
186}
188func TestCustomHeadersCoverAuthSourceAndAPIRequests(t *testing.T) {
189 requestPaths := make(chan string, 2)
190 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
191 requestPaths <- req.URL.Path + ":" + req.Header.Get("Proxy-Authorization")
192 w.WriteHeader(http.StatusOK)
193 }))
194 t.Cleanup(server.Close)
196 client, err := NewClient(
197 func(httpClient *http.Client) (gitlab.AuthSource, error) {
198 resp, err := httpClient.Get(server.URL + "/oauth/token")
199 if err != nil {
200 return nil, err
201 }
202 if err := resp.Body.Close(); err != nil {
203 return nil, err
204 }
205 return gitlab.Unauthenticated{}, nil
206 },
207 WithBaseURL(server.URL+"/api/v4/"),
208 WithCustomHeaders(map[string]string{"Proxy-Authorization": "Bearer proxy-token"}),
209 )
210 require.NoError(t, err)
212 baseURL, err := url.Parse(server.URL + "/api/v4/projects")
213 require.NoError(t, err)
214 req, err := NewHTTPRequest(t.Context(), client, http.MethodPost, baseURL, strings.NewReader(`{"key":"value"}`), nil, true)
215 require.NoError(t, err)
216 resp, err := client.HTTPClient().Do(req)
217 require.NoError(t, err)
218 require.NoError(t, resp.Body.Close())
220 assert.Equal(t, "/oauth/token:Bearer proxy-token", <-requestPaths)
221 assert.Equal(t, "/api/v4/projects:Bearer proxy-token", <-requestPaths)
222}
224func TestCustomHeadersCoverOAuthHostWhenAPIHostDiffers(t *testing.T) {
225 t.Setenv("GITLAB_TOKEN", "")
226 t.Setenv("GITLAB_ACCESS_TOKEN", "")
227 t.Setenv("OAUTH_TOKEN", "")
228 t.Setenv("GLAB_IS_OAUTH2", "")
230 oauthHeader := make(chan string, 1)
231 oauthServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
232 oauthHeader <- req.Header.Get("Proxy-Authorization")
233 w.Header().Set("Content-Type", "application/json")
234 _, _ = io.WriteString(w, `{"access_token":"refreshed-access-token","token_type":"Bearer","refresh_token":"refreshed-refresh-token","expires_in":3600}`)
235 }))
236 t.Cleanup(oauthServer.Close)
238 apiHeader := make(chan string, 1)
239 apiServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
240 apiHeader <- req.Header.Get("Proxy-Authorization")
241 w.WriteHeader(http.StatusNoContent)
242 }))
243 t.Cleanup(apiServer.Close)
245 repoHost := strings.TrimPrefix(oauthServer.URL, "https://")
246 apiHost := strings.TrimPrefix(apiServer.URL, "https://")
247 cfg := config.NewFromStringInDir(fmt.Sprintf(`hosts:
248 %q:
249 api_host: %q
250 api_protocol: https
251 skip_tls_verify: true
252 is_oauth2: true
253 client_id: test-client-id
254 token: expired-access-token
255 oauth2_refresh_token: stored-refresh-token
256 oauth2_expiry_date: %q
257 custom_headers:
258 - name: Proxy-Authorization
259 value: Bearer proxy-token
260`, repoHost, apiHost, time.Now().Add(-time.Hour).Format(time.RFC3339)), t.TempDir())
261 require.NoError(t, cfg.Write())
263 client, err := NewClientFromConfig(repoHost, cfg, false, "test-agent")
264 require.NoError(t, err)
266 _, _, err = client.AuthSource().Header(t.Context())
267 require.NoError(t, err)
268 resp, err := client.HTTPClient().Get(apiServer.URL + "/api/v4/projects")
269 require.NoError(t, err)
270 require.NoError(t, resp.Body.Close())
272 assert.Equal(t, "Bearer proxy-token", <-oauthHeader)
273 assert.Equal(t, "Bearer proxy-token", <-apiHeader)
274}
276func TestNewHTTPRequest_UnauthenticatedAuthSource(t *testing.T) {
277 client := &Client{
278 gitlabClient: &gitlab.Client{},
279 authSource: gitlab.Unauthenticated{},
280 }
282 baseURL, err := url.Parse("https://example.com/api")
283 require.NoError(t, err)
284 req, err := NewHTTPRequest(t.Context(), client, http.MethodGet, baseURL, nil, nil, false)
285 require.NoError(t, err)
286 require.NotNil(t, req)
287 assert.Empty(t, req.Header.Get("PRIVATE-TOKEN"))
288 assert.Empty(t, req.Header.Get("Authorization"))
289 assert.Empty(t, req.Header.Get("Job-Token"))
290}
292func TestClientInitializationWithNoCustomHeaders(t *testing.T) {
293 client, err := NewClient(
294 func(*http.Client) (gitlab.AuthSource, error) {
295 return gitlab.Unauthenticated{}, nil
296 },
297 WithBaseURL("https://example.com/api/v4/"),
298 )
299 require.NoError(t, err)
301 _, wrapped := client.HTTPClient().Transport.(*customHeadersTransport)
302 assert.False(t, wrapped)
303}
Diagnostics6 git calls · 23 ms
CallStatusSizeTime023 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 · 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=44Open pack map
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:02001.1 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=1165 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb 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=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetch2002.5 KiB · hit17 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=2546 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=b5d55eb0934504dde5ae98084c6fffef3647c759 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=2546 git.pkt.payload.max=2519 git.sideband.band1.bytes=2519 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
- a5e130191abfa9fe
- Request
- f9a3d013…
- 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