fix(df): present client_cert to an mTLS outbound proxy

Description

When an https:// corporate proxy requires a client certificate, every registry connection from the glab dependency-firewall inspection proxy failed with remote error: tls: certificate required, because glab never offered a certificate.

The inspection proxy now presents glab's client_cert and client_key for the GitLab host on the TLS handshake with an https:// outbound proxy. dfcmd reads them the same way the API client does (cfg.Get(host, ...)), so GLAB_CLIENT_CERT/CLIENT_CERT, GLAB_CLIENT_KEY/CLIENT_KEY and the CI auto-login mapping (CI_SERVER_TLS_CERT_FILE, CI_SERVER_TLS_KEY_FILE) apply. Registries never receive the certificate, whether they are reached through the proxy or directly.

The job-level proxy from !4323 (merged) (glab dependency-firewall job-proxy) builds its own inspection proxy in session.Serve rather than going through pm.Run, so it gets the same wiring: job-proxy serve reads the same three settings, and pm.Run and session.Serve both load the certificate through pm.UpstreamProxyCertificateOptions.

How the certificate is kept away from registries

http.Transport uses one TLSClientConfig for the handshake with an https:// proxy and for the registry handshake inside the CONNECT tunnel. A certificate in that config is offered to every registry that asks for one. A test in this MR checks this: with the certificate in TLSClientConfig, a registry that requests client certificates receives it.

The new proxy.WithUpstreamProxyCertificate option wraps the transport after all other options have run:

  • The Proxy func calls the configured func (http.ProxyFromEnvironment in production). When that returns an https:// proxy, it records the proxy's host:port (port 443 when the URL omits it) and returns the same URL with scheme http:// and the explicit port.
  • DialContext dials TCP. When the address is a recorded proxy, it runs the TLS handshake itself, using a clone of the transport's TLSClientConfig (the registry root pool from !4150 (merged), TLS 1.2 minimum, http/1.1 ALPN) plus the client certificate.
  • The transport then treats the connection as a plain HTTP proxy. It sends CONNECT with Proxy-Authorization from the URL's credentials and does the registry TLS handshake inside the tunnel with the unchanged TLSClientConfig, which holds no certificate.

NO_PROXY is unaffected, because the wrapped func still decides which requests use a proxy. Direct connections and http:// proxies dial as before. Without client_cert/client_key, the transport is exactly as in !4150 (merged).

Other behaviour:

  • A client_cert/client_key pair that cannot be loaded fails the run (failed to load client certificate: ...), and the job-level proxy fails before it reports ready. A bad pair already fails the API client, so this adds no new failure mode.
  • The certificate is loaded with the API client's loader from !4286 (merged) (api.LoadClientCertificates): a PEM or DER pair with client_key, or a PKCS#12 client_cert without client_key, decrypted with client_cert_password. As in the API client, a client_cert without client_key that isn't a PKCS#12 bundle is ignored.
  • glab dependency-firewall go module downloads are made by the go command itself (since !4170 (merged)) and do not present the certificate. The help text says so.

Docs: the glab dependency-firewall long help and the client_cert config description now describe this. docs/source/ is regenerated.

Related to #8635 (closed) Related to gitlab-org/gitlab#629431 Related to gitlab-org&24327

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

How has this been tested?

New tests in internal/dependencyfirewall/proxy/proxycert_test.go use an in-process TLS CONNECT proxy that requires basic auth and a client certificate from its own client CA (RequireAndVerifyClientCert). The registries are httptest TLS servers with ClientAuth: tls.RequestClientCert, and each one counts the client certificates it receives.

  • TestProxyPresentsClientCertificateOnlyToMTLSOutboundProxy: with the certificate, the tarball arrives through one tunnel and the registry receives no certificate.
  • TestProxyFailsWhenMTLSOutboundProxyGetsNoCertificate: without the certificate, the request fails, no tunnel opens and the registry is never contacted.
  • TestProxyWithClientCertificateStillVerifiesOutboundProxy: with the certificate but without the proxy's CA in the root pool, the request fails before any tunnel opens.
  • TestProxyNeverPresentsClientCertificateToDirectRegistry: one registry is reached through the mTLS proxy and one directly, as with NO_PROXY. Both requests succeed, only the first registry is tunnelled, and neither registry receives a certificate.
  • internal/commands/df/dfcmd: TestRunFailsWhenClientCertForRepoHostCannotBeLoaded checks that the per-host client_cert/client_key reach the run and that an unusable pair stops it before the package manager starts.
  • internal/commands/df/dfcmd: TestRunAcceptsDERClientCertForRepoHost and TestRunLoadsPKCS12ClientCertForRepoHost cover a DER pair and a PKCS#12 bundle, and TestRunIgnoresClientCertWithoutKeyThatIsNotPKCS12 covers a lone client_cert that is ignored.
  • internal/commands/df/jobproxy/serve: TestServeFailsWhenClientCertForRepoHostCannotBeLoaded checks that the job-level proxy reads the per-host settings and that an unusable pair stops it before it reports ready. Without the wiring, the proxy starts and the test fails after 10 seconds with the job-level proxy started without the outbound proxy client certificate.

I checked that each test fails first:

  • With presentCertToProxy as a no-op, the two success tests failed. GLAB_DEBUG showed upstream request to 127.0.0.1:… failed: remote error: tls: certificate required.
  • With the certificate placed in TLSClientConfig.Certificates, both success tests failed with Should be zero, but was 1: the proxied registry and the direct registry each received the certificate.
  • Before dfcmd passed the paths through, the dfcmd test failed with An error is expected but got nil.

Commands:

  • go test -race ./internal/dependencyfirewall/... ./internal/commands/df/... ./internal/config/... ./internal/api/... ./cmd/gen-docs/...: pass, also on Linux for the pm, proxy, session, dfcmd and jobproxy packages
  • golangci-lint run ./internal/dependencyfirewall/... ./internal/commands/df/...: 0 issues
  • go run ./scripts/comment-overlap -threshold 1.0 -max-findings 0 <changed Go files>: 0 findings
  • make gen-docs: the regenerated docs are committed

The Docker corporate-proxy harness has not been run on this branch yet. Cell s3-pem should pass.

Edited by Arpit Gogia

Merge request reports

Loading
Loading