Skip to content

Commit 71158da

Browse files
ningyu1carryxyh
authored andcommitted
Merge pull request apache#2018, fix redis auth problem for RedisProtocol.
Fixes apache#2017
1 parent 1376b26 commit 71158da

File tree

3 files changed

+119
-13
lines changed

3 files changed

+119
-13
lines changed

dubbo-registry/dubbo-registry-redis/src/main/java/org/apache/dubbo/registry/redis/RedisRegistry.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public RedisRegistry(URL url) {
120120
addresses.addAll(Arrays.asList(backups));
121121
}
122122

123-
String password = url.getPassword();
124123
for (String address : addresses) {
125124
int i = address.indexOf(':');
126125
String host;
@@ -132,15 +131,9 @@ public RedisRegistry(URL url) {
132131
host = address;
133132
port = DEFAULT_REDIS_PORT;
134133
}
135-
if (StringUtils.isEmpty(password)) {
136-
this.jedisPools.put(address, new JedisPool(config, host, port,
137-
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), null,
138-
url.getParameter("db.index", 0)));
139-
} else {
140-
this.jedisPools.put(address, new JedisPool(config, host, port,
141-
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password,
142-
url.getParameter("db.index", 0)));
143-
}
134+
this.jedisPools.put(address, new JedisPool(config, host, port,
135+
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), StringUtils.isEmpty(url.getPassword()) ? null : url.getPassword(),
136+
url.getParameter("db.index", 0)));
144137
}
145138

146139
this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);

dubbo-rpc/dubbo-rpc-redis/src/main/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocol.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.dubbo.common.serialize.ObjectInput;
2323
import org.apache.dubbo.common.serialize.ObjectOutput;
2424
import org.apache.dubbo.common.serialize.Serialization;
25+
import org.apache.dubbo.common.utils.StringUtils;
2526
import org.apache.dubbo.rpc.Exporter;
2627
import org.apache.dubbo.rpc.Invocation;
2728
import org.apache.dubbo.rpc.Invoker;
@@ -90,7 +91,9 @@ public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcExcept
9091
if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
9192
config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));
9293
final JedisPool jedisPool = new JedisPool(config, url.getHost(), url.getPort(DEFAULT_PORT),
93-
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
94+
url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT),
95+
StringUtils.isBlank(url.getPassword()) ? null : url.getPassword(),
96+
url.getParameter("db.index", 0));
9497
final int expiry = url.getParameter("expiry", 0);
9598
final String get = url.getParameter("get", "get");
9699
final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");

dubbo-rpc/dubbo-rpc-redis/src/test/java/org/apache/dubbo/rpc/protocol/redis/RedisProtocolTest.java

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,33 @@
1616
*/
1717
package org.apache.dubbo.rpc.protocol.redis;
1818

19+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
20+
import org.apache.dubbo.common.Constants;
1921
import org.apache.dubbo.common.URL;
2022
import org.apache.dubbo.common.extension.ExtensionLoader;
23+
import org.apache.dubbo.common.serialize.ObjectInput;
24+
import org.apache.dubbo.common.serialize.Serialization;
2125
import org.apache.dubbo.common.utils.NetUtils;
2226
import org.apache.dubbo.rpc.Invoker;
2327
import org.apache.dubbo.rpc.Protocol;
2428
import org.apache.dubbo.rpc.ProxyFactory;
2529
import org.apache.dubbo.rpc.RpcException;
30+
import org.apache.dubbo.rpc.RpcResult;
2631
import org.junit.After;
32+
import org.junit.Assert;
2733
import org.junit.Before;
34+
import org.junit.Rule;
2835
import org.junit.Test;
36+
import org.junit.rules.TestName;
37+
import redis.clients.jedis.Jedis;
38+
import redis.clients.jedis.JedisPool;
39+
import redis.clients.jedis.exceptions.JedisConnectionException;
40+
import redis.clients.jedis.exceptions.JedisDataException;
2941
import redis.embedded.RedisServer;
3042

