|
| 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 | + |
| 9 | +package org.opensearch.rest.action.list; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.action.admin.cluster.state.ClusterStateResponse; |
| 13 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse; |
| 14 | +import org.opensearch.action.pagination.IndexPaginationStrategy; |
| 15 | +import org.opensearch.action.pagination.PageParams; |
| 16 | +import org.opensearch.cluster.ClusterName; |
| 17 | +import org.opensearch.cluster.ClusterState; |
| 18 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 19 | +import org.opensearch.cluster.metadata.Metadata; |
| 20 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 21 | +import org.opensearch.cluster.routing.RoutingTable; |
| 22 | +import org.opensearch.common.breaker.ResponseLimitSettings; |
| 23 | +import org.opensearch.common.settings.ClusterSettings; |
| 24 | +import org.opensearch.common.settings.Settings; |
| 25 | +import org.opensearch.test.OpenSearchTestCase; |
| 26 | + |
| 27 | +import java.time.Instant; |
| 28 | +import java.time.temporal.ChronoUnit; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static org.opensearch.action.pagination.PageParams.PARAM_ASC_SORT_VALUE; |
| 34 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_CREATION_DATE; |
| 35 | + |
| 36 | +public class RestIndicesListActionTests extends OpenSearchTestCase { |
| 37 | + |
| 38 | + private RestIndicesListAction createAction() { |
| 39 | + final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); |
| 40 | + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, Settings.EMPTY); |
| 41 | + return new RestIndicesListAction(responseLimitSettings); |
| 42 | + } |
| 43 | + |
| 44 | + public void testGetPaginationStrategyFiltersByGetSettingsResponse() { |
| 45 | + // Build cluster state with 3 regular indices and 2 system indices |
| 46 | + ClusterState clusterState = ClusterState.builder(new ClusterName("test")) |
| 47 | + .metadata(Metadata.builder().build()) |
| 48 | + .routingTable(RoutingTable.builder().build()) |
| 49 | + .build(); |
| 50 | + clusterState = addIndex(clusterState, "test-index-1", 1); |
| 51 | + clusterState = addIndex(clusterState, "test-index-2", 2); |
| 52 | + clusterState = addIndex(clusterState, "test-index-3", 3); |
| 53 | + clusterState = addIndex(clusterState, ".opendistro_security", 4); |
| 54 | + clusterState = addIndex(clusterState, ".system-index", 5); |
| 55 | + |
| 56 | + ClusterStateResponse clusterStateResponse = new ClusterStateResponse(new ClusterName("test"), clusterState, false); |
| 57 | + |
| 58 | + // GetSettingsResponse only contains authorized indices (simulating security plugin filtering) |
| 59 | + Map<String, Settings> authorizedSettings = new HashMap<>(); |
| 60 | + authorizedSettings.put("test-index-1", Settings.EMPTY); |
| 61 | + authorizedSettings.put("test-index-2", Settings.EMPTY); |
| 62 | + authorizedSettings.put("test-index-3", Settings.EMPTY); |
| 63 | + GetSettingsResponse getSettingsResponse = new GetSettingsResponse(authorizedSettings, Collections.emptyMap()); |
| 64 | + |
| 65 | + // Set pageParams and call getPaginationStrategy |
| 66 | + RestIndicesListAction action = createAction(); |
| 67 | + action.pageParams = new PageParams(null, PARAM_ASC_SORT_VALUE, 10); |
| 68 | + |
| 69 | + IndexPaginationStrategy strategy = action.getPaginationStrategy(clusterStateResponse, getSettingsResponse); |
| 70 | + |
| 71 | + // Only authorized indices should be returned, system indices should be filtered out |
| 72 | + assertNotNull(strategy); |
| 73 | + assertEquals(3, strategy.getRequestedEntities().size()); |
| 74 | + assertTrue(strategy.getRequestedEntities().contains("test-index-1")); |
| 75 | + assertTrue(strategy.getRequestedEntities().contains("test-index-2")); |
| 76 | + assertTrue(strategy.getRequestedEntities().contains("test-index-3")); |
| 77 | + assertFalse(strategy.getRequestedEntities().contains(".opendistro_security")); |
| 78 | + assertFalse(strategy.getRequestedEntities().contains(".system-index")); |
| 79 | + // All indices fit in one page, so no next token |
| 80 | + assertNull(strategy.getResponseToken().getNextToken()); |
| 81 | + } |
| 82 | + |
| 83 | + public void testGetPaginationStrategyPaginatesFilteredIndicesCorrectly() { |
| 84 | + // Build cluster state with 5 regular indices and 2 system indices |
| 85 | + ClusterState clusterState = ClusterState.builder(new ClusterName("test")) |
| 86 | + .metadata(Metadata.builder().build()) |
| 87 | + .routingTable(RoutingTable.builder().build()) |
| 88 | + .build(); |
| 89 | + clusterState = addIndex(clusterState, "test-index-1", 1); |
| 90 | + clusterState = addIndex(clusterState, "test-index-2", 2); |
| 91 | + clusterState = addIndex(clusterState, ".opendistro_security", 3); |
| 92 | + clusterState = addIndex(clusterState, "test-index-3", 4); |
| 93 | + clusterState = addIndex(clusterState, ".system-index", 5); |
| 94 | + clusterState = addIndex(clusterState, "test-index-4", 6); |
| 95 | + clusterState = addIndex(clusterState, "test-index-5", 7); |
| 96 | + |
| 97 | + ClusterStateResponse clusterStateResponse = new ClusterStateResponse(new ClusterName("test"), clusterState, false); |
| 98 | + |
| 99 | + Map<String, Settings> authorizedSettings = new HashMap<>(); |
| 100 | + authorizedSettings.put("test-index-1", Settings.EMPTY); |
| 101 | + authorizedSettings.put("test-index-2", Settings.EMPTY); |
| 102 | + authorizedSettings.put("test-index-3", Settings.EMPTY); |
| 103 | + authorizedSettings.put("test-index-4", Settings.EMPTY); |
| 104 | + authorizedSettings.put("test-index-5", Settings.EMPTY); |
| 105 | + GetSettingsResponse getSettingsResponse = new GetSettingsResponse(authorizedSettings, Collections.emptyMap()); |
| 106 | + |
| 107 | + RestIndicesListAction action = createAction(); |
| 108 | + |
| 109 | + // First page: size=2 |
| 110 | + action.pageParams = new PageParams(null, PARAM_ASC_SORT_VALUE, 2); |
| 111 | + IndexPaginationStrategy strategy = action.getPaginationStrategy(clusterStateResponse, getSettingsResponse); |
| 112 | + |
| 113 | + assertEquals(2, strategy.getRequestedEntities().size()); |
| 114 | + assertEquals("test-index-1", strategy.getRequestedEntities().get(0)); |
| 115 | + assertEquals("test-index-2", strategy.getRequestedEntities().get(1)); |
| 116 | + assertNotNull(strategy.getResponseToken().getNextToken()); |
| 117 | + |
| 118 | + // Second page |
| 119 | + action.pageParams = new PageParams(strategy.getResponseToken().getNextToken(), PARAM_ASC_SORT_VALUE, 2); |
| 120 | + strategy = action.getPaginationStrategy(clusterStateResponse, getSettingsResponse); |
| 121 | + |
| 122 | + assertEquals(2, strategy.getRequestedEntities().size()); |
| 123 | + assertEquals("test-index-3", strategy.getRequestedEntities().get(0)); |
| 124 | + assertEquals("test-index-4", strategy.getRequestedEntities().get(1)); |
| 125 | + assertNotNull(strategy.getResponseToken().getNextToken()); |
| 126 | + |
| 127 | + // Third page (last) |
| 128 | + action.pageParams = new PageParams(strategy.getResponseToken().getNextToken(), PARAM_ASC_SORT_VALUE, 2); |
| 129 | + strategy = action.getPaginationStrategy(clusterStateResponse, getSettingsResponse); |
| 130 | + |
| 131 | + assertEquals(1, strategy.getRequestedEntities().size()); |
| 132 | + assertEquals("test-index-5", strategy.getRequestedEntities().get(0)); |
| 133 | + assertNull(strategy.getResponseToken().getNextToken()); |
| 134 | + } |
| 135 | + |
| 136 | + private ClusterState addIndex(ClusterState clusterState, String indexName, int creationOrder) { |
| 137 | + IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 138 | + .settings( |
| 139 | + settings(Version.CURRENT).put(SETTING_CREATION_DATE, Instant.now().plus(creationOrder, ChronoUnit.SECONDS).toEpochMilli()) |
| 140 | + ) |
| 141 | + .numberOfShards(1) |
| 142 | + .numberOfReplicas(0) |
| 143 | + .build(); |
| 144 | + IndexRoutingTable.Builder indexRoutingTableBuilder = new IndexRoutingTable.Builder(indexMetadata.getIndex()); |
| 145 | + return ClusterState.builder(clusterState) |
| 146 | + .metadata(Metadata.builder(clusterState.metadata()).put(indexMetadata, true).build()) |
| 147 | + .routingTable(RoutingTable.builder(clusterState.routingTable()).add(indexRoutingTableBuilder).build()) |
| 148 | + .build(); |
| 149 | + } |
| 150 | +} |
0 commit comments