Repository navigation
fix(core): honor HTTPS_PROXY for X.509 workload identity mTLS - #9772
abhiramshaji-cyber wants to merge 5 commits into
Conversation
X.509 workload identity federation built its own https.Agent with the client certificate and handed it to gaxios. When gaxios is given an agent it skips its proxy handling, so the STS token exchange and the service account impersonation call ignored HTTPS_PROXY, https_proxy and NO_PROXY, and getAccessToken() failed in networks where outbound traffic must go through a proxy. google-auth-library now passes the certificate and key to gaxios as cert and key options, so the existing proxy branch in gaxios decides whether to use the proxy and applies NO_PROXY. The client transporter drops any inherited agent so the certificate is always used, as it was before. In gaxios, the proxy branch gave cert and key to HttpsProxyAgent, which only uses them for the connection to the proxy itself. The TLS connection to the server behind the proxy had no client certificate, so an mTLS endpoint would reject it. The agent now adds cert and key to that inner TLS connection as well. Fixes googleapis#9594
The proxy agent now presents cert and key to every https server it reaches, not only to the proxy. Caching it by proxy URL alone meant a later request on the same Gaxios instance without a certificate would send the cached one to a server it was never meant for, and a first request without a certificate would make later mTLS requests silently go out without one. The cache key now includes cert and key when both are set, so each certificate gets its own agent and requests without one get a plain proxy agent. The two existing tests that expected a request without a certificate to reuse the mTLS agent are updated to the new behavior.
The test inspects the options the auth library hands to gaxios and that the proxy branch is chosen. Delivery of the certificate to the server behind the proxy is covered by the gaxios test, so the name now says that instead of claiming the server receives it.
There was a problem hiding this comment.
Code Review
This pull request updates gaxios and google-auth-library-nodejs to support mTLS client certificates when routing requests through a proxy. It replaces custom HTTPS agent creation in CertificateSubjectTokenSupplier with passing cert and key options directly to Gaxios, which now caches proxy agents using a key that incorporates these certificates. The feedback highlights three key areas for improvement: avoiding potential corruption of binary DER-formatted certificates by removing unnecessary .toString() calls, performing a deep copy of transporter defaults to prevent accidental mutation of nested configuration objects, and refining the proxy agent cache key logic to prevent cache pollution when only one of cert or key is provided.
|
Hi @quirogas, the Gemini review points are addressed (the agent cache key fix is in with tests, and I left notes on the other two). Whenever you have a moment, would you mind approving the workflow runs and taking a look? Happy to change anything. Thank you! |
|
Hi @quirogas, hope you're doing well! It's me again from my note here on 10/29. I know there's a lot to review across a monorepo this big, so no rush at all; the workflow runs here and on #9775 are still waiting, and I'd be glad to know if anything else would help or if you'd like either one shaped differently. Out of curiosity, now that gaxios and the auth library live together under core, are there areas there where outside help would be most useful? Any tips for contributing here are very welcome too, thank you! |
|
Hi @quirogas, hope you're doing well! It's me again from my note here on 11/10. I can see you've been busy with a lot of CI and Renovate work lately, so thank you for all of that, and no rush at all. The workflow runs here and on #9775 are still waiting whenever you get a chance. Is there anything else that would make these easier to review, or anything you'd like changed in either one? Any feedback is very welcome, thank you! |
|
Hi @shivanee-p, hope you're doing well! I've seen you reviewing a lot of the recent core changes, so I hope it's okay to ask you here. I merged the latest main into this branch with no conflicts, and the gaxios and auth library unit tests all pass locally. The workflow runs are waiting on a maintainer approval again, so would you mind approving them when you have a moment? And if someone else is a better fit to review this one, I'd be really grateful for a pointer. Thank you so much! |
Apps that sign in with a client certificate (X.509 workload identity federation) could not get a token behind a required proxy, and now those requests go through the proxy and still present the certificate.
What this code is for
When a credential file has
credential_source.certificate,IdentityPoolClient.retrieveSubjectToken()asksCertificateSubjectTokenSupplierto read the client certificate and key, then builds twoGaxiosinstances that use them: one for the STS token exchange atsts.mtls.googleapis.comand one (this.transporter) for the service account impersonation call. In gaxios,#prepareRequest()decides how each request connects: a caller suppliedagentwins, otherwise it builds anHttpsProxyAgentwhenHTTPS_PROXY/https_proxyis set andNO_PROXYdoes not match, otherwise it builds anhttps.Agentfromcertandkey.Before this PR
A workload in a network where all outbound traffic must go through a proxy runs
client.getAccessToken()withHTTPS_PROXY=http://127.0.0.1:3128, as in the steps @moibueno shared in #9594.retrieveSubjectToken()callscreateMtlsHttpsAgent(), which returnsnew https.Agent({key, cert}).new Gaxios({agent: mtlsAgent})for STS and asagentonthis.transporter.#prepareRequest(),if (opts.agent)is true, so gaxios skips the proxy branch.sts.mtls.googleapis.com:443.getAccessToken()fails.There is a second problem in gaxios. Even a request that reaches the gaxios proxy branch with
certandkeydoes not send the certificate to the server:HttpsProxyAgentuses those options only for its own connection to the proxy, and the TLS connection to the server behind the proxy gets no client certificate. I checked this with a local CONNECT proxy and a local server that asks for a client certificate: the request went through the tunnel, but the server saw an empty peer certificate.After this PR
Same workload, same
HTTPS_PROXY, samegetAccessToken()call.retrieveSubjectToken()callsgetMtlsOptions(), which returns{cert, key}as strings.new Gaxios(mtlsOptions)for STS and spread intothis.transporter, withagent: undefinedso an inherited agent cannot hide the certificate.#prepareRequest(), there is no agent yet, so the proxy branch runs, honoringHTTPS_PROXY,https_proxyandNO_PROXYthe same way as every other gaxios request.HttpsProxyAgentaddscertandkeyto the TLS connection to the server whenevertarget.secureEndpointis true.CONNECT sts.mtls.googleapis.com:443, the STS server receives the client certificate through the tunnel, and the token comes back. The impersonation call takes the same path.When
NO_PROXYmatches the host, step 3 takes the existingcert/keybranch instead and connects directly with anhttps.Agent, same as before.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: 0 CONNECT requests at the proxy"] direction TB B1["getAccessToken() with HTTPS_PROXY set"] --> B2["retrieveSubjectToken() reads cert and key"] B2 --> B3["createMtlsHttpsAgent() returns https.Agent"] B3 --> B4{"opts.agent is set, proxy skipped"} B4 -->|"direct TLS to sts.mtls.googleapis.com"| B5["blocked, no token"] B4 -->|"no proxy in the network"| B6["direct mTLS works"] end subgraph AFTER["AFTER: CONNECT through the proxy, certificate sent"] direction TB A1["getAccessToken() with HTTPS_PROXY set"] --> A2["retrieveSubjectToken() reads cert and key"] A2 --> A3["getMtlsOptions() returns cert and key"] A3 --> A4{"THE CHANGE: gaxios chooses the agent"} A4 -->|"HttpsProxyAgent adds cert to inner TLS"| A5["token through the proxy"] A4 -->|"NO_PROXY matches"| A6["direct mTLS, same as before"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The change
There are two edits, and both are needed for the token request to work behind a proxy, plus one cache change in gaxios that the second edit makes necessary.
google-auth-library:createMtlsHttpsAgent()is replaced bygetMtlsOptions(), andretrieveSubjectToken()passescertandkeytoGaxiosinstead of a prebuilt agent. This is the first option @moibueno suggested in the issue, and it reuses the proxy andNO_PROXYhandling gaxios already has, so the library does not read proxy variables itself.CertificateSubjectTokenSupplieris not exported from the package index.gaxios: in the proxy branch of#prepareRequest(), when bothcertandkeyare set, theHttpsProxyAgentinstance gets aconnectthat adds them to the options for the TLS connection to the server (only whentarget.secureEndpointis true). The options for the connection to the proxy itself stay as they were.gaxios: the proxy agent cache key now includescertandkeywhen both are set, instead of the proxy URL alone. Since the cached agent now presents its certificate to every https server it reaches, reusing it would send one caller's certificate on a later request made without one, and a first request without a certificate would make later mTLS requests go out without it.Tests:
test.identitypoolclient.tschecks that both the STS and impersonation requests use a proxy agent carrying the certificate withHTTPS_PROXY, thathttps_proxyplus aNO_PROXYmatch on the STS host sends STS direct with the certificate and impersonation through the proxy, and that atransporterOptions.agentdoes not drop the certificate.test.getch.tschecks that the proxy agent passescertandkeyto the server connection, and that requests without a certificate or with a different one get their own agent. The first two auth tests and the new gaxios tests fail without this change.A few things I would really value your opinion on, since you know this code far better than I do:
google-auth-librarydepends ongaxiosfrom npm (^8.3.1). Until the gaxios floor is raised, a user withHTTPS_PROXYset and an older gaxios would now go through the proxy without the certificate, so a setup that works today because direct connections happen to be allowed would start failing at STS. Would you prefer that I bump the floor in this PR, or right after the gaxios release, or do you usually handle that in the dependency floor updates?should load the proxy from the cache with mTLSand the Bun fetch test) expected a request without a certificate to reuse the cached mTLS proxy agent. I updated them so that request now gets its own agent with no certificate, because otherwise the certificate would reach servers it was not meant for. If that reuse was relied on somewhere I am not aware of, I would be grateful to hear it and happy to find another way.What changes for a user
With
HTTPS_PROXYorhttps_proxyset, the STS token exchange and the impersonation call now go through the proxy and present the client certificate to Google.NO_PROXYis honored for these requests, the same way as for every other request the library makes.Without any proxy set, requests connect directly with the client certificate, as before.
A custom
transporterOptions.agentis still replaced for these requests, so the certificate is always used, as before.The workaround from the issue that replaced
createMtlsHttpsAgenton the supplier is no longer needed; that method is no longer called.Thank you @moibueno for the very clear report and the two suggested fixes, which made this easy to follow.
Make sure to open an issue as a bug/issue before writing your code
Ensure the tests and linter pass
Code coverage does not decrease (if any source code was changed)
Appropriate docs were updated (if necessary)
Fixes #9594