Skip to content

Commit ceb36db

Browse files
committed
Test fix
1 parent 4169f06 commit ceb36db

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,80 @@
1111

1212
package org.opensearch.security.privileges;
1313

14+
import java.util.Set;
15+
16+
import org.junit.Test;
17+
18+
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest;
19+
import org.opensearch.action.search.SearchRequest;
20+
import org.opensearch.action.support.ActionRequestMetadata;
1421
import org.opensearch.cluster.ClusterState;
1522
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
1623
import org.opensearch.cluster.metadata.Metadata;
24+
import org.opensearch.cluster.metadata.OptionallyResolvedIndices;
25+
import org.opensearch.cluster.metadata.ResolvedIndices;
1726
import org.opensearch.common.settings.Settings;
1827
import org.opensearch.common.util.concurrent.ThreadContext;
1928
import org.opensearch.security.util.MockIndexMetadataBuilder;
29+
import org.opensearch.security.util.MockPrivilegeEvaluationContextBuilder;
30+
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertFalse;
33+
import static org.junit.Assert.fail;
34+
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.when;
2036

2137
public class IndicesRequestResolverTest {
2238

23-
static final Metadata metadata = MockIndexMetadataBuilder.indices("index1", "index2", "index3").build();
39+
static final Metadata metadata = MockIndexMetadataBuilder.indices("index_a1", "index_a2", "index_b1", "index_b2").build();
2440
final static ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build();
2541
static final IndicesRequestResolver subject = new IndicesRequestResolver(
2642
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY))
2743
);
28-
/*
44+
2945
@Test
3046
public void resolve_normal() {
3147
SearchRequest request = new SearchRequest("index1");
3248
ActionRequestMetadata<SearchRequest, ?> actionRequestMetadata = mock();
3349
ResolvedIndices resolvedIndices = ResolvedIndices.of("index1");
34-
when(actionRequestMetadata.resolvedIndices()).thenReturn(Optional.of(resolvedIndices));
50+
when(actionRequestMetadata.resolvedIndices()).thenReturn(resolvedIndices);
3551

36-
ResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
52+
OptionallyResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
3753
assertEquals(resolvedIndices, returnedResolvedIndices);
3854
}
3955

4056
@Test
4157
public void resolve_fallback() {
4258
SearchRequest request = new SearchRequest("index1");
4359
ActionRequestMetadata<SearchRequest, ?> actionRequestMetadata = mock();
44-
when(actionRequestMetadata.resolvedIndices()).thenReturn(Optional.empty());
60+
when(actionRequestMetadata.resolvedIndices()).thenReturn(OptionallyResolvedIndices.unknown());
4561

46-
ResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
47-
assertEquals(Set.of("index1"), returnedResolvedIndices.local().names());
62+
OptionallyResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
63+
if (returnedResolvedIndices instanceof ResolvedIndices castReturnedResovledIndices) {
64+
assertEquals(Set.of("index1"), castReturnedResovledIndices.local().names());
65+
} else {
66+
fail("Expected ResolvedIndices, got: " + returnedResolvedIndices);
67+
}
4868
}
4969

5070
@Test
5171
public void resolve_fallbackUnsupported() {
5272
ClusterStatsRequest request = new ClusterStatsRequest();
5373
ActionRequestMetadata<SearchRequest, ?> actionRequestMetadata = mock();
54-
when(actionRequestMetadata.resolvedIndices()).thenReturn(Optional.empty());
74+
when(actionRequestMetadata.resolvedIndices()).thenReturn(OptionallyResolvedIndices.unknown());
5575

56-
ResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
57-
assertTrue("Expected isAll(), got: " + returnedResolvedIndices, returnedResolvedIndices.local().isAll());
76+
OptionallyResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, () -> clusterState);
77+
assertFalse(returnedResolvedIndices instanceof ResolvedIndices);
5878
}
5979

6080
@Test
6181
public void resolve_withPrivilegesEvaluationContext() {
62-
SearchRequest request = new SearchRequest("index*");
82+
SearchRequest request = new SearchRequest("index_a*");
6383
ActionRequestMetadata<SearchRequest, ?> actionRequestMetadata = mock();
64-
when(actionRequestMetadata.resolvedIndices()).thenReturn(Optional.empty());
84+
when(actionRequestMetadata.resolvedIndices()).thenReturn(OptionallyResolvedIndices.unknown());
6585
PrivilegesEvaluationContext context = MockPrivilegeEvaluationContextBuilder.ctx().clusterState(clusterState).get();
6686

67-
ResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, context);
68-
assertEquals(Set.of("index1", "index2", "index3"), returnedResolvedIndices.local().names());
69-
}*/
87+
OptionallyResolvedIndices returnedResolvedIndices = subject.resolve(request, actionRequestMetadata, context);
88+
assertEquals(Set.of("index_a1", "index_a2"), returnedResolvedIndices.local().names(clusterState));
89+
}
7090
}

