Skip to content

Commit

Permalink
added support to specify authentication (password) for sharded and st…
Browse files Browse the repository at this point in the history
…andalone connection (cluster connection does not support it)

GluuFederation/oxAuth#785
  • Loading branch information
yuriyz committed Apr 17, 2018
1 parent 4cbf287 commit b252836
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.xdi.service.cache;

import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;

/**
* @author yuriyz
*/
Expand All @@ -22,6 +25,12 @@ public void testConnection() {
}
}

public void setAuthIfNeeded(Jedis jedis) {
if (StringUtils.isNotBlank(redisConfiguration.getPassword())) {
jedis.auth(redisConfiguration.getPassword());
}
}

public abstract void create();

public abstract void destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class RedisConfiguration implements Serializable {

private int defaultPutExpiration = 60; // in seconds

private String password;

public String getServers() {
return servers;
}
Expand All @@ -42,6 +44,14 @@ public void setRedisProviderType(RedisProviderType redisProviderType) {
this.redisProviderType = redisProviderType;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "RedisConfiguration{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ private static List<JedisShardInfo> shards(String servers) {

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
for (String serverWithPort : serverWithPorts) {
final String[] split = serverWithPort.trim().split(":");
String host = split[0];
int port = Integer.parseInt(split[1].trim());
shards.add(new JedisShardInfo(host, port));
serverWithPort = serverWithPort.trim();
if (serverWithPort.contains(":") && !serverWithPort.contains("@") && !servers.contains("//")) {
final String[] split = serverWithPort.trim().split(":");
String host = split[0];
int port = Integer.parseInt(split[1].trim());
shards.add(new JedisShardInfo(host, port));
} else {
shards.add(new JedisShardInfo(serverWithPort));
}
}
return shards;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void create() {
poolConfig.setMaxTotal(1000);
poolConfig.setMinIdle(2);


HostAndPort hostAndPort = RedisClusterProvider.hosts(redisConfiguration.getServers()).iterator().next();
pool = new JedisPool(poolConfig, hostAndPort.getHost(), hostAndPort.getPort());

Expand Down Expand Up @@ -61,6 +62,7 @@ public JedisPool getDelegate() {
@Override
public Object get(String key) {
Jedis jedis = pool.getResource();
setAuthIfNeeded(jedis);
try {
byte[] value = jedis.get(key.getBytes());
Object deserialized = null;
Expand All @@ -76,6 +78,7 @@ public Object get(String key) {
@Override
public void put(int expirationInSeconds, String key, Object object) {
Jedis jedis = pool.getResource();
setAuthIfNeeded(jedis);
try {
String status = jedis.setex(key.getBytes(),
expirationInSeconds,
Expand All @@ -89,6 +92,7 @@ public void put(int expirationInSeconds, String key, Object object) {
@Override
public void put(String key, Object object) {
Jedis jedis = pool.getResource();
setAuthIfNeeded(jedis);
try {
String status = jedis.set(key.getBytes(), SerializationUtils.serialize((Serializable) object));
LOG.trace("put - key: " + key + ", status: " + status);
Expand All @@ -100,6 +104,7 @@ public void put(String key, Object object) {
@Override
public void remove(String key) {
Jedis jedis = pool.getResource();
setAuthIfNeeded(jedis);
try {
Long entriesRemoved = jedis.del(key.getBytes());
LOG.trace("remove - key: " + key + ", entriesRemoved: " + entriesRemoved);
Expand All @@ -111,6 +116,7 @@ public void remove(String key) {
@Override
public void clear() {
Jedis jedis = pool.getResource();
setAuthIfNeeded(jedis);
try {
jedis.flushAll();
LOG.trace("clear");
Expand Down

0 comments on commit b252836

Please sign in to comment.