Skip to content

Commit

Permalink
Adds default scopes to validation list
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 6, 2024
1 parent ccd5d07 commit bfc39ad
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@

package org.opensearch.sample.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.opensearch.accesscontrol.resources.ResourceAccessScope;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.sample.SampleResourceScope;

public class Validation {
public static ActionRequestValidationException validateScopes(Set<String> scopes) {
Set<String> validScopes = new HashSet<>();
for (SampleResourceScope scope : SampleResourceScope.values()) {
validScopes.add(scope.name());
}
validScopes.add(ResourceAccessScope.READ_ONLY);
validScopes.add(ResourceAccessScope.READ_WRITE);

for (String s : scopes) {
try {
SampleResourceScope.valueOf(s);
} catch (IllegalArgumentException | NullPointerException e) {
if (!validScopes.contains(s)) {
ActionRequestValidationException exception = new ActionRequestValidationException();
exception.addValidationError(
"Invalid scope: " + s + ". Scope must be one of: " + Arrays.toString(SampleResourceScope.values())
);
exception.addValidationError("Invalid scope: " + s + ". Scope must be one of: " + validScopes);
return exception;
}
}
Expand Down

0 comments on commit bfc39ad

Please sign in to comment.