-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Support Sentinel with TLS #2339
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
93 changes: 93 additions & 0 deletions
93
src/test/java/redis/clients/jedis/tests/SSLJedisSentinelPoolTest.java
This file contains hidden or 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,93 @@ | ||
| package redis.clients.jedis.tests; | ||
|
|
||
| import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import redis.clients.jedis.HostAndPort; | ||
| import redis.clients.jedis.JedisSentinelPool; | ||
|
|
||
| import javax.net.ssl.HostnameVerifier; | ||
| import javax.net.ssl.SSLParameters; | ||
| import javax.net.ssl.SSLSocketFactory; | ||
| import java.io.File; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class SSLJedisSentinelPoolTest { | ||
| private static final String MASTER_NAME = "mymaster"; | ||
|
|
||
| protected Set<String> sentinels = new HashSet<String>(); | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| HostAndPort sentinel2 = HostAndPortUtil.getSentinelServers().get(1); | ||
| HostAndPort sentinel4 = HostAndPortUtil.getSentinelServers().get(3); | ||
| sentinels.add(sentinel2.getHost() + ":" + (sentinel2.getPort() + 10000)); | ||
| sentinels.add(sentinel4.getHost() + ":" + (sentinel4.getPort() + 10000)); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setupTrustStore() { | ||
| setJvmTrustStore("src/test/resources/truststore.jceks", "jceks"); | ||
| } | ||
|
|
||
| private static void setJvmTrustStore(String trustStoreFilePath, String trustStoreType) { | ||
| assertTrue(String.format("Could not find trust store at '%s'.", trustStoreFilePath), new File( | ||
| trustStoreFilePath).exists()); | ||
| System.setProperty("javax.net.ssl.trustStore", trustStoreFilePath); | ||
| System.setProperty("javax.net.ssl.trustStoreType", trustStoreType); | ||
| } | ||
|
|
||
| @Test | ||
| public void sentinelWithSslConnectsToRedisWithoutSsl() { | ||
| boolean masterSsl = false; | ||
| boolean sentinelSsl = true; | ||
| GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); | ||
| JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, poolConfig, 1000, 1000, | ||
| null, "foobared", 0, "clientName", masterSsl, 1000, 1000, null, null, "sentinelClientName", sentinelSsl, null, null, null); | ||
| pool.getResource().close(); | ||
| pool.destroy(); | ||
| } | ||
|
|
||
| @Test | ||
| public void sentinelWithSslConnectsToRedisWithSsl() { | ||
| boolean masterSsl = true; | ||
| boolean sentinelSsl = true; | ||
| GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); | ||
|
|
||
| class PortSwizzlingJedisSentinelPool extends JedisSentinelPool { | ||
| public PortSwizzlingJedisSentinelPool(String masterName, Set<String> sentinels, | ||
| GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, | ||
| int database, String clientName, boolean isMasterSslEnabled, int sentinelConnectionTimeout, | ||
| int sentinelSoTimeout, String sentinelPassword, String sentinelClientName, | ||
| boolean isSentinelSslEnabled, SSLSocketFactory sslSocketFactory, | ||
| SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { | ||
| super(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, null, password, | ||
| database, clientName, isMasterSslEnabled, sentinelConnectionTimeout, sentinelSoTimeout, | ||
| null, sentinelPassword, sentinelClientName, isSentinelSslEnabled, sslSocketFactory, | ||
| sslParameters, hostnameVerifier); | ||
| } | ||
|
|
||
| @Override | ||
| protected HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) { | ||
| // Sentinel broadcasts the non-ssl port number of redis, this swizzles it to the ssl port. | ||
| HashMap<Integer, Integer> portMapping = new HashMap<Integer, Integer>(); | ||
| portMapping.put(6381, 16381); | ||
| portMapping.put(6382, 16382); | ||
| HostAndPort original = super.toHostAndPort(getMasterAddrByNameResult); | ||
| int swizzled = portMapping.get(original.getPort()); | ||
| return new HostAndPort(original.getHost(), swizzled); | ||
| } | ||
| } | ||
| JedisSentinelPool pool = new PortSwizzlingJedisSentinelPool(MASTER_NAME, sentinels, poolConfig, 1000, 1000, | ||
| "foobared", 0, "clientName", masterSsl, 1000, 1000, null, "sentinelClientName", sentinelSsl, null, null, null); | ||
| pool.getResource().close(); | ||
| pool.destroy(); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.