Skip to content

Commit

Permalink
Remove more hardcoded connection settings
Browse files Browse the repository at this point in the history
  • Loading branch information
uglide committed May 8, 2024
1 parent a8ac729 commit 960ce90
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 148 deletions.
2 changes: 1 addition & 1 deletion src/test/java/redis/clients/jedis/ACLJedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ACLJedisPoolTest {
public static void prepare() throws Exception {
// Use to check if the ACL test should be ran. ACL are available only in 6.0 and later
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6));
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class ACLJedisSentinelPoolTest {

@BeforeClass
public static void prepare() throws Exception {
EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone2-primary");
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6));
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
}

@Before
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/redis/clients/jedis/ACLJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ACLJedisTest extends JedisCommandsTestBase {
@BeforeClass
public static void prepare() throws Exception {
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6));
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
}

public ACLJedisTest(RedisProtocol redisProtocol) {
Expand Down
163 changes: 82 additions & 81 deletions src/test/java/redis/clients/jedis/EndpointConfig.java
Original file line number Diff line number Diff line change
@@ -1,93 +1,94 @@
package redis.clients.jedis;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import redis.clients.jedis.util.JedisURIHelper;

import java.io.FileReader;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.*;

public class EndpointConfig {

private boolean tls;
private String username;
private String password;
private int bdbId;
private Object rawEndpoints;

private List<URI> endpoints;

public EndpointConfig(boolean tls, String username, String password, int bdbId, Object rawEndpoints) {
this.tls = tls;
this.username = username;
this.password = password;
this.bdbId = bdbId;
this.rawEndpoints = rawEndpoints;
}

public HostAndPort getHostAndPort() {
return JedisURIHelper.getHostAndPort(endpoints.get(0));
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

public String getHost() {
return getHostAndPort().getHost();
}

public int getPort() {
return getHostAndPort().getPort();
}

public URI getURI() {
return endpoints.get(0);
}

public URI getCustomizedURI(boolean withCredentials, String path)
{
return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path);
}

public URI getCustomizedURI(String u, String p, String path)
{
String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : "";
return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path);
}

public Connection getConnection() {
return new Connection(getHostAndPort(), getClientConfigBuilder().build());
}

public Connection getConnection(int timeoutMillis) {
return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
}

public Jedis getJedis() {
return new Jedis(getHostAndPort(), getClientConfigBuilder().build());
}

public Jedis getJedis(int timeoutMillis) {
return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
}

public DefaultJedisClientConfig.Builder getClientConfigBuilder() {
return DefaultJedisClientConfig.builder().user(username).password(password);
}

public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception {
Gson gson = new Gson();
HashMap<String, EndpointConfig> configs;
try (FileReader reader = new FileReader(filePath)) {
configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() {
}.getType());
private boolean tls;
private String username;
private String password;
private int bdbId;
private Object rawEndpoints;
private List<URI> endpoints;


public EndpointConfig(HostAndPort hnp, String username, String password) {
this.tls = false;
this.username = username;
this.password = password;
this.bdbId = 0;
this.rawEndpoints = null;
this.endpoints = Collections.singletonList(
URI.create("redis://" + hnp.getHost() + ":" + hnp.getPort())
);
}

public HostAndPort getHostAndPort() {
return JedisURIHelper.getHostAndPort(endpoints.get(0));
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

public String getHost() {
return getHostAndPort().getHost();
}

public int getPort() {
return getHostAndPort().getPort();
}

public URI getURI() {
return endpoints.get(0);
}

public URI getCustomizedURI(boolean withCredentials, String path) {
return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path);
}

public URI getCustomizedURI(String u, String p, String path) {
String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : "";
return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path);
}

public Connection getConnection() {
return new Connection(getHostAndPort(), getClientConfigBuilder().build());
}

public Connection getConnection(int timeoutMillis) {
return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
}

public Jedis getJedis() {
return new Jedis(getHostAndPort(), getClientConfigBuilder().build());
}

public Jedis getJedis(int timeoutMillis) {
return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build());
}

public DefaultJedisClientConfig.Builder getClientConfigBuilder() {
return DefaultJedisClientConfig.builder().user(username).password(password).ssl(tls);
}

