Add SSL service connection support for Redis

See gh-41137
This commit is contained in:
Moritz Halbritter
2025-02-11 10:22:31 +01:00
parent b773dcd356
commit 7cf9cc74a2
19 changed files with 460 additions and 103 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -92,21 +92,17 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
private JedisConnectionFactory createJedisConnectionFactory(
ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) {
JedisClientConfiguration clientConfiguration = getJedisClientConfiguration(builderCustomizers);
if (getSentinelConfig() != null) {
return new JedisConnectionFactory(getSentinelConfig(), clientConfiguration);
}
if (getClusterConfiguration() != null) {
return new JedisConnectionFactory(getClusterConfiguration(), clientConfiguration);
}
return new JedisConnectionFactory(getStandaloneConfig(), clientConfiguration);
return switch (this.mode) {
case STANDALONE -> new JedisConnectionFactory(getStandaloneConfig(), clientConfiguration);
case CLUSTER -> new JedisConnectionFactory(getClusterConfiguration(), clientConfiguration);
case SENTINEL -> new JedisConnectionFactory(getSentinelConfig(), clientConfiguration);
};
}
private JedisClientConfiguration getJedisClientConfiguration(
ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) {
JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder());
if (isSslEnabled()) {
applySsl(builder);
}
applySslIfNeeded(builder);
RedisProperties.Pool pool = getProperties().getJedis().getPool();
if (isPoolEnabled(pool)) {
applyPooling(pool, builder);
@@ -126,18 +122,19 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
return builder;
}
private void applySsl(JedisClientConfigurationBuilder builder) {
JedisSslClientConfigurationBuilder sslBuilder = builder.useSsl();
if (getProperties().getSsl().getBundle() != null) {
SslBundle sslBundle = getSslBundles().getBundle(getProperties().getSsl().getBundle());
sslBuilder.sslSocketFactory(sslBundle.createSslContext().getSocketFactory());
SslOptions sslOptions = sslBundle.getOptions();
SSLParameters sslParameters = new SSLParameters();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(sslOptions.getCiphers()).to(sslParameters::setCipherSuites);
map.from(sslOptions.getEnabledProtocols()).to(sslParameters::setProtocols);
sslBuilder.sslParameters(sslParameters);
private void applySslIfNeeded(JedisClientConfigurationBuilder builder) {
SslBundle sslBundle = getSslBundle();
if (sslBundle == null) {
return;
}
JedisSslClientConfigurationBuilder sslBuilder = builder.useSsl();
sslBuilder.sslSocketFactory(sslBundle.createSslContext().getSocketFactory());
SslOptions sslOptions = sslBundle.getOptions();
SSLParameters sslParameters = new SSLParameters();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(sslOptions.getCiphers()).to(sslParameters::setCipherSuites);
map.from(sslOptions.getEnabledProtocols()).to(sslParameters::setProtocols);
sslBuilder.sslParameters(sslParameters);
}
private void applyPooling(RedisProperties.Pool pool,

View File

@@ -116,19 +116,14 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> clientConfigurationBuilderCustomizers,
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources) {
LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(clientConfigurationBuilderCustomizers,
clientOptionsBuilderCustomizers, clientResources, getProperties().getLettuce().getPool());
return createLettuceConnectionFactory(clientConfig);
}
private LettuceConnectionFactory createLettuceConnectionFactory(LettuceClientConfiguration clientConfiguration) {
if (getSentinelConfig() != null) {
return new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
}
if (getClusterConfiguration() != null) {
return new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
}
return new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
LettuceClientConfiguration clientConfiguration = getLettuceClientConfiguration(
clientConfigurationBuilderCustomizers, clientOptionsBuilderCustomizers, clientResources,
getProperties().getLettuce().getPool());
return switch (this.mode) {
case STANDALONE -> new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
case CLUSTER -> new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
case SENTINEL -> new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
};
}
private LettuceClientConfiguration getLettuceClientConfiguration(
@@ -136,11 +131,12 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientOptionsBuilderCustomizers,
ClientResources clientResources, Pool pool) {
LettuceClientConfigurationBuilder builder = createBuilder(pool);
applyProperties(builder);
SslBundle sslBundle = getSslBundle();
applyProperties(builder, sslBundle);
if (StringUtils.hasText(getProperties().getUrl())) {
customizeConfigurationFromUrl(builder);
}
builder.clientOptions(createClientOptions(clientOptionsBuilderCustomizers));
builder.clientOptions(createClientOptions(clientOptionsBuilderCustomizers, sslBundle));
builder.clientResources(clientResources);
clientConfigurationBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
@@ -153,8 +149,8 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
return LettuceClientConfiguration.builder();
}
private void applyProperties(LettuceClientConfiguration.LettuceClientConfigurationBuilder builder) {
if (isSslEnabled()) {
private void applyProperties(LettuceClientConfigurationBuilder builder, SslBundle sslBundle) {
if (sslBundle != null) {
builder.useSsl();
}
if (getProperties().getTimeout() != null) {
@@ -195,14 +191,14 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
}
private ClientOptions createClientOptions(
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientConfigurationBuilderCustomizers) {
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientConfigurationBuilderCustomizers,
SslBundle sslBundle) {
ClientOptions.Builder builder = initializeClientOptionsBuilder();
Duration connectTimeout = getProperties().getConnectTimeout();
if (connectTimeout != null) {
builder.socketOptions(SocketOptions.builder().connectTimeout(connectTimeout).build());
}
if (isSslEnabled() && getProperties().getSsl().getBundle() != null) {
SslBundle sslBundle = getSslBundles().getBundle(getProperties().getSsl().getBundle());
if (sslBundle != null) {
io.lettuce.core.SslOptions.Builder sslOptionsBuilder = io.lettuce.core.SslOptions.builder();
sslOptionsBuilder.keyManager(sslBundle.getManagers().getKeyManagerFactory());
sslOptionsBuilder.trustManager(sslBundle.getManagers().getTrustManagerFactory());

View File

@@ -18,6 +18,11 @@ package org.springframework.boot.autoconfigure.data.redis;
import java.util.List;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Adapts {@link RedisProperties} to {@link RedisConnectionDetails}.
*
@@ -32,8 +37,11 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
private final RedisProperties properties;
PropertiesRedisConnectionDetails(RedisProperties properties) {
private final SslBundles sslBundles;
PropertiesRedisConnectionDetails(RedisProperties properties, SslBundles sslBundles) {
this.properties = properties;
this.sslBundles = sslBundles;
}
@Override
@@ -52,8 +60,21 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
public Standalone getStandalone() {
RedisUrl redisUrl = getRedisUrl();
return (redisUrl != null)
? Standalone.of(redisUrl.uri().getHost(), redisUrl.uri().getPort(), redisUrl.database())
: Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
? Standalone.of(redisUrl.uri().getHost(), redisUrl.uri().getPort(), redisUrl.database(), getSslBundle())
: Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase(),
getSslBundle());
}
private SslBundle getSslBundle() {
if (!this.properties.getSsl().isEnabled()) {
return null;
}
String bundleName = this.properties.getSsl().getBundle();
if (StringUtils.hasLength(bundleName)) {
Assert.notNull(this.sslBundles, "SSL bundle name has been set but no SSL bundles found in context");
return this.sslBundles.getBundle(bundleName);
}
return SslBundle.systemDefault();
}
@Override
@@ -65,8 +86,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
@Override
public Cluster getCluster() {
RedisProperties.Cluster cluster = this.properties.getCluster();
List<Node> nodes = (cluster != null) ? asNodes(cluster.getNodes()) : null;
return (nodes != null) ? () -> nodes : null;
return (cluster != null) ? new PropertiesCluster(cluster) : null;
}
private RedisUrl getRedisUrl() {
@@ -84,6 +104,29 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
return new Node(host, port);
}
/**
* {@link Cluster} implementation backed by properties.
*/
private class PropertiesCluster implements Cluster {
private final List<Node> nodes;
PropertiesCluster(RedisProperties.Cluster properties) {
this.nodes = asNodes(properties.getNodes());
}
@Override
public List<Node> getNodes() {
return this.nodes;
}
@Override
public SslBundle getSslBundle() {
return PropertiesRedisConnectionDetails.this.getSslBundle();
}
}
/**
* {@link Sentinel} implementation backed by properties.
*/
@@ -123,6 +166,11 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
return this.properties.getPassword();
}
@Override
public SslBundle getSslBundle() {
return PropertiesRedisConnectionDetails.this.getSslBundle();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -16,12 +16,14 @@
package org.springframework.boot.autoconfigure.data.redis;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -51,8 +53,9 @@ public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(RedisConnectionDetails.class)
PropertiesRedisConnectionDetails redisConnectionDetails(RedisProperties properties) {
return new PropertiesRedisConnectionDetails(properties);
PropertiesRedisConnectionDetails redisConnectionDetails(RedisProperties properties,
ObjectProvider<SslBundles> sslBundles) {
return new PropertiesRedisConnectionDetails(properties, sslBundles.getIfAvailable());
}
@Bean

View File

@@ -24,6 +24,7 @@ import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.Node;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
@@ -62,6 +63,8 @@ abstract class RedisConnectionConfiguration {
private final SslBundles sslBundles;
protected final Mode mode;
protected RedisConnectionConfiguration(RedisProperties properties, RedisConnectionDetails connectionDetails,
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
ObjectProvider<RedisSentinelConfiguration> sentinelConfigurationProvider,
@@ -73,6 +76,7 @@ abstract class RedisConnectionConfiguration {
this.clusterConfiguration = clusterConfigurationProvider.getIfAvailable();
this.connectionDetails = connectionDetails;
this.sslBundles = sslBundles.getIfAvailable();
this.mode = determineMode();
}
protected final RedisStandaloneConfiguration getStandaloneConfig() {
@@ -153,6 +157,17 @@ abstract class RedisConnectionConfiguration {
return this.sslBundles;
}
protected SslBundle getSslBundle() {
return switch (this.mode) {
case STANDALONE -> (this.connectionDetails.getStandalone() != null)
? this.connectionDetails.getStandalone().getSslBundle() : null;
case CLUSTER -> (this.connectionDetails.getCluster() != null)
? this.connectionDetails.getCluster().getSslBundle() : null;
case SENTINEL -> (this.connectionDetails.getSentinel() != null)
? this.connectionDetails.getSentinel().getSslBundle() : null;
};
}
protected final boolean isSslEnabled() {
return getProperties().getSsl().isEnabled();
}
@@ -178,4 +193,20 @@ abstract class RedisConnectionConfiguration {
return this.connectionDetails;
}
private Mode determineMode() {
if (getSentinelConfig() != null) {
return Mode.SENTINEL;
}
if (getClusterConfiguration() != null) {
return Mode.CLUSTER;
}
return Mode.STANDALONE;
}
enum Mode {
STANDALONE, CLUSTER, SENTINEL
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data.redis;
import java.util.List;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.util.Assert;
/**
@@ -98,11 +99,58 @@ public interface RedisConnectionDetails extends ConnectionDetails {
return 0;
}
static Standalone of(String host, int port) {
return of(host, port, 0);
/**
* SSL bundle to use.
* @return the SSL bundle to use
* @since 3.5.0
*/
default SslBundle getSslBundle() {
return null;
}
/**
* Creates a new instance with the given host and port.
* @param host the host
* @param port the port
* @return the new instance
*/
static Standalone of(String host, int port) {
return of(host, port, 0, null);
}
/**
* Creates a new instance with the given host, port and SSL bundle.
* @param host the host
* @param port the port
* @param sslBundle the SSL bundle
* @return the new instance
* @since 3.5.0
*/
static Standalone of(String host, int port, SslBundle sslBundle) {
return of(host, port, 0, sslBundle);
}
/**
* Creates a new instance with the given host, port and database.
* @param host the host
* @param port the port
* @param database the database
* @return the new instance
*/
static Standalone of(String host, int port, int database) {
return of(host, port, database, null);
}
/**
* Creates a new instance with the given host, port, database and SSL bundle.
* @param host the host
* @param port the port
* @param database the database
* @param sslBundle the SSL bundle
* @return the new instance
* @since 3.5.0
*/
static Standalone of(String host, int port, int database, SslBundle sslBundle) {
Assert.hasLength(host, "'host' must not be empty");
return new Standalone() {
@@ -121,6 +169,10 @@ public interface RedisConnectionDetails extends ConnectionDetails {
return database;
}
@Override
public SslBundle getSslBundle() {
return sslBundle;
}
};
}
@@ -161,6 +213,15 @@ public interface RedisConnectionDetails extends ConnectionDetails {
*/
String getPassword();
/**
* SSL bundle to use.
* @return the SSL bundle to use
* @since 3.5.0
*/
default SslBundle getSslBundle() {
return null;
}
}
/**
@@ -175,6 +236,15 @@ public interface RedisConnectionDetails extends ConnectionDetails {
*/
List<Node> getNodes();
/**
* SSL bundle to use.
* @return the SSL bundle to use
* @since 3.5.0
*/
default SslBundle getSslBundle() {
return null;
}
}
/**

View File

@@ -18,40 +18,54 @@ package org.springframework.boot.autoconfigure.data.redis;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails.Node;
import org.springframework.boot.ssl.DefaultSslBundleRegistry;
import org.springframework.boot.ssl.SslBundle;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link PropertiesRedisConnectionDetails}.
*
* @author Scott Frederick
* @author Moritz Halbritter
*/
class PropertiesRedisConnectionDetailsTests {
private final RedisProperties properties = new RedisProperties();
private RedisProperties properties;
private PropertiesRedisConnectionDetails connectionDetails;
private DefaultSslBundleRegistry sslBundleRegistry;
@BeforeEach
void setUp() {
this.properties = new RedisProperties();
this.sslBundleRegistry = new DefaultSslBundleRegistry();
this.connectionDetails = new PropertiesRedisConnectionDetails(this.properties, this.sslBundleRegistry);
}
@Test
void connectionIsConfiguredWithDefaults() {
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
RedisConnectionDetails.Standalone standalone = this.connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("localhost");
assertThat(standalone.getPort()).isEqualTo(6379);
assertThat(standalone.getDatabase()).isEqualTo(0);
assertThat(connectionDetails.getSentinel()).isNull();
assertThat(connectionDetails.getCluster()).isNull();
assertThat(connectionDetails.getUsername()).isNull();
assertThat(connectionDetails.getPassword()).isNull();
assertThat(this.connectionDetails.getSentinel()).isNull();
assertThat(this.connectionDetails.getCluster()).isNull();
assertThat(this.connectionDetails.getUsername()).isNull();
assertThat(this.connectionDetails.getPassword()).isNull();
}
@Test
void credentialsAreConfiguredFromUrlWithUsernameAndPassword() {
this.properties.setUrl("redis://user:secret@example.com");
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getUsername()).isEqualTo("user");
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
assertThat(this.connectionDetails.getUsername()).isEqualTo("user");
assertThat(this.connectionDetails.getPassword()).isEqualTo("secret");
}
@Test
@@ -59,9 +73,8 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setUrl("redis://user:@example.com");
this.properties.setUsername("notused");
this.properties.setPassword("notused");
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getUsername()).isEqualTo("user");
assertThat(connectionDetails.getPassword()).isEmpty();
assertThat(this.connectionDetails.getUsername()).isEqualTo("user");
assertThat(this.connectionDetails.getPassword()).isEmpty();
}
@Test
@@ -69,9 +82,8 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setUrl("redis://:secret@example.com");
this.properties.setUsername("notused");
this.properties.setPassword("notused");
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getUsername()).isEmpty();
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
assertThat(this.connectionDetails.getUsername()).isEmpty();
assertThat(this.connectionDetails.getPassword()).isEqualTo("secret");
}
@Test
@@ -79,18 +91,16 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setUrl("redis://secret@example.com");
this.properties.setUsername("notused");
this.properties.setPassword("notused");
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getUsername()).isNull();
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
assertThat(this.connectionDetails.getUsername()).isNull();
assertThat(this.connectionDetails.getPassword()).isEqualTo("secret");
}
@Test
void credentialsAreConfiguredFromProperties() {
this.properties.setUsername("user");
this.properties.setPassword("secret");
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getUsername()).isEqualTo("user");
assertThat(connectionDetails.getPassword()).isEqualTo("secret");
assertThat(this.connectionDetails.getUsername()).isEqualTo("user");
assertThat(this.connectionDetails.getPassword()).isEqualTo("secret");
}
@Test
@@ -99,8 +109,7 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setHost("notused");
this.properties.setPort(9999);
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
RedisConnectionDetails.Standalone standalone = this.connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("example.com");
assertThat(standalone.getPort()).isEqualTo(1234);
assertThat(standalone.getDatabase()).isEqualTo(9999);
@@ -110,7 +119,8 @@ class PropertiesRedisConnectionDetailsTests {
void standaloneIsConfiguredFromUrlWithoutDatabase() {
this.properties.setUrl("redis://example.com:1234");
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties,
null);
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("example.com");
assertThat(standalone.getPort()).isEqualTo(1234);
@@ -122,8 +132,7 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setHost("example.com");
this.properties.setPort(1234);
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
RedisConnectionDetails.Standalone standalone = this.connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("example.com");
assertThat(standalone.getPort()).isEqualTo(1234);
assertThat(standalone.getDatabase()).isEqualTo(5);
@@ -134,8 +143,7 @@ class PropertiesRedisConnectionDetailsTests {
RedisProperties.Cluster cluster = new RedisProperties.Cluster();
cluster.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
this.properties.setCluster(cluster);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
assertThat(connectionDetails.getCluster().getNodes()).containsExactly(new Node("localhost", 1111),
assertThat(this.connectionDetails.getCluster().getNodes()).containsExactly(new Node("localhost", 1111),
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
}
@@ -145,7 +153,8 @@ class PropertiesRedisConnectionDetailsTests {
sentinel.setNodes(List.of("localhost:1111", "127.0.0.1:2222", "[::1]:3333"));
this.properties.setSentinel(sentinel);
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties,
null);
assertThat(connectionDetails.getSentinel().getNodes()).containsExactly(new Node("localhost", 1111),
new Node("127.0.0.1", 2222), new Node("[::1]", 3333));
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(5);
@@ -158,8 +167,32 @@ class PropertiesRedisConnectionDetailsTests {
this.properties.setSentinel(sentinel);
this.properties.setUrl("redis://example.com:1234/9999");
this.properties.setDatabase(5);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties);
PropertiesRedisConnectionDetails connectionDetails = new PropertiesRedisConnectionDetails(this.properties,
null);
assertThat(connectionDetails.getSentinel().getDatabase()).isEqualTo(9999);
}
@Test
void shouldReturnSslBundle() {
SslBundle bundle1 = mock(SslBundle.class);
this.sslBundleRegistry.registerBundle("bundle-1", bundle1);
this.properties.getSsl().setBundle("bundle-1");
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
assertThat(sslBundle).isSameAs(bundle1);
}
@Test
void shouldReturnSystemBundleIfSslIsEnabledButBundleNotSet() {
this.properties.getSsl().setEnabled(true);
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
assertThat(sslBundle).isNotNull();
}
@Test
void shouldReturnNullIfSslIsNotEnabled() {
this.properties.getSsl().setEnabled(false);
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
assertThat(sslBundle).isNull();
}
}