GitLabgitlab-org

cli

main
ply7 git calls210 ms98 KiB4 cachedprotocol v2
162 lines5.7 KiBRaw
1//go:build !integration
3package proxy
5import (
6 "crypto/ecdsa"
7 "crypto/elliptic"
8 "crypto/rand"
9 "crypto/tls"
10 "crypto/x509"
11 "crypto/x509/pkix"
12 "io"
13 "net/http"
14 "net/http/httptest"
15 "net/url"
16 "sync/atomic"
17 "testing"
18 "time"
20 "github.com/stretchr/testify/assert"
21 "github.com/stretchr/testify/require"
24const tarballPath = "/left-pad/-/left-pad-1.3.0.tgz"
26// certRecordingRegistry requests, without requiring, a client certificate and
27// counts the ones it receives. A client that holds any certificate offers it
28// to such a server, so a zero count proves the certificate was withheld.
29type certRecordingRegistry struct {
30 server *httptest.Server
31 hits atomic.Int64
32 peerCerts atomic.Int64
35func startCertRecordingRegistry(t *testing.T) *certRecordingRegistry {
36 t.Helper()
37 r := &certRecordingRegistry{}
38 r.server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
39 r.hits.Add(1)
40 r.peerCerts.Add(int64(len(req.TLS.PeerCertificates)))
41 _, _ = io.WriteString(w, "TARBALL")
42 }))
43 r.server.TLS = &tls.Config{ClientAuth: tls.RequestClientCert}
44 r.server.StartTLS()
45 t.Cleanup(r.server.Close)
46 return r
49// startMTLSCorporateProxy starts a TLS corporate proxy that requires basic
50// auth and a client certificate from its own client CA. It returns the CA
51// that signed the proxy's server certificate and a client certificate the
52// proxy accepts.
53func startMTLSCorporateProxy(t *testing.T) (*corporateProxy, *x509.Certificate, tls.Certificate) {
54 t.Helper()
55 clientCA, err := newCertAuthority()
56 require.NoError(t, err)
57 clientCAs := x509.NewCertPool()
58 clientCAs.AddCert(clientCA.cert)
59 corporate, corporateCA := startTLSCorporateProxy(t, "corp", "secret", clientCAs)
61 key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
62 require.NoError(t, err)
63 serial, err := randomSerial()
64 require.NoError(t, err)
65 tmpl := &x509.Certificate{
66 SerialNumber: serial,
67 Subject: pkix.Name{CommonName: "glab user"},
68 NotBefore: time.Now().Add(-time.Hour),
69 NotAfter: time.Now().Add(time.Hour),
70 KeyUsage: x509.KeyUsageDigitalSignature,
71 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
72 }
73 der, err := x509.CreateCertificate(rand.Reader, tmpl, clientCA.cert, &key.PublicKey, clientCA.key)
74 require.NoError(t, err)
75 return corporate, corporateCA, tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key}
78func requireTarball(t *testing.T, p *Proxy, registry *certRecordingRegistry) {
79 t.Helper()
80 resp, err := newClient(t, p).Get(registry.server.URL + tarballPath)
81 require.NoError(t, err)
82 defer resp.Body.Close()
83 body, err := io.ReadAll(resp.Body)
84 require.NoError(t, err)
85 assert.Equal(t, http.StatusOK, resp.StatusCode)
86 assert.Equal(t, "TARBALL", string(body))
89func TestProxyPresentsClientCertificateOnlyToMTLSOutboundProxy(t *testing.T) {
90 t.Parallel()
91 registry := startCertRecordingRegistry(t)
92 corporate, corporateCA, cert := startMTLSCorporateProxy(t)
94 p := startProxyTrusting(t, []*x509.Certificate{registry.server.Certificate(), corporateCA},
95 WithUpstreamProxy(http.ProxyURL(corporate.URL(t, "corp", "secret"))),
96 WithUpstreamProxyCertificate(cert))
97 requireTarball(t, p, registry)
99 assert.Equal(t, []string{registry.server.Listener.Addr().String()}, corporate.Tunnels())
100 assert.Zero(t, registry.peerCerts.Load(), "the registry must never receive the proxy client certificate")
103func TestProxyFailsWhenMTLSOutboundProxyGetsNoCertificate(t *testing.T) {
104 t.Parallel()
105 registry := startCertRecordingRegistry(t)
106 corporate, corporateCA, _ := startMTLSCorporateProxy(t)
108 p := startProxyTrusting(t, []*x509.Certificate{registry.server.Certificate(), corporateCA},
109 WithUpstreamProxy(http.ProxyURL(corporate.URL(t, "corp", "secret"))))
110 resp, err := newClient(t, p).Get(registry.server.URL + tarballPath)
111 if err == nil {
112 resp.Body.Close()
113 t.Fatalf("expected the request to fail when the outbound proxy gets no client certificate, got status %d", resp.StatusCode)
114 }
116 assert.Empty(t, corporate.Tunnels())
117 assert.Zero(t, registry.hits.Load())
120func TestProxyWithClientCertificateStillVerifiesOutboundProxy(t *testing.T) {
121 t.Parallel()
122 registry := startCertRecordingRegistry(t)
123 corporate, _, cert := startMTLSCorporateProxy(t)
125 p := startProxyTrusting(t, []*x509.Certificate{registry.server.Certificate()},
126 WithUpstreamProxy(http.ProxyURL(corporate.URL(t, "corp", "secret"))),
127 WithUpstreamProxyCertificate(cert))
128 resp, err := newClient(t, p).Get(registry.server.URL + tarballPath)
129 if err == nil {
130 resp.Body.Close()
131 t.Fatalf("expected the request to fail when the outbound proxy's certificate is untrusted, got status %d", resp.StatusCode)
132 }
134 assert.Empty(t, corporate.Tunnels())
135 assert.Zero(t, registry.hits.Load())
138func TestProxyNeverPresentsClientCertificateToDirectRegistry(t *testing.T) {
139 t.Parallel()
140 proxied := startCertRecordingRegistry(t)
141 direct := startCertRecordingRegistry(t)
142 corporate, corporateCA, cert := startMTLSCorporateProxy(t)
143 corporateURL := corporate.URL(t, "corp", "secret")
144 directHost := direct.server.Listener.Addr().String()
145 // A host listed in NO_PROXY makes http.ProxyFromEnvironment return no proxy.
146 noProxyForDirect := func(req *http.Request) (*url.URL, error) {
147 if req.URL.Host == directHost {
148 return nil, nil
149 }
150 return corporateURL, nil
151 }
153 p := startProxyTrusting(t, []*x509.Certificate{proxied.server.Certificate(), corporateCA},
154 WithUpstreamProxy(noProxyForDirect),
155 WithUpstreamProxyCertificate(cert))
156 requireTarball(t, p, proxied)
157 requireTarball(t, p, direct)
159 assert.Equal(t, []string{proxied.server.Listener.Addr().String()}, corporate.Tunnels())
160 assert.Zero(t, proxied.peerCerts.Load(), "the proxied registry must never receive the proxy client certificate")
161 assert.Zero(t, direct.peerCerts.Load(), "a direct registry must never receive the proxy client certificate")
Diagnostics7 git calls · 210 ms
CallStatusSizeTime0210 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 KiB3 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=UPDATING
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 · 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=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: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=2
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
fetchtree:0200361 B · 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=361
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247
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=361
git.pkt.payload.max=334
git.sideband.band1.bytes=334
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=334
git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.0 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=199
git.response.bytes=1051
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=7a2a90605315ba5b37f63c08726e6b2caa26e48d
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=1051
git.pkt.payload.max=1024
git.sideband.band1.bytes=1024
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1024
git.pack.trailer=7503ef36a75d3595b473035c69e639fd0ce41598
Open pack map
fetch2001.8 KiB199 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1848
git.response.cache=MISS
git.response.duration_ms=199
git.fetch.wants=b6d3e7cae4647608fd7812279f0cc6db2acec48a
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=1848
git.pkt.payload.max=1821
git.sideband.band1.bytes=1821
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
a5e1df0fd8f5ecfa
Request
b14e971e…
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