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 5, 2024
1 parent ccd5d07 commit f43dc66
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

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) {
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 f43dc66

Please sign in to comment.