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
Proxyfunc calls the configured func (http.ProxyFromEnvironmentin production). When that returns anhttps://proxy, it records the proxy'shost:port(port 443 when the URL omits it) and returns the same URL with schemehttp://and the explicit port. DialContextdials TCP. When the address is a recorded proxy, it runs the TLS handshake itself, using a clone of the transport'sTLSClientConfig(the registry root pool from !4150 (merged), TLS 1.2 minimum,http/1.1ALPN) plus the client certificate.- The transport then treats the connection as a plain HTTP proxy. It sends
CONNECTwithProxy-Authorizationfrom the URL's credentials and does the registry TLS handshake inside the tunnel with the unchangedTLSClientConfig, 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_keypair 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 withclient_key, or a PKCS#12client_certwithoutclient_key, decrypted withclient_cert_password. As in the API client, aclient_certwithoutclient_keythat isn't a PKCS#12 bundle is ignored. glab dependency-firewall gomodule downloads are made by thegocommand 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 Issues
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 withNO_PROXY. Both requests succeed, only the first registry is tunnelled, and neither registry receives a certificate.internal/commands/df/dfcmd:TestRunFailsWhenClientCertForRepoHostCannotBeLoadedchecks that the per-hostclient_cert/client_keyreach the run and that an unusable pair stops it before the package manager starts.internal/commands/df/dfcmd:TestRunAcceptsDERClientCertForRepoHostandTestRunLoadsPKCS12ClientCertForRepoHostcover a DER pair and a PKCS#12 bundle, andTestRunIgnoresClientCertWithoutKeyThatIsNotPKCS12covers a loneclient_certthat is ignored.internal/commands/df/jobproxy/serve:TestServeFailsWhenClientCertForRepoHostCannotBeLoadedchecks 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 withthe job-level proxy started without the outbound proxy client certificate.
I checked that each test fails first:
- With
presentCertToProxyas a no-op, the two success tests failed.GLAB_DEBUGshowedupstream request to 127.0.0.1:… failed: remote error: tls: certificate required. - With the certificate placed in
TLSClientConfig.Certificates, both success tests failed withShould be zero, but was 1: the proxied registry and the direct registry each received the certificate. - Before
dfcmdpassed the paths through, thedfcmdtest failed withAn 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 thepm,proxy,session,dfcmdandjobproxypackagesgolangci-lint run ./internal/dependencyfirewall/... ./internal/commands/df/...: 0 issuesgo run ./scripts/comment-overlap -threshold 1.0 -max-findings 0 <changed Go files>: 0 findingsmake 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.