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 (opensearch-project#1970) (opensearch-project#1983)

Signed-off-by: Craig Perkins <[email protected]>
(cherry picked from commit 50a94b4)

Co-authored-by: Craig Perkins <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and cwperks authored Aug 1, 2022
1 parent bac8ca8 commit 14494e4
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 14494e4

Please sign in to comment.