Skip to content

Commit f513739

Browse files
authored
Add example configuration using SNI enabled TLS connection (#3045)
Make file updated to bootstrap one primary virtual service (127.0.0.1:36443) redirecting to redis-sni1(localhost:6480) or redis-sni2(localhost:6479) Redis instances based on the provided SNI server name.
1 parent 7f455ec commit f513739

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ work/stunnel.conf:
282282
@echo accept = 127.0.0.1:6443 >> $@
283283
@echo connect = 127.0.0.1:6479 >> $@
284284

285+
@echo [redis-sni-vritual] >> $@
286+
@echo accept = 127.0.0.1:36443 >> $@
287+
@echo cert=$(ROOT_DIR)/work/ca/certs/foo-host.cert.pem >> $@
288+
@echo key=$(ROOT_DIR)/work/ca/private/foo-host.decrypted.key.pem >> $@
289+
@echo connect = unavailable.internal.mydomain.com:6666 >> $@
290+
291+
@echo [redis-sni1] >> $@
292+
@echo sni = redis-sni-vritual:redis-sni1.local >> $@
293+
@echo key=$(ROOT_DIR)/work/ca/private/localhost.decrypted.key.pem >> $@
294+
@echo cert=$(ROOT_DIR)/work/ca/certs/localhost.cert.pem >> $@
295+
@echo connect = localhost:6480 >> $@
296+
297+
@echo [redis-sni2] >> $@
298+
@echo sni = redis-sni-vritual:redis-sni2.local >> $@
299+
@echo connect = localhost:6479 >> $@
300+
@echo cert=$(ROOT_DIR)/work/ca/certs/foo-host.cert.pem >> $@
301+
@echo key=$(ROOT_DIR)/work/ca/private/foo-host.decrypted.key.pem >> $@
302+
285303
@echo [foo-host] >> $@
286304
@echo accept = 127.0.0.1:6444 >> $@
287305
@echo connect = 127.0.0.1:6479 >> $@
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.lettuce.examples;
2+
3+
import io.lettuce.core.ClientOptions;
4+
import io.lettuce.core.RedisClient;
5+
import io.lettuce.core.SslOptions;
6+
import io.lettuce.core.api.StatefulRedisConnection;
7+
8+
import javax.net.ssl.SNIHostName;
9+
import javax.net.ssl.SNIServerName;
10+
import javax.net.ssl.SSLParameters;
11+
import java.io.File;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class ConnectToRedisSSLWithSni {
16+
17+
public static void main(String[] args) {
18+
// Syntax: rediss://[password@]host[:port][/databaseNumber]
19+
// Adapt the port to the stunnel port in front of your Redis instance
20+
RedisClient redisClient = RedisClient.create("rediss://127.0.0.1:36443");
21+
22+
List<SNIServerName> serverNames = new ArrayList<>();
23+
24+
// Hint : Enable SSL debugging (-Djavax.net.debug=ssl to the VM Args)
25+
// to verify/troubleshoot ssl configuration
26+
// Hint : Adapt the server name to switch between multiple instances
27+
serverNames.add(new SNIHostName("redis-sni1.local"));
28+
// serverNames.add(new SNIHostName("redis-sni2.local"));
29+
SslOptions sslOptions = SslOptions.builder().jdkSslProvider().truststore(new File("work/truststore.jks"), "changeit")
30+
.sslParameters(() -> {
31+
SSLParameters parameters = new SSLParameters();
32+
parameters.setServerNames(serverNames);
33+
return parameters;
34+
}).build();
35+
36+
ClientOptions clientOptions = ClientOptions.builder().sslOptions(sslOptions).build();
37+
redisClient.setOptions(clientOptions);
38+
39+
StatefulRedisConnection<String, String> connection = redisClient.connect();
40+
System.out.println("Connected to Redis using TLS with enabled SNI");
41+
42+
connection.close();
43+
redisClient.shutdown();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)