fix(df): trust the proxy through PIP_CERT for poetry

Description

glab sets PIP_PROXY for Poetry, so that pip run by Poetry, as with poetry run pip install, goes through glab's inspection proxy. But glab gave pip its CA bundle only through REQUESTS_CA_BUNDLE, and pip ignores REQUESTS_CA_BUNDLE whenever a proxy is set with --proxy or PIP_PROXY (pip 25.0.1 and 26.3 both do). So pip never trusted the proxy, and poetry run pip install under glab dependency-firewall poetry failed for every user, whether or not they had set a CA. It failed closed: nothing got past the firewall, but nothing installed either.

The production change is Poetry's two variable lists in poetry.go, which now match what pip and pipenv already have in pip.go. The rest of the diff is the poetryManager doc comment and tests.

In poetry.go What it controls main This MR Effect on the command glab runs
CATrustEnviron Variables glab sets to its bundle for the command REQUESTS_CA_BUNDLE PIP_CERT, REQUESTS_CA_BUNDLE pip gets PIP_CERT, which it reads even with PIP_PROXY set, so it trusts glab's proxy. glab's value replaces any PIP_CERT the user set.
ExistingBundleVars Variables whose files glab merges into its bundle REQUESTS_CA_BUNDLE PIP_CERT, REQUESTS_CA_BUNDLE The certificates in the file a user's PIP_CERT names go into the bundle, so the user's CA stays trusted.

The merge itself is existing code: writeCABundle in pm.go reads each listed variable from glab's environment, and writes the certificates from those files, then glab's CA, to the bundle. A plain poetry add doesn't read PIP_CERT, so it isn't affected.

With no user CA set, the bundle holds only glab's CA. A tool that uses PIP_CERT to connect directly, without the proxy, therefore loses the public roots. A follow-up MR adds the system roots to the bundle.

Split from !4281 (closed) (closed), with the npm/Yarn fix in !4505 (merged) and the Bundler fix in !4504 (merged). This MR and !4505 (merged) both change managers_test.go, so whichever lands second needs a small rebase.

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: TestManagerEnvironAndCATrust and TestManagerExistingBundleVars cover Poetry's new PIP_CERT. TestRunMergesUserCAIntoOverriddenVar checks through Run that the child's PIP_CERT names a bundle holding the user's CA and the proxy CA. go test over internal/dependencyfirewall/... and internal/commands/df/... and golangci-lint pass.

Before and after, run on this branch (078eaed74) against main (fddc76b15), on macOS with Poetry 2.5.1 and pip 26.3. glab and Poetry ran on the host, behind a TLS-intercepting corporate proxy (mitmproxy in Docker). "Corporate proxy" counts the requests that proxy logged during the run with the fix.

Case main With the fix Corporate proxy
poetry run pip install six, the user's CA in PIP_CERT “glab Dependency Firewall CA” certificate is not trusted, exit 1 Successfully installed six-1.17.0 3
The same, the user's CA in REQUESTS_CA_BUNDLE Same failure Successfully installed six-1.17.0 3
The same, no CA in the environment (glab trusts the corporate proxy through GLAB_CA_CERT) Same failure Successfully installed six-1.17.0 3
PIP_CERT as pip sees it, the user's CA in PIP_CERT The user's file, 1 certificate glab's bundle, 2 certificates n/a
poetry add six, the user's CA in PIP_CERT Not run Resolves six and writes the lock file 3

Without glab, pip installs through the corporate proxy with the user's PIP_CERT, so the setup itself works.

On !4281 (closed)'s last head (bdff30ccc), the same change also passed poetry run pip install six on Linux with Poetry 1.8.5 and 2.5.1 (pip 25.0.1), where main failed with CERTIFICATE_VERIFY_FAILED. There, poetry add six installed on main too.

Manual testing steps (bash or zsh)

These steps compare this branch with main at the branch's base. glab and Poetry 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 27–28 Installs without glab Installs without glab
B/C 29–30 poetry run pip install fails: glab's CA isn't trusted Installs
D 31–32 pip's PIP_CERT is the user's file, 1 certificate glab's bundle, 2 certificates
E 33–36 Fails with the user's CA in REQUESTS_CA_BUNDLE Installs
F 37–40 Fails with no CA in the environment Installs
G 41–43 Not run poetry add resolves through the proxy

1. Name a scratch directory

T=~/df-4503-test

2. Name this checkout

W=$(git rev-parse --show-toplevel)

3. Create the scratch directory

mkdir -p $T/mitm

4. Build this branch

make -C $W build

5. Keep this branch's binary

cp $W/bin/glab $T/glab-branch

6. 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 build

8. Keep main's binary

cp $T/main/bin/glab $T/glab-main

9. Create a virtual environment for Poetry

python3 -m venv $T/poetry

10. Install Poetry in it

$T/poetry/bin/pip install -q poetry

11. Start the corporate proxy

docker run -d --name df-4503-corp -p 127.0.0.1:8081:8080 \
  -v $T/mitm:/home/mitmproxy/.mitmproxy mitmproxy/mitmproxy mitmdump