src/integrationTest/java/org/opensearch/security/privileges/int_tests/DashboardMultiTenancyIntTests.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -595,20 +595,20 @@ public void bulk_withTenantHeader_humanResources() {
595595
""";
596596

597597
TestRestClient.HttpResponse response = restClient.postJson(
598-
"_bulk?pretty",
599-
bulkBody,
600-
new BasicHeader("securitytenant", "human_resources")
598+
"_bulk?pretty",
599+
bulkBody,
600+
new BasicHeader("securitytenant", "human_resources")
601601
);
602602

603603
assertThat(
604-
response,
605-
containsExactly(dashboards_index_human_resources).at("items[*].index[?(@.result == 'created')]._index")
606-
.butForbiddenIfIncomplete(user.reference(WRITE))
604+
response,
605+
containsExactly(dashboards_index_human_resources).at("items[*].index[?(@.result == 'created')]._index")
606+
.butForbiddenIfIncomplete(user.reference(WRITE))
607607
);
608608
} finally {
609609
delete(
610-
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_1",
611-
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_2"
610+
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_1",
611+
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_2"
612612
);
613613
}
614614
}
@@ -624,20 +624,21 @@ public void bulk_withTenantHeader_direct_humanResources_alias() {
624624
""";
625625

626626
TestRestClient.HttpResponse response = restClient.postJson(
627-
"_bulk?pretty",
628-
bulkBody,
629-
new BasicHeader("securitytenant", "human_resources")
627+
"_bulk?pretty",
628+
bulkBody,
629+
new BasicHeader("securitytenant", "human_resources")
630630
);
631631

632632
assertThat(
633-
response,
634-
containsExactly(dashboards_index_human_resources).at("items[*].index[?(@.result == 'created')]._index")
635-
.reducedBy(user.reference(WRITE)).whenEmpty(isOk())
633+
response,
634+
containsExactly(dashboards_index_human_resources).at("items[*].index[?(@.result == 'created')]._index")
635+
.reducedBy(user.reference(WRITE))
636+
.whenEmpty(isOk())
636637
);
637638
} finally {
638639
delete(
639-
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_1",
640-
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_2"
640+
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_1",
641+
dashboards_index_human_resources.name() + "/_doc/mt_bulk_doc_2"
641642
);
642643
}
643644
}
@@ -653,21 +654,19 @@ public void bulk_withTenantHeader_direct_global_index() {
653654
""";
654655

655656
TestRestClient.HttpResponse response = restClient.postJson(
656-
"_bulk?pretty",
657-
bulkBody,
658-
new BasicHeader("securitytenant", "human_resources")
657+
"_bulk?pretty",
658+
bulkBody,
659+
new BasicHeader("securitytenant", "human_resources")
659660
);
660661

661662
assertThat(
662-
response,
663-
containsExactly(dashboards_index_global).at("items[*].index[?(@.result == 'created')]._index")
664-
.reducedBy(user.reference(WRITE)).whenEmpty(isOk())
663+
response,
664+
containsExactly(dashboards_index_global).at("items[*].index[?(@.result == 'created')]._index")
665+
.reducedBy(user.reference(WRITE))
666+
.whenEmpty(isOk())
665667
);
666668
} finally {
667-
delete(
668-
dashboards_index_global.name() + "/_doc/mt_bulk_doc_1",
669-
dashboards_index_global.name() + "/_doc/mt_bulk_doc_2"
670-
);
669+
delete(dashboards_index_global.name() + "/_doc/mt_bulk_doc_1", dashboards_index_global.name() + "/_doc/mt_bulk_doc_2");
671670
}
672671
}
673672

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import org.opensearch.cluster.metadata.OptionallyResolvedIndices;
2121
import org.opensearch.cluster.metadata.ResolvedIndices;
2222

23+
/**
24+
* Provides a thin wrapper around ActionRequestMetadata.resolveIndices(), adding a fallback mechanism in case the
25+
* particular action does not support it.
26+
*/
2327
public class IndicesRequestResolver {
2428
protected final IndexNameExpressionResolver indexNameExpressionResolver;
2529

@@ -56,5 +60,4 @@ private OptionallyResolvedIndices resolveFallback(ActionRequest request, Cluster
5660
return ResolvedIndices.unknown();
5761
}
5862
}
59-
6063
}

0 commit comments

Comments
 (0)