Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Redis usage with username #1065

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion service-base/src/main/java/fi/nls/oskari/cache/JedisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public class JedisManager {
*/
private JedisManager() {}

public static HostAndPort getHostAndPort() {
return new HostAndPort(JedisManager.getHost(), JedisManager.getPort());
}

public static JedisClientConfig getClientConfig() {
return DefaultJedisClientConfig.builder()
.connectionTimeoutMillis(getConnectionTimeoutMs())
.user(getUser())
.password(getPassword())
.ssl(getUseSSL())
.build();
}

public static String getHost() {
return PropertyUtil.get(KEY_REDIS_HOSTNAME, "localhost");
}
Expand All @@ -51,6 +64,9 @@ private static int getConnectionTimeoutMs() {
private static String getPassword() {
return PropertyUtil.get("redis.password", null);
}
private static String getUser() {
return PropertyUtil.get("redis.user", null);
}
private static boolean getUseSSL() {
return PropertyUtil.getOptional("redis.ssl", false);
}
Expand Down Expand Up @@ -80,7 +96,7 @@ public static void connect(final int poolSize, final String host, final int port
poolConfig.setTestOnBorrow(true);
poolConfig.setBlockWhenExhausted(getBlockWhenExhausted());
final JedisPool oldPool = pool;
pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getPassword(), getUseSSL());
pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getUser(), getPassword(), getUseSSL());
// Should we use the long format to have an option to pass "client name" to Redis to help debugging issues with shared Redis instances?
// pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getSocketReadTimeoutMs(), getPassword(), Protocol.DEFAULT_DATABASE, getClientName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.util.OskariRuntimeException;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.*;

import java.util.*;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -153,7 +152,7 @@ private void startListening(String prefix) {
// NOTE!! create a new client for subscriptions instead of using pool to make sure clients don't conflict
private Jedis createClient() {
if (client == null) {
client = new Jedis(JedisManager.getHost(), JedisManager.getPort());
client = new Jedis(JedisManager.getHostAndPort(), JedisManager.getClientConfig());
}
return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import redis.clients.jedis.JedisClientConfig;

// TODO: Check if maxInactiveIntervalInSeconds can be configured
@Configuration
Expand All @@ -22,7 +24,14 @@ public class RedisSessionConfig extends WebMvcConfigurerAdapter {
public JedisConnectionFactory connectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(
JedisManager.getHost(), JedisManager.getPort());
JedisClientConfig clientConfig = JedisManager.getClientConfig();
config.setUsername(clientConfig.getUser());

RedisPassword pw = RedisPassword.of(clientConfig.getPassword());
config.setPassword(pw);
JedisConnectionFactory jedis = new JedisConnectionFactory(config);
// for some reason a call to afterPropertiesSet() is required for user/passwd to be used
jedis.afterPropertiesSet();
return jedis;
}
}
Loading