Skip to content

Commit 4c0a52e

Browse files
masseykejoegallo
andauthored
[8.18] Fix geoip databases index access after system feature migration (elastic#121196) (elastic#121955)
* Fix geoip databases index access after system feature migration (elastic#121196) * fixing the test for 8.x --------- Co-authored-by: Joe Gallo <[email protected]>
1 parent dd4537d commit 4c0a52e

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

docs/changelog/121196.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 121196
2+
summary: Fix geoip databases index access after system feature migration
3+
area: Ingest Node
4+
type: bug
5+
issues: []

modules/ingest-geoip/qa/full-cluster-restart/src/javaRestTest/java/org/elasticsearch/ingest/geoip/FullClusterRestartIT.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ public void testGeoIpSystemFeaturesMigration() throws Exception {
117117

118118
// before the upgrade, Kibana should work
119119
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), List.of()));
120+
121+
// as should a normal get *
122+
assertBusy(() -> testGetStar(List.of("my-index-00001"), List.of()));
120123
} else {
121124
// after the upgrade, but before the migration, Kibana should work
122125
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), maybeSecurityIndex));
123126

127+
// as should a normal get *
128+
assertBusy(() -> testGetStar(List.of("my-index-00001"), maybeSecurityIndex));
129+
124130
// migrate the system features and give the cluster a moment to settle
125131
Request migrateSystemFeatures = new Request("POST", "/_migration/system_features");
126132
assertOK(client().performRequest(migrateSystemFeatures));
@@ -130,9 +136,10 @@ public void testGeoIpSystemFeaturesMigration() throws Exception {
130136
assertBusy(() -> testIndexGeoDoc());
131137

132138
// after the migration, Kibana should work
133-
if (useSecurity == false) { // BUT IT DOESN'T if security is enabled
134-
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), maybeSecurityIndex));
135-
}
139+
assertBusy(() -> testGetStarAsKibana(List.of("my-index-00001"), maybeSecurityIndex));
140+
141+
// as should a normal get *
142+
assertBusy(() -> testGetStar(List.of("my-index-00001"), maybeSecurityIndex));
136143

