Skip to content

Commit

Permalink
Use Collections.synchronizedSet and Collections.synchronizedMap for r…
Browse files Browse the repository at this point in the history
…oles, securityRoles and attributes in User (#1970)

Signed-off-by: Craig Perkins <cwperx@amazon.com>
(cherry picked from commit 50a94b4)
  • Loading branch information
cwperks authored and github-actions[bot] committed Aug 1, 2022
1 parent 15dacf7 commit 476df53
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/opensearch/security/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ public class User implements Serializable, Writeable, CustomAttributesAware {
/**
* roles == backend_roles
*/
private final Set<String> roles = new HashSet<String>();
private final Set<String> securityRoles = new HashSet<String>();
private final Set<String> roles = Collections.synchronizedSet(new HashSet<String>());
private final Set<String> securityRoles = Collections.synchronizedSet(new HashSet<String>());
private String requestedTenant;
private Map<String, String> attributes = new HashMap<>();
private Map<String, String> attributes = Collections.synchronizedMap(new HashMap<>());
private boolean isInjected = false;

public User(final StreamInput in) throws IOException {
super();
name = in.readString();
roles.addAll(in.readList(StreamInput::readString));
requestedTenant = in.readString();
attributes = in.readMap(StreamInput::readString, StreamInput::readString);
attributes = Collections.synchronizedMap(in.readMap(StreamInput::readString, StreamInput::readString));
securityRoles.addAll(in.readList(StreamInput::readString));
}

Expand Down Expand Up @@ -250,7 +250,7 @@ public void writeTo(StreamOutput out) throws IOException {
*/
public synchronized final Map<String, String> getCustomAttributesMap() {
if(attributes == null) {
attributes = new HashMap<>();
attributes = Collections.synchronizedMap(new HashMap<>());
}
return attributes;
}
Expand All @@ -262,6 +262,6 @@ public final void addSecurityRoles(final Collection<String> securityRoles) {
}

public final Set<String> getSecurityRoles() {
return this.securityRoles == null ? Collections.emptySet() : Collections.unmodifiableSet(this.securityRoles);
return this.securityRoles == null ? Collections.synchronizedSet(Collections.emptySet()) : Collections.unmodifiableSet(this.securityRoles);
}
}

0 comments on commit 476df53

Please sign in to comment.