Skip to content

Commit 7d83fde

Browse files
tomatofrommarsralf0131
authored andcommitted
[Dubbo-3826] Auth support for redis metadata report (apache#3888)
1 parent 793bf82 commit 7d83fde

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.metadata.store.redis;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.common.logger.Logger;
2122
import org.apache.dubbo.common.logger.LoggerFactory;
@@ -37,7 +38,8 @@ public class RedisMetadataReport extends AbstractMetadataReport {
3738

3839
public RedisMetadataReport(URL url) {
3940
super(url);
40-
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort());
41+
int timeout = url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
42+
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword());
4143
}
4244

4345
@Override

dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.junit.jupiter.api.Assertions;
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.TestInfo;
3132
import redis.clients.jedis.Jedis;
33+
import redis.clients.jedis.exceptions.JedisConnectionException;
34+
import redis.clients.jedis.exceptions.JedisDataException;
3235
import redis.embedded.RedisServer;
3336

3437
import java.io.IOException;
@@ -45,13 +48,22 @@ public class RedisMetadataReportTest {
4548
RedisMetadataReport redisMetadataReport;
4649
RedisMetadataReport syncRedisMetadataReport;
4750
RedisServer redisServer;
51+
URL registryUrl;
4852

4953
@BeforeEach
50-
public void constructor() throws IOException {
54+
public void constructor(TestInfo testInfo) throws IOException {
5155
int redisPort = NetUtils.getAvailablePort();
52-
this.redisServer = new RedisServer(redisPort);
56+
String methodName = testInfo.getTestMethod().get().getName();
57+
if ("testAuthRedisMetadata".equals(methodName) || ("testWrongAuthRedisMetadata".equals(methodName))) {
58+
String password = "チェリー";
59+
redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build();
60+
registryUrl = URL.valueOf("redis://username:" + password + "@localhost:" + redisPort);
61+
} else {
62+
redisServer = RedisServer.builder().port(redisPort).build();
63+
registryUrl = URL.valueOf("redis://localhost:" + redisPort);
64+
}
65+
5366
this.redisServer.start();
54-
URL registryUrl = URL.valueOf("redis://localhost:" + redisPort);
5567
redisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl);
5668
URL asyncRegistryUrl = URL.valueOf("redis://localhost:" + redisPort + "?" + SYNC_REPORT_KEY + "=true");
5769
syncRedisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl);
@@ -173,4 +185,23 @@ private MetadataIdentifier storeConsumer(RedisMetadataReport redisMetadataReport
173185
return consumerMetadataIdentifier;
174186
}
175187

188+
@Test
189+
public void testAuthRedisMetadata() throws ClassNotFoundException {
190+
testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000);
191+
}
192+
193+
@Test
194+
public void testWrongAuthRedisMetadata() throws ClassNotFoundException {
195+
registryUrl = registryUrl.setPassword("123456");
196+
redisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl);
197+
try {
198+
testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000);
199+
} catch (RpcException e) {
200+
if (e.getCause() instanceof JedisConnectionException && e.getCause().getCause() instanceof JedisDataException) {
201+
Assertions.assertEquals("ERR invalid password", e.getCause().getCause().getMessage());
202+
} else {
203+
Assertions.fail("no invalid password exception!");
204+
}
205+
}
206+
}
176207
}

0 commit comments

Comments
 (0)