Skip to content

Commit c416e20

Browse files
feat: add isMtls property to ApacheHttpTransport (#1168)
* feat: support keystore in transport for mtls * fix format * update code * add tests * update test and doc * update names * create keystore from cert and key string * change certAndKey from string to inputstream * add mtls file * Update google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpTransport.java Co-authored-by: Jeff Ching <[email protected]> * Update google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpTransport.java Co-authored-by: Jeff Ching <[email protected]> * Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java Co-authored-by: Jeff Ching <[email protected]> * Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java Co-authored-by: Jeff Ching <[email protected]> * Update google-http-client/src/test/java/com/google/api/client/util/SecurityUtilsTest.java Co-authored-by: Jeff Ching <[email protected]> * Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java Co-authored-by: Jeff Ching <[email protected]> * update the code * fix name * chore: add Beta annotation for new mtls functions * update Beta * add since tag * feat: add isMtls property to ApacheHttpTransport * update Beta annotation * format * fix tag Co-authored-by: Jeff Ching <[email protected]>
1 parent 6818a02 commit c416e20

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.google.api.client.http.HttpMethods;
1818
import com.google.api.client.http.HttpTransport;
19+
import com.google.api.client.util.Beta;
1920
import java.io.IOException;
2021
import java.net.ProxySelector;
2122
import java.util.concurrent.TimeUnit;
@@ -56,13 +57,16 @@ public final class ApacheHttpTransport extends HttpTransport {
5657
/** Apache HTTP client. */
5758
private final HttpClient httpClient;
5859

60+
/** If the HTTP client uses mTLS channel. */
61+
private final boolean isMtls;
62+
5963
/**
6064
* Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client.
6165
*
6266
* @since 1.30
6367
*/
6468
public ApacheHttpTransport() {
65-
this(newDefaultHttpClient());
69+
this(newDefaultHttpClient(), false);
6670
}
6771

6872
/**
@@ -84,6 +88,32 @@ public ApacheHttpTransport() {
8488
*/
8589
public ApacheHttpTransport(HttpClient httpClient) {
8690
this.httpClient = httpClient;
91+
this.isMtls = false;
92+
}
93+
94+
/**
95+
* {@link Beta} <br>
96+
* Constructor that allows an alternative Apache HTTP client to be used.
97+
*
98+
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
99+
* able to do so.
100+
*
101+
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
102+
*
103+
* <ul>
104+
* <li>HTTP version is set to 1.1.
105+
* <li>Redirects are disabled (google-http-client handles redirects).
106+
* <li>Retries are disabled (google-http-client handles retries).
107+
* </ul>
108+
*
109+
* @param httpClient Apache HTTP client to use
110+
* @param isMtls If the HTTP client is mutual TLS
111+
* @since 1.38
112+
*/
113+
@Beta
114+
public ApacheHttpTransport(HttpClient httpClient, boolean isMtls) {
115+
this.httpClient = httpClient;
116+
this.isMtls = isMtls;
87117
}
88118

89119
/**
@@ -192,4 +222,10 @@ public void shutdown() throws IOException {
192222
public HttpClient getHttpClient() {
193223
return httpClient;
194224
}
225+
226+
/** Returns if the underlying HTTP client is mTLS. */
227+
@Override
228+
public boolean isMtls() {
229+
return isMtls;
230+
}
195231
}

google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.api.client.http.apache.v2;
1616

1717
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
1819
import static org.junit.Assert.assertNotNull;
1920
import static org.junit.Assert.assertTrue;
2021
import static org.junit.Assert.fail;
@@ -64,12 +65,14 @@ public class ApacheHttpTransportTest {
6465
public void testApacheHttpTransport() {
6566
ApacheHttpTransport transport = new ApacheHttpTransport();
6667
checkHttpTransport(transport);
68+
assertFalse(transport.isMtls());
6769
}
6870

6971
@Test
7072
public void testApacheHttpTransportWithParam() {
71-
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build());
73+
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build(), true);
7274
checkHttpTransport(transport);
75+
assertTrue(transport.isMtls());
7376
}
7477

7578
@Test

0 commit comments

Comments
 (0)