Skip to content

Commit

Permalink
fix proxy auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Sep 13, 2022
1 parent be95f1a commit e29960c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.utils.StringUtils;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;

public class HttpUtil {

Expand Down Expand Up @@ -174,4 +177,23 @@ public static boolean needProxy(String targetHost, String clientNoProxyList, Str
}
return true;
}

public static void readCredentialsFromApacheProxy(CredentialsProvider credentialsProvider, String proxy)
throws ClientException {
try {
if (!StringUtils.isEmpty(proxy)) {
URL proxyUrl = new URL(proxy);
String userInfo = proxyUrl.getUserInfo();
if (!StringUtils.isEmpty(userInfo)) {
final String[] userMessage = userInfo.split(":");
credentialsProvider.setCredentials(
new AuthScope(proxyUrl.getHost(),
proxyUrl.getPort()),
new UsernamePasswordCredentials(userMessage[0], userMessage[1]));
}
}
} catch (IOException e) {
throw new ClientException("SDK.InvalidProxy", "proxy url is invalid");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.aliyuncs.utils.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
Expand All @@ -25,6 +28,7 @@
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -189,6 +193,19 @@ protected void init(final HttpClientConfig config0) throws ClientException {
CredentialsProvider credentialsProvider = this.clientConfig.getCredentialsProvider();
if (null != credentialsProvider) {
builder.setDefaultCredentialsProvider(credentialsProvider);
} else {
BasicCredentialsProvider crePro = new BasicCredentialsProvider();
if (!StringUtils.isEmpty(clientConfig.getHttpsProxy())) {
HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpsProxy());
} else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpsProxy())) {
HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpsProxy());
}
if (!StringUtils.isEmpty(clientConfig.getHttpProxy())) {
HttpUtil.readCredentialsFromApacheProxy(crePro, clientConfig.getHttpProxy());
} else if (!StringUtils.isEmpty(EnvironmentUtils.getHttpProxy())) {
HttpUtil.readCredentialsFromApacheProxy(crePro, EnvironmentUtils.getHttpProxy());
}
builder.setDefaultCredentialsProvider(crePro);
}
// default request config
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout((int) config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import static org.mockito.Mockito.mock;

import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.sun.org.apache.xalan.internal.lib.ExsltBase;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -216,4 +221,32 @@ public void testNeedProxyHasEnvProxyList() {
boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com");
Assert.assertFalse(need);
}

@Test
public void testReadCredentialsFromApacheProxy() throws Exception {
BasicCredentialsProvider crePro = new BasicCredentialsProvider();
String proxy = "http://www.aliyun.com:80";
URL proxyUrl = new URL(proxy);
try {
// proxy without auth
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
Assert.assertNull(crePro.getCredentials(new AuthScope(proxyUrl.getHost(),
proxyUrl.getPort())));
// proxy with auth
proxy = "http://user:[email protected]";
proxyUrl = new URL(proxy);
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
Credentials credentials = crePro.getCredentials(new AuthScope(proxyUrl.getHost(),
proxyUrl.getPort()));
Assert.assertEquals("user", credentials.getUserPrincipal().getName());
Assert.assertEquals("passwd", credentials.getPassword());
// invalid proxy
proxy = "http0://www.aliyun.com";
HttpUtil.readCredentialsFromApacheProxy(crePro, proxy);
Assert.fail();
} catch (ClientException e) {
Assert.assertEquals("SDK.InvalidProxy : proxy url is invalid", e.getMessage());
}

}
}

0 comments on commit e29960c

Please sign in to comment.