Skip to content

DATAREDIS-974 - Apply SSL configuration when creating Jedis Cluster. #472

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAREDIS-975-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
* Connection factory creating <a href="https://github.com/xetorthio/jedis">Jedis</a> based connections.
Expand Down Expand Up @@ -425,12 +424,10 @@ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig, Ge

int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects() : 5;

int connectTimeout = getConnectTimeout();
int readTimeout = getReadTimeout();

return StringUtils.hasText(getPassword())
? new JedisCluster(hostAndPort, connectTimeout, readTimeout, redirects, getPassword(), poolConfig)
: new JedisCluster(hostAndPort, connectTimeout, readTimeout, redirects, poolConfig);
return new JedisCluster(hostAndPort, getConnectTimeout(), getReadTimeout(), redirects, getPassword(),
clientConfiguration.getClientName().orElse(null), poolConfig, isUseSsl(),
clientConfiguration.getSslSocketFactory().orElse(null), clientConfiguration.getSslParameters().orElse(null),
clientConfiguration.getHostnameVerifier().orElse(null), null);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.mockito.Mockito.*;

import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisClusterConnectionHandler;
import redis.clients.jedis.JedisClusterInfoCache;
import redis.clients.jedis.JedisPoolConfig;
import sun.net.www.protocol.https.DefaultHostnameVerifier;

Expand All @@ -27,13 +29,15 @@
import java.time.Duration;
import java.time.temporal.ChronoUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
Expand Down Expand Up @@ -287,6 +291,49 @@ public void shouldReturnClusterConfiguration() {
assertThat(connectionFactory.getClusterConfiguration()).isSameAs(configuration);
}

@Test // DATAREDIS-975
public void shouldApplySslConfigWhenCreatingClusterClient() throws NoSuchAlgorithmException {

SSLParameters sslParameters = new SSLParameters();
SSLContext context = SSLContext.getDefault();
SSLSocketFactory socketFactory = context.getSocketFactory();
JedisPoolConfig poolConfig = new JedisPoolConfig();
HostnameVerifier hostNameVerifier = new DefaultHostnameVerifier();

JedisClientConfiguration configuration = JedisClientConfiguration.builder() //
.useSsl() //
.hostnameVerifier(hostNameVerifier) //
.sslParameters(sslParameters) //
.sslSocketFactory(socketFactory).and() //
.clientName("my-client") //
.connectTimeout(Duration.ofMinutes(1)) //
.readTimeout(Duration.ofMinutes(5)) //
.usePooling().poolConfig(poolConfig) //
.build();

connectionFactory = new JedisConnectionFactory(new RedisClusterConfiguration(), configuration);
connectionFactory.afterPropertiesSet();

RedisClusterConnection connection = connectionFactory.getClusterConnection();
assertThat(connection).isInstanceOf(JedisClusterConnection.class);

JedisCluster cluster = ((JedisClusterConnection) connection).getCluster();

JedisClusterConnectionHandler connectionHandler = (JedisClusterConnectionHandler) ReflectionTestUtils
.getField(cluster, "connectionHandler");
JedisClusterInfoCache cache = (JedisClusterInfoCache) ReflectionTestUtils.getField(connectionHandler, "cache");

assertThat(ReflectionTestUtils.getField(cache, "connectionTimeout")).isEqualTo(60000);
assertThat(ReflectionTestUtils.getField(cache, "soTimeout")).isEqualTo(300000);
assertThat(ReflectionTestUtils.getField(cache, "password")).isNull();
assertThat(ReflectionTestUtils.getField(cache, "clientName")).isEqualTo("my-client");
assertThat(ReflectionTestUtils.getField(cache, "ssl")).isEqualTo(true);
assertThat(ReflectionTestUtils.getField(cache, "sslSocketFactory")).isEqualTo(socketFactory);
assertThat(ReflectionTestUtils.getField(cache, "sslParameters")).isEqualTo(sslParameters);
assertThat(ReflectionTestUtils.getField(cache, "hostnameVerifier")).isEqualTo(hostNameVerifier);
assertThat(ReflectionTestUtils.getField(cache, "hostAndPortMap")).isNull();
}

@Test(expected = IllegalStateException.class) // DATAREDIS-574
public void shouldDenyChangesToImmutableClientConfiguration() throws NoSuchAlgorithmException {

Expand Down