1515package com .google .api .client .googleapis .apache .v2 ;
1616
1717import com .google .api .client .googleapis .GoogleUtils ;
18+ import com .google .api .client .googleapis .mtls .MtlsProvider ;
19+ import com .google .api .client .googleapis .mtls .MtlsUtils ;
20+ import com .google .api .client .googleapis .util .Utils ;
1821import com .google .api .client .http .apache .v2 .ApacheHttpTransport ;
22+ import com .google .api .client .util .Beta ;
1923import com .google .api .client .util .SslUtils ;
2024import java .io .IOException ;
2125import java .net .ProxySelector ;
2428import java .util .concurrent .TimeUnit ;
2529import javax .net .ssl .SSLContext ;
2630import org .apache .http .client .HttpClient ;
27- import org .apache .http .config .SocketConfig ;
2831import org .apache .http .conn .socket .LayeredConnectionSocketFactory ;
2932import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
3033import org .apache .http .impl .client .HttpClientBuilder ;
3942public final class GoogleApacheHttpTransport {
4043
4144 /**
42- * Returns a new instance of {@link ApacheHttpTransport} that uses
43- * {@link GoogleUtils#getCertificateTrustStore()} for the trusted certificates.
45+ * Returns a new instance of {@link ApacheHttpTransport} that uses {@link
46+ * GoogleUtils#getCertificateTrustStore()} for the trusted certificates. If
47+ * `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true", and the default
48+ * client certificate key store from {@link Utils#loadDefaultMtlsKeyStore()} is not null, then the
49+ * transport uses the default client certificate and is mutual TLS.
4450 */
45- public static ApacheHttpTransport newTrustedTransport () throws GeneralSecurityException ,
46- IOException {
51+ public static ApacheHttpTransport newTrustedTransport ()
52+ throws GeneralSecurityException , IOException {
53+ return newTrustedTransport (MtlsUtils .getDefaultMtlsProvider ());
54+ }
55+
56+ /**
57+ * {@link Beta} <br>
58+ * Returns a new instance of {@link ApacheHttpTransport} that uses {@link
59+ * GoogleUtils#getCertificateTrustStore()} for the trusted certificates. mtlsProvider can be used
60+ * to configure mutual TLS for the transport.
61+ *
62+ * @param mtlsProvider MtlsProvider to configure mutual TLS for the transport
63+ */
64+ @ Beta
65+ public static ApacheHttpTransport newTrustedTransport (MtlsProvider mtlsProvider )
66+ throws GeneralSecurityException , IOException {
67+ KeyStore mtlsKeyStore = null ;
68+ String mtlsKeyStorePassword = null ;
69+ if (mtlsProvider .useMtlsClientCertificate ()) {
70+ mtlsKeyStore = mtlsProvider .getKeyStore ();
71+ mtlsKeyStorePassword = mtlsProvider .getKeyStorePassword ();
72+ }
73+
4774 PoolingHttpClientConnectionManager connectionManager =
4875 new PoolingHttpClientConnectionManager (-1 , TimeUnit .MILLISECONDS );
4976
@@ -53,22 +80,35 @@ public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityEx
5380 // Use the included trust store
5481 KeyStore trustStore = GoogleUtils .getCertificateTrustStore ();
5582 SSLContext sslContext = SslUtils .getTlsSslContext ();
56- SslUtils .initSslContext (sslContext , trustStore , SslUtils .getPkixTrustManagerFactory ());
83+
84+ boolean isMtls = false ;
85+ if (mtlsKeyStore != null && mtlsKeyStorePassword != null ) {
86+ isMtls = true ;
87+ SslUtils .initSslContext (
88+ sslContext ,
89+ trustStore ,
90+ SslUtils .getPkixTrustManagerFactory (),
91+ mtlsKeyStore ,
92+ mtlsKeyStorePassword ,
93+ SslUtils .getDefaultKeyManagerFactory ());
94+ } else {
95+ SslUtils .initSslContext (sslContext , trustStore , SslUtils .getPkixTrustManagerFactory ());
96+ }
5797 LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory (sslContext );
5898
59- HttpClient client = HttpClientBuilder .create ()
60- .useSystemProperties ()
61- .setSSLSocketFactory (socketFactory )
62- .setMaxConnTotal (200 )
63- .setMaxConnPerRoute (20 )
64- .setRoutePlanner (new SystemDefaultRoutePlanner (ProxySelector .getDefault ()))
65- .setConnectionManager (connectionManager )
66- .disableRedirectHandling ()
67- .disableAutomaticRetries ()
68- .build ();
69- return new ApacheHttpTransport (client );
99+ HttpClient client =
100+ HttpClientBuilder .create ()
101+ .useSystemProperties ()
102+ .setSSLSocketFactory (socketFactory )
103+ .setMaxConnTotal (200 )
104+ .setMaxConnPerRoute (20 )
105+ .setRoutePlanner (new SystemDefaultRoutePlanner (ProxySelector .getDefault ()))
106+ .setConnectionManager (connectionManager )
107+ .disableRedirectHandling ()
108+ .disableAutomaticRetries ()
109+ .build ();
110+ return new ApacheHttpTransport (client , isMtls );
70111 }
71112
72- private GoogleApacheHttpTransport () {
73- }
113+ private GoogleApacheHttpTransport () {}
74114}
0 commit comments