From cf080159610b41da95ee519069b2936d22289d38 Mon Sep 17 00:00:00 2001 From: Marlon Pohl <56829999+Keksnet@users.noreply.github.com> Date: Sat, 7 Jun 2025 02:46:17 +0200 Subject: [PATCH] feat: make redis user and database configurable (#518) * Make redis user and database configurable * Update documentation --- .../william278/husksync/config/Settings.java | 7 ++++++- .../husksync/redis/RedisManager.java | 19 ++++++++++++++++--- .../husksync/util/DumpProvider.java | 2 ++ .../william278/husksync/util/StatusLine.java | 4 ++++ docs/Config-File.md | 7 ++++++- docs/Redis.md | 10 ++++++++-- test/config.yml | 3 +++ 7 files changed, 45 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/config/Settings.java b/common/src/main/java/net/william278/husksync/config/Settings.java index 9a826984..6aecd69d 100644 --- a/common/src/main/java/net/william278/husksync/config/Settings.java +++ b/common/src/main/java/net/william278/husksync/config/Settings.java @@ -159,7 +159,9 @@ public class Settings { @NoArgsConstructor(access = AccessLevel.PRIVATE) public static class RedisSettings { - @Comment("Specify the credentials of your Redis server here. Set \"password\" to '' if you don't have one") + @Comment({"Specify the credentials of your Redis server here.", + "Set \"user\" to '' if you don't have one or would like to use the default user.", + "Set \"password\" to '' if you don't have one."}) private RedisCredentials credentials = new RedisCredentials(); @Getter @@ -168,6 +170,9 @@ public class Settings { public static class RedisCredentials { private String host = "localhost"; private int port = 6379; + @Comment("Only change the database if you know what you are doing. The default is 0.") + private int database = 0; + private String user = ""; private String password = ""; private boolean useSsl = false; } diff --git a/common/src/main/java/net/william278/husksync/redis/RedisManager.java b/common/src/main/java/net/william278/husksync/redis/RedisManager.java index fe289edf..fbc88e28 100644 --- a/common/src/main/java/net/william278/husksync/redis/RedisManager.java +++ b/common/src/main/java/net/william278/husksync/redis/RedisManager.java @@ -65,9 +65,11 @@ public class RedisManager extends JedisPubSub { @Blocking public void initialize() throws IllegalStateException { final Settings.RedisSettings.RedisCredentials credentials = plugin.getSettings().getRedis().getCredentials(); + final String user = credentials.getUser(); final String password = credentials.getPassword(); final String host = credentials.getHost(); final int port = credentials.getPort(); + final int database = credentials.getDatabase(); final boolean useSSL = credentials.isUseSsl(); // Create the jedis pool @@ -79,9 +81,20 @@ public class RedisManager extends JedisPubSub { final Settings.RedisSettings.RedisSentinel sentinel = plugin.getSettings().getRedis().getSentinel(); Set redisSentinelNodes = new HashSet<>(sentinel.getNodes()); if (redisSentinelNodes.isEmpty()) { - this.jedisPool = password.isEmpty() - ? new JedisPool(config, host, port, 0, useSSL) - : new JedisPool(config, host, port, 0, password, useSSL); + DefaultJedisClientConfig.Builder clientConfigBuilder = DefaultJedisClientConfig.builder() + .ssl(useSSL) + .database(database) + .timeoutMillis(0); + + if (!user.isEmpty()) { + clientConfigBuilder.user(user); + } + + if (!password.isEmpty()) { + clientConfigBuilder.password(password); + } + + this.jedisPool = new JedisPool(config, new HostAndPort(host, port), clientConfigBuilder.build()); } else { final String sentinelPassword = sentinel.getPassword(); this.jedisPool = new JedisSentinelPool(sentinel.getMaster(), redisSentinelNodes, password.isEmpty() diff --git a/common/src/main/java/net/william278/husksync/util/DumpProvider.java b/common/src/main/java/net/william278/husksync/util/DumpProvider.java index 681ae5a3..b511eb0e 100644 --- a/common/src/main/java/net/william278/husksync/util/DumpProvider.java +++ b/common/src/main/java/net/william278/husksync/util/DumpProvider.java @@ -100,6 +100,8 @@ public interface DumpProvider { Map.entry("Redis Version", StatusLine.REDIS_VERSION.getValue(getPlugin())), Map.entry("Redis Latency", StatusLine.REDIS_LATENCY.getValue(getPlugin())), Map.entry("Redis Sentinel", StatusLine.USING_REDIS_SENTINEL.getValue(getPlugin())), + Map.entry("Redis Database", StatusLine.REDIS_DATABASE.getValue(getPlugin())), + Map.entry("Redis User", StatusLine.USING_REDIS_USER.getValue(getPlugin())), Map.entry("Redis Password", StatusLine.USING_REDIS_PASSWORD.getValue(getPlugin())), Map.entry("Redis SSL", StatusLine.REDIS_USING_SSL.getValue(getPlugin())), Map.entry("Redis Local", StatusLine.IS_REDIS_LOCAL.getValue(getPlugin())) diff --git a/common/src/main/java/net/william278/husksync/util/StatusLine.java b/common/src/main/java/net/william278/husksync/util/StatusLine.java index ad9c8808..fb192588 100644 --- a/common/src/main/java/net/william278/husksync/util/StatusLine.java +++ b/common/src/main/java/net/william278/husksync/util/StatusLine.java @@ -60,6 +60,10 @@ public enum StatusLine { USING_REDIS_SENTINEL(plugin -> getBoolean( !plugin.getSettings().getRedis().getSentinel().getMaster().isBlank() )), + REDIS_DATABASE(plugin -> Component.text(plugin.getSettings().getRedis().getCredentials().getDatabase())), + USING_REDIS_USER(plugin -> getBoolean( + !plugin.getSettings().getRedis().getCredentials().getUser().isBlank() + )), USING_REDIS_PASSWORD(plugin -> getBoolean( !plugin.getSettings().getRedis().getCredentials().getPassword().isBlank() )), diff --git a/docs/Config-File.md b/docs/Config-File.md index 8bac9272..018f800c 100644 --- a/docs/Config-File.md +++ b/docs/Config-File.md @@ -65,10 +65,15 @@ database: user_data: husksync_user_data # Redis settings redis: - # Specify the credentials of your Redis server here. Set "password" to '' if you don't have one + # Specify the credentials of your Redis server here. + # Set "user" to '' if you don't have one or would like to use the default user. + # Set "password" to '' if you don't have one. credentials: host: localhost port: 6379 + # Only change the database if you know what you are doing. The default is 0. + database: 0 + user: '' password: '' use_ssl: false # Options for if you're using Redis sentinel. Don't modify this unless you know what you're doing! diff --git a/docs/Redis.md b/docs/Redis.md index bbbeb346..71f12fb0 100644 --- a/docs/Redis.md +++ b/docs/Redis.md @@ -16,10 +16,15 @@ To configure Redis, navigate to your [`config.yml`](Config-File) file and modify ```yaml # Redis settings redis: - # Specify the credentials of your Redis server here. Set "password" to '' if you don't have one + # Specify the credentials of your Redis server here. + # Set "user" to '' if you don't have one or would like to use the default user. + # Set "password" to '' if you don't have one. credentials: host: localhost port: 6379 + # Only change the database if you know what you are doing. The default is 0. + database: 0 + user: '' password: '' use_ssl: false # Options for if you're using Redis sentinel. Don't modify this unless you know what you're doing! @@ -33,8 +38,9 @@ redis: ### Credentials -Enter the hostname, port, and default user password of your Redis server. +Enter the hostname, port, user, and password of your Redis server. +If you don't have a Redis user, just use the default user password and leave the user field empty (`user: ''`). If your Redis default user doesn't have a password, leave the password field blank (`password: ''`') and the plugin will attempt to connect without a password. ### Default user password diff --git a/test/config.yml b/test/config.yml index 80e50a60..037ec148 100644 --- a/test/config.yml +++ b/test/config.yml @@ -66,6 +66,9 @@ redis: credentials: host: localhost port: 6379 + # Only change the database if you know what you are doing. The default is 0. + database: 0 + user: '' password: '' use_ssl: false