fix(df): trust the user's JVM truststore in the job-level proxy

Description

Maven and Gradle builds in a job that uses glab dependency-firewall job-proxy now reach registries whose certificate, or the certificate of the corporate proxy in front of them, comes from a CA that is only in the user's JVM truststore. Before, they failed: job-proxy start gives Maven and Gradle a truststore of their own, and the job proxy's connections to registries trusted only the system certificates, ca_cert and the CA bundle variables.

!4280 (merged) did this for glab dependency-firewall maven and gradle. This MR is the job-proxy follow-up promised on !4280 (merged).

  • The daemon reads the truststore that mvn's and gradle's JVMs would load, from the same options and project files the per-command wrappers read, or the JDK default under JAVA_HOME. When Maven and Gradle name different stores it trusts both, and it reads a store they share once.
  • .mvn/jvm.config and .mvn/maven.config are read from the project directory (CI_PROJECT_DIR, else the working directory). Options on a later mvn or gradle command line can't be seen when the proxy starts.
  • A truststore the daemon can't use, such as a JKS store, is reported in its daemon.log with its other diagnostics, not in the job log.
  • job-proxy start prints the per-command wrappers' warning to the job log when a truststore option replaces the one it gives Maven or Gradle: in MAVEN_DEBUG_OPTS or _JAVA_OPTIONS, or on Maven 3 in .mvn/maven.config or MAVEN_ARGS. mvn or gradle then rejects the proxy's certificate, and before this the job log didn't say why.
  • To share the code, jvmTrustAnchors is split into resolving the truststore properties and loading the store. The per-command tests pass unchanged.

Not covered, each planned as a follow-up:

  • CURL_CA_BUNDLE and GIT_SSL_CAINFO: the job's tools trust a CA set there, but the job proxy's registry connections don't.
  • Package-manager CA settings from !4281 (closed): npm cafile, Yarn's CA settings, Bundler's local ssl_ca_cert and TWINE_CERT. The job proxy doesn't apply them, so those tools reject the proxy.

A JDK cacerts in JKS format, as on Amazon Corretto and other Red Hat–based images, isn't read here either, as in the per-command wrappers.

Related to gitlab-org&24327

Part of the corporate-proxy stack. It is based on main.

How has this been tested?

Unit tests and real-tool runs
  • TestServeTrustsTheJVMTrustStoreMavenAndGradleWouldLoad runs the daemon in-process and fetches through it from an httptest TLS registry whose CA is only in a PKCS#12 truststore.
    • Fail on main and pass here: MAVEN_OPTS, GRADLE_OPTS, Maven and Gradle naming different stores, and the JDK cacerts.
    • Fails in both, as it should: no truststore.
    • A JKS store is reported in the daemon's log.
    • A mutant that reads only Maven's store fails the two Gradle cases.
    • .mvn/jvm.config and .mvn/maven.config in the project directory, each naming the store by a relative path, pass here. A mutant that hands UpstreamRootCAs the state directory instead of the project directory fails both.
  • TestWarnJobTrustStoreOverrides covers each option source that warns, and that MAVEN_OPTS and GRADLE_OPTS don't. In a container with a truststore option in MAVEN_DEBUG_OPTS, _JAVA_OPTIONS and .mvn/maven.config, job-proxy start printed a warning for each and still exited 0.
  • go test ./internal/dependencyfirewall/... ./internal/commands/df/..., go test -race -count=2 ./internal/dependencyfirewall/session/, golangci-lint (0 issues) and scripts/comment-overlap (0 findings) pass.
  • Real tools in Docker: eval "$(glab dependency-firewall job-proxy start)" behind mitmproxy, whose CA is only in the user's truststore, with glab from main (74ee3ce47) and from this MR (65ae86c93). Maven 3.9.16 ran on Temurin 21.0.12 and Gradle 9.8.2 on JDK 21.0.13. "Corporate proxy" counts the requests mitmproxy logged during the run with this MR.