12. Wait for the proxy to create its CA

sleep 3

13. Check that the CA exists. Prints the path; if it doesn't, wait a second and run it again.

ls $T/mitm/mitmproxy-ca-cert.pem

14. Create the project directory

mkdir $T/proj

15. Go into it

cd $T/proj

16. Write pyproject.toml

cat > pyproject.toml <<'EOF'
[project]
name = "df-4503"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = []

[tool.poetry]
package-mode = false
EOF

17. Keep the project's virtual environment inside the project

$T/poetry/bin/poetry config virtualenvs.in-project true --local

18. Clear CA and proxy settings from your shell

unset PIP_CERT REQUESTS_CA_BUNDLE CURL_CA_BUNDLE SSL_CERT_FILE GLAB_CA_CERT PIP_PROXY \
  HTTP_PROXY http_proxy https_proxy ALL_PROXY NO_PROXY no_proxy CI GITLAB_CI

19. Put Poetry on PATH

export PATH=$T/poetry/bin:$PATH

20. Send glab's traffic through the corporate proxy

export HTTPS_PROXY=http://127.0.0.1:8081

21. Let glab allow every package without calling GitLab

export GLAB_DF_FAKE_DEFAULT=allow GITLAB_REPO=gitlab.com/example/df-proxy-test GITLAB_TOKEN=fake

22. Keep your own glab and Poetry settings out of the test

export GLAB_CONFIG_DIR=$T/glab-config POETRY_CACHE_DIR=$T/poetry-cache

23. Turn off update checks and telemetry

export GLAB_SEND_TELEMETRY=false GLAB_CHECK_UPDATE=false PIP_DISABLE_PIP_VERSION_CHECK=1

24. Name the corporate CA

CA=$T/mitm/mitmproxy-ca-cert.pem

25. Define corp. It counts the requests the corporate proxy has logged.

corp() { docker logs df-4503-corp 2>&1 | grep -c -E '(GET|HEAD) https://'; }

26. Define try. It runs a command, then prints its exit code and the number of requests the corporate proxy saw during it.

try() {
  local n=$(corp)
  "$@"
  local rc=$?
  echo "exit=$rc, corporate proxy: +$(( $(corp) - n )) requests"
}

27. A: point pip at the corporate CA

export PIP_CERT=$CA

28. A: install without glab. Expect Successfully installed six-1.17.0 and exit=0, corporate proxy: +3 requests.

try poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

29. B: install through main. Expect No matching distribution found for six and exit=1, corporate proxy: +0 requests. On macOS, pip reports “glab Dependency Firewall CA” certificate is not trusted; on Linux, CERTIFICATE_VERIFY_FAILED.

try $T/glab-main df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

30. C: install through this branch. Expect Successfully installed six-1.17.0 and exit=0, corporate proxy: +3 requests.

try $T/glab-branch df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

31. D: the PIP_CERT pip sees under main. Expect the path of $CA and 1.

$T/glab-main df poetry run sh -c 'echo "$PIP_CERT"; grep -c "BEGIN CERTIFICATE" "$PIP_CERT"'

32. D: the PIP_CERT pip sees under this branch. Expect a glab-df-ca-*.pem file in your temporary directory and 2: the user's CA and glab's.

$T/glab-branch df poetry run sh -c 'echo "$PIP_CERT"; grep -c "BEGIN CERTIFICATE" "$PIP_CERT"'

33. E: drop PIP_CERT

unset PIP_CERT

34. E: put the corporate CA in REQUESTS_CA_BUNDLE instead

export REQUESTS_CA_BUNDLE=$CA

35. E: install through main. Expect exit=1 and +0. glab sets PIP_PROXY, and pip ignores REQUESTS_CA_BUNDLE when a proxy is set that way.

try $T/glab-main df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

36. E: install through this branch. Expect exit=0 and +3.

try $T/glab-branch df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

37. F: drop REQUESTS_CA_BUNDLE

unset REQUESTS_CA_BUNDLE

38. F: trust the corporate CA in glab only. GLAB_CA_CERT covers glab's own registry connections and puts no CA variable in pip's environment.

export GLAB_CA_CERT=$CA

39. F: install through main. Expect exit=1 and +0, for the same reason as step 35.

try $T/glab-main df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

40. F: install through this branch. Expect exit=0 and +3.

try $T/glab-branch df poetry run pip install --no-cache-dir --force-reinstall --retries 0 six

41. G: drop GLAB_CA_CERT

unset GLAB_CA_CERT

42. G: point pip at the corporate CA again

export PIP_CERT=$CA

43. G: add a dependency through this branch. Expect Writing lock file and exit=0, corporate proxy: +3 requests.

try $T/glab-branch df poetry add six

44. Leave the scratch directory

cd ~

45. Stop the corporate proxy

docker rm -f df-4503-corp

46. Remove the main checkout

git -C $W worktree remove --force $T/main

47. Delete the scratch directory

rm -rf $T
Edited by Arpit Gogia

Merge request reports

Loading
Loading