Repository navigation
Commit 215fabb
authored
fix(auth): present the full certificate chain for mTLS (#14621)
## Why
`X509Provider` and `SecureConnectProvider` build their mTLS key store
with google-http-client's `SecurityUtils.createMtlsKeyStore`. That
method keeps only the **first** `CERTIFICATE` in the PEM input, so Java
presents only the leaf certificate in the TLS handshake. This is
unchanged in the latest google-http-client release (2.2.0).
- **Google APIs over `*.mtls.googleapis.com` are unaffected**, because
Google's front ends already know the intermediates.
- **Peers that trust only a root CA can't verify the leaf without the
intermediate**, so they reject the handshake.
- A common case is agent-to-agent mTLS, where the receiving agent trusts
the SPIFFE trust bundle.
- Agent identity certificates on GKE (and Cloud Run) are issued by an
intermediate CA, so a Java agent that reuses the SDK's mTLS key store
can't call such a peer.
- Python sends the whole chain (`load_cert_chain`).
This bug predates the agent identity work; the agent identity testing
caught it.
## What changes
**New package-private
`MtlsKeyStoreUtils.createMtlsKeyStore(InputStream)`.** It replaces the
upstream method.
- **The change:** the key entry's chain holds **every distinct
certificate**, in input order. The first certificate is still the leaf.
- **Same as upstream:** key store type (JKS), alias, empty password,
accepted key format (PKCS#8 `PRIVATE KEY`), which private key is chosen
when there are several, and error messages.
- **Also different from upstream:** the input is decoded as UTF-8
instead of the platform default charset, and it's read to the end.
Upstream stopped once it had a certificate and a key.
**Used by:**
- `X509Provider.getKeyStore()`, on both the certificate-configuration
path (Cloud Run, WIF X.509) and the GKE credential-bundle path;
- `SecureConnectProvider.getKeyStore()` (ECP), which drains the bytes
already buffered on the helper's stdout after the process exits so a
background child process holding stdout open cannot block waiting for
EOF.
**Inputs that worked before still work.** Because more of the input is
now parsed, once a certificate and a private key have been found (the
point where upstream stopped reading):
- a malformed later PEM section ends the input instead of failing;
- an extra certificate that can't be parsed is left out of the chain
(and logged at `FINE`).
**Unchanged:**
- exception wrapping in the providers;
- the token side: the `cnf` binding is still the leaf thumbprint, read
from the certificate file;
- the public API (no clirr impact).
**Known limitations (unchanged from before):**
- GAX's deprecated `com.google.api.gax.rpc.mtls.MtlsProvider` (ECP-only)
still uses the upstream method. GAX's transports don't use it; they use
`com.google.auth.mtls.MtlsProvider` through
`DefaultMtlsProviderFactory`.
- There's no check that the private key matches the first certificate
(same as upstream).
## Tests
### Unit tests
- `MtlsKeyStoreUtilsTest` (new), using a generated test chain (root →
intermediate → leaf, EC P-256, long validity) in
`testresources/mtls/test_chain_{cert,key}.pem`:
- upstream `SecurityUtils.createMtlsKeyStore` keeps only the leaf for
the same input. This pins down the bug, and the test fails if upstream
ever fixes it;
- leaf + intermediate (and 3-certificate chains) in order, with the
certificates first or the key first, and duplicate certificates
deduplicated;
- the private key matches the leaf (sign/verify);
- a single certificate gives the same alias, type, certificate and key
as `SecurityUtils.createMtlsKeyStore`;
- non-certificate sections are ignored;
- with several private keys, the same key is chosen as upstream: the
last one before the first certificate, or the first one after it. Each
of these tests runs upstream on the same input and compares the encoded
key bytes;
- a malformed trailing section after a certificate and key is ignored,
while a malformed section after only a certificate or only a key throws
`IllegalArgumentException`, and an `IOException` mid-stream propagates;
- an unparsable or empty extra certificate is left out;
- an unparsable leaf, a missing certificate, or a missing key throws,
with the same messages as before.
- `X509ProviderTest`: a GKE bundle with an intermediate (certificates
first, and key first as GKE writes it), and a certificate config whose
`cert_path` holds a chain, all give leaf + intermediate.
- `SecureConnectProviderTest`: helper output with a chain gives leaf +
intermediate, and only buffered stdout bytes are read after the process
exits.
The provider tests fail without the fix (`expected: <2> but was: <1>`).
Full `oauth2_http` suite passes. `fmt` is clean.
### Live tests
| Environment | Check | Before | After |
|---|---|---|---|
| GKE agent identity | Agent-to-agent mTLS using the SDK's
`DefaultMtlsProviderFactory` key store, against a receiver that trusts
only the SPIFFE root | `Broken pipe` (handshake rejected) | **200** |
| GKE agent identity | GCS over `storage.mtls.googleapis.com`, GAX
clients over gRPC and HTTP/JSON, bound tokens | pass | pass (unchanged)
|
| Workload identity federation X.509 (STS mTLS) | Token exchange with a
leaf-only `cert_path` | success, leaf sent | success, leaf sent |
| Workload identity federation X.509 (STS mTLS) | Token exchange with a
leaf + intermediate `cert_path` | success, **leaf only** sent | success,
**leaf + intermediate** sent |
The certificates sent were confirmed from the TLS handshake debug log.
The live tests ran on the first commit (`b2876a4d3a9`). Subsequent
commits only refine how unusual or malformed inputs are handled, plus
tests, so well-formed GKE and WIF files behave the same.
**Internal links (Googlers only):** [live test
results](https://paste.googleplex.com/5884065727774720) · GKE
[setup](https://paste.googleplex.com/5635555912712192),
[manifests](https://paste.googleplex.com/6258309762514944) and
[harness](https://paste.googleplex.com/4882197073494016) (shared with
#14595)1 parent 4aad503 commit 215fabb
8 files changed
Lines changed: 706 additions & 11 deletions
File tree
- google-auth-library-java/oauth2_http
- javatests/com/google/auth/mtls
- java/com/google/auth/mtls
- testresources/mtls
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | 35 | | |
37 | 36 | | |
38 | 37 | | |
| 38 | + | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
150 | | - | |
151 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
152 | 170 | | |
153 | 171 | | |
154 | 172 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
143 | 142 | | |
144 | 143 | | |
145 | 144 | | |
146 | | - | |
| 145 | + | |
| 146 | + | |
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
| 172 | + | |
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
| 204 | + | |
205 | 205 | | |
206 | 206 | | |
207 | 207 | | |
| |||
0 commit comments