Skip to content

Commit 5039b9b

Browse files
authored
Security: cleanup code in file stores (#30348)
This commit cleans up some code in the FileUserPasswdStore and the FileUserRolesStore classes. The maps used in these classes are volatile so we need to make sure that we don't perform multiple operations with the map unless we are sure we are using a reference to the same map. The maps are also never null, but there were a few null checks in the code that were not needed. These checks have been removed.
1 parent f733de8 commit 5039b9b

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public int usersCount() {
8080
}
8181

8282
public AuthenticationResult verifyPassword(String username, SecureString password, java.util.function.Supplier<User> user) {
83-
char[] hash = users.get(username);
83+
final char[] hash = users.get(username);
8484
if (hash == null) {
8585
return AuthenticationResult.notHandled();
8686
}
@@ -91,7 +91,7 @@ public AuthenticationResult verifyPassword(String username, SecureString passwor
9191
}
9292

9393
public boolean userExists(String username) {
94-
return users != null && users.containsKey(username);
94+
return users.containsKey(username);
9595
}
9696

9797
public static Path resolveFile(Environment env) {

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ int entriesCount() {
7575
}
7676

7777
public String[] roles(String username) {
78-
if (userRoles == null) {
79-
return Strings.EMPTY_ARRAY;
80-
}
81-
String[] roles = userRoles.get(username);
82-
return roles == null ? Strings.EMPTY_ARRAY : userRoles.get(username);
78+
final String[] roles = userRoles.get(username);
79+
return roles == null ? Strings.EMPTY_ARRAY : roles;
8380
}
8481

8582
public static Path resolveFile(Environment env) {
@@ -160,11 +157,7 @@ public static Map<String, String[]> parseFile(Path path, @Nullable Logger logger
160157
}
161158

162159
for (String user : roleUsers) {
163-
List<String> roles = userToRoles.get(user);
164-
if (roles == null) {
165-
roles = new ArrayList<>();
166-
userToRoles.put(user, roles);
167-
}
160+
List<String> roles = userToRoles.computeIfAbsent(user, k -> new ArrayList<>());
168161
roles.add(role);
169162
}
170163
}
@@ -185,11 +178,7 @@ public static void writeFile(Map<String, String[]> userToRoles, Path path) {
185178
HashMap<String, List<String>> roleToUsers = new HashMap<>();
186179
for (Map.Entry<String, String[]> entry : userToRoles.entrySet()) {
187180
for (String role : entry.getValue()) {
188-
List<String> users = roleToUsers.get(role);
189-
if (users == null) {
190-
users = new ArrayList<>();
191-
roleToUsers.put(role, users);
192-
}
181+
List<String> users = roleToUsers.computeIfAbsent(role, k -> new ArrayList<>());
193182
users.add(entry.getKey());
194183
}
195184
}

0 commit comments

Comments
 (0)