|
| 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 org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.junit.runners.Parameterized; |
| 17 | +import org.junit.runners.Suite; |
| 18 | +import org.opensearch.action.ActionRequest; |
| 19 | +import org.opensearch.action.IndicesRequest; |
| 20 | +import org.opensearch.action.OriginalIndices; |
| 21 | +import org.opensearch.action.index.IndexRequest; |
| 22 | +import org.opensearch.action.search.SearchRequest; |
| 23 | +import org.opensearch.action.support.IndicesOptions; |
| 24 | +import org.opensearch.cluster.ClusterState; |
| 25 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 26 | +import org.opensearch.cluster.metadata.Metadata; |
| 27 | +import org.opensearch.cluster.metadata.ResolvedIndices; |
| 28 | +import org.opensearch.common.settings.Settings; |
| 29 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 30 | +import org.opensearch.security.util.MockIndexMetadataBuilder; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | +import static org.junit.Assert.assertFalse; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +@RunWith(Suite.class) |
| 41 | +@Suite.SuiteClasses({ |
| 42 | + IndexRequestModifierTest.SetLocalIndicesToEmpty.class }) |
| 43 | +public class IndexRequestModifierTest { |
| 44 | + |
| 45 | + static final IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)); |
| 46 | + static final Metadata metadata = MockIndexMetadataBuilder.indices("index").build(); |
| 47 | + final static ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build(); |
| 48 | + |
| 49 | + @RunWith(Parameterized.class) |
| 50 | + public static class SetLocalIndicesToEmpty { |
| 51 | + |
| 52 | + |
| 53 | + String description; |
| 54 | + IndicesRequest request; |
| 55 | + |
| 56 | + @Test |
| 57 | + public void setLocalIndicesToEmpty() { |
| 58 | + |
| 59 | + ResolvedIndices resolvedIndices = ResolvedIndices.of("index"); |
| 60 | + |
| 61 | + if (Arrays.asList(request.indices()).contains("remote:index")) { |
| 62 | + resolvedIndices = resolvedIndices.withRemoteIndices(Map.of("remote", new OriginalIndices(new String [] {"index"}, request.indicesOptions()))); |
| 63 | + } |
| 64 | + |
| 65 | + boolean success = new IndicesRequestModifier().setLocalIndicesToEmpty((ActionRequest) request, resolvedIndices); |
| 66 | + |
| 67 | + if (!(request instanceof IndicesRequest.Replaceable)) { |
| 68 | + assertFalse(success); |
| 69 | + } else |
| 70 | + if (!request.indicesOptions().allowNoIndices()) { |
| 71 | + assertFalse(success); |
| 72 | + } else { |
| 73 | + assertTrue(success); |
| 74 | + |
| 75 | + String [] finalResolvedIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request); |
| 76 | + |
| 77 | + assertEquals("Resolved to empty indices: " + Arrays.asList(finalResolvedIndices), 0, finalResolvedIndices.length); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Parameterized.Parameters(name = "{0}") |
| 83 | + public static Collection<Object[]> params() { |
| 84 | + return Arrays.asList( |
| 85 | + new Object [] { |
| 86 | + "lenient expand open", new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN) |
| 87 | + }, |
| 88 | + new Object [] { |
| 89 | + "lenient expand open/closed", new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED) |
| 90 | + }, |
| 91 | + new Object [] { |
| 92 | + "lenient expand open/closed/hidden", new SearchRequest("index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) |
| 93 | + }, |
| 94 | + new Object [] { |
| 95 | + "allow no indices", new SearchRequest("index").indicesOptions(IndicesOptions.fromOptions(false, true, false, false)) |
| 96 | + }, |
| 97 | + new Object [] { |
| 98 | + "ignore unavailable", new SearchRequest("index").indicesOptions(IndicesOptions.fromOptions(true, false, false, false)) |
| 99 | + }, |
| 100 | + new Object [] { |
| 101 | + "strict single index", new SearchRequest("index").indicesOptions(IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED) |
| 102 | + }, |
| 103 | + new Object [] { |
| 104 | + "with remote index", new SearchRequest("index", "remote:index").indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN) |
| 105 | + }, |
| 106 | + new Object [] { |
| 107 | + "not implementing IndicesRequest.Replaceable", new IndexRequest("index") |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + public SetLocalIndicesToEmpty(String description, IndicesRequest request) { |
| 114 | + this.description = description; |
| 115 | + this.request = request; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments