Skip to content

Commit

Permalink
Merge pull request #487 from entur/support-authenticated-redis-connec…
Browse files Browse the repository at this point in the history
…tion

feature: support authenticated redis connection
  • Loading branch information
testower authored Jun 21, 2024
2 parents 2195466 + a0cb4a4 commit 78345c6
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.codec.Kryo5Codec;
import org.redisson.config.BaseConfig;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.SingleServerConfig;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -42,8 +45,11 @@ public RedissonCacheConfig(
@Value("${org.entur.lamassu.redis.master.host}") String masterHost,
@Value("${org.entur.lamassu.redis.master.port}") String masterPort,
@Value("${org.entur.lamassu.redis.slave.enabled:false}") boolean slaveEnabled,
@Value("${org.entur.lamassu.redis.slave.host:na}") String slaveHost,
@Value("${org.entur.lamassu.redis.slave.port:na}") String slavePort,
@Value("${org.entur.lamassu.redis.slave.host:}") String slaveHost,
@Value("${org.entur.lamassu.redis.slave.port:}") String slavePort,
@Value(
"${org.entur.lamassu.redis.authentication.string:}"
) String authenticationString,
LamassuProjectInfoConfiguration lamassuProjectInfoConfiguration
) {
serializationVersion = lamassuProjectInfoConfiguration.getSerializationVersion();
Expand All @@ -58,13 +64,25 @@ public RedissonCacheConfig(

if (slaveEnabled) {
var slaveAddress = String.format("redis://%s:%s", slaveHost, slavePort);

redissonConfig
.useMasterSlaveServers()
MasterSlaveServersConfig masterSlaveServersConfig =
redissonConfig.useMasterSlaveServers();
masterSlaveServersConfig
.setMasterAddress(masterAddress)
.setSlaveAddresses(Set.of(slaveAddress));
configureRedisAuthentication(masterSlaveServersConfig, authenticationString);
} else {
redissonConfig.useSingleServer().setAddress(masterAddress);
SingleServerConfig singleServerConfig = redissonConfig.useSingleServer();
singleServerConfig.setAddress(masterAddress);
configureRedisAuthentication(singleServerConfig, authenticationString);
}
}

private <T extends BaseConfig<T>> void configureRedisAuthentication(
BaseConfig<T> config,
String authenticationString
) {
if (!authenticationString.isEmpty()) {
config.setPassword(authenticationString);
}
}

Expand Down

0 comments on commit 78345c6

Please sign in to comment.