137144
Request disableDownloader = new Request("PUT", "/_cluster/settings");
138145
disableDownloader.setJsonEntity("""
@@ -211,6 +218,23 @@ private void testIndexGeoDoc() throws IOException {
211218
assertEquals("Sweden", doc.evaluate("_source.geo.country_name"));
212219
}
213220

221+
private void testGetStar(List<String> indexNames, @Nullable List<String> additionalIndexNames) throws IOException {
222+
Request getStar = new Request("GET", "*?expand_wildcards=all");
223+
getStar.setOptions(
224+
RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE) // we don't care about warnings, just errors
225+
);
226+
Response response = client().performRequest(getStar);
227+
assertOK(response);
228+
229+
if (additionalIndexNames != null && additionalIndexNames.isEmpty() == false) {
230+
indexNames = new ArrayList<>(indexNames); // recopy into a mutable list
231+
indexNames.addAll(additionalIndexNames);
232+
}
233+
234+
Map<String, Object> map = responseAsMap(response);
235+
assertThat(map.keySet(), is(new HashSet<>(indexNames)));
236+
}
237+
214238
private void testGetStarAsKibana(List<String> indexNames, @Nullable List<String> additionalIndexNames) throws IOException {
215239
Request getStar = new Request("GET", "*?expand_wildcards=all");
216240
getStar.setOptions(

server/src/main/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ public static boolean isIndexVisible(
168168
final boolean isHidden = indexAbstraction.isHidden();
169169
boolean isVisible = isHidden == false || indicesOptions.expandWildcardsHidden() || isVisibleDueToImplicitHidden(expression, index);
170170
if (indexAbstraction.getType() == IndexAbstraction.Type.ALIAS) {
171+
if (indexAbstraction.isSystem()) {
172+
// check if it is net new
173+
if (resolver.getNetNewSystemIndexPredicate().test(indexAbstraction.getName())) {
174+
return isSystemIndexVisible(resolver, indexAbstraction);
175+
}
176+
}
177+
171178
// it's an alias, ignore expandWildcardsOpen and expandWildcardsClosed.
172179
// complicated to support those options with aliases pointing to multiple indices...
173180
isVisible = isVisible && indicesOptions.ignoreAliases() == false;

server/src/test/java/org/elasticsearch/cluster/metadata/IndexAbstractionResolverTests.java

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@
1313
import org.elasticsearch.common.settings.Settings;
1414
import org.elasticsearch.common.util.concurrent.ThreadContext;
1515
import org.elasticsearch.core.Tuple;
16+
import org.elasticsearch.index.IndexVersion;
1617
import org.elasticsearch.indices.EmptySystemIndices;
1718
import org.elasticsearch.indices.InvalidIndexNameException;
19+
import org.elasticsearch.indices.SystemIndexDescriptor;
20+
import org.elasticsearch.indices.SystemIndices;
1821
import org.elasticsearch.test.ESTestCase;
22+
import org.elasticsearch.xcontent.XContentBuilder;
1923

24+
import java.io.IOException;
25+
import java.io.UncheckedIOException;
2026
import java.util.List;
2127
import java.util.Set;
2228
import java.util.concurrent.TimeUnit;
2329
import java.util.function.Supplier;
2430

31+
import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
32+
import static org.elasticsearch.indices.SystemIndices.SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY;
33+
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
2534
import static org.hamcrest.Matchers.contains;
2635
import static org.hamcrest.Matchers.containsInAnyOrder;
2736
import static org.hamcrest.Matchers.either;
@@ -220,13 +229,80 @@ private boolean isIndexVisible(String index, String selector) {
220229
"*",
221230
selector,
222231
index,
223-
IndicesOptions.strictExpandOpen(),
232+
IndicesOptions.strictExpandHidden(),
224233
metadata,
225234
indexNameExpressionResolver,
226235
true
227236
);
228237
}
229238

239+
public void testIsNetNewSystemIndexVisible() {
240+
final Settings settings = Settings.builder()
241+
.put("index.number_of_replicas", 0)
242+
.put("index.number_of_shards", 1)
243+
.put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())
244+
.build();
245+
246+
final Settings hiddenSettings = Settings.builder().put(settings).put("index.hidden", true).build();
247+
248+
final IndexMetadata foo = IndexMetadata.builder(".foo").settings(hiddenSettings).system(true).build();
249+
final IndexMetadata barReindexed = IndexMetadata.builder(".bar-reindexed")
250+
.settings(hiddenSettings)
251+
.system(true)
252+
.putAlias(AliasMetadata.builder(".bar").isHidden(true).build())
253+
.build();
254+
final IndexMetadata other = IndexMetadata.builder("other").settings(settings).build();
255+
256+
final SystemIndexDescriptor fooDescriptor = SystemIndexDescriptor.builder()
257+
.setDescription("foo indices")
258+
.setOrigin("foo origin")
259+
.setVersionMetaKey("version")
260+
.setPrimaryIndex(".foo")
261+
.setIndexPattern(".foo*")
262+
.setSettings(settings)
263+
.setMappings(mappings())
264+
.setNetNew()
265+
.build();
266+
final SystemIndexDescriptor barDescriptor = SystemIndexDescriptor.builder()
267+
.setDescription("bar indices")
268+
.setOrigin("bar origin")
269+
.setVersionMetaKey("version")
270+
.setPrimaryIndex(".bar")
271+
.setIndexPattern(".bar*")
272+
.setSettings(settings)
273+
.setMappings(mappings())
274+
.setNetNew()
275+
.build();
276+
final SystemIndices systemIndices = new SystemIndices(
277+
List.of(new SystemIndices.Feature("name", "description", List.of(fooDescriptor, barDescriptor)))
278+
);
279+
280+
final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
281+
threadContext.putHeader(SYSTEM_INDEX_ACCESS_CONTROL_HEADER_KEY, "false");
282+
indexNameExpressionResolver = new IndexNameExpressionResolver(threadContext, systemIndices);
283+
indexAbstractionResolver = new IndexAbstractionResolver(indexNameExpressionResolver);
284+
285+
metadata = Metadata.builder().put(foo, true).put(barReindexed, true).put(other, true).build();
286+
287+
assertThat(isIndexVisible("other", "*"), is(true));
288+
assertThat(isIndexVisible(".foo", "*"), is(false));
289+
assertThat(isIndexVisible(".bar", "*"), is(false));
290+
}
291+
292+
private static XContentBuilder mappings() {
293+
try (XContentBuilder builder = jsonBuilder()) {
294+
return builder.startObject()
295+
.startObject(SINGLE_MAPPING_NAME)
296+
.startObject("_meta")
297+
.field(SystemIndexDescriptor.VERSION_META_KEY, 0)
298+
.endObject()
299+
.endObject()
300+
.endObject();
301+
} catch (IOException e) {
302+
throw new UncheckedIOException(e);
303+
}
304+
}
305+
230306
private List<String> resolveAbstractionsSelectorNotAllowed(List<String> expressions) {
231307
return resolveAbstractions(expressions, IndicesOptions.strictExpandHiddenNoSelectors(), defaultMask);
232308
}

0 commit comments

Comments
 (0)