Skip to content
Open
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 @@ -23,7 +23,7 @@
<beanutils>1.11.0</beanutils>
<xstream>1.4.21</xstream>
<pool>2.11.1</pool>
<lettuce>6.8.1.RELEASE</lettuce>
<lettuce>7.0.0.RELEASE</lettuce>
<jedis>7.0.0</jedis>
<multithreadedtc>1.01</multithreadedtc>
<netty>4.2.3.Final</netty>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/**
* @author Christoph Strobl
* @author Mark Paluch
* @author Mingyuan Wu
* @since 2.0
*/
@NullUnmarked
Expand Down Expand Up @@ -108,7 +109,7 @@ public Boolean move(byte @NonNull [] key, int dbIndex) {
Assert.notNull(pattern, "Pattern must not be null");

return LettuceConverters.toBytesSet(connection.getClusterCommandExecutor()
.executeCommandOnSingleNode((LettuceClusterCommandCallback<List<byte[]>>) client -> client.keys(pattern), node)
.executeCommandOnSingleNode((LettuceClusterCommandCallback<List<byte[]>>) client -> client.keysLegacy(pattern), node)
.getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.springframework.data.redis.domain.geo.GeoReference.*;

import io.lettuce.core.*;
import io.lettuce.core.RedisCredentialsProvider.ImmediateRedisCredentialsProvider;
import io.lettuce.core.cluster.models.partitions.Partitions;
import io.lettuce.core.cluster.models.partitions.RedisClusterNode.NodeFlag;

Expand Down Expand Up @@ -87,6 +88,7 @@
* @author Vikas Garg
* @author John Blum
* @author Roman Osadchuk
* @author Mingyuan Wu
*/
@SuppressWarnings("ConstantConditions")
public abstract class LettuceConverters extends Converters {
Expand Down Expand Up @@ -452,8 +454,9 @@ static RedisSentinelConfiguration createRedisSentinelConfiguration(RedisURI redi

RedisNode sentinelNode = new RedisNode(sentinelNodeRedisUri.getHost(), sentinelNodeRedisUri.getPort());

if (sentinelNodeRedisUri.getPassword() != null) {
sentinelConfiguration.setSentinelPassword(sentinelNodeRedisUri.getPassword());
RedisCredentials credential;
if( (credential = getRedisCredential(redisURI)) != null && credential.getPassword() != null) {
sentinelConfiguration.setSentinelPassword(credential.getPassword());
}

sentinelConfiguration.addSentinel(sentinelNode);
Expand All @@ -466,15 +469,35 @@ static RedisSentinelConfiguration createRedisSentinelConfiguration(RedisURI redi

private static void applyAuthentication(RedisURI redisURI, RedisConfiguration.WithAuthentication redisConfiguration) {

if (StringUtils.hasText(redisURI.getUsername())) {
redisConfiguration.setUsername(redisURI.getUsername());
}
RedisCredentials credential;
if( (credential = getRedisCredential(redisURI)) != null) {
if (StringUtils.hasText(credential.getUsername())) {
redisConfiguration.setUsername(credential.getUsername());
}

if (redisURI.getPassword() != null) {
redisConfiguration.setPassword(redisURI.getPassword());
}
if (credential.getPassword() != null) {
redisConfiguration.setPassword(credential.getPassword());
}
}
}

public static RedisCredentials getRedisCredential(RedisURI redisURI) {

if (redisURI != null) {
RedisCredentialsProvider credentialsProvider = redisURI.getCredentialsProvider();
if (credentialsProvider != null) {
RedisCredentials credential;
if(credentialsProvider instanceof ImmediateRedisCredentialsProvider immediateCredentialsProvider) {
credential = immediateCredentialsProvider.resolveCredentialsNow();
}else {
credential = credentialsProvider.resolveCredentials().block();
}
return credential;
}
}
return null;
}

@Contract("null -> null;!null -> !null")
public static byte @Nullable [] toBytes(@Nullable String source) {
return source != null ? source.getBytes() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author ihaohong
* @author Mingyuan Wu
* @since 2.0
*/
@NullUnmarked
Expand Down Expand Up @@ -126,7 +127,7 @@ public Long touch(byte @NonNull [] @NonNull... keys) {

Assert.notNull(pattern, "Pattern must not be null");

return connection.invoke().fromMany(RedisKeyAsyncCommands::keys, pattern).toSet();
return connection.invoke().fromMany(RedisKeyAsyncCommands::keysLegacy, pattern).toSet();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/**
* @author Christoph Strobl
* @author Mark Paluch
* @author Mingyuan Wu
* @since 2.0
*/
class LettuceReactiveClusterKeyCommands extends LettuceReactiveKeyCommands implements ReactiveClusterKeyCommands {
Expand All @@ -62,7 +63,7 @@ public Mono<List<ByteBuffer>> keys(RedisClusterNode node, ByteBuffer pattern) {

Assert.notNull(pattern, "Pattern must not be null");

return cmd.keys(pattern).collectList();
return cmd.keysLegacy(pattern).collectList();
}).next();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Dahye Anne Lee
* @author Mingyuan Wu
* @since 2.0
*/
class LettuceReactiveKeyCommands implements ReactiveKeyCommands {
Expand Down Expand Up @@ -125,7 +126,7 @@ public Flux<MultiValueResponse<ByteBuffer, ByteBuffer>> keys(Publisher<ByteBuffe

Assert.notNull(pattern, "Pattern must not be null");
// TODO: stream elements instead of collection
return cmd.keys(pattern).collectList().map(value -> new MultiValueResponse<>(pattern, value));
return cmd.keysLegacy(pattern).collectList().map(value -> new MultiValueResponse<>(pattern, value));
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
/**
* @author Christoph Strobl
* @author Mark Paluch
* @author Mingyuan Wu
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -196,15 +197,15 @@ void isClosedShouldReturnConnectionStateCorrectly() {
@Test // DATAREDIS-315
void keysShouldOnlyBeRunOnDedicatedNodeWhenPinned() {

when(clusterConnection2Mock.keys(any(byte[].class))).thenReturn(Collections.<byte[]> emptyList());
when(clusterConnection2Mock.keysLegacy(any(byte[].class))).thenReturn(Collections.<byte[]> emptyList());

byte[] pattern = LettuceConverters.toBytes("*");

connection.keys(CLUSTER_NODE_2, pattern);

verify(clusterConnection1Mock, never()).keys(pattern);
verify(clusterConnection2Mock, times(1)).keys(pattern);
verify(clusterConnection3Mock, never()).keys(pattern);
verify(clusterConnection1Mock, never()).keysLegacy(pattern);
verify(clusterConnection2Mock, times(1)).keysLegacy(pattern);
verify(clusterConnection3Mock, never()).keysLegacy(pattern);
}

@Test // DATAREDIS-315
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import static org.springframework.data.redis.connection.RedisConfiguration.*;
import static org.springframework.data.redis.test.extension.LettuceTestClientResources.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import static org.springframework.data.redis.connection.lettuce.LettuceConverters.getRedisCredential;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisCredentials;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SslVerifyMode;
Expand Down Expand Up @@ -78,6 +80,7 @@
* @author Chris Bono
* @author John Blum
* @author Zhian Chen
* @author Mingyuan Wu
*/
class LettuceConnectionFactoryUnitTests {

Expand Down Expand Up @@ -186,7 +189,9 @@ void passwordShouldBeSetCorrectlyOnClusterClient() {
Iterable<RedisURI> initialUris = (Iterable<RedisURI>) getField(client, "initialUris");

for (RedisURI uri : initialUris) {
assertThat(uri.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());
RedisCredentials credential = getRedisCredential(uri);
assertThat(credential).isNotNull();
assertThat(credential.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());
}
}

Expand Down Expand Up @@ -257,10 +262,14 @@ void passwordShouldNotBeSetOnSentinelClient() {

RedisURI redisUri = (RedisURI) getField(client, "redisURI");

assertThat(redisUri.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());
RedisCredentials credential = getRedisCredential(redisUri);
assertThat(credential).isNotNull();
assertThat(credential.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());

for (RedisURI sentinel : redisUri.getSentinels()) {
assertThat(sentinel.getPassword()).isNull();
RedisCredentials sentinelCredential = getRedisCredential(sentinel);
assertThat(sentinelCredential).isNotNull();
assertThat(sentinelCredential.getPassword()).isNull();
}
}

Expand All @@ -281,10 +290,14 @@ void sentinelPasswordShouldBeSetOnSentinelClient() {

RedisURI redisUri = (RedisURI) getField(client, "redisURI");

assertThat(redisUri.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());
RedisCredentials credential = getRedisCredential(redisUri);
assertThat(credential).isNotNull();
assertThat(credential.getPassword()).isEqualTo(connectionFactory.getPassword().toCharArray());

for (RedisURI sentinel : redisUri.getSentinels()) {
assertThat(sentinel.getPassword()).isEqualTo("sentinel-pwd".toCharArray());
RedisCredentials sentinelCredentials = getRedisCredential(sentinel);
assertThat(sentinelCredentials).isNotNull();
assertThat(sentinelCredentials.getPassword()).isEqualTo("sentinel-pwd".toCharArray());
}
}

Expand Down Expand Up @@ -338,10 +351,14 @@ void sentinelPasswordShouldNotLeakIntoDataNodeClient() {

RedisURI redisUri = (RedisURI) getField(client, "redisURI");

assertThat(redisUri.getPassword()).isNull();
RedisCredentials credential = getRedisCredential(redisUri);
assertThat(credential).isNotNull();
assertThat(credential.getPassword()).isNull();

for (RedisURI sentinel : redisUri.getSentinels()) {
assertThat(sentinel.getPassword()).isEqualTo("sentinel-pwd".toCharArray());
RedisCredentials sentinelCredentials = getRedisCredential(sentinel);
assertThat(sentinelCredentials).isNotNull();
assertThat(sentinelCredentials.getPassword()).isEqualTo("sentinel-pwd".toCharArray());
}
}

Expand Down Expand Up @@ -1224,7 +1241,7 @@ void createFullRedisSentinelConfiguration() {
.create("redis-sentinel://fooUser:fooPass@myserver1:111,myserver2:222/7?sentinelMasterId=5150");
// Set the passwords directly on the sentinels so that it gets picked up by converter
char[] sentinelPass = "changeme".toCharArray();
redisURI.getSentinels().forEach(sentinelRedisUri -> sentinelRedisUri.setPassword(sentinelPass));
redisURI.getSentinels().forEach(sentinelRedisUri -> sentinelRedisUri.setAuthentication(sentinelPass));

RedisSentinelConfiguration expected = new RedisSentinelConfiguration();
expected.setMaster("5150");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
import static org.springframework.data.redis.connection.lettuce.LettuceCommandArgsComparator.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import static org.springframework.data.redis.connection.lettuce.LettuceConverters.getRedisCredential;

import io.lettuce.core.GetExArgs;
import io.lettuce.core.Limit;
import io.lettuce.core.RedisCredentials;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SetArgs;
import io.lettuce.core.cluster.models.partitions.Partitions;
Expand Down Expand Up @@ -52,6 +54,7 @@
*
* @author Christoph Strobl
* @author Vikas Garg
* @author Mingyuan Wu
*/
class LettuceConvertersUnitTests {

Expand Down Expand Up @@ -277,12 +280,16 @@ void sentinelConfigurationWithAuth() {

RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);

assertThat(redisURI.getUsername()).isEqualTo("app");
assertThat(redisURI.getPassword()).isEqualTo(dataPassword.get());
RedisCredentials credential = getRedisCredential(redisURI);
assertThat(credential).isNotNull();
assertThat(credential.getUsername()).isEqualTo("app");
assertThat(credential.getPassword()).isEqualTo(dataPassword.get());

redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isEqualTo("admin");
assertThat(sentinel.getPassword()).isEqualTo(sentinelPassword.get());
RedisCredentials sentinelCredential = getRedisCredential(sentinel);
assertThat(sentinelCredential).isNotNull();
assertThat(sentinelCredential.getUsername()).isEqualTo("admin");
assertThat(sentinelCredential.getPassword()).isEqualTo(sentinelPassword.get());
});
}

Expand All @@ -299,11 +306,15 @@ void sentinelConfigurationSetSentinelPasswordIfUsernameNotPresent() {

RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);

assertThat(redisURI.getUsername()).isEqualTo("app");
RedisCredentials credential = getRedisCredential(redisURI);
assertThat(credential).isNotNull();
assertThat(credential.getUsername()).isEqualTo("app");

redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isNull();
assertThat(sentinel.getPassword()).isNotNull();
RedisCredentials sentinelCredential = getRedisCredential(sentinel);
assertThat(sentinelCredential).isNotNull();
assertThat(sentinelCredential.getUsername()).isNull();
assertThat(sentinelCredential.getPassword()).isNotNull();
});
}

Expand All @@ -320,11 +331,15 @@ void sentinelConfigurationShouldNotSetSentinelAuthIfUsernameIsPresentWithNoPassw

RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);

assertThat(redisURI.getUsername()).isEqualTo("app");
RedisCredentials credential = getRedisCredential(redisURI);
assertThat(credential).isNotNull();
assertThat(credential.getUsername()).isEqualTo("app");

redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isNull();
assertThat(sentinel.getPassword()).isNull();
RedisCredentials sentinelCredential = getRedisCredential(sentinel);
assertThat(sentinelCredential).isNotNull();
assertThat(sentinelCredential.getUsername()).isNull();
assertThat(sentinelCredential.getPassword()).isNull();
});
}

Expand Down