Skip to content

Commit

Permalink
Handle null parsing in util
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Dec 16, 2024
1 parent 400972e commit 2600160
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,14 @@ private RestChannelConsumer handlePost(RestRequest request, NodeClient client) {
* Extracts cluster permissions from the request body
*/
List<String> extractClusterPermissions(Map<String, Object> requestBody) {
if (!requestBody.containsKey(CLUSTER_PERMISSIONS_FIELD)) {
return Collections.emptyList();
}

return ParsingUtils.safeStringList(requestBody.get(CLUSTER_PERMISSIONS_FIELD), CLUSTER_PERMISSIONS_FIELD);
}

/**
* Extracts and builds index permissions from the request body
*/
List<ApiToken.IndexPermission> extractIndexPermissions(Map<String, Object> requestBody) {
if (!requestBody.containsKey(INDEX_PERMISSIONS_FIELD)) {
return Collections.emptyList();
}

List<Map<String, Object>> indexPerms = ParsingUtils.safeMapList(requestBody.get(INDEX_PERMISSIONS_FIELD), INDEX_PERMISSIONS_FIELD);

return indexPerms.stream().map(this::createIndexPermission).collect(Collectors.toList());
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/opensearch/security/util/ParsingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package org.opensearch.security.util;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -21,6 +22,9 @@ public class ParsingUtils {
* Safely casts an Object to List<String> with validation
*/
public static List<String> safeStringList(Object obj, String fieldName) {
if (obj == null) {
return Collections.emptyList();
}
if (!(obj instanceof List<?> list)) {
throw new IllegalArgumentException(fieldName + " must be an array");
}
Expand All @@ -39,6 +43,9 @@ public static List<String> safeStringList(Object obj, String fieldName) {
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> safeMapList(Object obj, String fieldName) {
if (obj == null) {
return Collections.emptyList();
}
if (!(obj instanceof List<?> list)) {
throw new IllegalArgumentException(fieldName + " must be an array");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.opensearch.security.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +29,9 @@ public class ParsingUtilsTest {

@Test
public void testSafeStringList() {
List<String> emptyResult = safeStringList(null, "test_field");
assertThat(emptyResult, is(Collections.emptyList()));

List<String> result = safeStringList(Arrays.asList("test1", "test2"), "test_field");
assertThat(result, is(Arrays.asList("test1", "test2")));

Expand All @@ -40,7 +44,9 @@ public void testSafeStringList() {

@Test
public void testSafeMapList() {
// Test valid map list
List<Map<String, Object>> emptyResult = safeMapList(null, "test_field");
assertThat(emptyResult, is(Collections.emptyList()));

Map<String, Object> map1 = new HashMap<>();
map1.put("key1", "value1");
map1.put("key2", 123);
Expand Down

0 comments on commit 2600160

Please sign in to comment.