Commit 5acd115c authored by Madhura Bhave's avatar Madhura Bhave

Add redis sentinel password property

Closes gh-21353
parent 038ae934
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -82,6 +82,9 @@ abstract class RedisConnectionConfiguration {
if (this.properties.getPassword() != null) {
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
if (sentinelProperties.getPassword() != null) {
config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
}
config.setDatabase(this.properties.getDatabase());
return config;
}
......
......@@ -301,6 +301,11 @@ public class RedisProperties {
*/
private List<String> nodes;
/**
* Password for authenticating with sentinel(s).
*/
private String password;
public String getMaster() {
return this.master;
}
......@@ -317,6 +322,14 @@ public class RedisProperties {
this.nodes = nodes;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
......
......@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
......@@ -199,17 +200,34 @@ class RedisAutoConfigurationTests {
}
@Test
void testRedisConfigurationWithSentinelAndPassword() {
void testRedisConfigurationWithSentinelAndDataNodePassword() {
this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
assertThat(connectionFactory.getPassword()).isEqualTo("password");
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
assertThat(sentinelConfiguration.getSentinelPassword().isPresent()).isFalse();
Set<RedisNode> sentinels = connectionFactory.getSentinelConfiguration().getSentinels();
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
.contains("127.0.0.1:26379", "127.0.0.1:26380");
});
}
@Test
void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() {
this.contextRunner.withPropertyValues("spring.redis.password=password", "spring.redis.sentinel.password=secret",
"spring.redis.sentinel.master:mymaster",
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
assertThat(connectionFactory.getPassword()).isEqualTo("password");
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
.contains("127.0.0.1:26379", "127.0.0.1:26380");
});
}
@Test
void testRedisConfigurationWithCluster() {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
......
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