Skip to content

Commit

Permalink
feature: support authenticated redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Jun 21, 2024
1 parent 49c29d0 commit b0228c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
redis:
image: "redis:alpine"
command: [ "redis-server", "/etc/redis/redis.conf" ]
volumes:
- ./redis.conf:/etc/redis/redis.conf
Empty file added docker-compose/redis.conf
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.entur.lamassu.config.cache;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Set;
import org.entur.gbfs.validation.model.ValidationResult;
import org.entur.lamassu.cache.StationSpatialIndexId;
Expand All @@ -15,7 +17,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 +47,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 +66,32 @@ 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 b0228c1

Please sign in to comment.