-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test DLS and FLS against runtime fields #61820
Merged
javanna
merged 4 commits into
elastic:feature/runtime_fields
from
javanna:test/runtime_fields_with_security
Sep 2, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
x-pack/plugin/runtime-fields/qa/with-security/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apply plugin: 'elasticsearch.testclusters' | ||
apply plugin: 'elasticsearch.standalone-rest-test' | ||
apply plugin: 'elasticsearch.rest-test' | ||
|
||
dependencies { | ||
testImplementation project(path: xpackProject('plugin').path, configuration: 'testArtifacts') | ||
} | ||
|
||
def clusterCredentials = [username: System.getProperty('tests.rest.cluster.username', 'test_admin'), | ||
password: System.getProperty('tests.rest.cluster.password', 'x-pack-test-password')] | ||
|
||
integTest { | ||
systemProperty 'tests.rest.cluster.username', clusterCredentials.username | ||
systemProperty 'tests.rest.cluster.password', clusterCredentials.password | ||
} | ||
|
||
testClusters.integTest { | ||
testDistribution = 'DEFAULT' | ||
setting 'xpack.security.enabled', 'true' | ||
setting 'xpack.watcher.enabled', 'false' | ||
setting 'xpack.ml.enabled', 'false' | ||
setting 'xpack.license.self_generated.type', 'trial' | ||
extraConfigFile 'roles.yml', file('roles.yml') | ||
user clusterCredentials | ||
user username: "test", password: "x-pack-test-password", role: "test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
test: | ||
indices: | ||
- names: [ 'dls' ] | ||
privileges: | ||
- read | ||
query: "{\"match\": {\"year\": 2016}}" | ||
- names: [ 'fls' ] | ||
privileges: | ||
- read | ||
field_security: | ||
grant: [ '*' ] | ||
except: [ 'year', 'hidden' ] | ||
|
244 changes: 244 additions & 0 deletions
244
...fields/qa/with-security/src/test/java/org/elasticsearch/xpack/security/PermissionsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security; | ||
|
||
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; | ||
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.common.document.DocumentField; | ||
import org.elasticsearch.common.settings.SecureString; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.test.rest.ESRestTestCase; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; | ||
|
||
public class PermissionsIT extends ESRestTestCase { | ||
|
||
private static HighLevelClient highLevelClient; | ||
private static HighLevelClient adminHighLevelClient; | ||
|
||
@Override | ||
protected Settings restClientSettings() { | ||
String token = basicAuthHeaderValue("test", new SecureString("x-pack-test-password".toCharArray())); | ||
return Settings.builder() | ||
.put(ThreadContext.PREFIX + ".Authorization", token) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected Settings restAdminSettings() { | ||
String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray())); | ||
return Settings.builder() | ||
.put(ThreadContext.PREFIX + ".Authorization", token) | ||
.build(); | ||
} | ||
|
||
@Before | ||
public void initHighLevelClient() { | ||
if (highLevelClient == null) { | ||
highLevelClient = new HighLevelClient(client()); | ||
adminHighLevelClient = new HighLevelClient(adminClient()); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void closeHighLevelClients() throws IOException { | ||
highLevelClient.close(); | ||
adminHighLevelClient.close(); | ||
highLevelClient = null; | ||
adminHighLevelClient = null; | ||
} | ||
|
||
public void testDLS() throws IOException { | ||
Request createIndex = new Request("PUT", "/dls"); | ||
createIndex.setJsonEntity("{\n" + | ||
" \"mappings\" : {\n" + | ||
" \"properties\" : {\n" + | ||
" \"date\" : {\"type\" : \"keyword\"},\n" + | ||
" \"year\" : {\n" + | ||
" \"type\" : \"runtime_script\", \n" + | ||
" \"runtime_type\" : \"keyword\",\n" + | ||
" \"script\" : \"emitValue(doc[\\\"date\\\"].value.substring(0,4))\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(createIndex)); | ||
|
||
Request indexDoc1 = new Request("PUT", "/dls/_doc/1"); | ||
indexDoc1.setJsonEntity("{\n" + | ||
" \"date\" : \"2009-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc1)); | ||
|
||
Request indexDoc2 = new Request("PUT", "/dls/_doc/2"); | ||
indexDoc2.setJsonEntity("{\n" + | ||
" \"date\" : \"2016-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc2)); | ||
|
||
Request indexDoc3 = new Request("PUT", "/dls/_doc/3"); | ||
indexDoc3.addParameter("refresh", "wait_for"); | ||
indexDoc3.setJsonEntity("{\n" + | ||
" \"date\" : \"2018-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc3)); | ||
|
||
SearchRequest searchRequest = new SearchRequest("dls"); | ||
{ | ||
SearchResponse searchResponse = adminHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
assertEquals(3, searchResponse.getHits().getTotalHits().value); | ||
} | ||
{ | ||
SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
assertEquals(1, searchResponse.getHits().getTotalHits().value); | ||
} | ||
} | ||
|
||
public void testFLSProtectsData() throws IOException { | ||
Request createIndex = new Request("PUT", "/fls"); | ||
createIndex.setJsonEntity("{\n" + | ||
" \"mappings\" : {\n" + | ||
" \"properties\" : {\n" + | ||
" \"hidden\" : {\"type\" : \"keyword\"},\n" + | ||
" \"hidden_values_count\" : {\n" + | ||
" \"type\" : \"runtime_script\", \n" + | ||
" \"runtime_type\" : \"long\",\n" + | ||
" \"script\" : \"emitValue(doc[\\\"hidden\\\"].size())\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(createIndex)); | ||
|
||
Request indexDoc1 = new Request("PUT", "/fls/_doc/1"); | ||
indexDoc1.setJsonEntity("{\n" + | ||
" \"hidden\" : \"should not be read\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc1)); | ||
|
||
Request indexDoc2 = new Request("PUT", "/fls/_doc/2"); | ||
indexDoc2.setJsonEntity("{\n" + | ||
" \"hidden\" : \"should not be read\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc2)); | ||
|
||
Request indexDoc3 = new Request("PUT", "/fls/_doc/3"); | ||
indexDoc3.addParameter("refresh", "wait_for"); | ||
indexDoc3.setJsonEntity("{\n" + | ||
" \"hidden\" : \"should not be read\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc3)); | ||
|
||
SearchRequest searchRequest = new SearchRequest("fls").source(new SearchSourceBuilder().docValueField("hidden_values_count")); | ||
{ | ||
SearchResponse searchResponse = adminHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
assertEquals(3, searchResponse.getHits().getTotalHits().value); | ||
for (SearchHit hit : searchResponse.getHits().getHits()) { | ||
assertEquals(1, hit.getFields().size()); | ||
assertEquals(1, (int)hit.getFields().get("hidden_values_count").getValue()); | ||
} | ||
} | ||
{ | ||
SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
assertEquals(3, searchResponse.getHits().getTotalHits().value); | ||
for (SearchHit hit : searchResponse.getHits().getHits()) { | ||
assertEquals(0, (int)hit.getFields().get("hidden_values_count").getValue()); | ||
} | ||
} | ||
} | ||
|
||
public void testFLSOnRuntimeField() throws IOException { | ||
Request createIndex = new Request("PUT", "/fls"); | ||
createIndex.setJsonEntity("{\n" + | ||
" \"mappings\" : {\n" + | ||
" \"properties\" : {\n" + | ||
" \"date\" : {\"type\" : \"keyword\"},\n" + | ||
" \"year\" : {\n" + | ||
" \"type\" : \"runtime_script\", \n" + | ||
" \"runtime_type\" : \"keyword\",\n" + | ||
" \"script\" : \"emitValue(doc[\\\"date\\\"].value.substring(0,4))\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(createIndex)); | ||
|
||
Request indexDoc1 = new Request("PUT", "/fls/_doc/1"); | ||
indexDoc1.setJsonEntity("{\n" + | ||
" \"date\" : \"2009-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc1)); | ||
|
||
Request indexDoc2 = new Request("PUT", "/fls/_doc/2"); | ||
indexDoc2.setJsonEntity("{\n" + | ||
" \"date\" : \"2016-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc2)); | ||
|
||
Request indexDoc3 = new Request("PUT", "/fls/_doc/3"); | ||
indexDoc3.addParameter("refresh", "wait_for"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to wait_for a refresh, we can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both work I guess |
||
indexDoc3.setJsonEntity("{\n" + | ||
" \"date\" : \"2018-11-15T14:12:12\"\n" + | ||
"}\n"); | ||
assertOK(adminClient().performRequest(indexDoc3)); | ||
|
||
//There is no FLS directly on runtime fields | ||
SearchRequest searchRequest = new SearchRequest("fls").source(new SearchSourceBuilder().docValueField("year")); | ||
SearchResponse searchResponse = highLevelClient.search(searchRequest, RequestOptions.DEFAULT); | ||
assertEquals(3, searchResponse.getHits().getTotalHits().value); | ||
for (SearchHit hit : searchResponse.getHits().getHits()) { | ||
Map<String, DocumentField> fields = hit.getFields(); | ||
assertEquals(1, fields.size()); | ||
switch(hit.getId()) { | ||
case "1": | ||
assertEquals("2009", fields.get("year").getValue().toString()); | ||
break; | ||
case "2": | ||
assertEquals("2016", fields.get("year").getValue().toString()); | ||
break; | ||
case "3": | ||
assertEquals("2018", fields.get("year").getValue().toString()); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
{ | ||
FieldCapabilitiesRequest fieldCapsRequest = new FieldCapabilitiesRequest().indices("fls").fields("year"); | ||
FieldCapabilitiesResponse fieldCapabilitiesResponse = adminHighLevelClient.fieldCaps(fieldCapsRequest, RequestOptions.DEFAULT); | ||
assertNotNull(fieldCapabilitiesResponse.get().get("year")); | ||
} | ||
{ | ||
//Though field_caps filters runtime fields out like ordinary fields | ||
FieldCapabilitiesRequest fieldCapsRequest = new FieldCapabilitiesRequest().indices("fls").fields("year"); | ||
FieldCapabilitiesResponse fieldCapabilitiesResponse = highLevelClient.fieldCaps(fieldCapsRequest, RequestOptions.DEFAULT); | ||
assertEquals(0, fieldCapabilitiesResponse.get().size()); | ||
} | ||
} | ||
|
||
private static class HighLevelClient extends RestHighLevelClient { | ||
private HighLevelClient(RestClient restClient) { | ||
super(restClient, (client) -> {}, Collections.emptyList()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/\\"/'` I think will work.