Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix proxy auth #746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}
}