|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.opensearch.security.privileges; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.junit.runners.Parameterized; |
| 22 | +import org.junit.runners.Suite; |
| 23 | + |
| 24 | +import org.opensearch.action.ActionRequest; |
| 25 | +import org.opensearch.action.IndicesRequest; |
| 26 | +import org.opensearch.action.OriginalIndices; |
| 27 | +import org.opensearch.action.index.IndexRequest; |
| 28 | +import org.opensearch.action.search.SearchRequest; |
| 29 | +import org.opensearch.action.support.IndicesOptions; |
| 30 | +import org.opensearch.cluster.ClusterState; |
| 31 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 32 | +import org.opensearch.cluster.metadata.Metadata; |
| 33 | +import org.opensearch.cluster.metadata.ResolvedIndices; |
| 34 | +import org.opensearch.common.settings.Settings; |
| 35 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 36 | +import org.opensearch.security.util.MockIndexMetadataBuilder; |
| 37 | + |
| 38 | +import static org.junit.Assert.assertArrayEquals; |
| 39 | +import static org.junit.Assert.assertEquals; |
| 40 | +import static org.junit.Assert.assertFalse; |
| 41 | +import static org.junit.Assert.assertTrue; |
| 42 | + |
| 43 | +@RunWith(Suite.class) |
| 44 | +@Suite.SuiteClasses({ IndexRequestModifierTest.SetLocalIndices.class, IndexRequestModifierTest.SetLocalIndicesToEmpty.class }) |
| 45 | +public class IndexRequestModifierTest { |
| 46 | + |
| 47 | + static final IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver( |
| 48 | + new ThreadContext(Settings.EMPTY) |
| 49 | + ); |
| 50 | + static final Metadata metadata = MockIndexMetadataBuilder.indices("index", "index1", "index2", "index3").build(); |
| 51 | + final static ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build(); |
| 52 | + static final IndicesRequestModifier subject = new IndicesRequestModifier(); |
| 53 | + |
| 54 | + public static class SetLocalIndices { |
| 55 | + @Test |
| 56 | + public void basic() { |
| 57 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index1"); |
| 58 | + SearchRequest request = new SearchRequest("index1", "index2", "index3"); |
| 59 | + |
| 60 | + boolean success = subject.setLocalIndices(request, resolvedIndices, Collections.singletonList("index1")); |
| 61 | + assertTrue(success); |
| 62 | + assertArrayEquals(new String[] { "index1" }, request.indices()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void withRemote() { |
| 67 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index1") |
| 68 | + .withRemoteIndices( |
| 69 | + Map.of("remote", new OriginalIndices(new String[] { "index_remote" }, IndicesOptions.LENIENT_EXPAND_OPEN)) |
| 70 | + ); |
| 71 | + SearchRequest request = new SearchRequest("index1", "index2", "index3", "remote:index_remote"); |
| 72 | + |
| 73 | + boolean success = subject.setLocalIndices(request, resolvedIndices, Collections.singletonList("index1")); |
| 74 | + assertTrue(success); |
| 75 | + assertArrayEquals(new String[] { "index1", "remote:index_remote" }, request.indices()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void empty() { |
| 80 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index1"); |
| 81 | + SearchRequest request = new SearchRequest("index1", "index2", "index3"); |
| 82 | + |
| 83 | + boolean success = subject.setLocalIndices(request, resolvedIndices, Collections.emptyList()); |
| 84 | + assertTrue(success); |
| 85 | + String[] finalResolvedIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request); |
| 86 | + assertArrayEquals(new String[0], finalResolvedIndices); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void unsupportedType() { |
| 91 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index1"); |
| 92 | + IndexRequest request = new IndexRequest("index1"); |
| 93 | + |
| 94 | + boolean success = subject.setLocalIndices(request, resolvedIndices, Collections.singletonList("index1")); |
| 95 | + assertFalse(success); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @RunWith(Parameterized.class) |
| 100 | + public static class SetLocalIndicesToEmpty { |
| 101 | + |
| 102 | + String description; |
| 103 | + IndicesRequest request; |
| 104 | + |
| 105 | + @Test |
| 106 | + public void setLocalIndicesToEmpty() { |
| 107 | + |
| 108 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index"); |
| 109 | + |
| 110 | + if (Arrays.asList(request.indices()).contains("remote:index")) { |
| 111 | + resolvedIndices = resolvedIndices.withRemoteIndices( |
| 112 | + Map.of("remote", new OriginalIndices(new String[] { "index" }, request.indicesOptions())) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + boolean success = subject.setLocalIndicesToEmpty((ActionRequest) request, resolvedIndices); |
| 117 | + |
| 118 | + if (!(request instanceof IndicesRequest.Replaceable)) { |
| 119 | + assertFalse(success); |
| 120 | + } else if (!request.indicesOptions().allowNoIndices()) { |
| 121 | + assertFalse(success); |
| 122 | + } else { |
| 123 | + assertTrue(success); |
| 124 | + |
| 125 | + String[] finalResolvedIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request); |
| 126 | + |
| 127 | + assertEquals("Resolved to empty indices: " + Arrays.asList(finalResolvedIndices), 0, finalResolvedIndices.length); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Parameterized.Parameters(name = "{0}") |
| 132 | + public static Collection<Object[]> params() { |
| 133 | + return Arrays.asList( |
| 134 | + new Object[] { "lenient expand open", new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN) }, |
| 135 | + new Object[] { |
| 136 | + "lenient expand open/closed", |
| 137 | + new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED) }, |
| 138 | + new Object[] { |
| 139 | + "lenient expand open/closed/hidden", |
| 140 | + new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) }, |
| 141 | + new Object[] { |
| 142 | + "allow no indices", |
| 143 | + new SearchRequest("index").indicesOptions(IndicesOptions.fromOptions(false, true, false, false)) }, |
| 144 | + new Object[] { |
| 145 | + "ignore unavailable", |
| 146 | + new SearchRequest("index").indicesOptions(IndicesOptions.fromOptions(true, false, false, false)) }, |
| 147 | + new Object[] { |
| 148 | + "strict single index", |
| 149 | + new SearchRequest("index").indicesOptions(IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED) }, |
| 150 | + new Object[] { |
| 151 | + "with remote index", |
| 152 | + new SearchRequest("index", "remote:index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN) }, |
| 153 | + new Object[] { "not implementing IndicesRequest.Replaceable", new IndexRequest("index") } |
| 154 | + ); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + public SetLocalIndicesToEmpty(String description, IndicesRequest request) { |
| 159 | + this.description = description; |
| 160 | + this.request = request; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments