|
23 | 23 | import org.apache.lucene.search.TotalHitCountCollectorManager; |
24 | 24 | import org.apache.lucene.store.Directory; |
25 | 25 | import org.elasticsearch.client.internal.Client; |
| 26 | +import org.elasticsearch.common.Strings; |
26 | 27 | import org.elasticsearch.common.bytes.BytesArray; |
27 | 28 | import org.elasticsearch.common.bytes.BytesReference; |
| 29 | +import org.elasticsearch.common.compress.CompressedXContent; |
28 | 30 | import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; |
| 31 | +import org.elasticsearch.common.lucene.search.Queries; |
29 | 32 | import org.elasticsearch.common.settings.Settings; |
30 | 33 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
31 | 34 | import org.elasticsearch.index.IndexSettings; |
32 | 35 | import org.elasticsearch.index.mapper.FieldMapper; |
33 | 36 | import org.elasticsearch.index.mapper.KeywordFieldMapper.KeywordFieldType; |
34 | 37 | import org.elasticsearch.index.mapper.MappedFieldType; |
35 | 38 | import org.elasticsearch.index.mapper.MapperMetrics; |
| 39 | +import org.elasticsearch.index.mapper.MapperService; |
36 | 40 | import org.elasticsearch.index.mapper.Mapping; |
37 | 41 | import org.elasticsearch.index.mapper.MappingLookup; |
38 | 42 | import org.elasticsearch.index.mapper.MockFieldMapper; |
| 43 | +import org.elasticsearch.index.mapper.SourceToParse; |
39 | 44 | import org.elasticsearch.index.query.ParsedQuery; |
40 | 45 | import org.elasticsearch.index.query.SearchExecutionContext; |
41 | 46 | import org.elasticsearch.index.query.TermsQueryBuilder; |
|
45 | 50 | import org.elasticsearch.search.internal.ContextIndexSearcher; |
46 | 51 | import org.elasticsearch.test.AbstractBuilderTestCase; |
47 | 52 | import org.elasticsearch.test.IndexSettingsModule; |
| 53 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 54 | +import org.elasticsearch.xcontent.XContentFactory; |
| 55 | +import org.elasticsearch.xcontent.XContentType; |
48 | 56 | import org.elasticsearch.xpack.core.security.SecurityContext; |
49 | 57 | import org.elasticsearch.xpack.core.security.authc.Authentication; |
50 | 58 | import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper; |
51 | 59 | import org.elasticsearch.xpack.core.security.authc.support.AuthenticationContextSerializer; |
52 | 60 | import org.elasticsearch.xpack.core.security.authz.permission.DocumentPermissions; |
53 | 61 | import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions; |
54 | 62 |
|
| 63 | +import java.io.IOException; |
| 64 | +import java.util.Arrays; |
55 | 65 | import java.util.HashSet; |
56 | 66 | import java.util.List; |
57 | 67 | import java.util.Set; |
@@ -340,6 +350,176 @@ protected IndicesAccessControl getIndicesAccessControl() { |
340 | 350 | directory.close(); |
341 | 351 | } |
342 | 352 |
|
| 353 | + @Override |
| 354 | + protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { |
| 355 | + XContentBuilder builder = XContentFactory.jsonBuilder() |
| 356 | + .startObject() |
| 357 | + .startObject("properties") |
| 358 | + .startObject("f1") |
| 359 | + .field("type", "keyword") |
| 360 | + .endObject() |
| 361 | + .startObject("nested1") |
| 362 | + .field("type", "nested") |
| 363 | + .startObject("properties") |
| 364 | + .startObject("field") |
| 365 | + .field("type", "keyword") |
| 366 | + .endObject() |
| 367 | + .endObject() |
| 368 | + .endObject() |
| 369 | + .endObject() |
| 370 | + .endObject(); |
| 371 | + mapperService.merge( |
| 372 | + MapperService.SINGLE_MAPPING_NAME, |
| 373 | + new CompressedXContent(Strings.toString(builder)), |
| 374 | + MapperService.MergeReason.MAPPING_UPDATE |
| 375 | + ); |
| 376 | + } |
| 377 | + |
| 378 | + public void testDLSWithNestedDocs() throws Exception { |
| 379 | + Directory directory = newDirectory(); |
| 380 | + try ( |
| 381 | + IndexWriter iw = new IndexWriter( |
| 382 | + directory, |
| 383 | + new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE) |
| 384 | + ) |
| 385 | + ) { |
| 386 | + var parser = mapperService().documentParser(); |
| 387 | + String doc = """ |
| 388 | + { |
| 389 | + "f1": "value", |
| 390 | + "nested1": [ |
| 391 | + { |
| 392 | + "field": "0" |
| 393 | + }, |
| 394 | + { |
| 395 | + "field": "1" |
| 396 | + }, |
| 397 | + {} |
| 398 | + ] |
| 399 | + } |
| 400 | + """; |
| 401 | + var parsedDoc = parser.parseDocument( |
| 402 | + new SourceToParse("0", new BytesArray(doc), XContentType.JSON), |
| 403 | + mapperService().mappingLookup() |
| 404 | + ); |
| 405 | + iw.addDocuments(parsedDoc.docs()); |
| 406 | + |
| 407 | + doc = """ |
| 408 | + { |
| 409 | + "nested1": [ |
| 410 | + { |
| 411 | + "field": "12" |
| 412 | + }, |
| 413 | + { |
| 414 | + "field": "13" |
| 415 | + }, |
| 416 | + {} |
| 417 | + ] |
| 418 | + } |
| 419 | + """; |
| 420 | + parsedDoc = parser.parseDocument( |
| 421 | + new SourceToParse("1", new BytesArray(doc), XContentType.JSON), |
| 422 | + mapperService().mappingLookup() |
| 423 | + ); |
| 424 | + iw.addDocuments(parsedDoc.docs()); |
| 425 | + |
| 426 | + doc = """ |
| 427 | + { |
| 428 | + "f1": "value", |
| 429 | + "nested1": [ |
| 430 | + { |
| 431 | + "field": "12" |
| 432 | + }, |
| 433 | + {} |
| 434 | + ] |
| 435 | + } |
| 436 | + """; |
| 437 | + parsedDoc = parser.parseDocument( |
| 438 | + new SourceToParse("2", new BytesArray(doc), XContentType.JSON), |
| 439 | + mapperService().mappingLookup() |
| 440 | + ); |
| 441 | + iw.addDocuments(parsedDoc.docs()); |
| 442 | + |
| 443 | + doc = """ |
| 444 | + { |
| 445 | + "nested1": [ |
| 446 | + { |
| 447 | + "field": "12" |
| 448 | + }, |
| 449 | + {} |
| 450 | + ] |
| 451 | + } |
| 452 | + """; |
| 453 | + parsedDoc = parser.parseDocument( |
| 454 | + new SourceToParse("3", new BytesArray(doc), XContentType.JSON), |
| 455 | + mapperService().mappingLookup() |
| 456 | + ); |
| 457 | + iw.addDocuments(parsedDoc.docs()); |
| 458 | + |
| 459 | + iw.commit(); |
| 460 | + } |
| 461 | + |
| 462 | + DirectoryReader directoryReader = ElasticsearchDirectoryReader.wrap( |
| 463 | + DirectoryReader.open(directory), |
| 464 | + new ShardId(indexSettings().getIndex(), 0) |
| 465 | + ); |
| 466 | + SearchExecutionContext context = createSearchExecutionContext(new IndexSearcher(directoryReader)); |
| 467 | + |
| 468 | + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 469 | + final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); |
| 470 | + final Authentication authentication = AuthenticationTestHelper.builder().build(); |
| 471 | + new AuthenticationContextSerializer().writeToContext(authentication, threadContext); |
| 472 | + |
| 473 | + Set<BytesReference> queries = new HashSet<>(); |
| 474 | + queries.add(new BytesArray("{\"bool\": { \"must_not\": { \"exists\": { \"field\": \"f1\" } } } }")); |
| 475 | + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl( |
| 476 | + FieldPermissions.DEFAULT, |
| 477 | + DocumentPermissions.filteredBy(queries) |
| 478 | + ); |
| 479 | + |
| 480 | + DocumentSubsetBitsetCache bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); |
| 481 | + |
| 482 | + final MockLicenseState licenseState = mock(MockLicenseState.class); |
| 483 | + when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(true); |
| 484 | + ScriptService scriptService = mock(ScriptService.class); |
| 485 | + SecurityIndexReaderWrapper wrapper = new SecurityIndexReaderWrapper( |
| 486 | + s -> context, |
| 487 | + bitsetCache, |
| 488 | + securityContext, |
| 489 | + licenseState, |
| 490 | + scriptService |
| 491 | + ) { |
| 492 | + |
| 493 | + @Override |
| 494 | + protected IndicesAccessControl getIndicesAccessControl() { |
| 495 | + IndicesAccessControl indicesAccessControl = new IndicesAccessControl( |
| 496 | + true, |
| 497 | + singletonMap(indexSettings().getIndex().getName(), indexAccessControl) |
| 498 | + ); |
| 499 | + return indicesAccessControl; |
| 500 | + } |
| 501 | + }; |
| 502 | + |
| 503 | + DirectoryReader wrappedDirectoryReader = wrapper.apply(directoryReader); |
| 504 | + IndexSearcher indexSearcher = new ContextIndexSearcher( |
| 505 | + wrappedDirectoryReader, |
| 506 | + IndexSearcher.getDefaultSimilarity(), |
| 507 | + IndexSearcher.getDefaultQueryCache(), |
| 508 | + IndexSearcher.getDefaultQueryCachingPolicy(), |
| 509 | + true |
| 510 | + ); |
| 511 | + |
| 512 | + ScoreDoc[] hits = indexSearcher.search(new MatchAllDocsQuery(), 1000).scoreDocs; |
| 513 | + assertThat(Arrays.stream(hits).map(h -> h.doc).collect(Collectors.toSet()), containsInAnyOrder(4, 5, 6, 7, 11, 12, 13)); |
| 514 | + |
| 515 | + hits = indexSearcher.search(Queries.newNonNestedFilter(context.indexVersionCreated()), 1000).scoreDocs; |
| 516 | + assertThat(Arrays.stream(hits).map(h -> h.doc).collect(Collectors.toSet()), containsInAnyOrder(7, 13)); |
| 517 | + |
| 518 | + bitsetCache.close(); |
| 519 | + directoryReader.close(); |
| 520 | + directory.close(); |
| 521 | + } |
| 522 | + |
343 | 523 | private static MappingLookup createMappingLookup(List<MappedFieldType> concreteFields) { |
344 | 524 | List<FieldMapper> mappers = concreteFields.stream().map(MockFieldMapper::new).collect(Collectors.toList()); |
345 | 525 | return MappingLookup.fromMappers(Mapping.EMPTY, mappers, emptyList()); |
|
0 commit comments