43+
import java.io.ByteArrayInputStream;
44+
import java.io.IOException;
45+
3146
import static org.hamcrest.CoreMatchers.is;
3247
import static org.hamcrest.CoreMatchers.nullValue;
3348
import static org.junit.Assert.assertThat;
@@ -38,12 +53,21 @@ public class RedisProtocolTest {
3853
private RedisServer redisServer;
3954
private URL registryUrl;
4055

56+
@Rule
57+
public TestName name = new TestName();
58+
4159
@Before
4260
public void setUp() throws Exception {
4361
int redisPort = NetUtils.getAvailablePort();
44-
this.redisServer = new RedisServer(redisPort);
62+
if (name.getMethodName().equals("testAuthRedis") || name.getMethodName().equals("testWrongAuthRedis")) {
63+
String password = "123456";
64+
this.redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build();
65+
this.registryUrl = URL.valueOf("redis://username:"+password+"@localhost:"+redisPort+"?db.index=0");
66+
} else {
67+
this.redisServer = RedisServer.builder().port(redisPort).build();
68+
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
69+
}
4570
this.redisServer.start();
46-
this.registryUrl = URL.valueOf("redis://localhost:" + redisPort);
4771
}
4872

4973
@After
@@ -109,4 +133,90 @@ public void testWrongRedis() {
109133
public void testExport() {
110134
protocol.export(protocol.refer(IDemoService.class, registryUrl));
111135
}
136+
137+
@Test
138+
public void testAuthRedis() {
139+
// default db.index=0
140+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
141+
registryUrl
142+
.addParameter("max.idle", 10)
143+
.addParameter("max.active", 20));
144+
IDemoService demoService = this.proxy.getProxy(refer);
145+
146+
String value = demoService.get("key");
147+
assertThat(value, is(nullValue()));
148+
149+
demoService.set("key", "newValue");
150+
value = demoService.get("key");
151+
assertThat(value, is("newValue"));
152+
153+
demoService.delete("key");
154+
value = demoService.get("key");
155+
assertThat(value, is(nullValue()));
156+
157+
refer.destroy();
158+
159+
//change db.index=1
160+
String password = "123456";
161+
int database = 1;
162+
this.registryUrl = this.registryUrl.setPassword(password).addParameter("db.index", database);
163+
refer = protocol.refer(IDemoService.class,
164+
registryUrl
165+
.addParameter("max.idle", 10)
166+
.addParameter("max.active", 20));
167+
demoService = this.proxy.getProxy(refer);
168+
169+
demoService.set("key", "newValue");
170+
value = demoService.get("key");
171+
assertThat(value, is("newValue"));
172+
173+
// jedis gets the result comparison
174+
JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String)null);
175+
Jedis jedis = null;
176+
try {
177+
jedis = pool.getResource();
178+
byte[] valueByte = jedis.get("key".getBytes());
179+
Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(this.registryUrl.getParameter(Constants.SERIALIZATION_KEY, "java"));
180+
ObjectInput oin = serialization.deserialize(this.registryUrl, new ByteArrayInputStream(valueByte));
181+
String actual = (String) oin.readObject();
182+
assertThat(value, is(actual));
183+
} catch(Exception e) {
184+
Assert.fail("jedis gets the result comparison is error!");
185+
} finally {
186+
if (jedis != null) {
187+
jedis.close();
188+
}
189+
pool.destroy();
190+
}
191+
192+
demoService.delete("key");
193+
value = demoService.get("key");
194+
assertThat(value, is(nullValue()));
195+
196+
refer.destroy();
197+
}
198+
199+
@Test
200+
public void testWrongAuthRedis() {
201+
String password = "1234567";
202+
this.registryUrl = this.registryUrl.setPassword(password);
203+
Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
204+
registryUrl
205+
.addParameter("max.idle", 10)
206+
.addParameter("max.active", 20));
207+
IDemoService demoService = this.proxy.getProxy(refer);
208+
209+
try {
210+
String value = demoService.get("key");
211+
assertThat(value, is(nullValue()));
212+
} catch (RpcException e) {
213+
if (e.getCause() instanceof JedisConnectionException && e.getCause().getCause() instanceof JedisDataException) {
214+
Assert.assertEquals("ERR invalid password" , e.getCause().getCause().getMessage());
215+
} else {
216+
Assert.fail("no invalid password exception!");
217+
}
218+
}
219+
220+
refer.destroy();
221+
}
112222
}

0 commit comments

Comments
 (0)