Case main This MR Corporate proxy
Maven, truststore in MAVEN_OPTS BUILD FAILURE; the daemon logs x509: certificate signed by unknown authority BUILD SUCCESS 222
Maven, CA imported into the JDK cacerts Same failure BUILD SUCCESS 222
Gradle, truststore in GRADLE_OPTS Could not resolve org.apache.commons:commons-lang3:3.14.0, same daemon error Resolves commons-lang3-3.14.0.jar 6
Manual testing steps (bash or zsh)

These steps compare this branch with main at the branch's base. Maven and Gradle run in Docker containers that start the job-level proxy with eval "$(glab dependency-firewall job-proxy start)", as a CI/CD job would. mitmproxy plays a TLS-intercepting corporate proxy, and its CA is only in the user's JVM truststore. Run the steps from the root of a checkout of this branch, with Docker and Go, in a new terminal tab. Each step is one command, even when it spans several lines.

Case Steps main This branch
1. Maven, truststore in MAVEN_OPTS 20–21 BUILD FAILURE; the daemon logs x509: certificate signed by unknown authority BUILD SUCCESS
2. Maven, CA imported into the JDK cacerts 22–23 Same failure BUILD SUCCESS
3. Gradle, truststore in GRADLE_OPTS 24–25 Could not resolve org.apache.commons:commons-lang3:3.14.0, same daemon error RESOLVED commons-lang3-3.14.0.jar

1. Name a scratch directory

T=~/df-4488-test

2. Name this checkout

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

3. Create the scratch directories

mkdir -p $T/mitm $T/ts $T/mvn $T/gradle

4. Build this branch for Linux

CGO_ENABLED=0 GOOS=linux GOARCH=$(go env GOARCH) go -C $W build -trimpath -o $T/glab-branch ./cmd/glab

5. Check out main at this branch's base

git -C $W worktree add --detach $T/main "$(git -C $W merge-base HEAD origin/main)"

6. Build main for Linux

CGO_ENABLED=0 GOOS=linux GOARCH=$(go env GOARCH) go -C $T/main build -trimpath -o $T/glab-main ./cmd/glab

7. Create a Docker network for the job and the corporate proxy

docker network create df-4488-net

8. Start the corporate proxy

docker run -d --name df-4488-corp --network df-4488-net --network-alias corp \
  -v $T/mitm:/home/mitmproxy/.mitmproxy mitmproxy/mitmproxy mitmdump

9. Wait for the proxy to create its CA

sleep 3

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

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

11. Put the corporate CA in the user's truststore. Prints Certificate was added to keystore.

docker run --rm -v $T/mitm:/ca:ro -v $T/ts:/ts maven:3.9-eclipse-temurin-21 \
  keytool -importcert -noprompt -alias corp -file /ca/mitmproxy-ca-cert.pem -keystore /ts/corp.p12 -storetype PKCS12 -storepass changeit

12. Write the Maven project

printf '%s\n' '<project xmlns="http://maven.apache.org/POM/4.0.0"><modelVersion>4.0.0</modelVersion><groupId>example</groupId><artifactId>df-4488</artifactId><version>1.0.0</version><dependencies><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.14.0</version></dependency></dependencies></project>' > $T/mvn/pom.xml

13. Write the Gradle settings

printf 'rootProject.name = "df-4488"\n' > $T/gradle/settings.gradle

14. Write the Gradle build. Its resolve task downloads commons-lang3 and prints the file it got.

printf '%s\n' 'repositories { mavenCentral() }' 'configurations { deps }' 'dependencies { deps "org.apache.commons:commons-lang3:3.14.0" }' 'tasks.register("resolve") { doLast { configurations.deps.files.each { println "RESOLVED ${it.name}" } } }' > $T/gradle/build.gradle

15. Write the job's environment. The job uses the corporate proxy, and glab allows every package without calling GitLab. GITLAB_CI is not set, because glab ignores the fake policy in CI/CD.

printf '%s\n' HTTPS_PROXY=http://corp:8080 GLAB_DF_FAKE_DEFAULT=allow GITLAB_REPO=gitlab.com/example/df-proxy-test GITLAB_TOKEN=fake GLAB_SEND_TELEMETRY=false GLAB_CHECK_UPDATE=false > $T/job.env

16. Name the truststore options

