Skip to content

Commit a2e28a5

Browse files
authored
BI-549 - PreemptiveHttpClient is missing credentials when initialized with proxy
1 parent e97e03f commit a2e28a5

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

Diff for: build-info-client/src/main/java/org/jfrog/build/client/PreemptiveHttpClient.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ public class PreemptiveHttpClient implements AutoCloseable {
5252
* Used for storing the original host name, before a redirect to a new URL, on the request context.
5353
*/
5454
private static final String ORIGINAL_HOST_CONTEXT_PARAM = "original.host.context.param";
55-
56-
private PoolingHttpClientConnectionManager connectionManager;
57-
private BasicCredentialsProvider basicCredentialsProvider;
58-
private String accessToken;
59-
private AuthCache authCache;
60-
private CloseableHttpClient httpClient;
61-
private int connectionRetries;
55+
BasicCredentialsProvider basicCredentialsProvider;
56+
private final PoolingHttpClientConnectionManager connectionManager;
57+
private final String accessToken;
58+
private final AuthCache authCache;
59+
private final CloseableHttpClient httpClient;
60+
private final int connectionRetries;
6261
private Log log;
6362

6463
public PreemptiveHttpClient(PoolingHttpClientConnectionManager connectionManager, BasicCredentialsProvider credentialsProvider, String accessToken, AuthCache authCache, HttpClientBuilder clientBuilder, int connectionRetries, Log log) {
@@ -230,7 +229,7 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
230229

231230
private class PreemptiveRedirectStrategy extends DefaultRedirectStrategy {
232231

233-
private Set<String> redirectableMethods = CommonUtils.newHashSet(
232+
private final Set<String> redirectableMethods = CommonUtils.newHashSet(
234233
HttpGet.METHOD_NAME.toLowerCase(),
235234
HttpPost.METHOD_NAME.toLowerCase(),
236235
HttpHead.METHOD_NAME.toLowerCase(),

Diff for: build-info-client/src/main/java/org/jfrog/build/client/PreemptiveHttpClientBuilder.java

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ protected void createCredentialsAndAuthCache() {
142142
authCache = new BasicAuthCache();
143143
// Generate BASIC scheme object and add it to the local auth cache
144144
authCache.put(proxy, new BasicScheme());
145-
return;
146145
}
147146

148147
if (StringUtils.isEmpty(accessToken)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.jfrog.build.client;
2+
3+
import org.apache.http.auth.AuthScope;
4+
import org.apache.http.auth.Credentials;
5+
import org.apache.http.auth.UsernamePasswordCredentials;
6+
import org.apache.http.impl.client.BasicCredentialsProvider;
7+
import org.jfrog.build.api.util.NullLog;
8+
import org.testng.annotations.Test;
9+
10+
import static org.testng.Assert.assertEquals;
11+
import static org.testng.Assert.assertNotNull;
12+
13+
@Test
14+
public class PreemptiveHttpClientBuilderTest {
15+
public void testCredentialsMaintainedWithProxy() {
16+
String rtUser = "rt-user";
17+
String rtPassword = "rt-password";
18+
19+
String proxyHost = "127.0.0.1";
20+
int proxyPort = 8000;
21+
String proxyUser = "proxy-user";
22+
String proxyPassword = "proxy-password";
23+
24+
PreemptiveHttpClientBuilder clientBuilder = new PreemptiveHttpClientBuilder()
25+
.setConnectionRetries(3)
26+
.setInsecureTls(false)
27+
.setTimeout(300)
28+
.setLog(new NullLog())
29+
.setProxyConfiguration(createProxyConfiguration(proxyHost, proxyPort, proxyUser, proxyPassword))
30+
.setUserName(rtUser)
31+
.setPassword(rtPassword);
32+
PreemptiveHttpClient deployClient = clientBuilder.build();
33+
34+
// Assert both Artifactory and proxy credentials exist in the credentials provider.
35+
BasicCredentialsProvider credentialsProvider = deployClient.basicCredentialsProvider;
36+
Credentials rtCredentials = credentialsProvider.getCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT));
37+
assertNotNull(rtCredentials);
38+
assertEquals(rtCredentials, new UsernamePasswordCredentials(rtUser, rtPassword));
39+
40+
Credentials portCredentials = credentialsProvider.getCredentials(new AuthScope(proxyHost, proxyPort));
41+
assertNotNull(portCredentials);
42+
assertEquals(portCredentials, new UsernamePasswordCredentials(proxyUser, proxyPassword));
43+
}
44+
45+
private ProxyConfiguration createProxyConfiguration(String host, int port, String proxyUser, String proxyPassword) {
46+
ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
47+
proxyConfiguration.host = host;
48+
proxyConfiguration.port = port;
49+
proxyConfiguration.username = proxyUser;
50+
proxyConfiguration.password = proxyPassword;
51+
return proxyConfiguration;
52+
}
53+
}

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/client/ArtifactoryBuildInfoClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public ArtifactoryVersion verifyCompatibleArtifactoryVersion() throws VersionExc
395395
try {
396396
version = httpClient.getVersion();
397397
} catch (IOException e) {
398-
throw new VersionException("Error occurred while requesting version information: " + e.getMessage(), e,
398+
throw new VersionException("Error occurred while requesting version information: " + e, e,
399399
VersionCompatibilityType.NOT_FOUND);
400400
}
401401
if (version.isNotFound()) {

0 commit comments

Comments
 (0)