Skip to content

Commit

Permalink
Upgrade google-http-client to eliminate deprecated usage (#1882)
Browse files Browse the repository at this point in the history
* Upgrade google-http-client

* Handle SSLHandshakeException

* SSLException for all

* Migrate deprecated usage

* Upgrade Guava and Google HTTP Client

* Migrate

* No need to set proxy credentials on our side

* Wrap up

* Update comment

* Upgrade to newer verisons

* wip

* Apply fix from google-http-client PR

* Unset default SSLSocketFactory to create new one

* Add test for proxy credential properties

* Update comment

* Add proxy properties test

* No need to create readers repeatedly

* Simplify

* Remove blank lines

* Use Apache v2 client

* Migrate to 1.31
  • Loading branch information
chanseokoh authored Aug 1, 2019
1 parent 33536d1 commit 61ff620
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 225 deletions.
5 changes: 4 additions & 1 deletion jib-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ group 'com.google.cloud.tools'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
compileJava.options.encoding = 'UTF-8'
compileJava.options.compilerArgs += [ '-Xlint:deprecation' ]
compileTestJava.options.compilerArgs += [ '-Xlint:deprecation' ]

repositories {
mavenCentral()
Expand All @@ -35,7 +37,8 @@ configurations {

dependencies {
// Make sure these are consistent with jib-maven-plugin.
implementation 'com.google.http-client:google-http-client:1.27.0'
implementation 'com.google.http-client:google-http-client:1.31.0'
implementation 'com.google.http-client:google-http-client-apache-v2:1.31.0'
implementation 'org.apache.commons:commons-compress:1.18'
implementation 'com.google.guava:guava:27.0.1-jre'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.9.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.api.client.util.SslUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.Closeable;
Expand All @@ -31,9 +32,8 @@
import java.security.GeneralSecurityException;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.HttpClientBuilder;

/**
* Sends an HTTP {@link Request} and stores the {@link Response}. Clients should not send more than
Expand All @@ -56,17 +56,12 @@ public class Connection implements Closeable {
* @return {@link Connection} factory, a function that generates a {@link Connection} to a URL
*/
public static Function<URL, Connection> getConnectionFactory() {
/*
* Do not use {@link NetHttpTransport}. It does not process response errors properly. A new
* {@link ApacheHttpTransport} needs to be created for each connection because otherwise HTTP
* connection persistence causes the connection to throw {@link NoHttpResponseException}.
*
* @see <a
* href="https://github.com/google/google-http-java-client/issues/39">https://github.com/google/google-http-java-client/issues/39</a>
*/
ApacheHttpTransport transport = new ApacheHttpTransport();
addProxyCredentials(transport);
return url -> new Connection(url, transport);
// Do not use NetHttpTransport. It does not process response errors properly.
// See https://github.com/google/google-http-java-client/issues/39
//
// A new ApacheHttpTransport needs to be created for each connection because otherwise HTTP
// connection persistence causes the connection to throw NoHttpResponseException.
return url -> new Connection(url, new ApacheHttpTransport());
}

/**
Expand All @@ -77,44 +72,14 @@ public static Function<URL, Connection> getConnectionFactory() {
*/
public static Function<URL, Connection> getInsecureConnectionFactory()
throws GeneralSecurityException {
// Do not use {@link NetHttpTransport}. See {@link getConnectionFactory} for details.
ApacheHttpTransport transport =
new ApacheHttpTransport.Builder().doNotValidateCertificate().build();
addProxyCredentials(transport);
return url -> new Connection(url, transport);
}

/**
* Registers proxy credentials onto transport client, in order to deal with proxies that require
* basic authentication.
*
* @param transport Apache HTTP transport
*/
@VisibleForTesting
static void addProxyCredentials(ApacheHttpTransport transport) {
addProxyCredentials(transport, "https");
addProxyCredentials(transport, "http");
}

private static void addProxyCredentials(ApacheHttpTransport transport, String protocol) {
Preconditions.checkArgument(protocol.equals("http") || protocol.equals("https"));

String proxyHost = System.getProperty(protocol + ".proxyHost");
String proxyUser = System.getProperty(protocol + ".proxyUser");
String proxyPassword = System.getProperty(protocol + ".proxyPassword");
if (proxyHost == null || proxyUser == null || proxyPassword == null) {
return;
}

String defaultProxyPort = protocol.equals("http") ? "80" : "443";
int proxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", defaultProxyPort));

DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
httpClient
.getCredentialsProvider()
.setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(proxyUser, proxyPassword));
HttpClientBuilder httpClientBuilder =
ApacheHttpTransport.newDefaultHttpClientBuilder()
.setSSLSocketFactory(null) // creates new factory with the SSLContext given below
.setSSLContext(SslUtils.trustAllSSLContext())
.setSSLHostnameVerifier(new NoopHostnameVerifier());

// Do not use NetHttpTransport. See comments in getConnectionFactory for details.
return url -> new Connection(url, new ApacheHttpTransport(httpClientBuilder.build()));
}

private HttpRequestFactory requestFactory;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.tools.jib.http;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import java.io.IOException;
import java.util.function.BiFunction;

Expand Down
3 changes: 2 additions & 1 deletion jib-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ configurations {

dependencies {
// These are copied over from jib-core and are necessary for the jib-core sourcesets.
compile 'com.google.http-client:google-http-client:1.27.0'
compile 'com.google.http-client:google-http-client:1.31.0'
compile 'com.google.http-client:google-http-client-apache-v2:1.31.0'
compile 'org.apache.commons:commons-compress:1.18'
compile 'com.google.guava:guava:27.0.1-jre'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.9.2'
Expand Down
8 changes: 7 additions & 1 deletion jib-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.27.0</version>
<version>1.31.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-apache-v2</artifactId>
<version>1.31.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
3 changes: 2 additions & 1 deletion jib-plugins-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ sourceSets {

dependencies {
// Make sure these are consistent with jib-maven-plugin.
compile 'com.google.http-client:google-http-client:1.27.0'
compile 'com.google.http-client:google-http-client:1.31.0'
compile 'com.google.http-client:google-http-client-apache-v2:1.31.0'
compile 'org.apache.commons:commons-compress:1.18'
compile 'com.google.guava:guava:27.0.1-jre'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.9.2'
Expand Down

0 comments on commit 61ff620

Please sign in to comment.