Skip to content

Commit 2e5945a

Browse files
authored
HLRC: PutUserRequest should not be closeable (#34196)
The PutUserRequest implemented closeable as it assumed ownership of the password provided to the class. This change removes the ownership of the password, documents it in the javadoc, and removes the closeable implementation. Additionally, the intermediate bytes used for writing the password to XContent are now cleared. This makes the PutUserRequest consistent with the behavior discussed in #33509.
1 parent 306e178 commit 2e5945a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserRequest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.xcontent.ToXContentObject;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
2727

28-
import java.io.Closeable;
2928
import java.io.IOException;
3029
import java.util.Arrays;
3130
import java.util.Collections;
@@ -37,7 +36,7 @@
3736
/**
3837
* Request object to create or update a user in the native realm.
3938
*/
40-
public final class PutUserRequest implements Validatable, Closeable, ToXContentObject {
39+
public final class PutUserRequest implements Validatable, ToXContentObject {
4140

4241
private final String username;
4342
private final List<String> roles;
@@ -48,6 +47,20 @@ public final class PutUserRequest implements Validatable, Closeable, ToXContentO
4847
private final boolean enabled;
4948
private final RefreshPolicy refreshPolicy;
5049

50+
/**
51+
* Creates a new request that is used to create or update a user in the native realm.
52+
*
53+
* @param username the username of the user to be created or updated
54+
* @param password the password of the user. The password array is not modified by this class.
55+
* It is the responsibility of the caller to clear the password after receiving
56+
* a response.
57+
* @param roles the roles that this user is assigned
58+
* @param fullName the full name of the user that may be used for display purposes
59+
* @param email the email address of the user
60+
* @param enabled true if the user is enabled and allowed to access elasticsearch
61+
* @param metadata a map of additional user attributes that may be used in templating roles
62+
* @param refreshPolicy the refresh policy for the request.
63+
*/
5164
public PutUserRequest(String username, char[] password, List<String> roles, String fullName, String email, boolean enabled,
5265
Map<String, Object> metadata, RefreshPolicy refreshPolicy) {
5366
this.username = Objects.requireNonNull(username, "username is required");
@@ -114,13 +127,6 @@ public int hashCode() {
114127
return result;
115128
}
116129

117-
@Override
118-
public void close() {
119-
if (password != null) {
120-
Arrays.fill(password, (char) 0);
121-
}
122-
}
123-
124130
@Override
125131
public Optional<ValidationException> validate() {
126132
if (metadata != null && metadata.keySet().stream().anyMatch(s -> s.startsWith("_"))) {
@@ -137,7 +143,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
137143
builder.field("username", username);
138144
if (password != null) {
139145
byte[] charBytes = CharArrays.toUtf8Bytes(password);
140-
builder.field("password").utf8Value(charBytes, 0, charBytes.length);
146+
try {
147+
builder.field("password").utf8Value(charBytes, 0, charBytes.length);
148+
} finally {
149+
Arrays.fill(charBytes, (byte) 0);
150+
}
141151
}
142152
if (roles != null) {
143153
builder.field("roles", roles);

0 commit comments

Comments
 (0)