|
11 | 11 | import org.apache.lucene.search.BooleanClause;
|
12 | 12 | import org.apache.lucene.search.BooleanQuery;
|
13 | 13 | import org.apache.lucene.search.IndexSearcher;
|
| 14 | +import org.apache.lucene.search.QueryCachingPolicy; |
14 | 15 | import org.apache.lucene.search.TermQuery;
|
15 | 16 | import org.apache.lucene.search.Weight;
|
16 | 17 | import org.apache.lucene.store.Directory;
|
| 18 | +import org.elasticsearch.Version; |
| 19 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 22 | +import org.elasticsearch.index.IndexSettings; |
| 23 | +import org.elasticsearch.indices.IndicesQueryCache; |
| 24 | +import org.elasticsearch.license.XPackLicenseState; |
17 | 25 | import org.elasticsearch.test.ESTestCase;
|
| 26 | +import org.elasticsearch.xpack.core.security.authz.AuthorizationServiceField; |
18 | 27 | import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl;
|
19 | 28 | import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions;
|
20 | 29 | import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition;
|
|
24 | 33 | import java.io.IOException;
|
25 | 34 | import java.util.HashSet;
|
26 | 35 |
|
| 36 | +import static org.mockito.Matchers.same; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 40 | +import static org.mockito.Mockito.when; |
| 41 | + |
27 | 42 | /** Simple tests for opt out query cache*/
|
28 | 43 | public class OptOutQueryCacheTests extends ESTestCase {
|
29 | 44 | IndexSearcher searcher;
|
@@ -107,6 +122,66 @@ public void testOptOutQueryCacheSafetyCheck() throws IOException {
|
107 | 122 | assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions));
|
108 | 123 | }
|
109 | 124 |
|
| 125 | + public void testOptOutQueryCacheAuthIsNotAllowed() { |
| 126 | + final Settings.Builder settings = Settings.builder() |
| 127 | + .put("index.version.created", Version.CURRENT) |
| 128 | + .put("index.number_of_shards", 1) |
| 129 | + .put("index.number_of_replicas", 0); |
| 130 | + final IndexMetaData indexMetaData = IndexMetaData.builder("index").settings(settings).build(); |
| 131 | + final IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); |
| 132 | + final IndicesQueryCache indicesQueryCache = mock(IndicesQueryCache.class); |
| 133 | + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 134 | + final XPackLicenseState licenseState = mock(XPackLicenseState.class); |
| 135 | + when(licenseState.isAuthAllowed()).thenReturn(false); |
| 136 | + final OptOutQueryCache cache = new OptOutQueryCache(indexSettings, indicesQueryCache, threadContext, licenseState); |
| 137 | + final Weight weight = mock(Weight.class); |
| 138 | + final QueryCachingPolicy policy = mock(QueryCachingPolicy.class); |
| 139 | + cache.doCache(weight, policy); |
| 140 | + verify(indicesQueryCache).doCache(same(weight), same(policy)); |
| 141 | + } |
| 142 | + |
| 143 | + public void testOptOutQueryCacheNoIndicesPermissions() { |
| 144 | + final Settings.Builder settings = Settings.builder() |
| 145 | + .put("index.version.created", Version.CURRENT) |
| 146 | + .put("index.number_of_shards", 1) |
| 147 | + .put("index.number_of_replicas", 0); |
| 148 | + final IndexMetaData indexMetaData = IndexMetaData.builder("index").settings(settings).build(); |
| 149 | + final IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); |
| 150 | + final IndicesQueryCache indicesQueryCache = mock(IndicesQueryCache.class); |
| 151 | + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 152 | + final XPackLicenseState licenseState = mock(XPackLicenseState.class); |
| 153 | + when(licenseState.isAuthAllowed()).thenReturn(true); |
| 154 | + final OptOutQueryCache cache = new OptOutQueryCache(indexSettings, indicesQueryCache, threadContext, licenseState); |
| 155 | + final Weight weight = mock(Weight.class); |
| 156 | + final QueryCachingPolicy policy = mock(QueryCachingPolicy.class); |
| 157 | + final Weight w = cache.doCache(weight, policy); |
| 158 | + assertSame(w, weight); |
| 159 | + verifyNoMoreInteractions(indicesQueryCache); |
| 160 | + } |
| 161 | + |
| 162 | + public void testOptOutQueryCacheIndexDoesNotHaveFieldLevelSecurity() { |
| 163 | + final Settings.Builder settings = Settings.builder() |
| 164 | + .put("index.version.created", Version.CURRENT) |
| 165 | + .put("index.number_of_shards", 1) |
| 166 | + .put("index.number_of_replicas", 0); |
| 167 | + final IndexMetaData indexMetaData = IndexMetaData.builder("index").settings(settings).build(); |
| 168 | + final IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); |
| 169 | + final IndicesQueryCache indicesQueryCache = mock(IndicesQueryCache.class); |
| 170 | + final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); |
| 171 | + final IndicesAccessControl.IndexAccessControl indexAccessControl = mock(IndicesAccessControl.IndexAccessControl.class); |
| 172 | + when(indexAccessControl.getFieldPermissions()).thenReturn(new FieldPermissions()); |
| 173 | + final IndicesAccessControl indicesAccessControl = mock(IndicesAccessControl.class); |
| 174 | + when(indicesAccessControl.getIndexPermissions("index")).thenReturn(indexAccessControl); |
| 175 | + threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, indicesAccessControl); |
| 176 | + final XPackLicenseState licenseState = mock(XPackLicenseState.class); |
| 177 | + when(licenseState.isAuthAllowed()).thenReturn(true); |
| 178 | + final OptOutQueryCache cache = new OptOutQueryCache(indexSettings, indicesQueryCache, threadContext, licenseState); |
| 179 | + final Weight weight = mock(Weight.class); |
| 180 | + final QueryCachingPolicy policy = mock(QueryCachingPolicy.class); |
| 181 | + cache.doCache(weight, policy); |
| 182 | + verify(indicesQueryCache).doCache(same(weight), same(policy)); |
| 183 | + } |
| 184 | + |
110 | 185 | private static FieldPermissionsDefinition fieldPermissionDef(String[] granted, String[] denied) {
|
111 | 186 | return new FieldPermissionsDefinition(granted, denied);
|
112 | 187 | }
|
|
0 commit comments