9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

feat: make redis user and database configurable (#518)

* Make redis user and database configurable

* Update documentation
This commit is contained in:
Marlon Pohl
2025-06-07 02:46:17 +02:00
committed by GitHub
parent cb09e0cfb2
commit cf08015961
7 changed files with 45 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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<String> 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()

View File

@@ -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()))

View File

@@ -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()
)),