Skip to content

Commit

Permalink
Add example configuration using SNI enabled TLS connection (#3045)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ggivo authored Nov 5, 2024
1 parent 7f455ec commit f513739
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ work/stunnel.conf:
@echo accept = 127.0.0.1:6443 >> $@
@echo connect = 127.0.0.1:6479 >> $@

@echo [redis-sni-vritual] >> $@
@echo accept = 127.0.0.1:36443 >> $@
@echo cert=$(ROOT_DIR)/work/ca/certs/foo-host.cert.pem >> $@
@echo key=$(ROOT_DIR)/work/ca/private/foo-host.decrypted.key.pem >> $@
@echo connect = unavailable.internal.mydomain.com:6666 >> $@

@echo [redis-sni1] >> $@
@echo sni = redis-sni-vritual:redis-sni1.local >> $@
@echo key=$(ROOT_DIR)/work/ca/private/localhost.decrypted.key.pem >> $@
@echo cert=$(ROOT_DIR)/work/ca/certs/localhost.cert.pem >> $@
@echo connect = localhost:6480 >> $@

@echo [redis-sni2] >> $@
@echo sni = redis-sni-vritual:redis-sni2.local >> $@
@echo connect = localhost:6479 >> $@
@echo cert=$(ROOT_DIR)/work/ca/certs/foo-host.cert.pem >> $@
@echo key=$(ROOT_DIR)/work/ca/private/foo-host.decrypted.key.pem >> $@

@echo [foo-host] >> $@
@echo accept = 127.0.0.1:6444 >> $@
@echo connect = 127.0.0.1:6479 >> $@
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/io/lettuce/examples/ConnectToRedisSSLWithSni.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.lettuce.examples;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisClient;
import io.lettuce.core.SslOptions;
import io.lettuce.core.api.StatefulRedisConnection;

import javax.net.ssl.SNIHostName;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SSLParameters;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ConnectToRedisSSLWithSni {

public static void main(String[] args) {
// Syntax: rediss://[password@]host[:port][/databaseNumber]
// Adapt the port to the stunnel port in front of your Redis instance
RedisClient redisClient = RedisClient.create("rediss://127.0.0.1:36443");

List<SNIServerName> serverNames = new ArrayList<>();

// Hint : Enable SSL debugging (-Djavax.net.debug=ssl to the VM Args)
// to verify/troubleshoot ssl configuration
// Hint : Adapt the server name to switch between multiple instances
serverNames.add(new SNIHostName("redis-sni1.local"));
// serverNames.add(new SNIHostName("redis-sni2.local"));
SslOptions sslOptions = SslOptions.builder().jdkSslProvider().truststore(new File("work/truststore.jks"), "changeit")
.sslParameters(() -> {
SSLParameters parameters = new SSLParameters();
parameters.setServerNames(serverNames);
return parameters;
}).build();

ClientOptions clientOptions = ClientOptions.builder().sslOptions(sslOptions).build();
redisClient.setOptions(clientOptions);

StatefulRedisConnection<String, String> connection = redisClient.connect();
System.out.println("Connected to Redis using TLS with enabled SNI");

connection.close();
redisClient.shutdown();
}

}

0 comments on commit f513739

Please sign in to comment.