Skip to content

Commit e886efa

Browse files
fix: socketfactory not registered for apache (#1637)
* fix: socketfactory not registered for apache * refactor code and add test
1 parent a14925f commit e886efa

File tree

3 files changed

+75
-31
lines changed

3 files changed

+75
-31
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransport.java

+60-30
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
2222
import com.google.api.client.util.Beta;
2323
import com.google.api.client.util.SslUtils;
24+
import com.google.common.annotations.VisibleForTesting;
2425
import java.io.IOException;
2526
import java.net.ProxySelector;
2627
import java.security.GeneralSecurityException;
2728
import java.security.KeyStore;
2829
import java.util.concurrent.TimeUnit;
2930
import javax.net.ssl.SSLContext;
3031
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;
3135
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
36+
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
3237
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
3338
import org.apache.http.impl.client.HttpClientBuilder;
3439
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
@@ -64,50 +69,75 @@ public static ApacheHttpTransport newTrustedTransport()
6469
@Beta
6570
public static ApacheHttpTransport newTrustedTransport(MtlsProvider mtlsProvider)
6671
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);
7473
PoolingHttpClientConnectionManager connectionManager =
75-
new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);
74+
new PoolingHttpClientConnectionManager(
75+
handler.getSocketFactoryRegistry(), null, null, null, -1, TimeUnit.MILLISECONDS);
7676

77-
// Disable the stale connection check (previously configured in the HttpConnectionParams
77+
// Disable the stale connection check (previously configured in the
78+
// HttpConnectionParams
7879
connectionManager.setValidateAfterInactivity(-1);
7980

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-
9981
HttpClient client =
10082
HttpClientBuilder.create()
10183
.useSystemProperties()
102-
.setSSLSocketFactory(socketFactory)
10384
.setMaxConnTotal(200)
10485
.setMaxConnPerRoute(20)
10586
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
10687
.setConnectionManager(connectionManager)
10788
.disableRedirectHandling()
10889
.disableAutomaticRetries()
10990
.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+
}
111141
}
112142

113143
private GoogleApacheHttpTransport() {}

google-api-client/src/test/java/com/google/api/client/googleapis/apache/v2/GoogleApacheHttpTransportTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,30 @@
1414

1515
package com.google.api.client.googleapis.apache.v2;
1616

17+
import static org.junit.Assert.assertNotNull;
18+
import static org.junit.Assert.assertTrue;
19+
20+
import com.google.api.client.googleapis.apache.v2.GoogleApacheHttpTransport.SocketFactoryRegistryHandler;
1721
import com.google.api.client.googleapis.mtls.MtlsProvider;
1822
import com.google.api.client.googleapis.mtls.MtlsTransportBaseTest;
1923
import com.google.api.client.http.HttpTransport;
2024
import java.io.IOException;
2125
import java.security.GeneralSecurityException;
26+
import org.junit.Test;
2227

2328
public class GoogleApacheHttpTransportTest extends MtlsTransportBaseTest {
2429
@Override
2530
protected HttpTransport buildTrustedTransport(MtlsProvider mtlsProvider)
2631
throws GeneralSecurityException, IOException {
2732
return GoogleApacheHttpTransport.newTrustedTransport(mtlsProvider);
2833
}
34+
35+
@Test
36+
public void socketFactoryRegistryHandlerTest() throws GeneralSecurityException, IOException {
37+
MtlsProvider mtlsProvider = new TestMtlsProvider(true, createTestMtlsKeyStore(), "", false);
38+
SocketFactoryRegistryHandler handler = new SocketFactoryRegistryHandler(mtlsProvider);
39+
assertNotNull(handler.getSocketFactoryRegistry().lookup("http"));
40+
assertNotNull(handler.getSocketFactoryRegistry().lookup("https"));
41+
assertTrue(handler.isMtls());
42+
}
2943
}

google-api-client/src/test/java/com/google/api/client/googleapis/mtls/MtlsTransportBaseTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected static class TestMtlsProvider implements MtlsProvider {
4141
private String keyStorePassword;
4242
private boolean throwExceptionForGetKeyStore;
4343

44-
TestMtlsProvider(
44+
public TestMtlsProvider(
4545
boolean useClientCertificate,
4646
KeyStore keystore,
4747
String keyStorePassword,

0 commit comments

Comments
 (0)