GitLabgitlab-org
cli
main
1package pm
3import (
4 "crypto/x509"
5 "errors"
6 "net/url"
7 "os"
8 "strings"
10 "gitlab.com/gitlab-org/cli/internal/dbg"
11 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
12)
14// gradleManager runs gradle through the proxy. Unlike Maven's resolver, the
15// Gradle daemon honors the -Dhttp(s).proxyHost/proxyPort JVM system
16// properties, so both routing and the MITM truststore are delivered via
17// GRADLE_OPTS; the user's build files and ~/.gradle are left untouched.
18type gradleManager struct {
19 proxyURL string
20}
22// Gradle returns a fresh gradle package manager.
23func Gradle() PackageManager { return &gradleManager{} }
25func (m *gradleManager) Name() string { return "gradle" }
26func (m *gradleManager) Binary() string { return "gradle" }
28func (m *gradleManager) Environment(proxyURL string) []string {
29 m.proxyURL = proxyURL
30 return nil
31}
33func (m *gradleManager) CATrustEnviron(caPath string) []string {
34 // Proxy routing is a hard requirement: the truststore only makes gradle
35 // trust the proxy CA, it does not route traffic. If routing cannot be
36 // established, adding the truststore alone would make gradle trust the
37 // proxy while connecting directly to the real repository — a silent
38 // firewall bypass (fail-open). So resolve the proxy first and bail out
39 // entirely if it fails, rather than emitting truststore-only GRADLE_OPTS.
40 u, err := url.Parse(m.proxyURL)
41 if err != nil || u.Hostname() == "" || u.Port() == "" {
42 if err != nil {
43 dbg.Debugf("dependency firewall: failed to parse proxy URL %q for gradle: %v", m.proxyURL, err)
44 } else {
45 dbg.Debugf("dependency firewall: no usable proxy host/port for gradle (%q); not routing gradle through the proxy", m.proxyURL)
46 }
47 return nil
48 }
50 args := []string{
51 "-Dhttp.proxyHost=" + u.Hostname(),
52 "-Dhttp.proxyPort=" + u.Port(),
53 "-Dhttps.proxyHost=" + u.Hostname(),
54 "-Dhttps.proxyPort=" + u.Port(),
55 }
57 if raw, err := os.ReadFile(caPath); err != nil {
58 dbg.Debugf("dependency firewall: failed to read CA bundle %s for gradle truststore: %v", caPath, err)
59 } else if cas := parseCertsPEM(raw); len(cas) == 0 {
60 dbg.Debugf("dependency firewall: CA bundle %s has no parsable certificates; gradle will not trust the proxy", caPath)
61 } else {
62 // Write the truststore next to the CA bundle so Run, which already
63 // removes caPath and its ".p12" sibling, cleans it up too.
64 tsPath := caPath + ".p12"
65 if password, tsErr := writeJVMTrustStoreAt(tsPath, cas...); tsErr != nil {
66 dbg.Debugf("dependency firewall: failed to write gradle truststore %s: %v", tsPath, tsErr)
67 } else {
68 args = append(args, jvmTrustArgs(tsPath, password)...)
69 }
70 }
72 opts := strings.Join(args, " ")
73 if existing := os.Getenv("GRADLE_OPTS"); existing != "" {
74 opts = existing + " " + opts
75 }
76 return []string{"GRADLE_OPTS=" + opts}
77}
79// ExistingBundleVars is empty: Gradle trusts the proxy through a generated JVM
80// truststore (GRADLE_OPTS), not a user-provided CA-bundle environment
81// variable, so there is nothing for the engine to prepend.
82func (m *gradleManager) ExistingBundleVars() []string { return nil }
84// CleanupCAFiles removes the PKCS#12 truststore this manager writes next to
85// the CA bundle in CATrustEnviron.
86func (m *gradleManager) CleanupCAFiles(caPath string) {
87 _ = os.Remove(caPath + ".p12")
88}
90// upstreamTrustAnchors returns the certificates in the truststore gradle's
91// JVM would load. glab replaces that truststore for the child, so the user's
92// anchors move to the registry side. The gradle launcher passes JAVA_OPTS
93// before GRADLE_OPTS. The JVM reads JAVA_TOOL_OPTIONS first, the java
94// launcher puts JDK_JAVA_OPTIONS before the command line, and _JAVA_OPTIONS
95// comes last. The error also reports a truststore option in _JAVA_OPTIONS or
96// on the gradle command line, which gradle applies over GRADLE_OPTS, so it
97// replaces glab's in the child.
98func (m *gradleManager) upstreamTrustAnchors(env []string, dir string, args []string) ([]*x509.Certificate, error) {
99 certs, err := jvmTrustAnchors(env, dir, nil,
100 lookupEnv(env, "JAVA_TOOL_OPTIONS"), lookupEnv(env, "JDK_JAVA_OPTIONS"), lookupEnv(env, "JAVA_OPTS"),
101 lookupEnv(env, "GRADLE_OPTS"), lookupEnv(env, "_JAVA_OPTIONS"))
102 return certs, errors.Join(err,
103 jvmTrustStoreOverride(env, "gradle", "GRADLE_OPTS", "_JAVA_OPTIONS"),
104 argTrustStoreOverride(gradleArgProperties(args), "-D%s in the gradle arguments replaces glab's truststore, so gradle cannot trust the dependency firewall proxy; set it in GRADLE_OPTS instead"))
105}
107// gradleArgProperties returns the system properties the gradle command line
108// sets with -Dk=v, -D k=v, --system-prop k=v or --system-prop=k=v, the later
109// winning.
110func gradleArgProperties(args []string) map[string]string {
111 props := map[string]string{}
112 for i := 0; i < len(args); i++ {
113 var kv string
114 switch a := args[i]; {
115 case (a == "-D" || a == "--system-prop") && i+1 < len(args):
116 i++
117 kv = args[i]
118 case strings.HasPrefix(a, "--system-prop="):
119 kv = strings.TrimPrefix(a, "--system-prop=")
120 case strings.HasPrefix(a, "-D") && len(a) > 2:
121 kv = a[2:]
122 default:
123 continue
124 }
125 k, v, _ := strings.Cut(kv, "=")
126 props[k] = v
127 }
128 return props
129}
131func (m *gradleManager) Matcher() proxy.Matcher { return proxy.MavenMatcher{} }
Diagnostics7 git calls · 343 ms
CallStatusSizeTime0343 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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=2 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
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 · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=5 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=26d32585a3a474717e00a318584ccd16918c9d32Open 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=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2002.1 KiB331 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=331 git.response.bytes=2140 git.response.cache=MISS git.response.duration_ms=331 git.fetch.wants=8a60115e78b2596f3cd62cd38bf8f65b702e19e8 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=2140 git.pkt.payload.max=2113 git.sideband.band1.bytes=2113 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
- a5e2134789b2c6f8
- Request
- c292da74…
- 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