feat(df): trust the proxy despite a local Bundler ssl_ca_cert
Description
Bundler ranks a project's local config above the environment. So an ssl_ca_cert set with bundle config set --local, the usual way to point Bundler at a company CA, outranks the CA bundle glab gives Bundler. Bundler then rejects glab's inspection proxy and fails with Could not verify the SSL certificate. It fails closed: nothing gets past the firewall, but nothing installs either.
When the local config sets BUNDLE_SSL_CA_CERT:
- glab finds that config the way Bundler does:
.bundle/configin the directory ofBUNDLE_GEMFILEor of the nearestGemfileorgems.rb, orconfigin the directoryBUNDLE_APP_CONFIGnames. - It points
BUNDLE_APP_CONFIGat a temporary copy of that directory. The copy's CA file holds the user's CA and glab's bundle, and the directory's other entries, such as local plugins, are linked into the copy. - After the run, it writes Bundler's own changes back to the user's config in one rename, with the user's CA line restored, and moves back anything Bundler added to the directory. A config that is a symlink stays one.
bundle install --gemfile naming another project still fails closed: Bundler keeps the user's CA.
Split from !4281 (closed) (closed), with the npm/Yarn fix in !4505 (merged) and the Poetry fix in !4503 (merged). Sam's review of this code is on !4281 (closed).
Related Issues
Related to gitlab-org&24327
How has this been tested?
Unit tests and before/after runs behind a corporate proxy
Unit tests in internal/dependencyfirewall/pm:
TestBundleConfigCACert: the config-line parser, checked against Bundler 2.5.22.TestBundleSetupFindsLocalConfigandTestBundleSetupLeavesOtherConfigsAlone: finding the local config.TestBundleSetupReplacesLocalConfigCACert,TestBundleSetupWritesBackBundlerConfigChanges,TestBundleSetupWritesBackThroughASymlinkedConfigandTestBundleSetupKeepsUserConfigWhenCALineCannotBeRestored: the copy and write-back.TestBundleSetupKeepsEntriesBundlerCreates,TestBundleSetupWarnsWhenACreatedEntryCannotBeSaved, theTestCopyEntrytests and the warnings for an unreadable copy or CA: entries Bundler adds, and failures.TestRunTrustsProxyDespiteLocalBundleConfig:Runend to end.
go test over internal/dependencyfirewall/... and internal/commands/df/... and golangci-lint pass.
Before and after, run on !4281 (closed)'s last head (bdff30ccc), which has the same Bundler change, against main (2b5d4bcb9). The runs used real Bundler in Docker, behind a TLS-intercepting corporate proxy (mitmproxy). Its CA was set with bundle config set --local ssl_ca_cert and in GLAB_CA_CERT. "Corporate proxy" counts the requests that proxy logged during the run with the fix.
| Case | main |
With the fix | Corporate proxy |
|---|---|---|---|
Bundler 2.5.22, bundle install from the project root |
Could not verify the SSL certificate, exit 17 |
Bundle complete! |
3 |
| The same, from a subdirectory | Could not verify the SSL certificate, exit 17 |
Bundle complete! |
3 |
With the fix:
- When Bundler writes a setting during the run (
bundle config set --local path vendor/bundle), the config keeps the user's CA line and gainsBUNDLE_PATH, and no temporary directory is left. - A symlinked
.bundle/configstays a symlink, and its target gets the change. - After
kill -9of glab and Bundler mid-install, the user's config is byte-identical, and the temporary copy stays in/tmp. bundle install --gemfilenaming another project fails withCould not verify the SSL certificate.
Manual testing steps (bash or zsh)
These steps compare this branch with main at the branch's base. glab and Bundler run on your machine, and mitmproxy runs in Docker as a TLS-intercepting corporate proxy on port 8081. Run them from the root of a checkout of this branch, with Docker running, in a new terminal tab: they set a fake token and proxy settings. Each step is one command, even when it spans several lines.
| Case | Steps | main |
This branch |
|---|---|---|---|
| A | 26 | Installs without glab | Installs without glab |
| B/C | 27–28 | Fails: Could not verify the SSL certificate |
Installs |
| D | 29–30 | Bundler uses the project's CA | Bundler uses a temporary copy's CA file |
| E | 31–32 | That file holds 1 certificate | 3: the project's CA, then glab's bundle |
| F | 33–34 | Not run | The project's config is unchanged, and no copy is left |
| G | 35–36 | Not run | A setting Bundler changes reaches the project's config |
| H | 37–41 | Fails from a subdirectory | Installs from a subdirectory |
| I | 42–46 | Not run | A symlinked config stays a symlink |
1. Name the scratch directory
T=~/df-4504-test2. Name this checkout
W=$(git rev-parse --show-toplevel)3. Create the scratch directory
mkdir -p $T/mitm4. Build this branch
make -C $W build5. Keep this branch's binary
cp $W/bin/glab $T/glab-branch6. Check out main at this branch's base
git -C $W worktree add --detach $T/main "$(git -C $W merge-base HEAD origin/main)"7. Build main
make -C $T/main build8. Keep main's binary
cp $T/main/bin/glab $T/glab-main9. Start the corporate proxy
docker run -d --name df-4504-corp -p 127.0.0.1:8081:8080 \
-v $T/mitm:/home/mitmproxy/.mitmproxy mitmproxy/mitmproxy mitmdumpPrints the container ID.
10. Wait for the proxy to create its CA
sleep 311. Check that the CA exists
ls $T/mitm/mitmproxy-ca-cert.pemPrints the path. If it doesn't, wait a second and run it again.
12. Clear CA, proxy and Bundler settings from your shell
unset SSL_CERT_FILE BUNDLE_SSL_CA_CERT BUNDLE_APP_CONFIG BUNDLE_GEMFILE BUNDLE_PATH BUNDLE_USER_HOME \
GEM_HOME GEM_PATH GLAB_CA_CERT HTTP_PROXY HTTPS_PROXY http_proxy https_proxy ALL_PROXY NO_PROXY no_proxy CI GITLAB_CI13. Check that a recent Bundler comes first on PATH
bundle -vPrints Bundler 2.5 or later; these steps were run with 2.7.1. If yours is older, put a newer Ruby first on PATH. With mise, run export MISE_RUBY_VERSION=3.3.9 and wait for the next prompt.
14. Name the corporate CA
CA=$T/mitm/mitmproxy-ca-cert.pem15. Point at the corporate proxy and trust its CA
glab reads HTTPS_PROXY, and Bundler reads HTTP_PROXY. Bundler downloads .gem files through RubyGems, which ignores Bundler's ssl_ca_cert and uses SSL_CERT_FILE.
export HTTPS_PROXY=http://127.0.0.1:8081 HTTP_PROXY=http://127.0.0.1:8081 SSL_CERT_FILE=$CA16. Let glab allow every package without calling GitLab
export GLAB_DF_FAKE_DEFAULT=allow GITLAB_REPO=gitlab.com/example/df-proxy-test GITLAB_TOKEN=fake17. Keep your own glab and Bundler settings out of the test
export GLAB_CONFIG_DIR=$T/glab-config BUNDLE_USER_HOME=$T/bundle-home18. Turn off glab's telemetry and update check
export GLAB_SEND_TELEMETRY=false GLAB_CHECK_UPDATE=false19. Define try
try deletes the project's installed gems and lockfile, so every install downloads again, then runs the command and prints its exit code.
try() { rm -rf $T/proj/vendor $T/proj/Gemfile.lock; "$@"; echo "exit=$?"; }20. Create the project directory
mkdir $T/proj21. Go into it
cd $T/proj22. Write the Gemfile
cat > Gemfile <<'EOF'
source "https://rubygems.org"
gem "colorize"
EOF23. Install gems inside the project
bundle config set --local path vendor/bundle24. Point Bundler at the corporate CA in the project's config
bundle config set --local ssl_ca_cert $CA25. Show the project's config
cat .bundle/configPrints ---, BUNDLE_PATH: "vendor/bundle" and BUNDLE_SSL_CA_CERT: with the path of $CA.
26. A: install without glab
try bundle installPrints Bundle complete! 1 Gemfile dependency, 2 gems now installed. and exit=0.
27. B: install through main
try $T/glab-main df bundle installPrints Could not verify the SSL certificate for https://rubygems.org/quick/Marshal.4.8/colorize-0.5.0.gemspec.rz. and exit=17. Bundler uses the project's CA, which doesn't include glab's.
28. C: install through this branch
try $T/glab-branch df bundle installPrints Bundle complete! 1 Gemfile dependency, 2 gems now installed. and exit=0.
29. D: which ssl_ca_cert Bundler uses under main
$T/glab-main df bundle config get ssl_ca_certPrints Set for your local app (…/proj/.bundle/config): with the path of $CA, above Set via BUNDLE_SSL_CA_CERT: with glab's glab-df-ca-*.pem. The top line wins: the project's config outranks the variable glab sets.
30. D: which ssl_ca_cert Bundler uses under this branch
$T/glab-branch df bundle config get ssl_ca_certPrints Set for your local app (…/glab-df-bundle-*/bundle/config): "…/glab-df-bundle-*/ca.pem" above the same BUNDLE_SSL_CA_CERT line. Bundler reads glab's temporary copy of the config.
31. E: what that CA file holds under main
$T/glab-main df bundle exec ruby -e 'f = Bundler.settings[:ssl_ca_cert]; puts f, File.read(f).scan("BEGIN CERTIFICATE").size'Prints the path of $CA and 1.
32. E: what that CA file holds under this branch
$T/glab-branch df bundle exec ruby -e 'f = Bundler.settings[:ssl_ca_cert]; puts f, File.read(f).scan("BEGIN CERTIFICATE").size'Prints a …/glab-df-bundle-*/ca.pem path and 3: the project's CA, then glab's bundle, which holds the CA from SSL_CERT_FILE and glab's CA. glab deletes the copy when the command ends.
33. F: the project's config is unchanged
cat .bundle/configPrints the same lines as step 25.
34. F: no temporary copy is left
ls "${TMPDIR:-/tmp}" | grep -c glab-df-bundlePrints 0.
35. G: have Bundler change a setting through this branch
$T/glab-branch df bundle config set --local without developmentPrints nothing. Bundler writes the setting to glab's copy, and glab saves it back.
36. G: show the project's config
cat .bundle/configPrints the lines from step 25 plus BUNDLE_WITHOUT: "development". BUNDLE_SSL_CA_CERT still names $CA, not the copy.
37. H: create a subdirectory
mkdir lib38. H: go into it
cd lib39. H: install through main from the subdirectory
try $T/glab-main df bundle installPrints Could not verify the SSL certificate … and exit=17.
40. H: install through this branch from the subdirectory
try $T/glab-branch df bundle installPrints Bundle complete! … and exit=0. glab finds the project's config from the nearest Gemfile above, as Bundler does.
41. H: go back to the project
cd ..42. I: move the config elsewhere
mv .bundle/config $T/shared-config43. I: link it back as a symlink
ln -s $T/shared-config .bundle/config44. I: have Bundler change a setting through this branch
$T/glab-branch df bundle config set --local jobs 4Prints nothing.
45. I: check that the config is still a symlink
ls -l .bundle/configPrints .bundle/config -> …/shared-config.
46. I: show the symlink's target
cat $T/shared-configPrints the lines from step 36 plus BUNDLE_JOBS: "4".
47. Leave the scratch directory
cd ~48. Stop the corporate proxy
docker rm -f df-4504-corp49. Remove the main checkout
git -C $W worktree remove --force $T/main50. Delete the scratch directory
rm -rf $T