Skip to content

Add getPasswordAsChars and deprecate getPassword #4126

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public String getPassword() {
return password == null ? null : new String(password);
}

@Override
public char[] getPasswordAsChars() {
return credentialsProvider.get().getPassword().clone();
}

@Override
public Supplier<RedisCredentials> getCredentialsProvider() {
return credentialsProvider;
Expand Down Expand Up @@ -157,6 +162,7 @@ public static class Builder {

private String user = null;
private String password = null;
private char[] passwordAsChars = null;
private Supplier<RedisCredentials> credentialsProvider;
private int database = Protocol.DEFAULT_DATABASE;
private String clientName = null;
Expand Down Expand Up @@ -227,11 +233,21 @@ public Builder user(String user) {
return this;
}

/**
* @deprecated This method is deprecated in favor of {@link #passwordAsChars(char[])} due to security concerns.
* Storing passwords as Strings can lead to security risks since Strings are immutable and stay in memory
* until garbage collected. Use {@link #passwordAsChars(char[])} instead to handle passwords more securely.
*/
public Builder password(String password) {
this.password = password;
return this;
}

public Builder passwordAsChars(char[] password) {
this.passwordAsChars = password;
return this;
}

public Builder credentials(RedisCredentials credentials) {
this.credentialsProvider = new DefaultRedisCredentialsProvider(credentials);
return this;
Expand Down Expand Up @@ -357,6 +373,7 @@ public static DefaultJedisClientConfig copyConfig(JedisClientConfig copy) {
} else {
builder.user(copy.getUser());
builder.password(copy.getPassword());
builder.passwordAsChars(copy.getPasswordAsChars());
}

builder.database(copy.getDatabase());
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/JedisClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ default String getUser() {
return null;
}

/**
* @deprecated This method is deprecated in favor of {@link #getPasswordAsChars()} due to security concerns.
* Storing passwords as Strings can lead to security risks since Strings are immutable and stay in memory
* until garbage collected. Use {@link #getPasswordAsChars()} instead to handle passwords more securely.
*/
@Deprecated
default String getPassword() {
return null;
}

default char[] getPasswordAsChars() { return null; }

// TODO: return null
default Supplier<RedisCredentials> getCredentialsProvider() {
return new DefaultRedisCredentialsProvider(
Expand Down