GitLabgitlab-org
cli
main
1package pm
3import (
4 "crypto/x509"
5 "encoding/pem"
6 "errors"
7 "io"
8 "os"
9 "os/exec"
10 "runtime"
11 "slices"
12 "strings"
13 "sync"
15 "software.sslmate.com/src/go-pkcs12"
17 "gitlab.com/gitlab-org/cli/internal/dbg"
18)
20// jvmTrustStorePassword is the password for the generated PKCS#12 truststore.
21// The store holds only public certificates (no private keys) and lives in a
22// temp file removed after the run, so the password is not a secret; the JVM
23// simply requires one.
24const jvmTrustStorePassword = "df-truststore-not-secret"
26// writeJVMTrustStoreAt builds a PKCS#12 truststore at path containing the
27// given CA certificates (the proxy MITM CA, plus any user CAs prepended into
28// the proxy bundle) and the host's system roots, so the JVM (Maven, Gradle)
29// trusts the proxy while still validating public TLS dependencies. On the Run
30// path the proxy bundle already holds the system roots, so a root in cas is
31// not added twice. The caller ties the file's lifecycle to a known path it
32// already cleans up. It returns the truststore password.
33func writeJVMTrustStoreAt(path string, cas ...*x509.Certificate) (string, error) {
34 f, err := os.Create(path)
35 if err != nil {
36 return "", err
37 }
38 if err := encodeJVMTrustStore(f, cas...); err != nil {
39 return "", errors.Join(err, f.Close())
40 }
41 if err := f.Close(); err != nil {
42 return "", err
43 }
44 return jvmTrustStorePassword, nil
45}
47// encodeJVMTrustStore encodes cas plus the system roots into a PKCS#12
48// truststore and writes it to w.
49func encodeJVMTrustStore(w io.Writer, cas ...*x509.Certificate) error {
50 sysRoots := systemRoots()
51 roots := slices.Clone(cas)
52 for _, r := range sysRoots {
53 if !slices.ContainsFunc(cas, r.Equal) {
54 roots = append(roots, r)
55 }
56 }
58 // pkcs12.Modern.EncodeTrustStore marks the certs with the Java TrustStore
59 // OID (Java 8+). The method form takes (certs, password); the package-level
60 // pkcs12.EncodeTrustStore additionally takes a rand source.
61 der, err := pkcs12.Modern.EncodeTrustStore(roots, jvmTrustStorePassword)
62 if err != nil {
63 return err
64 }
65 _, err = w.Write(der)
66 return err
67}
69// jvmTrustArgs returns the JVM system-property flags that point at the
70// truststore at path with the given password.
71func jvmTrustArgs(path, password string) []string {
72 return []string{
73 "-Djavax.net.ssl.trustStore=" + path,
74 "-Djavax.net.ssl.trustStorePassword=" + password,
75 "-Djavax.net.ssl.trustStoreType=PKCS12",
76 }
77}
79// JVMTrustOpts reads the PEM CA bundle at caPath, writes a PKCS#12 truststore
80// at caPath+".p12" (which Run cleans up), and returns the JVM truststore flags
81// as a single space-joined string, prepending any value already in MAVEN_OPTS.
82// It returns "" when the CA bundle has no parsable certificates.
83func JVMTrustOpts(caPath string) string {
84 raw, err := os.ReadFile(caPath)
85 if err != nil {
86 dbg.Debugf("dependency firewall: failed to read CA bundle %s for JVM truststore: %v", caPath, err)
87 return ""
88 }
89 cas := parseCertsPEM(raw)
90 if len(cas) == 0 {
91 dbg.Debugf("dependency firewall: CA bundle %s has no parsable certificates; JVM will not trust the proxy", caPath)
92 return ""
93 }
94 tsPath := caPath + ".p12"
95 password, err := writeJVMTrustStoreAt(tsPath, cas...)
96 if err != nil {
97 dbg.Debugf("dependency firewall: failed to write JVM truststore %s: %v", tsPath, err)
98 return ""
99 }
100 opts := strings.Join(jvmTrustArgs(tsPath, password), " ")
101 if existing := os.Getenv("MAVEN_OPTS"); existing != "" {
102 opts = existing + " " + opts
103 }
104 return opts
105}
107// systemRoots returns the host's trusted root certificates, read once per
108// process. Most CA settings glab points at its bundle, like the JVM's
109// -Djavax.net.ssl.trustStore flag, replace (not augment) a tool's default
110// trust, so a bundle or truststore built from the proxy CA alone would break
111// every connection the proxy does not intercept. The Go standard library's
112// x509.SystemCertPool does not expose the underlying certificates (only DER
113// subjects), so the roots are sourced per platform: PEM CA-bundle files on
114// Linux, the security(1) keychains on macOS, and the LocalMachine certificate
115// stores on Windows. It returns nil when none can be read.
116var systemRoots = sync.OnceValue(func() []*x509.Certificate {
117 var roots []*x509.Certificate
118 switch runtime.GOOS {
119 case "darwin":
120 roots = darwinSystemRoots()
121 case "windows":
122 roots = windowsSystemRoots()
123 default:
124 roots = unixSystemRoots()
125 }
126 if len(roots) == 0 {
127 dbg.Debugf("dependency firewall: no system root certificates found for %s; tools that skip the proxy may fail to verify public hosts", runtime.GOOS)
128 }
129 return roots
130})
132// SystemCABundlePaths are the common locations of the OS CA bundle across
133// Linux and BSD distributions, tried in order.
134var SystemCABundlePaths = []string{
135 "/etc/ssl/certs/ca-certificates.crt", // Debian, Ubuntu, Alpine
136 "/etc/pki/tls/certs/ca-bundle.crt", // Fedora, RHEL, CentOS
137 "/etc/ssl/ca-bundle.pem", // openSUSE
138 "/etc/pki/tls/cacert.pem",
139 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
140 "/etc/ssl/cert.pem", // Alpine, some BSDs
141}
143// unixSystemRoots reads the first CA bundle in SystemCABundlePaths that holds
144// certificates.
145func unixSystemRoots() []*x509.Certificate {
146 for _, p := range SystemCABundlePaths {
147 raw, err := os.ReadFile(p)
148 if err != nil {
149 continue
150 }
151 if certs := parseCertsPEM(raw); len(certs) > 0 {
152 return certs
153 }
154 }
155 return nil
156}
158// darwinSystemRoots exports the CA certificates in the macOS keychains via
159// security(1), which prints the certificates as concatenated PEM.
160func darwinSystemRoots() []*x509.Certificate {
161 keychains := []string{
162 "/System/Library/Keychains/SystemRootCertificates.keychain",
163 "/Library/Keychains/System.keychain",
164 }
165 var certs []*x509.Certificate
166 for _, kc := range keychains {
167 out, err := exec.Command("/usr/bin/security", "find-certificate", "-a", "-p", kc).Output()
168 if err != nil {
169 dbg.Debugf("dependency firewall: security find-certificate failed for %s: %v", kc, err)
170 continue
171 }
172 for _, c := range parseCertsPEM(out) {
173 if c.IsCA {
174 certs = append(certs, c)
175 }
176 }
177 }
178 return certs
179}
181// windowsSystemRoots exports the trusted roots from the Windows LocalMachine
182// certificate stores via PowerShell, which prints them as concatenated PEM.
183func windowsSystemRoots() []*x509.Certificate {
184 const script = `$ErrorActionPreference='Stop';` +
185 `foreach ($store in 'Root','CA') {` +
186 `Get-ChildItem -Path "Cert:\LocalMachine\$store" | ForEach-Object {` +
187 `"-----BEGIN CERTIFICATE-----";` +
188 `[System.Convert]::ToBase64String($_.RawData, 'InsertLineBreaks');` +
189 `"-----END CERTIFICATE-----" } }`
190 out, err := exec.Command("powershell", "-NoProfile", "-NonInteractive", "-Command", script).Output()
191 if err != nil {
192 dbg.Debugf("dependency firewall: powershell certificate export failed: %v", err)
193 return nil
194 }
195 return parseCertsPEM(out)
196}
198func parseCertsPEM(raw []byte) []*x509.Certificate {
199 var certs []*x509.Certificate
200 for {
201 block, rest := pem.Decode(raw)
202 if block == nil {
203 break
204 }
205 raw = rest
206 if block.Type != "CERTIFICATE" {
207 continue
208 }
209 if c, err := x509.ParseCertificate(block.Bytes); err == nil {
210 certs = append(certs, c)
211 }
212 }
213 return certs
214}
Diagnostics7 git calls · 497 ms
CallStatusSizeTime0497 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 KiB189 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=176 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=189 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 · 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=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=44Open 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 · 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=361 git.response.cache=HIT git.response.duration_ms=3 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=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:02001.3 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=1327 git.response.cache=HIT git.response.duration_ms=3 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=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2002.8 KiB298 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=298 git.response.bytes=2841 git.response.cache=MISS git.response.duration_ms=298 git.fetch.wants=d5e334cd95a171cffa5d18a89d416d58b86bb423 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=2841 git.pkt.payload.max=2814 git.sideband.band1.bytes=2814 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
- a5e1ba463fcb6c75
- Request
- f755918e…
- 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