Support sentinel username in SentinelConfiguration.

Closes #2218
Original pull request: #2224.
This commit is contained in:
Vikas Garg
2022-01-04 16:22:12 -05:00
committed by Mark Paluch
parent be1663c95e
commit f333b184a5
5 changed files with 143 additions and 11 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
*
* @author Christoph Strobl
* @author Luis De Bello
* @author Vikas Garg
* @since 2.1
*/
public interface RedisConfiguration {
@@ -438,6 +439,21 @@ public interface RedisConfiguration {
* @since 2.2.2
*/
RedisPassword getSentinelPassword();
/**
* Create and set a username with the given {@link String}. Requires Redis 6 or newer.
*
* @param sentinelUsername the username for sentinel.
*/
void setSentinelUsername(@Nullable String sentinelUsername);
/**
* Get the username to use when connecting.
*
* @return {@literal null} if none set.
*/
@Nullable
String getSentinelUsername();
}
/**

View File

@@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
* @author Vikas Garg
* @since 1.4
*/
public class RedisSentinelConfiguration implements RedisConfiguration, SentinelConfiguration {
@@ -46,12 +47,14 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
private static final String REDIS_SENTINEL_MASTER_CONFIG_PROPERTY = "spring.redis.sentinel.master";
private static final String REDIS_SENTINEL_NODES_CONFIG_PROPERTY = "spring.redis.sentinel.nodes";
private static final String REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY = "spring.redis.sentinel.password";
private static final String REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY = "spring.redis.sentinel.username";
private @Nullable NamedNode master;
private Set<RedisNode> sentinels;
private int database;
private @Nullable String dataNodeUsername = null;
private @Nullable String sentinelUsername = null;
private RedisPassword dataNodePassword = RedisPassword.none();
private RedisPassword sentinelPassword = RedisPassword.none();
@@ -107,6 +110,10 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
if (propertySource.containsProperty(REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY)) {
this.setSentinelPassword(propertySource.getProperty(REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY).toString());
}
if (propertySource.containsProperty(REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY)) {
this.setSentinelUsername(propertySource.getProperty(REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY).toString());
}
}
/**
@@ -270,6 +277,25 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
this.dataNodePassword = password;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisConfiguration.SentinelConfiguration#getSentinelUsername()
*/
@Nullable
@Override
public String getSentinelUsername() {
return this.sentinelUsername;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisConfiguration.SentinelConfiguration#setSentinelUsername(String)
*/
@Override
public void setSentinelUsername(@Nullable String sentinelUsername) {
this.sentinelUsername = sentinelUsername;
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisConfiguration.SentinelConfiguration#setSentinelPassword(org.springframework.data.redis.connection.RedisPassword)

View File

@@ -82,6 +82,7 @@ import org.springframework.util.StringUtils;
* @author Ninad Divadkar
* @author dengliming
* @author Chris Bono
* @author Vikas Garg
*/
public abstract class LettuceConverters extends Converters {
@@ -512,7 +513,13 @@ public abstract class LettuceConverters extends Converters {
RedisURI.Builder sentinelBuilder = RedisURI.Builder.redis(sentinel.getHost(), sentinel.getPort());
sentinelPassword.toOptional().ifPresent(sentinelBuilder::withPassword);
String sentinelUsername = sentinelConfiguration.getSentinelUsername();
if (StringUtils.hasText(sentinelUsername) && sentinelPassword.isPresent()) {
// See https://github.com/lettuce-io/lettuce-core/issues/1404
sentinelBuilder.withAuthentication(sentinelUsername, new String(sentinelPassword.toOptional().orElse((new char[0]))));
} else {
sentinelPassword.toOptional().ifPresent(sentinelBuilder::withPassword);
}
builder.withSentinel(sentinelBuilder.build());
}