GitLabgitlab-org
cli
main
1package pm
3import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "io/fs"
9 "os"
10 "path/filepath"
11 "slices"
12 "strings"
14 "gitlab.com/gitlab-org/cli/internal/dbg"
15 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/fsx"
16 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
17 "gitlab.com/gitlab-org/cli/internal/iostreams"
18)
20// bundleManager runs Bundler through the proxy. Bundler honors the lowercase
21// proxy variables and SSL_CERT_FILE/BUNDLE_SSL_CA_CERT for CA trust; the
22// Gemfile is left untouched.
23//
24// Known gap (same as gem): a gemrc "http_proxy:" setting outranks the env
25// pins. Bundler applies Gem.configuration[:http_proxy] on top of its :ENV
26// proxy (2.5.22 fetcher.rb). Its sources are the system gemrc, ~/.gemrc, or a
27// file named by an inherited GEMRC env var — none repo-committable, so only a
28// compromised CI image or pipeline env, not a committed repo file, can route
29// bundle traffic around inspection this way.
30type bundleManager struct{}
32// Bundle returns the bundle package manager.
33func Bundle() PackageManager { return bundleManager{} }
35func (bundleManager) Name() string { return "bundle" }
36func (bundleManager) Binary() string { return "bundle" }
38// Environment adds the lowercase proxy variables. The engine already sets
39// uppercase HTTP_PROXY/HTTPS_PROXY in proxyEnviron, but Bundler resolves the
40// proxy through net-http-persistent constructed with proxy: :ENV, which reads
41// the lowercase http_proxy/https_proxy names (it does check both cases, so the
42// lowercase pair is defense-in-depth rather than the sole routing path). A
43// repo-local .bundle/config BUNDLE_HTTP_PROXY does not override these: Bundler
44// never consults Bundler.settings for the proxy at all — its fetcher only uses
45// the :ENV mode — so a committed BUNDLE_HTTP_PROXY is simply inert (verified
46// against Bundler 2.5.22 fetcher.rb). Only the lowercase pair is added; the
47// uppercase names the engine sets are not duplicated.
48func (bundleManager) Environment(proxyURL string) []string {
49 return []string{
50 "https_proxy=" + proxyURL,
51 "http_proxy=" + proxyURL,
52 }
53}
55// CATrustEnviron trusts the proxy CA via both the generic OpenSSL SSL_CERT_FILE
56// (honored by RubyGems/Net::HTTP) and Bundler's BUNDLE_SSL_CA_CERT. A local
57// config's BUNDLE_SSL_CA_CERT outranks this one; Setup handles it.
58func (bundleManager) CATrustEnviron(caPath string) []string {
59 return []string{
60 "SSL_CERT_FILE=" + caPath,
61 "BUNDLE_SSL_CA_CERT=" + caPath,
62 }
63}
65// ExistingBundleVars lists both CA vars CATrustEnviron overwrites so a user
66// who set only BUNDLE_SSL_CA_CERT keeps their anchors, not just one who set
67// SSL_CERT_FILE.
68func (bundleManager) ExistingBundleVars() []string {
69 return []string{"SSL_CERT_FILE", "BUNDLE_SSL_CA_CERT"}
70}
72func (bundleManager) CleanupCAFiles(string) {}
74func (bundleManager) Matcher() proxy.Matcher { return proxy.GemMatcher{} }
76func (bundleManager) Setup(_ context.Context, in setupInput) (setupResult, error) {
77 noop := setupResult{stop: func() {}}
78 cwd, err := os.Getwd()
79 if err != nil {
80 return setupResult{}, fmt.Errorf("failed to get the working directory: %w", err)
81 }
82 dir := bundleAppConfigDir(in.parent, cwd)
83 if dir == "" {
84 return noop, nil
85 }
86 user, ok, err := readBundleLocalConfig(dir)
87 if err != nil {
88 return setupResult{}, err
89 }
90 if !ok {
91 return noop, nil
92 }
93 cp, err := writeBundleConfigCopy(user, bundleUserCA(in.io, cwd, user), in.caPath)
94 if err != nil {
95 return setupResult{}, fmt.Errorf("failed to copy the local Bundler config: %w", err)
96 }
97 return setupResult{env: []string{"BUNDLE_APP_CONFIG=" + cp.appDir}, stop: func() { cp.save(in.io, user) }}, nil
98}
100type bundleLocalConfig struct {
101 dir, path string
102 lines []string
103 userCA, caLine string
104}
106func readBundleLocalConfig(dir string) (bundleLocalConfig, bool, error) {
107 c := bundleLocalConfig{dir: dir, path: filepath.Join(dir, "config")}
108 raw, err := os.ReadFile(c.path)
109 if errors.Is(err, fs.ErrNotExist) {
110 return c, false, nil
111 }
112 if err != nil {
113 return c, false, fmt.Errorf("failed to read the local Bundler config: %w", err)
114 }
115 c.lines = strings.Split(string(raw), "\n")
116 for _, line := range c.lines {
117 if v, ok := bundleConfigCACert(line); ok {
118 c.userCA, c.caLine = v, line
119 }
120 }
121 return c, c.caLine != "", nil
122}
124func bundleUserCA(ios *iostreams.IOStreams, cwd string, c bundleLocalConfig) []byte {
125 if c.userCA == "" {
126 return nil
127 }
128 path := c.userCA
129 if !filepath.IsAbs(path) {
130 path = filepath.Join(cwd, path)
131 }
132 caPEM, err := os.ReadFile(path)
133 if err != nil {
134 ios.LogErrorf("warning: could not read BUNDLE_SSL_CA_CERT %q from %s: %v\n", c.userCA, c.path, err)
135 }
136 if len(caPEM) > 0 && caPEM[len(caPEM)-1] != '\n' {
137 caPEM = append(caPEM, '\n')
138 }
139 return caPEM
140}
142type bundleConfigCopy struct {
143 tmp, appDir, caFile string
144 generated []byte
145}
147func writeBundleConfigCopy(c bundleLocalConfig, userCA []byte, glabCAPath string) (bundleConfigCopy, error) {
148 tmp, err := os.MkdirTemp("", "glab-df-bundle-*")
149 if err != nil {
150 return bundleConfigCopy{}, err
151 }
152 cp := bundleConfigCopy{tmp: tmp, appDir: filepath.Join(tmp, "bundle"), caFile: filepath.Join(tmp, "ca.pem")}
153 if err := cp.fill(c, userCA, glabCAPath); err != nil {
154 return bundleConfigCopy{}, errors.Join(err, os.RemoveAll(tmp))
155 }
156 return cp, nil
157}
159func (cp *bundleConfigCopy) fill(c bundleLocalConfig, userCA []byte, glabCAPath string) error {
160 glabCA, err := os.ReadFile(glabCAPath)
161 if err != nil {
162 return err
163 }
164 if err := os.WriteFile(cp.caFile, append(userCA, glabCA...), 0o600); err != nil {
165 return err
166 }
167 if err := os.Mkdir(cp.appDir, 0o700); err != nil {
168 return err
169 }
170 copied := slices.Clone(c.lines)
171 for i, line := range copied {
172 if _, ok := bundleConfigCACert(line); ok {
173 copied[i] = `BUNDLE_SSL_CA_CERT: "` + cp.caFile + `"`
174 }
175 }
176 cp.generated = []byte(strings.Join(copied, "\n"))
177 if err := os.WriteFile(filepath.Join(cp.appDir, "config"), cp.generated, 0o600); err != nil {
178 return err
179 }
180 entries, err := os.ReadDir(c.dir)
181 if err != nil {
182 return err
183 }
184 for _, e := range entries {
185 if e.Name() == "config" {
186 continue
187 }
188 if err := os.Symlink(filepath.Join(c.dir, e.Name()), filepath.Join(cp.appDir, e.Name())); err != nil {
189 return err
190 }
191 }
192 return nil
193}
195func (cp bundleConfigCopy) save(ios *iostreams.IOStreams, c bundleLocalConfig) {
196 cp.saveConfig(ios, c)
197 if !moveAddedEntries(ios, cp.appDir, c.dir) {
198 _ = os.RemoveAll(cp.tmp)
199 }
200}
202func (cp bundleConfigCopy) saveConfig(ios *iostreams.IOStreams, c bundleLocalConfig) {
203 current, err := os.ReadFile(filepath.Join(cp.appDir, "config"))
204 switch {
205 case err != nil:
206 ios.LogErrorf("warning: could not save Bundler's changes to %s: %v\n", c.path, err)
207 return
208 case bytes.Equal(current, cp.generated):
209 return
210 }
211 restored := restoreBundleCALine(current, cp.caFile, c.caLine)
212 if bytes.Contains(restored, []byte(filepath.Base(cp.tmp))) {
213 ios.LogErrorf("warning: could not restore BUNDLE_SSL_CA_CERT in Bundler's changes to %s, so they were not saved\n", c.path)
214 return
215 }
216 path := c.path
217 if target, err := filepath.EvalSymlinks(path); err == nil {
218 path = target
219 }
220 if err := fsx.WriteOwnerOnly(path, restored); err != nil {
221 ios.LogErrorf("warning: could not save Bundler's changes to %s: %v\n", c.path, err)
222 }
223}
225func restoreBundleCALine(config []byte, caFile, caLine string) []byte {
226 lines := strings.Split(string(config), "\n")
227 for i, line := range lines {
228 v, ok := bundleConfigCACert(line)
229 if ok && (v == caFile || v == strings.ReplaceAll(caFile, `\`, `\\`)) {
230 lines[i] = caLine
231 }
232 }
233 return []byte(strings.Join(lines, "\n"))
234}
236func moveAddedEntries(ios *iostreams.IOStreams, copyDir, userDir string) bool {
237 entries, err := os.ReadDir(copyDir)
238 if err != nil {
239 ios.LogErrorf("warning: could not check %s for files Bundler added: %v\n", copyDir, err)
240 return false
241 }
242 left := false
243 for _, e := range entries {
244 if e.Name() == "config" || e.Type()&fs.ModeSymlink != 0 {
245 continue
246 }
247 src, dst := filepath.Join(copyDir, e.Name()), filepath.Join(userDir, e.Name())
248 if err := os.Rename(src, dst); err != nil {
249 if copyErr := copyEntry(src, dst); copyErr != nil {
250 ios.LogErrorf("warning: could not save %s, which Bundler added to %s; it is still in %s: %v\n", e.Name(), userDir, src, errors.Join(err, copyErr))
251 left = true
252 } else {
253 dbg.Debugf("dependency firewall: could not move %s (%v), copied it instead", src, err)
254 }
255 }
256 }
257 return left
258}
260func copyEntry(src, dst string) error {
261 switch _, err := os.Lstat(dst); {
262 case err == nil:
263 return fmt.Errorf("%s: %w", dst, fs.ErrExist)
264 case !errors.Is(err, fs.ErrNotExist):
265 return err
266 }
267 info, err := os.Lstat(src)
268 if err != nil {
269 return err
270 }
271 if info.IsDir() {
272 err = os.CopyFS(dst, os.DirFS(src))
273 } else {
274 var data []byte
275 if data, err = os.ReadFile(src); err == nil {
276 err = os.WriteFile(dst, data, info.Mode().Perm())
277 }
278 }
279 if err != nil {
280 return errors.Join(err, os.RemoveAll(dst))
281 }
282 return nil
283}
285func bundleAppConfigDir(env []string, cwd string) string {
286 root := bundleRoot(env, cwd)
287 if root == "" {
288 return ""
289 }
290 v := lookupEnv(env, "BUNDLE_APP_CONFIG")
291 switch {
292 case v == "":
293 return filepath.Join(root, ".bundle")
294 case filepath.IsAbs(v):
295 return v
296 }
297 return filepath.Join(root, v)
298}
300func bundleRoot(env []string, cwd string) string {
301 if gemfile := lookupEnv(env, "BUNDLE_GEMFILE"); gemfile != "" {
302 if !filepath.IsAbs(gemfile) {
303 gemfile = filepath.Join(cwd, gemfile)
304 }
305 return filepath.Dir(gemfile)
306 }
307 for dir := cwd; ; {
308 for _, name := range []string{"gems.rb", "Gemfile"} {
309 if info, err := os.Stat(filepath.Join(dir, name)); err == nil && info.Mode().IsRegular() {
310 return dir
311 }
312 }
313 parent := filepath.Dir(dir)
314 if parent == dir {
315 return ""
316 }
317 dir = parent
318 }
319}
321func bundleConfigCACert(line string) (string, bool) {
322 value, ok := strings.CutPrefix(line, "BUNDLE_SSL_CA_CERT:")
323 if !ok {
324 return "", false
325 }
326 return strings.Trim(strings.TrimSpace(value), `"'`), true
327}
Diagnostics7 git calls · 241 ms
CallStatusSizeTime0241 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=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 · hit3 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=HIT 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 · 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: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
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=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=231 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
fetch2003.4 KiB231 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3439 git.response.cache=MISS git.response.duration_ms=231 git.fetch.wants=56f8198dbf85756445b3683278c4cdf719c3e73c 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=3439 git.pkt.payload.max=3412 git.sideband.band1.bytes=3412 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
- a5e23048f808a594
- Request
- d329834e…
- 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