Skip to content

Commit

Permalink
Changed withSecure to withCertificateVerification. Also adjusted the …
Browse files Browse the repository at this point in the history
…structure of the certificate verification code so that it is easier to reason about when first looking at it.
  • Loading branch information
rpmoore committed Oct 13, 2014
1 parent 87d0931 commit bc2f40a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Ds3Client fromEnv() {

public static Ds3Client insecureFromEnv() {
final Ds3ClientBuilder builder = clientBuilder();
builder.withSecure(false);
builder.withCertificateVerification(false);
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static class Builder implements com.spectralogic.ds3client.utils.Builder<Connect
private URI proxy = null;
private int retries = 5;
private int bufferSize = 1024 * 1024;
private boolean secure;
private boolean certificateVerification;

private Builder(final String endpoint, final Credentials credentials) {
this.endpoint = endpoint;
Expand All @@ -56,8 +56,8 @@ public Builder withBufferSize(final int bufferSize) {
return this;
}

public Builder withSecure(final boolean secure) {
this.secure = secure;
public Builder withCertificateVerification(final boolean certificateVerification) {
this.certificateVerification = certificateVerification;
return this;
}

Expand All @@ -74,7 +74,7 @@ public ConnectionDetailsImpl build() {
private final URI proxy;
private final int retries;
private final int bufferSize;
private final boolean secure;
private final boolean certificateVerification;

static Builder builder(final String uriEndpoint, final Credentials credentials) {
return new Builder(uriEndpoint, credentials);
Expand All @@ -87,7 +87,7 @@ private ConnectionDetailsImpl(final Builder builder) {
this.proxy = builder.proxy;
this.retries = builder.retries;
this.bufferSize = builder.bufferSize;
this.secure = builder.secure;
this.certificateVerification = builder.certificateVerification;
}

@Override
Expand Down Expand Up @@ -121,8 +121,8 @@ public int getBufferSize() {
}

@Override
public boolean isSecure() {
return secure;
public boolean isCertificateVerification() {
return certificateVerification;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Ds3ClientBuilder implements Builder<Ds3Client> {
final private Credentials credentials;

private boolean https = true;
private boolean secure = true;
private boolean certificateVerification = true;
private URI proxy = null;
private int retries = 5;
private int bufferSize = 1024 * 1024;
Expand Down Expand Up @@ -67,8 +67,8 @@ public Ds3ClientBuilder withBufferSize(final int bufferSize) {
/**
* Specifies if the library should perform SSL certificate validation.
*/
public Ds3ClientBuilder withSecure(final boolean secure) {
this.secure = secure;
public Ds3ClientBuilder withCertificateVerification(final boolean certificateVerification) {
this.certificateVerification = certificateVerification;
return this;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ public Ds3ClientBuilder withRedirectRetries(final int retries) {
@Override
public Ds3Client build() {
final ConnectionDetailsImpl.Builder connBuilder = ConnectionDetailsImpl.builder(this.endpoint, this.credentials)
.withProxy(this.proxy).withHttps(this.https).withSecure(this.secure).withRedirectRetries(this.retries).withBufferSize(this.bufferSize);
.withProxy(this.proxy).withHttps(this.https).withCertificateVerification(this.certificateVerification).withRedirectRetries(this.retries).withBufferSize(this.bufferSize);

final NetworkClient netClient = new NetworkClientImpl(connBuilder.build());
return new Ds3ClientImpl(netClient);
Expand Down
37 changes: 19 additions & 18 deletions sdk/src/main/java/com/spectralogic/ds3client/NetworkClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,26 @@ private HttpHost buildHost() throws MalformedURLException {
}

private CloseableHttpClient getClient() {
if (!NetworkClientImpl.this.getConnectionDetails().isHttps()) {
return HttpClients.createDefault();
if (NetworkClientImpl.this.getConnectionDetails().isHttps() && !NetworkClientImpl.this.getConnectionDetails().isCertificateVerification()) {
try {

final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
return true;
}
}).useTLS().build();

final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(
sslsf).build();

} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new SSLSetupException(e);
}
}

try {

final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
return true;
}
}).useTLS().build();

final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(
sslsf).build();

} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new SSLSetupException(e);
else {
return HttpClients.createDefault();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ public interface ConnectionDetails {
* Returns true if the network layer should perform certificate authentication for SSL. False will disable
* certificate authentication.
*/
boolean isSecure();
boolean isCertificateVerification();
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public SeekableByteChannel buildChannel(final String key) throws IOException {
return new ByteArraySeekableByteChannel();
}
});
Assert.fail("Should have failed with an exception before we got here.");
}

@Test
Expand Down

0 comments on commit bc2f40a

Please sign in to comment.