Commit 557361c9 authored by Stephane Nicoll's avatar Stephane Nicoll

Properly set redis password for Cluster and Sentinel config

Closes gh-9583
parent 1a839d66
...@@ -79,6 +79,9 @@ abstract class RedisConnectionConfiguration { ...@@ -79,6 +79,9 @@ abstract class RedisConnectionConfiguration {
RedisSentinelConfiguration config = new RedisSentinelConfiguration(); RedisSentinelConfiguration config = new RedisSentinelConfiguration();
config.master(sentinelProperties.getMaster()); config.master(sentinelProperties.getMaster());
config.setSentinels(createSentinels(sentinelProperties)); config.setSentinels(createSentinels(sentinelProperties));
if (this.properties.getPassword() != null) {
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
return config; return config;
} }
return null; return null;
...@@ -101,6 +104,9 @@ abstract class RedisConnectionConfiguration { ...@@ -101,6 +104,9 @@ abstract class RedisConnectionConfiguration {
if (clusterProperties.getMaxRedirects() != null) { if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects()); config.setMaxRedirects(clusterProperties.getMaxRedirects());
} }
if (this.properties.getPassword() != null) {
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
return config; return config;
} }
......
...@@ -125,6 +125,16 @@ public class RedisAutoConfigurationJedisTests { ...@@ -125,6 +125,16 @@ public class RedisAutoConfigurationJedisTests {
.isRedisSentinelAware()).isTrue(); .isRedisSentinelAware()).isTrue();
} }
@Test
public void testRedisConfigurationWithSentinelAndPassword() {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
load("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:" + StringUtils
.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(JedisConnectionFactory.class)
.getPassword()).isEqualTo("password");
}
@Test @Test
public void testRedisConfigurationWithCluster() throws Exception { public void testRedisConfigurationWithCluster() throws Exception {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380"); List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
......
...@@ -148,6 +148,16 @@ public class RedisAutoConfigurationTests { ...@@ -148,6 +148,16 @@ public class RedisAutoConfigurationTests {
.isRedisSentinelAware()).isTrue(); .isRedisSentinelAware()).isTrue();
} }
@Test
public void testRedisConfigurationWithSentinelAndPassword() throws Exception {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
load("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.getPassword()).isEqualTo("password");
}
@Test @Test
public void testRedisConfigurationWithCluster() throws Exception { public void testRedisConfigurationWithCluster() throws Exception {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380"); List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
...@@ -157,6 +167,16 @@ public class RedisAutoConfigurationTests { ...@@ -157,6 +167,16 @@ public class RedisAutoConfigurationTests {
.getClusterConnection()).isNotNull(); .getClusterConnection()).isNotNull();
} }
@Test
public void testRedisConfigurationWithClusterAndPassword() throws Exception {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
load("spring.redis.password=password",
"spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.getPassword()).isEqualTo("password");
}
private DefaultLettucePool getDefaultLettucePool(LettuceConnectionFactory factory) { private DefaultLettucePool getDefaultLettucePool(LettuceConnectionFactory factory) {
return (DefaultLettucePool) ReflectionTestUtils.getField(factory, "pool"); return (DefaultLettucePool) ReflectionTestUtils.getField(factory, "pool");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment