DATAREDIS-524 - Use password to connect Redis nodes using Sentinel.

LettuceConnectionFactory and DefaultLettucePool now use the configured password to authenticate with Redis. Authentication is applied when connecting to nodes that were obtained from Redis Sentinel.

Original Pull Request: #204
This commit is contained in:
Mark Paluch
2016-06-22 09:35:07 +02:00
committed by Christoph Strobl
parent 74a5652ff0
commit ea33cd40e6
4 changed files with 61 additions and 2 deletions

View File

@@ -15,8 +15,11 @@
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
@@ -26,9 +29,11 @@ import org.junit.Test;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.PoolConfig;
import org.springframework.data.redis.connection.PoolException;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import com.lambdaworks.redis.RedisAsyncConnection;
import com.lambdaworks.redis.RedisException;
import com.lambdaworks.redis.RedisURI;
/**
* Unit test of {@link DefaultLettucePool}
@@ -189,6 +194,22 @@ public class DefaultLettucePoolTests {
pool.getResource();
}
/**
* @see DATAREDIS-524
*/
@Test
public void testCreateSentinelWithPassword() {
pool = new DefaultLettucePool(new RedisSentinelConfiguration("mymaster", Collections.singleton("host:1234")));
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setPassword("foo");
pool.afterPropertiesSet();
RedisURI redisURI = (RedisURI) getField(pool.getClient(), "redisURI");
assertThat(redisURI.getPassword(), is(equalTo(pool.getPassword().toCharArray())));
}
/**
* @see DATAREDIS-462
*/

View File

@@ -21,6 +21,7 @@ import static org.hamcrest.core.IsInstanceOf.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.After;
@@ -28,6 +29,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import com.lambdaworks.redis.AbstractRedisClient;
import com.lambdaworks.redis.RedisClient;
@@ -114,6 +116,28 @@ public class LettuceConnectionFactoryUnitTests {
}
}
/**
* @see DATAREDIS-524
*/
@Test
@SuppressWarnings("unchecked")
public void passwordShouldBeSetCorrectlyOnSentinelClient() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(
new RedisSentinelConfiguration("mymaster", Collections.singleton("host:1234")));
connectionFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
connectionFactory.setPassword("o_O");
connectionFactory.afterPropertiesSet();
ConnectionFactoryTracker.add(connectionFactory);
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
assertThat(client, instanceOf(RedisClient.class));
RedisURI redisURI = (RedisURI) getField(client, "redisURI");
assertThat(redisURI.getPassword(), is(equalTo(connectionFactory.getPassword().toCharArray())));
}
/**
* @see DATAREDIS-462
*/