-
Notifications
You must be signed in to change notification settings - Fork 45
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
add https support #47
Conversation
.config(user, password) | ||
.config(maxTotal, maxPerRoute) | ||
.config(protocol, trustStoreFile, trustStorePassword) | ||
.build()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align
.config(protocol, trustStoreFile, trustStorePassword) | ||
.build()); | ||
} | ||
private TrustManager[] getTrustManager() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add empty line
} | ||
private TrustManager[] getTrustManager() { | ||
return new TrustManager[]{ | ||
new X509TrustManager() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add class define NonCheckTrustManager
try { | ||
SslConfigurator sslConfig = SslConfigurator.newInstance(); | ||
sslConfig.trustStoreFile(config.getProperties().get("trustStoreFile").toString()) | ||
.trustStorePassword(config.getProperties().get("trustStorePassword").toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align
.build(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address it
.sslContext(sc) | ||
.build(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't swallow exception
int timeout, int maxTotal, int maxPerRoute, | ||
String protocol, String trustStoreFile, | ||
String trustStorePassword,int status) { | ||
super(url, user, password, timeout, maxTotal, maxPerRoute,protocol, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add space before protocol
String protocol, String trustStoreFile, | ||
String trustStorePassword,int status) { | ||
super(url, user, password, timeout, maxTotal, maxPerRoute,protocol, | ||
trustStoreFile,trustStorePassword); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add space before trustStorePassword
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.glassfish.jersey.apache.connector.ApacheClientProperties.CONNECTION_MANAGER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep origin order
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address it
.build(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address it
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} catch (KeyManagementException e) { | ||
throw new ClientException("security key management exception", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
improve message
TrustManager[] trustAllCerts = NoCheckTrustManager(); | ||
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
client = ClientBuilder.newBuilder() | ||
.hostnameVerifier(new HostNameVerifier(url)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align with .newBuilder
private static class X509TrustManager implements javax.net.ssl.X509TrustManager { | ||
@Override | ||
public void checkClientTrusted(X509Certificate[] chain, String authType) | ||
throws CertificateException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align with X509Certificate
private TrustManager[] NoCheckTrustManager() { | ||
return new TrustManager[]{ | ||
new X509TrustManager() | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put in one line
return hv.verify(hostname, session); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove it
Codecov Report
@@ Coverage Diff @@
## master #47 +/- ##
============================================
+ Coverage 81.58% 81.60% +0.01%
- Complexity 671 675 +4
============================================
Files 56 56
Lines 2004 2049 +45
Branches 297 300 +3
============================================
+ Hits 1635 1672 +37
- Misses 244 249 +5
- Partials 125 128 +3
Continue to review full report at Codecov.
|
try { | ||
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
} catch (KeyManagementException e) { | ||
throw new ClientException("security key init management exception", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Failed to init security key management"
} catch (KeyManagementException e) { | ||
throw new ClientException("security key management exception", e); | ||
} | ||
client = TrustConfig(url, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrapTrustConfig()
} | ||
|
||
private Client wrapTrustConfig(String url, ClientConfig config) { | ||
Client client = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to line 142
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.glassfish.jersey.apache.connector.ApacheClientProperties.CONNECTION_MANAGER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address it
SSLContext sc = sslConfig.createSSLContext(); | ||
TrustManager[] trustAllCerts = NoCheckTrustManager(); | ||
try { | ||
sc.init(null, trustAllCerts, new java.security.SecureRandom()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import java.security.SecureRandom
return new TrustManager[]{new X509TrustManager()}; | ||
} | ||
|
||
private static class X509TrustManager implements javax.net.ssl.X509TrustManager { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to NoCheckTrustManager
.build()); | ||
} | ||
|
||
private TrustManager[] NoCheckTrustManager() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method name must be started with lower case, rename to createNoCheckTrustManager
String protocol, String trustStoreFile, | ||
String trustStorePassword, int status) { | ||
super(url, user, password, timeout, maxTotal, maxPerRoute, protocol, | ||
trustStoreFile, trustStorePassword); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align
private Client wrapTrustConfig(String url, ClientConfig config) { | ||
Client client = null; | ||
SslConfigurator sslConfig = SslConfigurator.newInstance(); | ||
sslConfig.trustStoreFile(config.getProperties().get("trustStoreFile").toString()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use config.getProperty("trustStoreFile") instead
Client client = null; | ||
SslConfigurator sslConfig = SslConfigurator.newInstance(); | ||
sslConfig.trustStoreFile(config.getProperties().get("trustStoreFile").toString()) | ||
.trustStorePassword(config.getProperties().get("trustStorePassword").toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
public RestClient(String url, ClientConfig config) { | ||
this.client = ClientBuilder.newClient(config); | ||
Client client = null; | ||
Object protocol = config.getProperties().get("protocol"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
this(url, new ConfigBuilder().config(timeout) | ||
.config(user, password) | ||
.config(maxTotal, maxPerRoute) | ||
.config(protocol, trustStoreFile, trustStorePassword) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 109 seems exceed 80 chars, wrap line like
.config(protocol, trustStoreFile,
trustStorePassword)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also pay attention to other places
} catch (KeyManagementException e) { | ||
throw new ClientException("Failed to init security key management", e); | ||
} | ||
client = ClientBuilder.newBuilder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just return ClientBuilder.newBuilder().... is ok, no need client
this.headers = ImmutableMultivaluedMap.empty(); | ||
this.content = ""; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just add a test case: testHttpsGet:
public void testHttpsGet() {
RestClient client = new RestClientImpl(...);
RestResult restResult = client.get("path", "id1");
Assert.assertEquals(200, restResult.status());
}
String trustStorePassword = "changeit"; | ||
RestClient client = new RestClientImpl("/test", "user", "", 1000, | ||
10, 5, "https", trustStoreFile, | ||
trustStorePassword, 200); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align
if (!this.url.equals("") && this.url.contains(hostname)) { | ||
return true; | ||
} else { | ||
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename hv to verifier
|
||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
if (!this.url.equals("") && this.url.contains(hostname)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use this.url.isEmpty() instead of this.url.equals("")
@@ -284,6 +356,25 @@ private static String encode(String raw) { | |||
return UriComponent.encode(raw, UriComponent.Type.PATH_SEGMENT); | |||
} | |||
|
|||
private static class HostNameVerifier implements HostnameVerifier { | |||
|
|||
private String url; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set final
RestResult restResult = client.post("path", "body"); | ||
Assert.assertEquals(200, restResult.status()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add test for HostNameVerifier
add https support
implement https://github.com/hugegraph/hugegraph/issues/445