Skip to content

Commit 4a14af4

Browse files
authored
[Backport 2.x] Fix Document GET with DLS terms query (#3136) (#3175)
Backport #3136 to 2.x
1 parent 4fc555d commit 4a14af4

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/main/java/org/opensearch/security/configuration/DlsFlsFilterLeafReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public DlsGetEvaluator(final Query dlsQuery, final LeafReader in, boolean applyD
232232
// https://github.com/apache/lucene-solr/blob/branch_6_3/lucene/misc/src/java/org/apache/lucene/index/PKIndexSplitter.java
233233
final IndexSearcher searcher = new IndexSearcher(DlsFlsFilterLeafReader.this);
234234
searcher.setQueryCache(null);
235-
final Weight preserveWeight = searcher.createWeight(dlsQuery, ScoreMode.COMPLETE_NO_SCORES, 1f);
235+
final Weight preserveWeight = searcher.rewrite(dlsQuery).createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f);
236236

237237
final int maxDoc = in.maxDoc();
238238
final FixedBitSet bits = new FixedBitSet(maxDoc);

src/test/java/org/opensearch/security/dlic/dlsfls/DlsTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ protected void populateData(Client tc) {
3535
new IndexRequest("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"amount\": 1500}", XContentType.JSON)
3636
).actionGet();
3737

38+
tc.index(
39+
new IndexRequest("terms").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"foo\": \"bar\"}", XContentType.JSON)
40+
).actionGet();
41+
tc.index(
42+
new IndexRequest("terms").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"foo\": \"baz\"}", XContentType.JSON)
43+
).actionGet();
44+
3845
try {
3946
Thread.sleep(3000);
4047
} catch (InterruptedException e) {
@@ -44,6 +51,7 @@ protected void populateData(Client tc) {
4451
System.out.println("q");
4552
System.out.println(Strings.toString(XContentType.JSON, tc.search(new SearchRequest().indices(".opendistro_security")).actionGet()));
4653
tc.search(new SearchRequest().indices("deals")).actionGet();
54+
tc.search(new SearchRequest().indices("terms")).actionGet();
4755
}
4856

4957
@Test
@@ -250,6 +258,32 @@ public void testDls() throws Exception {
250258

251259
}
252260

261+
@Test
262+
public void testDlsWithTermsQuery() throws Exception {
263+
264+
setup();
265+
266+
HttpResponse res;
267+
268+
Assert.assertEquals(
269+
HttpStatus.SC_OK,
270+
(res = rh.executeGetRequest("/terms/_search?pretty", encodeBasicHeader("dept_manager", "password"))).getStatusCode()
271+
);
272+
Assert.assertEquals(res.getTextFromJsonBody("/hits/total/value"), "1");
273+
Assert.assertEquals(res.getTextFromJsonBody("/_shards/failed"), "0");
274+
275+
Assert.assertEquals(
276+
HttpStatus.SC_OK,
277+
(res = rh.executeGetRequest("/terms/_doc/0", encodeBasicHeader("dept_manager", "password"))).getStatusCode()
278+
);
279+
Assert.assertEquals(res.getTextFromJsonBody("/_source/foo"), "bar");
280+
281+
Assert.assertEquals(
282+
HttpStatus.SC_NOT_FOUND,
283+
rh.executeGetRequest("/terms/_doc/1", encodeBasicHeader("dept_manager", "password")).getStatusCode()
284+
);
285+
}
286+
253287
@Test
254288
public void testNonDls() throws Exception {
255289

src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import javax.net.ssl.SSLContext;
4545

46+
import com.fasterxml.jackson.core.JsonProcessingException;
4647
import com.fasterxml.jackson.databind.JsonNode;
4748
import org.apache.commons.io.IOUtils;
4849
import org.apache.http.Header;
@@ -344,6 +345,22 @@ public boolean isJsonContentType() {
344345
return ct.contains("application/json");
345346
}
346347

348+
public String getTextFromJsonBody(String jsonPointer) {
349+
return getJsonNodeAt(jsonPointer).asText();
350+
}
351+
352+
private JsonNode getJsonNodeAt(String jsonPointer) {
353+
try {
354+
return toJsonNode().at(jsonPointer);
355+
} catch (IOException e) {
356+
throw new IllegalArgumentException("Cound not convert response body to JSON node ", e);
357+
}
358+
}
359+
360+
private JsonNode toJsonNode() throws JsonProcessingException, IOException {
361+
return DefaultObjectMapper.objectMapper.readTree(getBody());
362+
}
363+
347364
public CloseableHttpResponse getInner() {
348365
return inner;
349366
}

src/test/resources/dlsfls/roles.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,3 +2482,12 @@ logs_index_with_dls:
24822482
masked_fields: null
24832483
allowed_actions:
24842484
- "OPENDISTRO_SECURITY_READ"
2485+
2486+
terms_index_with_dls:
2487+
index_permissions:
2488+
- index_patterns:
2489+
- "terms"
2490+
dls: "{ \"terms\": { \"foo\" : [\"bar\"] } }"
2491+
masked_fields: null
2492+
allowed_actions:
2493+
- "OPENDISTRO_SECURITY_READ"

src/test/resources/dlsfls/roles_mapping.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,7 @@ opendistro_security_mapped:
247247
logs_index_with_dls:
248248
users:
249249
- dept_manager
250+
251+
terms_index_with_dls:
252+
users:
253+
- dept_manager

0 commit comments

Comments
 (0)