TS='-Djavax.net.ssl.trustStore=/ts/corp.p12 -Djavax.net.ssl.trustStorePassword=changeit -Djavax.net.ssl.trustStoreType=PKCS12'

17. Name the Maven job script. It starts the job-level proxy, runs Maven, and prints Maven's exit code and result and any certificate error the daemon logged.

M='eval "$(glab dependency-firewall job-proxy start)" && mvn -B -ntp dependency:resolve > /tmp/out 2>&1; echo "exit=$?"; grep -E "BUILD (SUCCESS|FAILURE)" /tmp/out; grep -h x509 "$GLAB_DF_JOB_PROXY_STATE_DIR"/*.log'

18. Name the Maven job script that first imports the CA into the JDK cacerts

C='keytool -importcert -noprompt -cacerts -storepass changeit -alias corp -file /ca/mitmproxy-ca-cert.pem > /dev/null 2>&1; '"$M"

19. Name the Gradle job script

G='eval "$(glab dependency-firewall job-proxy start)" && gradle --no-daemon -q resolve > /tmp/out 2>&1; echo "exit=$?"; grep -E "RESOLVED|Could not resolve org" /tmp/out; grep -h x509 "$GLAB_DF_JOB_PROXY_STATE_DIR"/*.log'

20. Case 1 on main. Expect exit=1, [INFO] BUILD FAILURE and warning: dependency firewall request to registry repo.maven.apache.org:443 failed: tls: failed to verify certificate: x509: certificate signed by unknown authority.

docker run --rm --network df-4488-net --env-file $T/job.env -e MAVEN_OPTS="$TS" \
  -v $T/glab-main:/usr/local/bin/glab:ro -v $T/ts:/ts:ro -v $T/mvn:/proj -w /proj maven:3.9-eclipse-temurin-21 bash -c "$M"

21. Case 1 on this branch. Expect exit=0 and [INFO] BUILD SUCCESS, with no x509 line.

docker run --rm --network df-4488-net --env-file $T/job.env -e MAVEN_OPTS="$TS" \
  -v $T/glab-branch:/usr/local/bin/glab:ro -v $T/ts:/ts:ro -v $T/mvn:/proj -w /proj maven:3.9-eclipse-temurin-21 bash -c "$M"

22. Case 2 on main. Expect the same failure as step 20.

docker run --rm --network df-4488-net --env-file $T/job.env \
  -v $T/glab-main:/usr/local/bin/glab:ro -v $T/mitm:/ca:ro -v $T/mvn:/proj -w /proj maven:3.9-eclipse-temurin-21 bash -c "$C"

23. Case 2 on this branch. Expect exit=0 and [INFO] BUILD SUCCESS.

docker run --rm --network df-4488-net --env-file $T/job.env \
  -v $T/glab-branch:/usr/local/bin/glab:ro -v $T/mitm:/ca:ro -v $T/mvn:/proj -w /proj maven:3.9-eclipse-temurin-21 bash -c "$C"

24. Case 3 on main. Expect exit=1, Could not resolve org.apache.commons:commons-lang3:3.14.0. and the same x509 warning.

docker run --rm --network df-4488-net --env-file $T/job.env -e GRADLE_OPTS="$TS" \
  -v $T/glab-main:/usr/local/bin/glab:ro -v $T/ts:/ts:ro -v $T/gradle:/proj -w /proj gradle:9.8.2-jdk21 bash -c "$G"

25. Case 3 on this branch. Expect exit=0 and RESOLVED commons-lang3-3.14.0.jar.

docker run --rm --network df-4488-net --env-file $T/job.env -e GRADLE_OPTS="$TS" \
  -v $T/glab-branch:/usr/local/bin/glab:ro -v $T/ts:/ts:ro -v $T/gradle:/proj -w /proj gradle:9.8.2-jdk21 bash -c "$G"

26. Stop the corporate proxy

docker rm -f df-4488-corp

27. Remove the Docker network

docker network rm df-4488-net

28. Remove the main checkout

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

29. Delete the scratch directory. On Linux, the containers ran as root, so files they wrote here are owned by root; if rm fails, run it with sudo.

rm -rf $T
Edited by Arpit Gogia

Merge request reports

Loading
Loading