Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example configuration using SNI enabled TLS connection #3045

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}
Loading