Skip to content

Commit 4692a36

Browse files
committed
Fix IllegalArgumentException when resolved indices are empty
1 parent 112559c commit 4692a36

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/integrationTest/java/org/opensearch/security/privileges/ActionPrivilegesTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.Random;
2121
import java.util.Set;
22+
import java.util.function.Supplier;
2223
import java.util.stream.Collectors;
2324

2425
import com.google.common.collect.ImmutableList;
@@ -31,9 +32,12 @@
3132
import org.junit.runners.Suite;
3233

3334
import org.opensearch.action.support.IndicesOptions;
35+
import org.opensearch.cluster.ClusterName;
36+
import org.opensearch.cluster.ClusterState;
3437
import org.opensearch.cluster.metadata.IndexAbstraction;
3538
import org.opensearch.cluster.metadata.IndexMetadata;
3639
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
40+
import org.opensearch.cluster.metadata.Metadata;
3741
import org.opensearch.common.settings.Settings;
3842
import org.opensearch.common.util.concurrent.ThreadContext;
3943
import org.opensearch.core.common.unit.ByteSizeUnit;
@@ -960,6 +964,43 @@ public void aliasesOnDataStreamBackingIndices() throws Exception {
960964
);
961965
assertThat(resultForIndexNotCoveredByAlias, isForbidden());
962966
}
967+
968+
/**
969+
* Tests the behavior of hasIndexPrivilege when the resolved indices are empty.
970+
* @throws Exception If failed.
971+
*/
972+
@Test
973+
public void hasIndexPrivilegeEmptyResolvedIndices() throws Exception {
974+
SecurityDynamicConfiguration<RoleV7> roles = SecurityDynamicConfiguration.fromYaml(
975+
"test_role:\n"
976+
+ " index_permissions:\n"
977+
+ " - index_patterns: ['*']\n"
978+
+ " allowed_actions: ['indices:monitor/recovery']",
979+
CType.ROLES
980+
);
981+
982+
PrivilegesEvaluationContext context = ctxWithState(
983+
() -> ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
984+
.metadata(Metadata.builder().build())
985+
.build(),
986+
"test_role"
987+
);
988+
989+
ActionPrivileges subject = new ActionPrivileges(
990+
roles,
991+
FlattenedActionGroups.EMPTY,
992+
Collections::emptyMap,
993+
Settings.EMPTY
994+
);
995+
996+
PrivilegesEvaluatorResponse result = subject.hasIndexPrivilege(
997+
context,
998+
ImmutableSet.of("indices:monitor/recovery"),
999+
IndexResolverReplacer.Resolved._LOCAL_ALL
1000+
);
1001+
1002+
assertThat(result, isAllowed());
1003+
}
9631004
}
9641005

9651006
/**
@@ -1071,6 +1112,10 @@ static SecurityDynamicConfiguration<RoleV7> createRoles(int numberOfRoles, int n
10711112
}
10721113

10731114
static PrivilegesEvaluationContext ctx(String... roles) {
1115+
return ctxWithState(null, roles);
1116+
}
1117+
1118+
static PrivilegesEvaluationContext ctxWithState(Supplier<ClusterState> clusterStateSupplier, String... roles) {
10741119
User user = new User("test_user");
10751120
user.addAttributes(ImmutableMap.of("attrs.dept_no", "a11"));
10761121
return new PrivilegesEvaluationContext(
@@ -1081,7 +1126,7 @@ static PrivilegesEvaluationContext ctx(String... roles) {
10811126
null,
10821127
null,
10831128
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
1084-
null
1129+
clusterStateSupplier
10851130
);
10861131
}
10871132

src/main/java/org/opensearch/security/privileges/ActionPrivileges.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,17 @@ public PrivilegesEvaluatorResponse hasIndexPrivilege(
189189
return PrivilegesEvaluatorResponse.ok();
190190
}
191191

192+
Set<String> allIndicesResolved = resolvedIndices.getAllIndicesResolved(context.getClusterStateSupplier(),
193+
context.getIndexNameExpressionResolver());
194+
195+
if (allIndicesResolved.isEmpty()) {
196+
log.debug("No resolved indices; grant the request");
197+
return PrivilegesEvaluatorResponse.ok();
198+
}
199+
192200
// TODO one might want to consider to create a semantic wrapper for action in order to be better tell apart
193201
// what's the action and what's the index in the generic parameters of CheckTable.
194-
CheckTable<String, String> checkTable = CheckTable.create(
195-
resolvedIndices.getAllIndicesResolved(context.getClusterStateSupplier(), context.getIndexNameExpressionResolver()),
196-
actions
197-
);
202+
CheckTable<String, String> checkTable = CheckTable.create(allIndicesResolved, actions);
198203

199204
StatefulIndexPrivileges statefulIndex = this.statefulIndex.get();
200205
PrivilegesEvaluatorResponse resultFromStatefulIndex = null;

0 commit comments

Comments
 (0)