Skip to content

Commit

Permalink
feat: add isMtls property to ApacheHttpTransport (#1168)
Browse files Browse the repository at this point in the history
* 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 <chingor@google.com>

* Update google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpTransport.java

Co-authored-by: Jeff Ching <chingor@google.com>

* Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java

Co-authored-by: Jeff Ching <chingor@google.com>

* Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java

Co-authored-by: Jeff Ching <chingor@google.com>

* Update google-http-client/src/test/java/com/google/api/client/util/SecurityUtilsTest.java

Co-authored-by: Jeff Ching <chingor@google.com>

* Update google-http-client/src/main/java/com/google/api/client/util/SslUtils.java

Co-authored-by: Jeff Ching <chingor@google.com>

* 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 <chingor@google.com>
  • Loading branch information
arithmetic1728 and chingor13 committed Nov 2, 2020
1 parent 6818a02 commit c416e20
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Expand Up @@ -16,6 +16,7 @@

import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.util.Beta;
import java.io.IOException;
import java.net.ProxySelector;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -56,13 +57,16 @@ public final class ApacheHttpTransport extends HttpTransport {
/** Apache HTTP client. */
private final HttpClient httpClient;

/** If the HTTP client uses mTLS channel. */
private final boolean isMtls;

/**
* Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client.
*
* @since 1.30
*/
public ApacheHttpTransport() {
this(newDefaultHttpClient());
this(newDefaultHttpClient(), false);
}

/**
Expand All @@ -84,6 +88,32 @@ public ApacheHttpTransport() {
*/
public ApacheHttpTransport(HttpClient httpClient) {
this.httpClient = httpClient;
this.isMtls = false;
}

/**
* {@link Beta} <br>
* Constructor that allows an alternative Apache HTTP client to be used.
*
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
* able to do so.
*
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
*
* <ul>
* <li>HTTP version is set to 1.1.
* <li>Redirects are disabled (google-http-client handles redirects).
* <li>Retries are disabled (google-http-client handles retries).
* </ul>
*
* @param httpClient Apache HTTP client to use
* @param isMtls If the HTTP client is mutual TLS
* @since 1.38
*/
@Beta
public ApacheHttpTransport(HttpClient httpClient, boolean isMtls) {
this.httpClient = httpClient;
this.isMtls = isMtls;
}

/**
Expand Down Expand Up @@ -192,4 +222,10 @@ public void shutdown() throws IOException {
public HttpClient getHttpClient() {
return httpClient;
}

/** Returns if the underlying HTTP client is mTLS. */
@Override
public boolean isMtls() {
return isMtls;
}
}
Expand Up @@ -15,6 +15,7 @@
package com.google.api.client.http.apache.v2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -64,12 +65,14 @@ public class ApacheHttpTransportTest {
public void testApacheHttpTransport() {
ApacheHttpTransport transport = new ApacheHttpTransport();
checkHttpTransport(transport);
assertFalse(transport.isMtls());
}

@Test
public void testApacheHttpTransportWithParam() {
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build());
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build(), true);
checkHttpTransport(transport);
assertTrue(transport.isMtls());
}

@Test
Expand Down

0 comments on commit c416e20

Please sign in to comment.