Skip to content

Commit

Permalink
[Dubbo-3826] Auth support for redis metadata report (apache#3888)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomatofrommars authored and vio-lin committed Apr 29, 2019
1 parent e5b7414 commit a36e17d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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!");
}
}
}
}

0 comments on commit a36e17d

Please sign in to comment.