Browse Source

Rationalize SSL bundles in RedisConnectionDetails

This commit exposes a single ssl bundle rather than a specific bundle
for each of the three supported modes.

Closes gh-47375
pull/47381/head
Stéphane Nicoll 3 months ago
parent
commit
b28d390c69
  1. 3
      module/spring-boot-data-redis/src/dockerTest/java/org/springframework/boot/data/redis/docker/compose/DataRedisDockerComposeConnectionDetailsFactoryIntegrationTests.java
  2. 9
      module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/DataRedisConnectionConfiguration.java
  3. 61
      module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/DataRedisConnectionDetails.java
  4. 28
      module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/PropertiesDataRedisConnectionDetails.java
  5. 13
      module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/docker/compose/RedisDockerComposeConnectionDetailsFactory.java
  6. 10
      module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/testcontainers/RedisContainerConnectionDetailsFactory.java
  7. 6
      module/spring-boot-data-redis/src/test/java/org/springframework/boot/data/redis/autoconfigure/PropertiesRedisConnectionDetailsTests.java

3
module/spring-boot-data-redis/src/dockerTest/java/org/springframework/boot/data/redis/docker/compose/DataRedisDockerComposeConnectionDetailsFactoryIntegrationTests.java

@ -46,8 +46,7 @@ class DataRedisDockerComposeConnectionDetailsFactoryIntegrationTests { @@ -46,8 +46,7 @@ class DataRedisDockerComposeConnectionDetailsFactoryIntegrationTests {
additionalResources = { "ca.crt", "server.crt", "server.key", "client.crt", "client.key" })
void runWithSslCreatesConnectionDetails(DataRedisConnectionDetails connectionDetails) {
assertConnectionDetails(connectionDetails);
Standalone standalone = connectionDetails.getStandalone();
SslBundle sslBundle = standalone.getSslBundle();
SslBundle sslBundle = connectionDetails.getSslBundle();
assertThat(sslBundle).isNotNull();
SSLContext sslContext = sslBundle.createSslContext();
assertThat(sslContext).isNotNull();

9
module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/DataRedisConnectionConfiguration.java

@ -156,14 +156,7 @@ abstract class DataRedisConnectionConfiguration { @@ -156,14 +156,7 @@ abstract class DataRedisConnectionConfiguration {
}
protected @Nullable 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;
};
return this.connectionDetails.getSslBundle();
}
protected final boolean isSslEnabled() {

61
module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/DataRedisConnectionDetails.java

@ -49,6 +49,14 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -49,6 +49,14 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
return null;
}
/**
* SSL bundle to use.
* @return the SSL bundle to use
*/
default @Nullable SslBundle getSslBundle() {
return null;
}
/**
* Redis standalone configuration. Mutually exclusive with {@link #getSentinel()} and
* {@link #getCluster()}.
@ -101,14 +109,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -101,14 +109,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
return 0;
}
/**
* SSL bundle to use.
* @return the SSL bundle to use
*/
default @Nullable SslBundle getSslBundle() {
return null;
}
/**
* Creates a new instance with the given host and port.
* @param host the host
@ -116,18 +116,7 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -116,18 +116,7 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
* @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
*/
static Standalone of(String host, int port, @Nullable SslBundle sslBundle) {
return of(host, port, 0, sslBundle);
return of(host, port, 0);
}
/**
@ -138,18 +127,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -138,18 +127,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
* @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
*/
static Standalone of(String host, int port, int database, @Nullable SslBundle sslBundle) {
Assert.hasLength(host, "'host' must not be empty");
return new Standalone() {
@ -168,10 +145,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -168,10 +145,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
return database;
}
@Override
public @Nullable SslBundle getSslBundle() {
return sslBundle;
}
};
}
@ -212,14 +185,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -212,14 +185,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
*/
@Nullable String getPassword();
/**
* SSL bundle to use.
* @return the SSL bundle to use
*/
default @Nullable SslBundle getSslBundle() {
return null;
}
}
/**
@ -234,14 +199,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails { @@ -234,14 +199,6 @@ public interface DataRedisConnectionDetails extends ConnectionDetails {
*/
List<Node> getNodes();
/**
* SSL bundle to use.
* @return the SSL bundle to use
*/
default @Nullable SslBundle getSslBundle() {
return null;
}
}
/**

28
module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/autoconfigure/PropertiesDataRedisConnectionDetails.java

@ -60,15 +60,7 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails @@ -60,15 +60,7 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails
}
@Override
public Standalone getStandalone() {
DataRedisUrl redisUrl = getRedisUrl();
return (redisUrl != null)
? Standalone.of(redisUrl.uri().getHost(), redisUrl.uri().getPort(), redisUrl.database(), getSslBundle())
: Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase(),
getSslBundle());
}
private @Nullable SslBundle getSslBundle() {
public @Nullable SslBundle getSslBundle() {
if (!this.properties.getSsl().isEnabled()) {
return null;
}
@ -80,6 +72,14 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails @@ -80,6 +72,14 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails
return SslBundle.systemDefault();
}
@Override
public Standalone getStandalone() {
DataRedisUrl 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());
}
@Override
public @Nullable Sentinel getSentinel() {
DataRedisProperties.Sentinel sentinel = this.properties.getSentinel();
@ -126,11 +126,6 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails @@ -126,11 +126,6 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails
return this.nodes;
}
@Override
public @Nullable SslBundle getSslBundle() {
return PropertiesDataRedisConnectionDetails.this.getSslBundle();
}
}
/**
@ -174,11 +169,6 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails @@ -174,11 +169,6 @@ class PropertiesDataRedisConnectionDetails implements DataRedisConnectionDetails
return this.properties.getPassword();
}
@Override
public @Nullable SslBundle getSslBundle() {
return PropertiesDataRedisConnectionDetails.this.getSslBundle();
}
}
}

13
module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/docker/compose/RedisDockerComposeConnectionDetailsFactory.java

@ -16,10 +16,13 @@ @@ -16,10 +16,13 @@
package org.springframework.boot.data.redis.docker.compose;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.data.redis.autoconfigure.DataRedisConnectionDetails;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.ssl.SslBundle;
/**
* {@link DockerComposeConnectionDetailsFactory} to create
@ -56,9 +59,17 @@ class RedisDockerComposeConnectionDetailsFactory @@ -56,9 +59,17 @@ class RedisDockerComposeConnectionDetailsFactory
private final Standalone standalone;
private final @Nullable SslBundle sslBundle;
RedisDockerComposeConnectionDetails(RunningService service) {
super(service);
this.standalone = Standalone.of(service.host(), service.ports().get(REDIS_PORT), getSslBundle(service));
this.standalone = Standalone.of(service.host(), service.ports().get(REDIS_PORT));
this.sslBundle = getSslBundle(service);
}
@Override
public @Nullable SslBundle getSslBundle() {
return this.sslBundle;
}
@Override

10
module/spring-boot-data-redis/src/main/java/org/springframework/boot/data/redis/testcontainers/RedisContainerConnectionDetailsFactory.java

@ -20,10 +20,12 @@ import java.util.List; @@ -20,10 +20,12 @@ import java.util.List;
import com.redis.testcontainers.RedisContainer;
import com.redis.testcontainers.RedisStackContainer;
import org.jspecify.annotations.Nullable;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.springframework.boot.data.redis.autoconfigure.DataRedisConnectionDetails;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
@ -75,10 +77,14 @@ class RedisContainerConnectionDetailsFactory @@ -75,10 +77,14 @@ class RedisContainerConnectionDetailsFactory
super(source);
}
@Override
public @Nullable SslBundle getSslBundle() {
return super.getSslBundle();
}
@Override
public Standalone getStandalone() {
return Standalone.of(getContainer().getHost(), getContainer().getMappedPort(REDIS_PORT),
super.getSslBundle());
return Standalone.of(getContainer().getHost(), getContainer().getMappedPort(REDIS_PORT));
}
}

6
module/spring-boot-data-redis/src/test/java/org/springframework/boot/data/redis/autoconfigure/PropertiesRedisConnectionDetailsTests.java

@ -177,21 +177,21 @@ class PropertiesRedisConnectionDetailsTests { @@ -177,21 +177,21 @@ class PropertiesRedisConnectionDetailsTests {
SslBundle bundle1 = mock(SslBundle.class);
this.sslBundleRegistry.registerBundle("bundle-1", bundle1);
this.properties.getSsl().setBundle("bundle-1");
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
SslBundle sslBundle = this.connectionDetails.getSslBundle();
assertThat(sslBundle).isSameAs(bundle1);
}
@Test
void shouldReturnSystemBundleIfSslIsEnabledButBundleNotSet() {
this.properties.getSsl().setEnabled(true);
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
SslBundle sslBundle = this.connectionDetails.getSslBundle();
assertThat(sslBundle).isNotNull();
}
@Test
void shouldReturnNullIfSslIsNotEnabled() {
this.properties.getSsl().setEnabled(false);
SslBundle sslBundle = this.connectionDetails.getStandalone().getSslBundle();
SslBundle sslBundle = this.connectionDetails.getSslBundle();
assertThat(sslBundle).isNull();
}

Loading…
Cancel
Save