Skip to content

Commit 1deba99

Browse files
authored
Revert Google HTTP Client upgrade (#1980)
* Revert Google HTTP Client upgrade * Exclude google-http-client from google-auth-library-oauth2-http
1 parent cdb696f commit 1deba99

File tree

7 files changed

+217
-42
lines changed

7 files changed

+217
-42
lines changed

build.gradle

+2-8
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ subprojects {
4646
// For Google libraries, check <http-client-bom.version>, <google.auth.version>, <guava.version>,
4747
// ... in https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-clients/pom.xml
4848
// for best compatibility.
49-
GOOGLE_HTTP_CLIENT: '1.31.0',
50-
GOOGLE_HTTP_CLIENT_APACHE_V2: '1.31.0',
49+
GOOGLE_HTTP_CLIENT: '1.27.0',
5150
GOOGLE_AUTH_LIBRARY_OAUTH2_HTTP: '0.16.2',
5251
GUAVA: '28.0-jre',
5352

54-
// TODO: remove once https://github.com/googleapis/google-http-java-client/issues/795 is fixed and released.
55-
// Forcing to downgrade this to 4.5.6 fixes https://github.com/GoogleContainerTools/jib/issues/1914
56-
// However, #795 and upgrading httpclient alone may not fix #1914. We may need to explicitly disable URI
57-
// normalization as discussed in #795.
58-
APACHE_HTTP_CLIENT_OVERRIDE: '4.5.6',
5953
COMMONS_COMPRESS: '1.18',
6054
JACKSON_DATABIND: '2.9.9.2',
6155
ASM: '7.0',
@@ -70,7 +64,7 @@ subprojects {
7064
// Use this to ensure we correctly override transitive dependencies
7165
// TODO: There might be a plugin that does this
7266
task ensureTransitiveDependencyOverrides {
73-
def rules = ["httpclient": dependencyVersions.APACHE_HTTP_CLIENT_OVERRIDE]
67+
def rules = ["google-http-client": dependencyVersions.GOOGLE_HTTP_CLIENT]
7468
doLast {
7569
configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.each { artifact ->
7670
def dependency = artifact.moduleVersion.id

jib-core/build.gradle

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ plugins {
44
}
55

66
dependencies {
7-
implementation("com.google.http-client:google-http-client:${dependencyVersions.GOOGLE_HTTP_CLIENT}") {
8-
exclude group: "org.apache.httpcomponents", module: "httpclient"
9-
}
10-
implementation("com.google.http-client:google-http-client-apache-v2:${dependencyVersions.GOOGLE_HTTP_CLIENT_APACHE_V2}") {
11-
exclude group: "org.apache.httpcomponents", module: "httpclient"
12-
}
7+
implementation "com.google.http-client:google-http-client:${dependencyVersions.GOOGLE_HTTP_CLIENT}"
138
implementation("com.google.auth:google-auth-library-oauth2-http:${dependencyVersions.GOOGLE_AUTH_LIBRARY_OAUTH2_HTTP}") {
14-
exclude group: "org.apache.httpcomponents", module: "httpclient"
9+
exclude group: "com.google.http-client", module: "google-http-client"
1510
}
16-
implementation "org.apache.httpcomponents:httpclient:${dependencyVersions.APACHE_HTTP_CLIENT_OVERRIDE}"
1711

1812
implementation "org.apache.commons:commons-compress:${dependencyVersions.COMMONS_COMPRESS}"
1913
implementation "com.google.guava:guava:${dependencyVersions.GUAVA}"

jib-core/src/main/java/com/google/cloud/tools/jib/http/Connection.java

+44-12
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import com.google.api.client.http.HttpRequestFactory;
2323
import com.google.api.client.http.HttpResponse;
2424
import com.google.api.client.http.HttpTransport;
25-
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
26-
import com.google.api.client.util.SslUtils;
25+
import com.google.api.client.http.apache.ApacheHttpTransport;
2726
import com.google.common.annotations.VisibleForTesting;
2827
import com.google.common.base.Preconditions;
2928
import java.io.Closeable;
@@ -32,8 +31,9 @@
3231
import java.security.GeneralSecurityException;
3332
import java.util.function.Function;
3433
import javax.annotation.Nullable;
35-
import org.apache.http.conn.ssl.NoopHostnameVerifier;
36-
import org.apache.http.impl.client.HttpClientBuilder;
34+
import org.apache.http.auth.AuthScope;
35+
import org.apache.http.auth.UsernamePasswordCredentials;
36+
import org.apache.http.impl.client.DefaultHttpClient;
3737

3838
/**
3939
* Sends an HTTP {@link Request} and stores the {@link Response}. Clients should not send more than
@@ -61,7 +61,9 @@ public static Function<URL, Connection> getConnectionFactory() {
6161
//
6262
// A new ApacheHttpTransport needs to be created for each connection because otherwise HTTP
6363
// connection persistence causes the connection to throw NoHttpResponseException.
64-
return url -> new Connection(url, new ApacheHttpTransport());
64+
ApacheHttpTransport transport = new ApacheHttpTransport();
65+
addProxyCredentials(transport);
66+
return url -> new Connection(url, transport);
6567
}
6668

6769
/**
@@ -72,14 +74,44 @@ public static Function<URL, Connection> getConnectionFactory() {
7274
*/
7375
public static Function<URL, Connection> getInsecureConnectionFactory()
7476
throws GeneralSecurityException {
75-
HttpClientBuilder httpClientBuilder =
76-
ApacheHttpTransport.newDefaultHttpClientBuilder()
77-
.setSSLSocketFactory(null) // creates new factory with the SSLContext given below
78-
.setSSLContext(SslUtils.trustAllSSLContext())
79-
.setSSLHostnameVerifier(new NoopHostnameVerifier());
80-
8177
// Do not use NetHttpTransport. See comments in getConnectionFactory for details.
82-
return url -> new Connection(url, new ApacheHttpTransport(httpClientBuilder.build()));
78+
ApacheHttpTransport transport =
79+
new ApacheHttpTransport.Builder().doNotValidateCertificate().build();
80+
addProxyCredentials(transport);
81+
return url -> new Connection(url, transport);
82+
}
83+
84+
/**
85+
* Registers proxy credentials onto transport client, in order to deal with proxies that require
86+
* basic authentication.
87+
*
88+
* @param transport Apache HTTP transport
89+
*/
90+
@VisibleForTesting
91+
static void addProxyCredentials(ApacheHttpTransport transport) {
92+
addProxyCredentials(transport, "https");
93+
addProxyCredentials(transport, "http");
94+
}
95+
96+
private static void addProxyCredentials(ApacheHttpTransport transport, String protocol) {
97+
Preconditions.checkArgument(protocol.equals("http") || protocol.equals("https"));
98+
99+
String proxyHost = System.getProperty(protocol + ".proxyHost");
100+
String proxyUser = System.getProperty(protocol + ".proxyUser");
101+
String proxyPassword = System.getProperty(protocol + ".proxyPassword");
102+
if (proxyHost == null || proxyUser == null || proxyPassword == null) {
103+
return;
104+
}
105+
106+
String defaultProxyPort = protocol.equals("http") ? "80" : "443";
107+
int proxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", defaultProxyPort));
108+
109+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
110+
httpClient
111+
.getCredentialsProvider()
112+
.setCredentials(
113+
new AuthScope(proxyHost, proxyPort),
114+
new UsernamePasswordCredentials(proxyUser, proxyPassword));
83115
}
84116

85117
private HttpRequestFactory requestFactory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright 2018 Google LLC.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.http;
18+
19+
import com.google.api.client.http.apache.ApacheHttpTransport;
20+
import com.google.common.collect.ImmutableList;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.function.Consumer;
24+
import org.apache.http.auth.AuthScope;
25+
import org.apache.http.auth.Credentials;
26+
import org.apache.http.impl.client.DefaultHttpClient;
27+
import org.junit.After;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
/** Tests for {@link Connection} with setting proxy credentials. */
33+
public class ConnectionWithProxyCredentialsTest {
34+
35+
private static final ImmutableList<String> proxyProperties =
36+
ImmutableList.of(
37+
"http.proxyHost",
38+
"http.proxyPort",
39+
"http.proxyUser",
40+
"http.proxyPassword",
41+
"https.proxyHost",
42+
"https.proxyPort",
43+
"https.proxyUser",
44+
"https.proxyPassword");
45+
46+
// HashMap to allow saving null values.
47+
private final HashMap<String, String> savedProperties = new HashMap<>();
48+
49+
private final ApacheHttpTransport transport = new ApacheHttpTransport();
50+
51+
@Before
52+
public void setUp() {
53+
proxyProperties.stream().forEach(key -> savedProperties.put(key, System.getProperty(key)));
54+
proxyProperties.stream().forEach(key -> System.clearProperty(key));
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
Consumer<Map.Entry<String, String>> restoreProperty =
60+
entry -> {
61+
if (entry.getValue() == null) {
62+
System.clearProperty(entry.getKey());
63+
} else {
64+
System.setProperty(entry.getKey(), entry.getValue());
65+
}
66+
};
67+
savedProperties.entrySet().stream().forEach(restoreProperty);
68+
}
69+
70+
@Test
71+
public void testAddProxyCredentials_undefined() {
72+
Connection.addProxyCredentials(transport);
73+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
74+
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
75+
Assert.assertNull(credentials);
76+
}
77+
78+
@Test
79+
public void testAddProxyCredentials() {
80+
System.setProperty("http.proxyHost", "http://localhost");
81+
System.setProperty("http.proxyPort", "1080");
82+
System.setProperty("http.proxyUser", "user");
83+
System.setProperty("http.proxyPassword", "pass");
84+
85+
System.setProperty("https.proxyHost", "https://host.com");
86+
System.setProperty("https.proxyPort", "1443");
87+
System.setProperty("https.proxyUser", "s-user");
88+
System.setProperty("https.proxyPassword", "s-pass");
89+
90+
Connection.addProxyCredentials(transport);
91+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
92+
Credentials httpCredentials =
93+
httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 1080));
94+
Assert.assertEquals("user", httpCredentials.getUserPrincipal().getName());
95+
Assert.assertEquals("pass", httpCredentials.getPassword());
96+
97+
Credentials httpsCredentials =
98+
httpClient.getCredentialsProvider().getCredentials(new AuthScope("https://host.com", 1443));
99+
Assert.assertEquals("s-user", httpsCredentials.getUserPrincipal().getName());
100+
Assert.assertEquals("s-pass", httpsCredentials.getPassword());
101+
}
102+
103+
@Test
104+
public void testAddProxyCredentials_defaultPorts() {
105+
System.setProperty("http.proxyHost", "http://localhost");
106+
System.setProperty("http.proxyUser", "user");
107+
System.setProperty("http.proxyPassword", "pass");
108+
109+
System.setProperty("https.proxyHost", "https://host.com");
110+
System.setProperty("https.proxyUser", "s-user");
111+
System.setProperty("https.proxyPassword", "s-pass");
112+
113+
Connection.addProxyCredentials(transport);
114+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
115+
Credentials httpCredentials =
116+
httpClient.getCredentialsProvider().getCredentials(new AuthScope("http://localhost", 80));
117+
Assert.assertEquals("user", httpCredentials.getUserPrincipal().getName());
118+
Assert.assertEquals("pass", httpCredentials.getPassword());
119+
120+
Credentials httpsCredentials =
121+
httpClient.getCredentialsProvider().getCredentials(new AuthScope("https://host.com", 443));
122+
Assert.assertEquals("s-user", httpsCredentials.getUserPrincipal().getName());
123+
Assert.assertEquals("s-pass", httpsCredentials.getPassword());
124+
}
125+
126+
@Test
127+
public void testAddProxyCredentials_hostUndefined() {
128+
System.setProperty("http.proxyUser", "user");
129+
System.setProperty("http.proxyPassword", "pass");
130+
131+
System.setProperty("https.proxyUser", "s-user");
132+
System.setProperty("https.proxyPassword", "s-pass");
133+
134+
Connection.addProxyCredentials(transport);
135+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
136+
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
137+
Assert.assertNull(credentials);
138+
}
139+
140+
@Test
141+
public void testAddProxyCredentials_userUndefined() {
142+
System.setProperty("http.proxyHost", "http://localhost");
143+
System.setProperty("http.proxyPassword", "pass");
144+
145+
System.setProperty("https.proxyHost", "https://host.com");
146+
System.setProperty("https.proxyPassword", "s-pass");
147+
148+
Connection.addProxyCredentials(transport);
149+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
150+
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
151+
Assert.assertNull(credentials);
152+
}
153+
154+
@Test
155+
public void testAddProxyCredentials_passwordUndefined() {
156+
System.setProperty("http.proxyHost", "http://localhost");
157+
System.setProperty("http.proxyUser", "user");
158+
159+
System.setProperty("https.proxyHost", "https://host.com");
160+
System.setProperty("https.proxyUser", "s-user");
161+
162+
Connection.addProxyCredentials(transport);
163+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
164+
Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
165+
Assert.assertNull(credentials);
166+
}
167+
}

jib-core/src/test/java/com/google/cloud/tools/jib/http/MockConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.cloud.tools.jib.http;
1818

1919
import com.google.api.client.http.GenericUrl;
20-
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
20+
import com.google.api.client.http.apache.ApacheHttpTransport;
2121
import java.io.IOException;
2222
import java.util.function.BiFunction;
2323

jib-gradle-plugin/build.gradle

-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,9 @@ repositories {
1414
}
1515

1616
dependencies {
17-
1817
sourceProject project(":jib-core")
1918
sourceProject project(":jib-plugins-common")
2019

21-
implementation ("com.google.http-client:google-http-client:${dependencyVersions.GOOGLE_HTTP_CLIENT}") {
22-
exclude group: "org.apache.httpcomponents", module: "httpclient"
23-
}
24-
implementation "org.apache.httpcomponents:httpclient:${dependencyVersions.APACHE_HTTP_CLIENT_OVERRIDE}"
25-
26-
implementation "com.google.guava:guava:${dependencyVersions.GUAVA}"
27-
implementation "com.fasterxml.jackson.core:jackson-databind:${dependencyVersions.JACKSON_DATABIND}"
28-
2920
testImplementation "junit:junit:${dependencyVersions.JUNIT}"
3021
testImplementation "org.mockito:mockito-core:${dependencyVersions.MOCKITO_CORE}"
3122
testImplementation "org.slf4j:slf4j-api:${dependencyVersions.SLF4J_API}"

jib-plugins-common/build.gradle

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
dependencies {
22
implementation project(':jib-core')
3-
implementation ("com.google.http-client:google-http-client:${dependencyVersions.GOOGLE_HTTP_CLIENT}") {
4-
exclude group: "org.apache.httpcomponents", module: "httpclient"
5-
}
6-
implementation "org.apache.httpcomponents:httpclient:${dependencyVersions.APACHE_HTTP_CLIENT_OVERRIDE}"
3+
implementation "com.google.http-client:google-http-client:${dependencyVersions.GOOGLE_HTTP_CLIENT}"
74
implementation "com.google.guava:guava:${dependencyVersions.GUAVA}"
85
implementation "com.fasterxml.jackson.core:jackson-databind:${dependencyVersions.JACKSON_DATABIND}"
96

0 commit comments

Comments
 (0)