15
15
package com .google .api .client .googleapis .apache .v2 ;
16
16
17
17
import 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 ;
18
21
import com .google .api .client .http .apache .v2 .ApacheHttpTransport ;
22
+ import com .google .api .client .util .Beta ;
19
23
import com .google .api .client .util .SslUtils ;
20
24
import java .io .IOException ;
21
25
import java .net .ProxySelector ;
24
28
import java .util .concurrent .TimeUnit ;
25
29
import javax .net .ssl .SSLContext ;
26
30
import org .apache .http .client .HttpClient ;
27
- import org .apache .http .config .SocketConfig ;
28
31
import org .apache .http .conn .socket .LayeredConnectionSocketFactory ;
29
32
import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
30
33
import org .apache .http .impl .client .HttpClientBuilder ;
39
42
public final class GoogleApacheHttpTransport {
40
43
41
44
/**
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.
44
50
*/
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
+
47
74
PoolingHttpClientConnectionManager connectionManager =
48
75
new PoolingHttpClientConnectionManager (-1 , TimeUnit .MILLISECONDS );
49
76
@@ -53,22 +80,35 @@ public static ApacheHttpTransport newTrustedTransport() throws GeneralSecurityEx
53
80
// Use the included trust store
54
81
KeyStore trustStore = GoogleUtils .getCertificateTrustStore ();
55
82
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
+ }
57
97
LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory (sslContext );
58
98
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 );
70
111
}
71
112
72
- private GoogleApacheHttpTransport () {
73
- }
113
+ private GoogleApacheHttpTransport () {}
74
114
}
0 commit comments