From 68e980f12368be9249160eb45bff304dff279bc5 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Sat, 7 Nov 2026 22:19:41 +0900 Subject: [PATCH] fix: enable SSL for Redis Cluster nodes when sslEnable is set getRedisClusterClient() took the SSL flag of each cluster node entry. Those entries are created by RedisConnectionConfig.Builder.build() from host and port only, so their sslEnable is always false and the cluster client connected without TLS even with withSslEnable(true). Use the flag of the data source config, like the standalone and Sentinel clients and like the timeout and password of the cluster nodes. Fixes #3533 --- .../datasource/redis/RedisDataSource.java | 2 +- .../ClusterModeRedisDataSourceSslTest.java | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 sentinel-extension/sentinel-datasource-redis/src/test/java/com/alibaba/csp/sentinel/datasource/redis/ClusterModeRedisDataSourceSslTest.java diff --git a/sentinel-extension/sentinel-datasource-redis/src/main/java/com/alibaba/csp/sentinel/datasource/redis/RedisDataSource.java b/sentinel-extension/sentinel-datasource-redis/src/main/java/com/alibaba/csp/sentinel/datasource/redis/RedisDataSource.java index d10bcf08fa..f5218f9387 100644 --- a/sentinel-extension/sentinel-datasource-redis/src/main/java/com/alibaba/csp/sentinel/datasource/redis/RedisDataSource.java +++ b/sentinel-extension/sentinel-datasource-redis/src/main/java/com/alibaba/csp/sentinel/datasource/redis/RedisDataSource.java @@ -172,7 +172,7 @@ private RedisClusterClient getRedisClusterClient(RedisConnectionConfig connectio RedisURI.Builder clusterRedisUriBuilder = RedisURI.builder(); clusterRedisUriBuilder.withHost(config.getHost()) .withPort(config.getPort()) - .withSsl(config.isSslEnable()) + .withSsl(connectionConfig.isSslEnable()) .withTimeout(Duration.ofMillis(connectionConfig.getTimeout())); //All redis nodes must have same password if (password != null) { diff --git a/sentinel-extension/sentinel-datasource-redis/src/test/java/com/alibaba/csp/sentinel/datasource/redis/ClusterModeRedisDataSourceSslTest.java b/sentinel-extension/sentinel-datasource-redis/src/test/java/com/alibaba/csp/sentinel/datasource/redis/ClusterModeRedisDataSourceSslTest.java new file mode 100644 index 0000000000..9febd8bad7 --- /dev/null +++ b/sentinel-extension/sentinel-datasource-redis/src/test/java/com/alibaba/csp/sentinel/datasource/redis/ClusterModeRedisDataSourceSslTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.csp.sentinel.datasource.redis; + +import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Checks that a cluster mode data source with SSL enabled starts a TLS handshake with the cluster nodes. + */ +public class ClusterModeRedisDataSourceSslTest { + + /** + * First byte of a TLS record carrying a handshake message (ClientHello). + */ + private static final int TLS_HANDSHAKE_RECORD = 0x16; + + @Test(timeout = 30000) + public void testClusterNodeConnectionUsesSslWhenEnabled() throws Exception { + try (ServerSocket server = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) { + CompletableFuture firstByte = new CompletableFuture<>(); + Thread acceptor = new Thread(() -> { + while (!server.isClosed()) { + try (Socket socket = server.accept()) { + InputStream in = socket.getInputStream(); + firstByte.complete(in.read()); + } catch (IOException e) { + // server closed + } + } + }); + acceptor.setDaemon(true); + acceptor.start(); + + RedisConnectionConfig config = RedisConnectionConfig.builder() + .withRedisCluster(server.getInetAddress().getHostAddress(), server.getLocalPort()) + .withSslEnable(true) + .withTimeout(2000) + .build(); + try { + new RedisDataSource<>(config, "sentinel.rules.flow.ruleKey", "sentinel.rules.flow.channel", + source -> source); + } catch (Exception e) { + // there is no Redis behind the socket + } + + Assert.assertEquals(TLS_HANDSHAKE_RECORD, (int) firstByte.get(10, TimeUnit.SECONDS)); + } + } +}