Skip to content

Commit 5acd115

Browse files
committed
Add redis sentinel password property
Closes gh-21353
1 parent 038ae93 commit 5acd115

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,6 +82,9 @@ protected final RedisSentinelConfiguration getSentinelConfig() {
8282
if (this.properties.getPassword() != null) {
8383
config.setPassword(RedisPassword.of(this.properties.getPassword()));
8484
}
85+
if (sentinelProperties.getPassword() != null) {
86+
config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
87+
}
8588
config.setDatabase(this.properties.getDatabase());
8689
return config;
8790
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ public static class Sentinel {
301301
*/
302302
private List<String> nodes;
303303

304+
/**
305+
* Password for authenticating with sentinel(s).
306+
*/
307+
private String password;
308+
304309
public String getMaster() {
305310
return this.master;
306311
}
@@ -317,6 +322,14 @@ public void setNodes(List<String> nodes) {
317322
this.nodes = nodes;
318323
}
319324

325+
public String getPassword() {
326+
return this.password;
327+
}
328+
329+
public void setPassword(String password) {
330+
this.password = password;
331+
}
332+
320333
}
321334

322335
/**

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.context.annotation.Configuration;
3838
import org.springframework.data.redis.connection.RedisClusterConfiguration;
3939
import org.springframework.data.redis.connection.RedisNode;
40+
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
4041
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
4142
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder;
4243
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@@ -199,17 +200,34 @@ void testRedisConfigurationWithSentinelAndDatabase() {
199200
}
200201

201202
@Test
202-
void testRedisConfigurationWithSentinelAndPassword() {
203+
void testRedisConfigurationWithSentinelAndDataNodePassword() {
203204
this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
204205
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
205206
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
206207
assertThat(connectionFactory.getPassword()).isEqualTo("password");
208+
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
209+
assertThat(sentinelConfiguration.getSentinelPassword().isPresent()).isFalse();
207210
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration().getSentinels();
208211
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
209212
.contains("127.0.0.1:26379", "127.0.0.1:26380");
210213
});
211214
}
212215

216+
@Test
217+
void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() {
218+
this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret",
219+
"spring.redis.sentinel.master:mymaster",
220+
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
221+
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
222+
assertThat(connectionFactory.getPassword()).isEqualTo("password");
223+
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
224+
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
225+
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
226+
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
227+
.contains("127.0.0.1:26379", "127.0.0.1:26380");
228+
});
229+
}
230+
213231
@Test
214232
void testRedisConfigurationWithCluster() {
215233
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");

0 commit comments

Comments
 (0)