From e7e24c453cf8c041eabf3b0a83c7023167cc14dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 30 Jun 2017 16:52:47 +0200 Subject: [PATCH] Reenable SecurityIndexSearcherWrapperIntegrationTests (elastic/x-pack-elasticsearch#1894) Original commit: elastic/x-pack-elasticsearch@03ff1bf9a520c2105ca1700bba6caa135347c0d7 --- .../SecurityIndexSearcherWrapper.java | 3 +-- ...yIndexSearcherWrapperIntegrationTests.java | 20 ++----------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java index 002f6af893c..b1a2bf8f0b5 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapper.java @@ -42,7 +42,6 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.EngineException; -import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoostingQueryBuilder; import org.elasticsearch.index.query.ConstantScoreQueryBuilder; @@ -135,7 +134,7 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper { String templateResult = evaluateTemplate(bytesReference.utf8ToString()); try (XContentParser parser = XContentFactory.xContent(templateResult) .createParser(queryShardContext.getXContentRegistry(), templateResult)) { - QueryBuilder queryBuilder = AbstractQueryBuilder.parseInnerQueryBuilder(parser); + QueryBuilder queryBuilder = queryShardContext.parseInnerQueryBuilder(parser); verifyRoleQuery(queryBuilder); failIfQueryUsesClient(scriptService, queryBuilder, queryShardContext); ParsedQuery parsedQuery = queryShardContext.toFilter(queryBuilder); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperIntegrationTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperIntegrationTests.java index 691ca135e6d..0d8c6eb9fa1 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperIntegrationTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/SecurityIndexSearcherWrapperIntegrationTests.java @@ -26,13 +26,10 @@ import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContentParser; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.ParsedQuery; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.TermsQueryBuilder; @@ -51,13 +48,13 @@ import static org.hamcrest.Matchers.equalTo; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase { - @AwaitsFix(bugUrl="https://github.com/elastic/x-pack-elasticsearch/issues/1890") public void testDLS() throws Exception { ShardId shardId = new ShardId("_index", "_na_", 0); MapperService mapperService = mock(MapperService.class); @@ -77,7 +74,6 @@ public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase { QueryShardContext realQueryShardContext = new QueryShardContext(shardId.id(), indexSettings, null, null, mapperService, null, null, xContentRegistry(), client, null, () -> nowInMillis); QueryShardContext queryShardContext = spy(realQueryShardContext); - QueryParseContext queryParseContext = mock(QueryParseContext.class); IndexSettings settings = IndexSettingsModule.newIndexSettings("_index", Settings.EMPTY); BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(settings, new BitsetFilterCache.Listener() { @Override @@ -146,8 +142,7 @@ public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase { DirectoryReader directoryReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), shardId); for (int i = 0; i < numValues; i++) { ParsedQuery parsedQuery = new ParsedQuery(new TermQuery(new Term("field", values[i]))); - when(queryShardContext.newParseContext(anyParser())).thenReturn(queryParseContext); - when(AbstractQueryBuilder.parseInnerQueryBuilder(anyParser())).thenReturn(new TermQueryBuilder("field", values[i])); + doReturn(new TermQueryBuilder("field", values[i])).when(queryShardContext).parseInnerQueryBuilder(any(XContentParser.class)); when(queryShardContext.toFilter(new TermsQueryBuilder("field", values[i]))).thenReturn(parsedQuery); DirectoryReader wrappedDirectoryReader = wrapper.wrap(directoryReader); IndexSearcher indexSearcher = wrapper.wrap(new IndexSearcher(wrappedDirectoryReader)); @@ -164,15 +159,4 @@ public class SecurityIndexSearcherWrapperIntegrationTests extends ESTestCase { directoryReader.close(); directory.close(); } - - /* - QueryShardContext is spied (not mocked!) and because of that when we report a matcher can't pass in null to - queryShardContext.newParseContext(...), so we pass in a dummy parser instances. This allows us to report a - matcher and queryShardContext.newParseContext(...) fail with NPE. - */ - private static XContentParser anyParser() { - any(XContentParser.class); - return new JsonXContentParser(null, null); - } - }