GitLabgitlab-org

cli

main
ply7 git calls206 ms99 KiB4 cachedprotocol v2
202 lines6.1 KiBRaw
1//go:build !integration
3package pm
5import (
6 "crypto/rand"
7 "crypto/rsa"
8 "crypto/x509"
9 "crypto/x509/pkix"
10 "encoding/pem"
11 "math/big"
12 "os"
13 "path/filepath"
14 "runtime"
15 "strings"
16 "testing"
17 "time"
19 "github.com/stretchr/testify/assert"
20 "github.com/stretchr/testify/require"
21 "software.sslmate.com/src/go-pkcs12"
24// selfSignedTestCA builds a throwaway self-signed CA certificate for tests.
25func selfSignedTestCA(t *testing.T) *x509.Certificate {
26 t.Helper()
27 key, err := rsa.GenerateKey(rand.Reader, 2048)
28 require.NoError(t, err)
29 tmpl := &x509.Certificate{
30 SerialNumber: big.NewInt(1),
31 Subject: pkix.Name{CommonName: "glab-df-test-ca"},
32 NotBefore: time.Now().Add(-time.Hour),
33 NotAfter: time.Now().Add(time.Hour),
34 IsCA: true,
35 BasicConstraintsValid: true,
36 }
37 der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
38 require.NoError(t, err)
39 cert, err := x509.ParseCertificate(der)
40 require.NoError(t, err)
41 return cert
44// caPEM PEM-encodes a certificate for tests that need the on-disk form.
45func caPEM(t *testing.T, ca *x509.Certificate) []byte {
46 t.Helper()
47 return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.Raw})
50func TestWriteJVMTrustStoreAt(t *testing.T) {
51 t.Parallel()
52 ca := selfSignedTestCA(t)
53 path := filepath.Join(t.TempDir(), "ts.p12")
55 password, err := writeJVMTrustStoreAt(path, ca)
56 require.NoError(t, err)
57 assert.NotEmpty(t, password)
59 raw, err := os.ReadFile(path)
60 require.NoError(t, err)
62 certs, err := pkcs12.DecodeTrustStore(raw, password)
63 require.NoError(t, err)
64 // The proxy CA must be present among the trusted certs.
65 found := false
66 for _, c := range certs {
67 if c.Equal(ca) {
68 found = true
69 }
70 }
71 assert.True(t, found, "proxy CA should be in the truststore")
74func TestJVMTrustArgs(t *testing.T) {
75 t.Parallel()
76 args := jvmTrustArgs("/tmp/ts.p12", "secret")
77 joined := strings.Join(args, " ")
78 assert.Contains(t, joined, "-Djavax.net.ssl.trustStore=/tmp/ts.p12")
79 assert.Contains(t, joined, "-Djavax.net.ssl.trustStorePassword=secret")
80 assert.Contains(t, joined, "-Djavax.net.ssl.trustStoreType=PKCS12")
83func TestJVMTrustOpts(t *testing.T) {
84 t.Parallel()
85 ca := selfSignedTestCA(t)
86 caFile := filepath.Join(t.TempDir(), "ca.pem")
87 require.NoError(t, os.WriteFile(caFile, caPEM(t, ca), 0o600))
89 opts := JVMTrustOpts(caFile)
90 require.NotEmpty(t, opts)
91 assert.Contains(t, opts, "-Djavax.net.ssl.trustStore="+caFile+".p12")
92 assert.Contains(t, opts, "-Djavax.net.ssl.trustStoreType=PKCS12")
94 // The generated PKCS#12 store must actually trust the CA.
95 tsPath := caFile + ".p12"
96 raw, err := os.ReadFile(tsPath)
97 require.NoError(t, err)
98 certs, err := pkcs12.DecodeTrustStore(raw, jvmTrustStorePassword)
99 require.NoError(t, err)
100 found := false
101 for _, c := range certs {
102 if c.Equal(ca) {
103 found = true
104 }
105 }
106 assert.True(t, found, "CA from the bundle should be in the JVM truststore")
109func TestJVMTrustOptsPrependsExisting(t *testing.T) {
110 ca := selfSignedTestCA(t)
111 caFile := filepath.Join(t.TempDir(), "ca.pem")
112 require.NoError(t, os.WriteFile(caFile, caPEM(t, ca), 0o600))
114 t.Setenv("MAVEN_OPTS", "-Xmx512m")
115 opts := JVMTrustOpts(caFile)
116 assert.True(t, strings.HasPrefix(opts, "-Xmx512m "),
117 "existing MAVEN_OPTS must be preserved ahead of the truststore flags")
120func TestJVMTrustOptsEmptyOnNoCerts(t *testing.T) {
121 t.Parallel()
122 caFile := filepath.Join(t.TempDir(), "empty.pem")
123 require.NoError(t, os.WriteFile(caFile, []byte("not a certificate"), 0o600))
125 assert.Empty(t, JVMTrustOpts(caFile),
126 "a bundle with no parsable certificates yields no JVM flags")
129// TestSystemRootsReturnsHostRoots guards the cross-platform sourcing: on a
130// normally-configured host the JVM truststore must carry the public roots in
131// addition to the proxy CA, otherwise -Djavax.net.ssl.trustStore (which
132// replaces the default store) breaks every non-intercepted TLS connection.
133func TestSystemRootsReturnsHostRoots(t *testing.T) {
134 t.Parallel()
135 roots := systemRoots()
136 if len(roots) == 0 {
137 t.Skipf("no system roots available on %s test host; sourcing exercised elsewhere", runtime.GOOS)
138 }
139 for _, c := range roots {
140 require.NotNil(t, c)
141 }
144// TestEncodeJVMTrustStoreIncludesSystemRoots verifies the encoded truststore
145// carries both the proxy CA and the host system roots, so the JVM keeps
146// validating public dependencies alongside proxy-intercepted traffic.
147func TestEncodeJVMTrustStoreIncludesSystemRoots(t *testing.T) {
148 t.Parallel()
149 sysRoots := systemRoots()
150 if len(sysRoots) == 0 {
151 t.Skipf("no system roots available on %s test host", runtime.GOOS)
152 }
153 proxyCA := selfSignedTestCA(t)
155 path := filepath.Join(t.TempDir(), "ts.p12")
156 _, err := writeJVMTrustStoreAt(path, proxyCA)
157 require.NoError(t, err)
159 raw, err := os.ReadFile(path)
160 require.NoError(t, err)
161 certs, err := pkcs12.DecodeTrustStore(raw, jvmTrustStorePassword)
162 require.NoError(t, err)
164 foundProxy := false
165 for _, c := range certs {
166 if c.Equal(proxyCA) {
167 foundProxy = true
168 }
169 }
170 assert.True(t, foundProxy, "proxy CA must be trusted")
171 assert.GreaterOrEqual(t, len(certs), len(sysRoots)+1,
172 "truststore must contain the system roots plus the proxy CA")
175func TestEncodeJVMTrustStoreListsEachRootOnce(t *testing.T) {
176 t.Parallel()
177 sysRoots := systemRoots()
178 if len(sysRoots) == 0 {
179 t.Skipf("no system roots available on %s test host", runtime.GOOS)
180 }
181 proxyCA := selfSignedTestCA(t)
183 path := filepath.Join(t.TempDir(), "ts.p12")
184 _, err := writeJVMTrustStoreAt(path, append([]*x509.Certificate{proxyCA}, sysRoots...)...)
185 require.NoError(t, err)
187 raw, err := os.ReadFile(path)
188 require.NoError(t, err)
189 certs, err := pkcs12.DecodeTrustStore(raw, jvmTrustStorePassword)
190 require.NoError(t, err)
191 assert.Len(t, certs, 1+len(sysRoots), "a system root already in the CA bundle must not be added again")
194func TestSystemRootsHoldOnlyCAsOnMacOS(t *testing.T) {
195 t.Parallel()
196 if runtime.GOOS != "darwin" {
197 t.Skip("only macOS reads the keychains, which also hold leaf certificates")
198 }
199 for _, c := range systemRoots() {
200 assert.True(t, c.IsCA, "%s is not a CA, so it must not become a trust anchor", c.Subject)
201 }
Diagnostics7 git calls · 206 ms
CallStatusSizeTime0206 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 KiB2 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=UPDATING
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=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: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=2
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.3 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=1327
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62
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=1327
git.pkt.payload.max=1300
git.sideband.band1.bytes=1300
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1300
git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4
Open pack map
fetch2002.0 KiB196 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=196
git.response.bytes=2048
git.response.cache=MISS
git.response.duration_ms=196
git.fetch.wants=32a3048664efa37c2282d8e652804a40bd956ec2
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=2048
git.pkt.payload.max=2021
git.sideband.band1.bytes=2021
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
a5e1bb0169036c75
Request
21d838e7…
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