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

[Dubbo-2017]Fix redis auth problem for RedisProtocol(merge to 2.6.x PR) #2146

Merged
merged 1 commit into from
Jul 31, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public RedisRegistry(URL url) {
addresses.addAll(Arrays.asList(backups));
}

String password = url.getPassword();
for (String address : addresses) {
int i = address.indexOf(':');
String host;
Expand All @@ -132,15 +131,9 @@ public RedisRegistry(URL url) {
host = address;
port = DEFAULT_REDIS_PORT;
}
if (StringUtils.isEmpty(password)) {
this.jedisPools.put(address, new JedisPool(config, host, port,
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), null,
url.getParameter("db.index", 0)));
} else {
this.jedisPools.put(address, new JedisPool(config, host, port,
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password,
url.getParameter("db.index", 0)));
}
this.jedisPools.put(address, new JedisPool(config, host, port,
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), StringUtils.isEmpty(url.getPassword()) ? null : url.getPassword(),
url.getParameter("db.index", 0)));
}

this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.dubbo.common.serialize.ObjectInput;
import com.alibaba.dubbo.common.serialize.ObjectOutput;
import com.alibaba.dubbo.common.serialize.Serialization;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.rpc.Exporter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -90,7 +91,9 @@ public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcExcept
if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));
final JedisPool jedisPool = new JedisPool(config, url.getHost(), url.getPort(DEFAULT_PORT),
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT),
StringUtils.isBlank(url.getPassword()) ? null : url.getPassword(),
url.getParameter("db.index", 0));
final int expiry = url.getParameter("expiry", 0);
final String get = url.getParameter("get", "get");
final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@
*/
package com.alibaba.dubbo.rpc.protocol.redis;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.serialize.ObjectInput;
import com.alibaba.dubbo.common.serialize.Serialization;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Protocol;
import com.alibaba.dubbo.rpc.ProxyFactory;
import com.alibaba.dubbo.rpc.RpcException;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.embedded.RedisServer;

import java.io.ByteArrayInputStream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
Expand All @@ -38,12 +52,21 @@ public class RedisProtocolTest {
private RedisServer redisServer;
private URL registryUrl;

@Rule
public TestName name = new TestName();

@Before
public void setUp() throws Exception {
int redisPort = NetUtils.getAvailablePort();
this.redisServer = new RedisServer(redisPort);
if (name.getMethodName().equals("testAuthRedis") || name.getMethodName().equals("testWrongAuthRedis")) {
String password = "123456";
this.redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build();
this.registryUrl = URL.valueOf("redis://username:"+password+"@localhost:"+redisPort+"?db.index=0");
} else {
this.redisServer = RedisServer.builder().port(redisPort).build();
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
}
this.redisServer.start();
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
}

@After
Expand Down Expand Up @@ -109,4 +132,90 @@ public void testWrongRedis() {
public void testExport() {
protocol.export(protocol.refer(IDemoService.class, registryUrl));
}

@Test
public void testAuthRedis() {
// default db.index=0
Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
registryUrl
.addParameter("max.idle", 10)
.addParameter("max.active", 20));
IDemoService demoService = this.proxy.getProxy(refer);

String value = demoService.get("key");
assertThat(value, is(nullValue()));

demoService.set("key", "newValue");
value = demoService.get("key");
assertThat(value, is("newValue"));

demoService.delete("key");
value = demoService.get("key");
assertThat(value, is(nullValue()));

refer.destroy();

//change db.index=1
String password = "123456";
int database = 1;
this.registryUrl = this.registryUrl.setPassword(password).addParameter("db.index", database);
refer = protocol.refer(IDemoService.class,
registryUrl
.addParameter("max.idle", 10)
.addParameter("max.active", 20));
demoService = this.proxy.getProxy(refer);

demoService.set("key", "newValue");
value = demoService.get("key");
assertThat(value, is("newValue"));

// jedis gets the result comparison
JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String)null);
Jedis jedis = null;
try {
jedis = pool.getResource();
byte[] valueByte = jedis.get("key".getBytes());
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(this.registryUrl.getParameter(Constants.SERIALIZATION_KEY, "java"));
ObjectInput oin = serialization.deserialize(this.registryUrl, new ByteArrayInputStream(valueByte));
String actual = (String) oin.readObject();
assertThat(value, is(actual));
} catch(Exception e) {
Assert.fail("jedis gets the result comparison is error!");
} finally {
if (jedis != null) {
jedis.close();
}
pool.destroy();
}

demoService.delete("key");
value = demoService.get("key");
assertThat(value, is(nullValue()));

refer.destroy();
}

@Test
public void testWrongAuthRedis() {
String password = "1234567";
this.registryUrl = this.registryUrl.setPassword(password);
Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
registryUrl
.addParameter("max.idle", 10)
.addParameter("max.active", 20));
IDemoService demoService = this.proxy.getProxy(refer);

try {
String value = demoService.get("key");
assertThat(value, is(nullValue()));
} catch (RpcException e) {
if (e.getCause() instanceof JedisConnectionException && e.getCause().getCause() instanceof JedisDataException) {
Assert.assertEquals("ERR invalid password" , e.getCause().getCause().getMessage());
} else {
Assert.fail("no invalid password exception!");
}
}

refer.destroy();
}
}