From 56ae1eadbc53465f2a92989501eba2dd6f6173f3 Mon Sep 17 00:00:00 2001 From: Athou Date: Thu, 4 May 2023 08:48:03 +0200 Subject: [PATCH] enable redis connections with ACLs --- commafeed-server/config.dev.yml | 2 + commafeed-server/config.yml.example | 2 + commafeed-server/pom.xml | 4 +- .../backend/cache/RedisPoolFactory.java | 46 +++++++++++++++---- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/commafeed-server/config.dev.yml b/commafeed-server/config.dev.yml index 8eecd4c1..d521d20b 100644 --- a/commafeed-server/config.dev.yml +++ b/commafeed-server/config.dev.yml @@ -125,6 +125,8 @@ logging: redis: host: localhost port: 6379 + # username is only required when using ACLs + username: password: timeout: 2000 database: 0 diff --git a/commafeed-server/config.yml.example b/commafeed-server/config.yml.example index 014fe5d3..1726bdac 100644 --- a/commafeed-server/config.yml.example +++ b/commafeed-server/config.yml.example @@ -130,6 +130,8 @@ logging: redis: host: localhost port: 6379 + # username is only required when using ACLs + username: password: timeout: 2000 database: 0 diff --git a/commafeed-server/pom.xml b/commafeed-server/pom.xml index 73e95c8f..13bb2099 100644 --- a/commafeed-server/pom.xml +++ b/commafeed-server/pom.xml @@ -114,7 +114,7 @@ + implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> com.commafeed.CommaFeedApplication @@ -387,7 +387,7 @@ redis.clients jedis - 2.7.2 + 4.3.2 com.sun.mail diff --git a/commafeed-server/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java b/commafeed-server/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java index b5e6a519..d2d2d1f1 100644 --- a/commafeed-server/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java +++ b/commafeed-server/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java @@ -1,27 +1,53 @@ package com.commafeed.backend.cache; -import org.apache.commons.lang3.StringUtils; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Protocol; +@Slf4j @Getter public class RedisPoolFactory { - private final String host = "localhost"; - private final int port = Protocol.DEFAULT_PORT; - private String password; - private final int timeout = Protocol.DEFAULT_TIMEOUT; - private final int database = Protocol.DEFAULT_DATABASE; - private final int maxTotal = 500; + @JsonProperty + private String host = "localhost"; + + @JsonProperty + private int port = Protocol.DEFAULT_PORT; + + @JsonProperty + private String username; + + @JsonProperty + private String password; + + @JsonProperty + private int timeout = Protocol.DEFAULT_TIMEOUT; + + @JsonProperty + private int database = Protocol.DEFAULT_DATABASE; + + @JsonProperty + private int maxTotal = 500; public JedisPool build() { - JedisPoolConfig config = new JedisPoolConfig(); - config.setMaxTotal(maxTotal); + JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxTotal(maxTotal); - return new JedisPool(config, host, port, timeout, StringUtils.trimToNull(password), database); + JedisClientConfig clientConfig = DefaultJedisClientConfig.builder() + .user(username) + .password(password) + .timeoutMillis(timeout) + .database(database) + .build(); + + return new JedisPool(poolConfig, new HostAndPort(host, port), clientConfig); } }