-
Notifications
You must be signed in to change notification settings - Fork 986
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/io/lettuce/examples/ConnectToRedisSSLWithSni.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |