Polishing.

Reformat code. Consider sentinel username in JedisConnectionFactory. Update years in license headers.

See #2218
Original pull request: #2224.
This commit is contained in:
Mark Paluch
2022-01-07 11:04:58 +01:00
parent f333b184a5
commit d0f17b167e
10 changed files with 95 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-2022 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.
@@ -23,11 +23,12 @@ import java.util.HashSet;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.util.StringUtils;
/**
* Unit tests for {@link RedisSentinelConfiguration}.
*
* @author Christoph Strobl
* @author Vikas Garg
*/
@@ -134,13 +135,14 @@ class RedisSentinelConfigurationUnitTests {
assertThat(configuration.getSentinelPassword()).isEqualTo(RedisPassword.none());
}
@Test
@Test // GH-2218
void dataNodeUsernameDoesNotAffectSentinelUsername() {
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration("myMaster",
Collections.singleton(HOST_AND_PORT_1));
configuration.setUsername("data-admin");
configuration.setSentinelUsername("sentinel-admin");
configuration.setUsername("app");
assertThat(configuration.getDataNodeUsername()).isEqualTo("data-admin");
assertThat(configuration.getSentinelUsername()).isEqualTo("sentinel-admin");
}
@@ -158,17 +160,19 @@ class RedisSentinelConfigurationUnitTests {
assertThat(config.getSentinels()).hasSize(1).contains(new RedisNode("127.0.0.1", 123));
}
@Test
@Test // GH-2218
void readSentinelUsernameFromConfigProperty() {
MockPropertySource propertySource = new MockPropertySource();
propertySource.setProperty("spring.redis.sentinel.master", "myMaster");
propertySource.setProperty("spring.redis.sentinel.nodes", HOST_AND_PORT_1);
propertySource.setProperty("spring.redis.sentinel.username", "sentinel-admin");
propertySource.setProperty("spring.redis.sentinel.password", "foo");
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
assertThat(config.getSentinelUsername()).isEqualTo(RedisPassword.of("sentinel-admin"));
assertThat(config.getSentinelUsername()).isEqualTo("sentinel-admin");
assertThat(config.getSentinelPassword()).isEqualTo(RedisPassword.of("foo"));
assertThat(config.getSentinels()).hasSize(1).contains(new RedisNode("127.0.0.1", 123));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -282,6 +282,20 @@ class JedisConnectionFactoryUnitTests {
assertThat(connectionFactory.getClusterConfiguration()).isNull();
}
@Test // GH-2218
void shouldConsiderSentinelAuthentication() {
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
configuration.setSentinelUsername("sentinel");
configuration.setSentinelPassword("the-password");
connectionFactory = new JedisConnectionFactory(configuration, JedisClientConfiguration.defaultConfiguration());
JedisClientConfig clientConfig = connectionFactory.createSentinelClientConfig(configuration);
assertThat(clientConfig.getUser()).isEqualTo("sentinel");
assertThat(clientConfig.getPassword()).isEqualTo("the-password");
}
@Test // DATAREDIS-574
void shouldReturnClusterConfiguration() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -45,6 +45,8 @@ import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.core.types.RedisClientInfo;
/**
* Unit tests for {@link LettuceConverters}.
*
* @author Christoph Strobl
* @author Vikas Garg
*/
@@ -256,54 +258,73 @@ class LettuceConvertersUnitTests {
.isEqualTo(new GetExArgs().pxAt(10));
}
@Test
@Test // GH-2218
void sentinelConfigurationWithAuth() {
RedisPassword password = RedisPassword.of("88888888-8x8-getting-creative-now");
RedisPassword dataPassword = RedisPassword.of("data-secret");
RedisPassword sentinelPassword = RedisPassword.of("sentinel-secret");
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration()
.master(MASTER_NAME)
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
sentinelConfiguration.setSentinelUsername("admin");
sentinelConfiguration.setSentinelPassword(password);
sentinelConfiguration.setUsername("app");
sentinelConfiguration.setPassword(password);
sentinelConfiguration.setPassword(dataPassword);
sentinelConfiguration.setSentinelUsername("admin");
sentinelConfiguration.setSentinelPassword(sentinelPassword);
RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
assertThat(redisURI.getUsername()).isEqualTo("app");
assertThat(redisURI.getPassword()).isEqualTo(dataPassword.get());
redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isEqualTo("admin");
assertThat(sentinel.getPassword()).isEqualTo(sentinelPassword.get());
});
}
@Test
@Test // GH-2218
void sentinelConfigurationSetSentinelPasswordIfUsernameNotPresent() {
RedisPassword password = RedisPassword.of("88888888-8x8-getting-creative-now");
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration()
.master(MASTER_NAME)
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
sentinelConfiguration.setSentinelPassword(password);
sentinelConfiguration.setUsername("app");
sentinelConfiguration.setPassword(password);
sentinelConfiguration.setSentinelPassword(password);
RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
assertThat(redisURI.getUsername()).isEqualTo("app");
redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isNull();
assertThat(sentinel.getPassword()).isNotNull();
});
}
@Test
@Test // GH-2218
void sentinelConfigurationShouldNotSetSentinelAuthIfUsernameIsPresentWithNoPassword() {
RedisPassword password = RedisPassword.of("88888888-8x8-getting-creative-now");
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration()
.master(MASTER_NAME)
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
sentinelConfiguration.setSentinelUsername("admin");
sentinelConfiguration.setUsername("app");
sentinelConfiguration.setPassword(password);
sentinelConfiguration.setSentinelUsername("admin");
RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
assertThat(redisURI.getUsername()).isEqualTo("app");
redisURI.getSentinels().forEach(sentinel -> {
assertThat(sentinel.getUsername()).isNull();
assertThat(sentinel.getPassword()).isNull();