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.
This commit is contained in:
Jay Modi 2018-05-10 13:28:19 -06:00 committed by GitHub
parent f733de8e67
commit 5039b9bcb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 17 deletions

View File

@ -80,7 +80,7 @@ public class FileUserPasswdStore {
}
public AuthenticationResult verifyPassword(String username, SecureString password, java.util.function.Supplier<User> user) {
char[] hash = users.get(username);
final char[] hash = users.get(username);
if (hash == null) {
return AuthenticationResult.notHandled();
}
@ -91,7 +91,7 @@ public class FileUserPasswdStore {
}
public boolean userExists(String username) {
return users != null && users.containsKey(username);
return users.containsKey(username);
}
public static Path resolveFile(Environment env) {

View File

@ -75,11 +75,8 @@ public class FileUserRolesStore {
}
public String[] roles(String username) {
if (userRoles == null) {
return Strings.EMPTY_ARRAY;
}
String[] roles = userRoles.get(username);
return roles == null ? Strings.EMPTY_ARRAY : userRoles.get(username);
final String[] roles = userRoles.get(username);
return roles == null ? Strings.EMPTY_ARRAY : roles;
}
public static Path resolveFile(Environment env) {
@ -160,11 +157,7 @@ public class FileUserRolesStore {
}
for (String user : roleUsers) {
List<String> roles = userToRoles.get(user);
if (roles == null) {
roles = new ArrayList<>();
userToRoles.put(user, roles);
}
List<String> roles = userToRoles.computeIfAbsent(user, k -> new ArrayList<>());
roles.add(role);
}
}
@ -185,11 +178,7 @@ public class FileUserRolesStore {
HashMap<String, List<String>> roleToUsers = new HashMap<>();
for (Map.Entry<String, String[]> entry : userToRoles.entrySet()) {
for (String role : entry.getValue()) {
List<String> users = roleToUsers.get(role);
if (users == null) {
users = new ArrayList<>();
roleToUsers.put(role, users);
}
List<String> users = roleToUsers.computeIfAbsent(role, k -> new ArrayList<>());
users.add(entry.getKey());
}
}