Skip to content

Commit

Permalink
Polishing #1326
Browse files Browse the repository at this point in the history
Rename option to handshakeTimeout. Add unit test.

Original pull request: #1338
  • Loading branch information
mp911de committed Jul 9, 2020
1 parent cbb4b5f commit 2d55c12
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/lettuce/core/SslConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*
* @author Mark Paluch
* @author Amin Mohtashami
* @author Felipe Ruiz
*/
public class SslConnectionBuilder extends ConnectionBuilder {

Expand Down Expand Up @@ -181,10 +182,9 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}

SslHandler sslHandler = new SslHandler(sslEngine, startTls);
Duration sslHandshakeTimeout = sslOptions.getSslHandshakeTimeout();
if (sslHandshakeTimeout != null) {
sslHandler.setHandshakeTimeoutMillis(sslHandshakeTimeout.toMillis());
}
Duration sslHandshakeTimeout = sslOptions.getHandshakeTimeout();
sslHandler.setHandshakeTimeoutMillis(sslHandshakeTimeout.toMillis());

channel.pipeline().addLast(sslHandler);

if (channel.pipeline().get("channelActivator") == null) {
Expand Down
63 changes: 31 additions & 32 deletions src/main/java/io/lettuce/core/SslOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*
* @author Mark Paluch
* @author Amin Mohtashami
* @author Felipe Ruiz
* @since 4.3
*/
public class SslOptions {
Expand Down Expand Up @@ -74,11 +75,12 @@ public class SslOptions {

private final KeystoreAction trustmanager;

private final Duration sslHandshakeTimeout;
private final Duration handshakeTimeout;

protected SslOptions(Builder builder) {
this.keyStoreType = builder.keyStoreType;
this.sslProvider = builder.sslProvider;
this.handshakeTimeout = builder.sslHandshakeTimeout;
this.keystore = builder.keystore;
this.keystorePassword = builder.keystorePassword;
this.truststore = builder.truststore;
Expand All @@ -91,12 +93,12 @@ protected SslOptions(Builder builder) {
this.sslParametersSupplier = builder.sslParametersSupplier;
this.keymanager = builder.keymanager;
this.trustmanager = builder.trustmanager;
this.sslHandshakeTimeout = builder.sslHandshakeTimeout;
}

protected SslOptions(SslOptions original) {
this.keyStoreType = original.keyStoreType;
this.sslProvider = original.getSslProvider();
this.handshakeTimeout = original.handshakeTimeout;
this.keystore = original.keystore;
this.keystorePassword = original.keystorePassword;
this.truststore = original.getTruststore();
Expand All @@ -109,7 +111,6 @@ protected SslOptions(SslOptions original) {
this.sslParametersSupplier = original.sslParametersSupplier;
this.keymanager = original.keymanager;
this.trustmanager = original.trustmanager;
this.sslHandshakeTimeout = original.sslHandshakeTimeout;
}

/**
Expand Down Expand Up @@ -171,7 +172,7 @@ public static class Builder {

private KeystoreAction trustmanager = KeystoreAction.NO_OP;

private Duration sslHandshakeTimeout = null;
private Duration sslHandshakeTimeout = Duration.ofSeconds(10);

private Builder() {
}
Expand Down Expand Up @@ -225,6 +226,21 @@ private Builder sslProvider(SslProvider sslProvider) {
return this;
}

/**
* Sets a timeout for the SSL handshake.
*
* @param timeout {@link Duration}.
* @return {@code this}
* @since 5.3.2
*/
public Builder handshakeTimeout(Duration timeout) {

LettuceAssert.notNull(timeout, "SSL Handshake Timeout must not be null");

this.sslHandshakeTimeout = timeout;
return this;
}

/**
* Sets the KeyStore type. Defaults to {@link KeyStore#getDefaultType()} if not set.
*
Expand Down Expand Up @@ -585,18 +601,6 @@ public Builder sslParameters(Supplier<SSLParameters> sslParametersSupplier) {
return this;
}

/**
* Sets a timeout for the SSL handshake.
*
* @param timeout {@link Duration}.
* @return {@code this}
*/
public Builder sslHandshakeTimeout(Duration timeout) {

this.sslHandshakeTimeout = timeout;
return this;
}

/**
* Create a new instance of {@link SslOptions}
*
Expand Down Expand Up @@ -681,13 +685,12 @@ public SslOptions.Builder mutate() {
builder.sslParametersSupplier = this.sslParametersSupplier;
builder.keymanager = this.keymanager;
builder.trustmanager = this.trustmanager;
builder.sslHandshakeTimeout = this.sslHandshakeTimeout;
builder.sslHandshakeTimeout = this.handshakeTimeout;

return builder;
}

/**
*
* @return the configured {@link SslProvider}.
*/
@Deprecated
Expand All @@ -696,7 +699,6 @@ public SslProvider getSslProvider() {
}

/**
*
* @return the keystore {@link URL}.
* @deprecated since 5.3, {@link javax.net.ssl.KeyManager} is configured via {@link #createSslContextBuilder()}.
*/
Expand All @@ -706,23 +708,28 @@ public URL getKeystore() {
}

/**
*
* @return the set of protocols.
*/
public String[] getProtocols() {
return protocols;
}

/**
*
* @return the set of cipher suites.
*/
public String[] getCipherSuites() {
return cipherSuites;
}

/**
*
* @return the SSL handshake timeout
* @since 5.3.2
*/
public Duration getHandshakeTimeout() {
return handshakeTimeout;
}

/**
* @return the password for the keystore. May be empty.
* @deprecated since 5.3, {@link javax.net.ssl.KeyManager} is configured via {@link #createSslContextBuilder()}.
*/
Expand All @@ -732,7 +739,6 @@ public char[] getKeystorePassword() {
}

/**
*
* @return the truststore {@link URL}.
* @deprecated since 5.3, {@link javax.net.ssl.TrustManager} is configured via {@link #createSslContextBuilder()}.
*/
Expand All @@ -751,13 +757,6 @@ public char[] getTruststorePassword() {
return Arrays.copyOf(truststorePassword, truststorePassword.length);
}

/**
* @return the SSL handshake timeout
*/
public Duration getSslHandshakeTimeout() {
return sslHandshakeTimeout;
}

private static KeyManagerFactory createKeyManagerFactory(InputStream inputStream, char[] storePassword, String keyStoreType)
throws GeneralSecurityException, IOException {

Expand Down Expand Up @@ -827,7 +826,7 @@ public interface Resource {
* @param url the URL to obtain the {@link InputStream} from.
* @return a {@link Resource} that opens a connection to the URL and obtains the {@link InputStream} for it.
*/
public static Resource from(URL url) {
static Resource from(URL url) {

LettuceAssert.notNull(url, "URL must not be null");

Expand All @@ -840,7 +839,7 @@ public static Resource from(URL url) {
* @param file the File to obtain the {@link InputStream} from.
* @return a {@link Resource} that obtains the {@link FileInputStream} for the given {@link File}.
*/
public static Resource from(File file) {
static Resource from(File file) {

LettuceAssert.notNull(file, "File must not be null");

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/io/lettuce/core/SslOptionsUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import java.util.Collections;

import javax.net.ssl.SSLParameters;
Expand All @@ -41,6 +42,14 @@ void shouldCreateEmptySslOptions() throws Exception {
assertThat(options.createSslContextBuilder()).isNotNull();
}

@Test
void shouldConfigureSslHandshakeTimeout() {

SslOptions options = SslOptions.builder().handshakeTimeout(Duration.ofSeconds(1)).build();

assertThat(options.getHandshakeTimeout()).isEqualTo(Duration.ofSeconds(1));
}

@Test
void shouldConfigureCipherSuiteAndProtocol() {

Expand Down

0 comments on commit 2d55c12

Please sign in to comment.