diff --git a/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java index 4f001484422..42d88802e5b 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java +++ b/dubbo-metadata-report/dubbo-metadata-report-redis/src/main/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReport.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.metadata.store.redis; +import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; @@ -37,7 +38,8 @@ public class RedisMetadataReport extends AbstractMetadataReport { public RedisMetadataReport(URL url) { super(url); - pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort()); + int timeout = url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT); + pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword()); } @Override diff --git a/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java b/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java index d7cbe346aee..4984bde5b19 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java +++ b/dubbo-metadata-report/dubbo-metadata-report-redis/src/test/java/org/apache/dubbo/metadata/store/redis/RedisMetadataReportTest.java @@ -28,7 +28,10 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import redis.clients.jedis.Jedis; +import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.exceptions.JedisDataException; import redis.embedded.RedisServer; import java.io.IOException; @@ -45,13 +48,22 @@ public class RedisMetadataReportTest { RedisMetadataReport redisMetadataReport; RedisMetadataReport syncRedisMetadataReport; RedisServer redisServer; + URL registryUrl; @BeforeEach - public void constructor() throws IOException { + public void constructor(TestInfo testInfo) throws IOException { int redisPort = NetUtils.getAvailablePort(); - this.redisServer = new RedisServer(redisPort); + String methodName = testInfo.getTestMethod().get().getName(); + if ("testAuthRedisMetadata".equals(methodName) || ("testWrongAuthRedisMetadata".equals(methodName))) { + String password = "チェリー"; + redisServer = RedisServer.builder().port(redisPort).setting("requirepass " + password).build(); + registryUrl = URL.valueOf("redis://username:" + password + "@localhost:" + redisPort); + } else { + redisServer = RedisServer.builder().port(redisPort).build(); + registryUrl = URL.valueOf("redis://localhost:" + redisPort); + } + this.redisServer.start(); - URL registryUrl = URL.valueOf("redis://localhost:" + redisPort); redisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl); URL asyncRegistryUrl = URL.valueOf("redis://localhost:" + redisPort + "?" + SYNC_REPORT_KEY + "=true"); syncRedisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl); @@ -173,4 +185,23 @@ private MetadataIdentifier storeConsumer(RedisMetadataReport redisMetadataReport return consumerMetadataIdentifier; } + @Test + public void testAuthRedisMetadata() throws ClassNotFoundException { + testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000); + } + + @Test + public void testWrongAuthRedisMetadata() throws ClassNotFoundException { + registryUrl = registryUrl.setPassword("123456"); + redisMetadataReport = (RedisMetadataReport) new RedisMetadataReportFactory().createMetadataReport(registryUrl); + try { + testStoreProvider(redisMetadataReport, "1.0.0.redis.md.p1", 3000); + } catch (RpcException e) { + if (e.getCause() instanceof JedisConnectionException && e.getCause().getCause() instanceof JedisDataException) { + Assertions.assertEquals("ERR invalid password", e.getCause().getCause().getMessage()); + } else { + Assertions.fail("no invalid password exception!"); + } + } + } }