public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception {
Gson gson = new Gson();
HashMap<String, EndpointConfig> configs;
try (FileReader reader = new FileReader(filePath)) {
configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() {
}.getType());
}
return configs;
}
return configs;
}
}
14 changes: 8 additions & 6 deletions src/test/java/redis/clients/jedis/JedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public JedisTest(RedisProtocol protocol) {
@Test
public void useWithoutConnecting() {
try (Jedis j = new Jedis()) {
j.auth("foobared");
j.auth(endpoint.getPassword());
j.dbSize();
}
}
Expand Down Expand Up @@ -240,15 +240,17 @@ public void shouldNotUpdateDbIndexIfSelectFails() {

@Test
public void allowUrlWithNoDBAndNoPassword() {
try (Jedis j1 = new Jedis("redis://localhost:6380")) {
j1.auth("foobared");
EndpointConfig endpoint1 = HostAndPorts.getRedisEndpoint("standalone1");

try (Jedis j1 = new Jedis(endpoint1.getURI().toString())) {
j1.auth(endpoint1.getPassword());
// assertEquals("localhost", j1.getClient().getHost());
// assertEquals(6380, j1.getClient().getPort());
assertEquals(0, j1.getDB());
}

try (Jedis j2 = new Jedis("redis://localhost:6380/")) {
j2.auth("foobared");
try (Jedis j2 = new Jedis(endpoint1.getURI().toString())) {
j2.auth(endpoint1.getPassword());
// assertEquals("localhost", j2.getClient().getHost());
// assertEquals(6380, j2.getClient().getPort());
assertEquals(0, j2.getDB());
Expand Down Expand Up @@ -288,7 +290,7 @@ public void checkCloseableAfterConnect() {
@Test
public void checkCloseableAfterCommand() {
Jedis bj = new Jedis();
bj.auth("foobared");
bj.auth(endpoint.getPassword());
bj.close();
}

Expand Down
8 changes: 5 additions & 3 deletions src/test/java/redis/clients/jedis/MigratePipeliningTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void setUp() throws Exception {
dest.select(db);

destAuth = new Jedis(host, portAuth, 500);
destAuth.auth("foobared");
destAuth.auth(endpoint.getPassword());
destAuth.flushAll();
destAuth.select(dbAuth);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ public void migrateAuth() {
Pipeline p = jedis.pipelined();

p.set("foo", "bar");
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), "foo");
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth(endpoint.getPassword()), "foo");
p.get("foo");

assertThat(p.syncAndReturnAll(),
Expand All @@ -274,7 +274,7 @@ public void migrateAuthBinary() {
Pipeline p = jedis.pipelined();

p.set(bfoo, bbar);
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth("foobared"), bfoo);
p.migrate(host, portAuth, dbAuth, timeout, new MigrateParams().auth(endpoint.getPassword()), bfoo);
p.get(bfoo);

assertThat(p.syncAndReturnAll(),
Expand All @@ -287,6 +287,8 @@ public void migrateAuthBinary() {
public void migrateAuth2() {
assertNull(jedis.get("foo"));



Pipeline p = destAuth.pipelined();

p.set("foo", "bar");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/redis/clients/jedis/PipeliningTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public void testSyncWithNoCommandQueued() {
public void testCloseable() throws IOException {
// we need to test with fresh instance of Jedis
Jedis jedis2 = new Jedis(endpoint.getHost(), endpoint.getPort(), 500);
jedis2.auth("foobared");
jedis2.auth(endpoint.getPassword());

Pipeline pipeline = jedis2.pipelined();
Response<String> retFuture1 = pipeline.set("a", "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public class SSLACLJedisClusterTest extends JedisClusterTestBase {

@BeforeClass
public static void prepare() {
// TODO(imalinovskyi): Remove hardcoded connection settings
// once this test is refactored to support RE
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6));
RedisVersionUtil.checkRedisMajorVersionNumber(6,
new EndpointConfig(new HostAndPort("localhost", 8379),
"default", "cluster")));

SSLJedisTest.setupTrustStore();
}
Expand Down
26 changes: 14 additions & 12 deletions src/test/java/redis/clients/jedis/SSLACLJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.SecureRandom;
Expand All @@ -23,50 +22,53 @@
*/
public class SSLACLJedisTest {

protected static final EndpointConfig endpoint = HostAndPorts.getRedisEndpoint("standalone0-acl-tls");

protected static final EndpointConfig endpointWithDefaultUser = HostAndPorts.getRedisEndpoint("standalone0-tls");

@BeforeClass
public static void prepare() {
// Use to check if the ACL test should be ran. ACL are available only in 6.0 and later
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6));

SSLJedisTest.setupTrustStore();
org.junit.Assume.assumeTrue("Not running ACL test on this version of Redis",
RedisVersionUtil.checkRedisMajorVersionNumber(6, endpoint));
}

@Test
public void connectWithSsl() {
try (Jedis jedis = new Jedis("localhost", 6390, true)) {
jedis.auth("acljedis", "fizzbuzz");
try (Jedis jedis = new Jedis(endpoint.getHost(), endpoint.getPort(), true)) {
jedis.auth(endpoint.getUsername(), endpoint.getPassword());
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithConfig() {
try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390),
try (Jedis jedis = new Jedis(endpoint.getHostAndPort(),
DefaultJedisClientConfig.builder().ssl(true).build())) {
jedis.auth("acljedis", "fizzbuzz");
jedis.auth(endpoint.getUsername(), endpoint.getPassword());
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithUrl() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) {
try (Jedis jedis = new Jedis(endpointWithDefaultUser.getCustomizedURI(true, "").toString())) {
assertEquals("PONG", jedis.ping());
}
try (Jedis jedis = new Jedis("rediss://acljedis:fizzbuzz@localhost:6390")) {
try (Jedis jedis = new Jedis(endpoint.getCustomizedURI(true, "").toString())) {
assertEquals("PONG", jedis.ping());
}
}

@Test
public void connectWithUri() {
// The "rediss" scheme instructs jedis to open a SSL/TLS connection.
try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) {
try (Jedis jedis = new Jedis(endpointWithDefaultUser.getCustomizedURI(true, ""))) {
assertEquals("PONG", jedis.ping());
}
try (Jedis jedis = new Jedis(URI.create("rediss://acljedis:fizzbuzz@localhost:6390"))) {
try (Jedis jedis = new Jedis(endpoint.getCustomizedURI(true, ""))) {
assertEquals("PONG", jedis.ping());
}
}
Expand Down
Loading

0 comments on commit 960ce90

Please sign in to comment.