Skip to content

Commit f66722f

Browse files
author
Christoph Büscher
committed
Fix potential NPE in UsersTool (#37660)
It looks like the output of FileUserPasswdStore.parseFile shouldn't be wrapped into another map since its output can be null. Doing this wrapping after the null check (which potentially raises an exception) instead.
1 parent 9a5b959 commit f66722f

File tree

1 file changed

+3
-1
lines changed
  • x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool

1 file changed

+3
-1
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import joptsimple.OptionSet;
99
import joptsimple.OptionSpec;
10+
1011
import org.elasticsearch.cli.EnvironmentAwareCommand;
1112
import org.elasticsearch.cli.ExitCodes;
1213
import org.elasticsearch.cli.LoggingAwareMultiCommand;
@@ -221,14 +222,15 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
221222

222223
Path file = FileUserPasswdStore.resolveFile(env);
223224
FileAttributesChecker attributesChecker = new FileAttributesChecker(file);
224-
Map<String, char[]> users = new HashMap<>(FileUserPasswdStore.parseFile(file, null, env.settings()));
225+
Map<String, char[]> users = FileUserPasswdStore.parseFile(file, null, env.settings());
225226
if (users == null) {
226227
throw new UserException(ExitCodes.CONFIG, "Configuration file [" + file + "] is missing");
227228
}
228229
if (users.containsKey(username) == false) {
229230
throw new UserException(ExitCodes.NO_USER, "User [" + username + "] doesn't exist");
230231
}
231232
final Hasher hasher = Hasher.resolve(XPackSettings.PASSWORD_HASHING_ALGORITHM.get(env.settings()));
233+
users = new HashMap<>(users); // make modifiable
232234
users.put(username, hasher.hash(new SecureString(password)));
233235
FileUserPasswdStore.writeFile(users, file);
234236

0 commit comments

Comments
 (0)