|
21 | 21 | import com.google.api.client.http.apache.v2.ApacheHttpTransport;
|
22 | 22 | import com.google.api.client.util.Beta;
|
23 | 23 | import com.google.api.client.util.SslUtils;
|
| 24 | +import com.google.common.annotations.VisibleForTesting; |
24 | 25 | import java.io.IOException;
|
25 | 26 | import java.net.ProxySelector;
|
26 | 27 | import java.security.GeneralSecurityException;
|
27 | 28 | import java.security.KeyStore;
|
28 | 29 | import java.util.concurrent.TimeUnit;
|
29 | 30 | import javax.net.ssl.SSLContext;
|
30 | 31 | import org.apache.http.client.HttpClient;
|
| 32 | +import org.apache.http.config.Registry; |
| 33 | +import org.apache.http.config.RegistryBuilder; |
| 34 | +import org.apache.http.conn.socket.ConnectionSocketFactory; |
31 | 35 | import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
|
| 36 | +import org.apache.http.conn.socket.PlainConnectionSocketFactory; |
32 | 37 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
33 | 38 | import org.apache.http.impl.client.HttpClientBuilder;
|
34 | 39 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
@@ -64,50 +69,75 @@ public static ApacheHttpTransport newTrustedTransport()
|
64 | 69 | @Beta
|
65 | 70 | public static ApacheHttpTransport newTrustedTransport(MtlsProvider mtlsProvider)
|
66 | 71 | 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 |
| - |
| 72 | + SocketFactoryRegistryHandler handler = new SocketFactoryRegistryHandler(mtlsProvider); |
74 | 73 | PoolingHttpClientConnectionManager connectionManager =
|
75 |
| - new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS); |
| 74 | + new PoolingHttpClientConnectionManager( |
| 75 | + handler.getSocketFactoryRegistry(), null, null, null, -1, TimeUnit.MILLISECONDS); |
76 | 76 |
|
77 |
| - // Disable the stale connection check (previously configured in the HttpConnectionParams |
| 77 | + // Disable the stale connection check (previously configured in the |
| 78 | + // HttpConnectionParams |
78 | 79 | connectionManager.setValidateAfterInactivity(-1);
|
79 | 80 |
|
80 |
| - // Use the included trust store |
81 |
| - KeyStore trustStore = GoogleUtils.getCertificateTrustStore(); |
82 |
| - SSLContext sslContext = SslUtils.getTlsSslContext(); |
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 |
| - } |
97 |
| - LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext); |
98 |
| - |
99 | 81 | HttpClient client =
|
100 | 82 | HttpClientBuilder.create()
|
101 | 83 | .useSystemProperties()
|
102 |
| - .setSSLSocketFactory(socketFactory) |
103 | 84 | .setMaxConnTotal(200)
|
104 | 85 | .setMaxConnPerRoute(20)
|
105 | 86 | .setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
|
106 | 87 | .setConnectionManager(connectionManager)
|
107 | 88 | .disableRedirectHandling()
|
108 | 89 | .disableAutomaticRetries()
|
109 | 90 | .build();
|
110 |
| - return new ApacheHttpTransport(client, isMtls); |
| 91 | + return new ApacheHttpTransport(client, handler.isMtls()); |
| 92 | + } |
| 93 | + |
| 94 | + @VisibleForTesting |
| 95 | + static class SocketFactoryRegistryHandler { |
| 96 | + private final Registry<ConnectionSocketFactory> socketFactoryRegistry; |
| 97 | + private final boolean isMtls; |
| 98 | + |
| 99 | + public SocketFactoryRegistryHandler(MtlsProvider mtlsProvider) |
| 100 | + throws GeneralSecurityException, IOException { |
| 101 | + KeyStore mtlsKeyStore = null; |
| 102 | + String mtlsKeyStorePassword = null; |
| 103 | + if (mtlsProvider.useMtlsClientCertificate()) { |
| 104 | + mtlsKeyStore = mtlsProvider.getKeyStore(); |
| 105 | + mtlsKeyStorePassword = mtlsProvider.getKeyStorePassword(); |
| 106 | + } |
| 107 | + |
| 108 | + // Use the included trust store |
| 109 | + KeyStore trustStore = GoogleUtils.getCertificateTrustStore(); |
| 110 | + SSLContext sslContext = SslUtils.getTlsSslContext(); |
| 111 | + |
| 112 | + if (mtlsKeyStore != null && mtlsKeyStorePassword != null) { |
| 113 | + this.isMtls = true; |
| 114 | + SslUtils.initSslContext( |
| 115 | + sslContext, |
| 116 | + trustStore, |
| 117 | + SslUtils.getPkixTrustManagerFactory(), |
| 118 | + mtlsKeyStore, |
| 119 | + mtlsKeyStorePassword, |
| 120 | + SslUtils.getDefaultKeyManagerFactory()); |
| 121 | + } else { |
| 122 | + this.isMtls = false; |
| 123 | + SslUtils.initSslContext(sslContext, trustStore, SslUtils.getPkixTrustManagerFactory()); |
| 124 | + } |
| 125 | + LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext); |
| 126 | + |
| 127 | + this.socketFactoryRegistry = |
| 128 | + RegistryBuilder.<ConnectionSocketFactory>create() |
| 129 | + .register("http", PlainConnectionSocketFactory.getSocketFactory()) |
| 130 | + .register("https", socketFactory) |
| 131 | + .build(); |
| 132 | + } |
| 133 | + |
| 134 | + public Registry<ConnectionSocketFactory> getSocketFactoryRegistry() { |
| 135 | + return this.socketFactoryRegistry; |
| 136 | + } |
| 137 | + |
| 138 | + public boolean isMtls() { |
| 139 | + return this.isMtls; |
| 140 | + } |
111 | 141 | }
|
112 | 142 |
|
113 | 143 | private GoogleApacheHttpTransport() {}
|
|
0 commit comments