-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ESQL: List/get query API #124832
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
Merged
Merged
ESQL: List/get query API #124832
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d6b0a92
ESQL: List/get query API
GalLalouche d961e27
Added super simple test
GalLalouche 55e7625
More tests
GalLalouche 8ad4d6d
Add operators to constants, rename
GalLalouche fd08751
ESQL: List/get query API
GalLalouche f1aab5d
Added super simple test
GalLalouche af2b5ee
More tests
GalLalouche 1d95cbc
Add operators to constants, rename
GalLalouche cb4ec47
Update docs/changelog/124832.yaml
GalLalouche 2fa0085
Merge branch 'main' into feature/list_query_api
GalLalouche ff0fe90
Moved test
GalLalouche 758319a
Merge remote-tracking branch 'origin/feature/list_query_api' into fea…
GalLalouche 894dad6
TEMP
GalLalouche 30f5ef1
Merge branch 'main' into feature/list_query_api
GalLalouche 9bcdb10
More CR fixes
GalLalouche 6686702
Merge branch 'main' into feature/list_query_api
GalLalouche 9847add
Add null URL to rest API
GalLalouche 1d0d15c
Merge branch 'main' into feature/list_query_api
GalLalouche 9fe9243
Merge branch 'main' into feature/list_query_api
GalLalouche f786222
Update privileges
GalLalouche 6134a8e
Security fixes
GalLalouche 789362f
Merge branch 'main' into feature/list_query_api
GalLalouche 0cedb9a
CR fixes
GalLalouche 09dc300
Merge branch 'main' into feature/list_query_api
GalLalouche 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
This file contains hidden or 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,6 @@ | ||
| pr: 124832 | ||
| summary: List/get query API | ||
| area: ES|QL | ||
| type: feature | ||
| issues: | ||
| - 124827 |
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
rest-api-spec/src/main/resources/rest-api-spec/api/esql.get_query.json
This file contains hidden or 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,32 @@ | ||
| { | ||
| "esql.get_query": { | ||
| "documentation": { | ||
| "url": null, | ||
| "description": "Executes a get ESQL query request" | ||
| }, | ||
| "stability": "experimental", | ||
| "visibility": "public", | ||
| "headers": { | ||
| "accept": [], | ||
| "content_type": [ | ||
| "application/json" | ||
| ] | ||
| }, | ||
| "url": { | ||
| "paths": [ | ||
| { | ||
| "path": "/_query/queries/{id}", | ||
| "methods": [ | ||
| "GET" | ||
| ], | ||
| "parts": { | ||
| "id": { | ||
| "type": "string", | ||
| "description": "The query ID" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
rest-api-spec/src/main/resources/rest-api-spec/api/esql.list_queries.json
This file contains hidden or 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 @@ | ||
| { | ||
| "esql.list_queries": { | ||
| "documentation": { | ||
| "url": null, | ||
| "description": "Executes a list ESQL queries request" | ||
| }, | ||
| "stability": "experimental", | ||
| "visibility": "public", | ||
| "headers": { | ||
| "accept": [], | ||
| "content_type": [ | ||
| "application/json" | ||
| ] | ||
| }, | ||
| "url": { | ||
| "paths": [ | ||
| { | ||
| "path": "/_query/queries", | ||
| "methods": [ | ||
| "GET" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
test/framework/src/main/java/org/elasticsearch/test/IntOrLongMatcher.java
This file contains hidden or 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,79 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.test; | ||
|
|
||
| import org.hamcrest.BaseMatcher; | ||
| import org.hamcrest.Description; | ||
| import org.hamcrest.Matcher; | ||
|
|
||
| import static org.hamcrest.Matchers.anyOf; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.isA; | ||
|
|
||
| /** | ||
| * A type-agnostic way of comparing integer values, not caring if it's a long or an integer. | ||
| */ | ||
| public abstract sealed class IntOrLongMatcher<T> extends BaseMatcher<T> { | ||
| public static IntOrLongMatcher<Integer> matches(int expected) { | ||
| return new IntMatcher(expected); | ||
| } | ||
|
|
||
| public static IntOrLongMatcher<Long> matches(long expected) { | ||
| return new LongMatcher(expected); | ||
| } | ||
|
|
||
| private static final class IntMatcher extends IntOrLongMatcher<Integer> { | ||
| private final int expected; | ||
|
|
||
| private IntMatcher(int expected) { | ||
| this.expected = expected; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(Object o) { | ||
| return switch (o) { | ||
| case Integer i -> expected == i; | ||
| case Long l -> expected == l; | ||
| default -> false; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void describeTo(Description description) { | ||
| equalTo(expected).describeTo(description); | ||
| } | ||
| } | ||
|
|
||
| private static final class LongMatcher extends IntOrLongMatcher<Long> { | ||
| private final long expected; | ||
|
|
||
| LongMatcher(long expected) { | ||
| this.expected = expected; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(Object o) { | ||
| return switch (o) { | ||
| case Integer i -> expected == i; | ||
| case Long l -> expected == l; | ||
| default -> false; | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void describeTo(Description description) { | ||
| equalTo(expected).describeTo(description); | ||
| } | ||
| } | ||
|
|
||
| public static Matcher<Object> isIntOrLong() { | ||
| return anyOf(isA(Integer.class), isA(Long.class)); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import static org.elasticsearch.test.MapMatcher.matchesMap; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.not; | ||
|
|
||
| public class EsqlSecurityIT extends ESRestTestCase { | ||
| @ClassRule | ||
|
|
@@ -69,6 +70,8 @@ public class EsqlSecurityIT extends ESRestTestCase { | |
| .user("logs_foo_after_2021", "x-pack-test-password", "logs_foo_after_2021", false) | ||
| .user("logs_foo_after_2021_pattern", "x-pack-test-password", "logs_foo_after_2021_pattern", false) | ||
| .user("logs_foo_after_2021_alias", "x-pack-test-password", "logs_foo_after_2021_alias", false) | ||
| .user("user_without_monitor_privileges", "x-pack-test-password", "user_without_monitor_privileges", false) | ||
| .user("user_with_monitor_privileges", "x-pack-test-password", "user_with_monitor_privileges", false) | ||
| .build(); | ||
|
|
||
| @Override | ||
|
|
@@ -309,7 +312,7 @@ public void testIndexPatternErrorMessageComparison_ESQL_SearchDSL() throws Excep | |
| json.endObject(); | ||
| Request searchRequest = new Request("GET", "/index-user1,index-user2/_search"); | ||
| searchRequest.setJsonEntity(Strings.toString(json)); | ||
| searchRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", "metadata1_read2")); | ||
| setUser(searchRequest, "metadata1_read2"); | ||
|
|
||
| // ES|QL query on the same index pattern | ||
| var esqlResp = expectThrows(ResponseException.class, () -> runESQLCommand("metadata1_read2", "FROM index-user1,index-user2")); | ||
|
|
@@ -429,13 +432,13 @@ public void testFieldLevelSecurityAllow() throws Exception { | |
|
|
||
| public void testFieldLevelSecurityAllowPartial() throws Exception { | ||
| Request request = new Request("GET", "/index*/_field_caps"); | ||
| request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", "fls_user")); | ||
| setUser(request, "fls_user"); | ||
| request.addParameter("error_trace", "true"); | ||
| request.addParameter("pretty", "true"); | ||
| request.addParameter("fields", "*"); | ||
|
|
||
| request = new Request("GET", "/index*/_search"); | ||
| request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", "fls_user")); | ||
| setUser(request, "fls_user"); | ||
| request.addParameter("error_trace", "true"); | ||
| request.addParameter("pretty", "true"); | ||
|
|
||
|
|
@@ -761,6 +764,36 @@ public void testFromLookupIndexForbidden() throws Exception { | |
| assertThat(resp.getResponse().getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_BAD_REQUEST)); | ||
| } | ||
|
|
||
| public void testListQueryAllowed() throws Exception { | ||
|
Contributor
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. Nice! |
||
| Request request = new Request("GET", "_query/queries"); | ||
| setUser(request, "user_with_monitor_privileges"); | ||
| var resp = client().performRequest(request); | ||
| assertOK(resp); | ||
| } | ||
|
|
||
| public void testListQueryForbidden() throws Exception { | ||
| Request request = new Request("GET", "_query/queries"); | ||
| setUser(request, "user_without_monitor_privileges"); | ||
| var resp = expectThrows(ResponseException.class, () -> client().performRequest(request)); | ||
| assertThat(resp.getResponse().getStatusLine().getStatusCode(), equalTo(403)); | ||
| assertThat(resp.getMessage(), containsString("this action is granted by the cluster privileges [monitor_esql,monitor,manage,all]")); | ||
| } | ||
|
|
||
| public void testGetQueryAllowed() throws Exception { | ||
| // This is a bit tricky, since there is no such running query. We just make sure it didn't fail on forbidden privileges. | ||
| Request request = new Request("GET", "_query/queries/foo:1234"); | ||
| var resp = expectThrows(ResponseException.class, () -> client().performRequest(request)); | ||
| assertThat(resp.getResponse().getStatusLine().getStatusCode(), not(equalTo(404))); | ||
| } | ||
|
|
||
| public void testGetQueryForbidden() throws Exception { | ||
| Request request = new Request("GET", "_query/queries/foo:1234"); | ||
| setUser(request, "user_without_monitor_privileges"); | ||
| var resp = expectThrows(ResponseException.class, () -> client().performRequest(request)); | ||
| assertThat(resp.getResponse().getStatusLine().getStatusCode(), equalTo(403)); | ||
| assertThat(resp.getMessage(), containsString("this action is granted by the cluster privileges [monitor_esql,monitor,manage,all]")); | ||
| } | ||
|
|
||
| private void createEnrichPolicy() throws Exception { | ||
| createIndex("songs", Settings.EMPTY, """ | ||
| "properties":{"song_id": {"type": "keyword"}, "title": {"type": "keyword"}, "artist": {"type": "keyword"} } | ||
|
|
@@ -837,11 +870,16 @@ protected Response runESQLCommand(String user, String command) throws IOExceptio | |
| json.endObject(); | ||
| Request request = new Request("POST", "_query"); | ||
| request.setJsonEntity(Strings.toString(json)); | ||
| request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); | ||
| setUser(request, user); | ||
| request.addParameter("error_trace", "true"); | ||
| return client().performRequest(request); | ||
| } | ||
|
|
||
| private static void setUser(Request request, String user) { | ||
| request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("es-security-runas-user", user)); | ||
|
|
||
| } | ||
|
|
||
| static void addRandomPragmas(XContentBuilder builder) throws IOException { | ||
| if (Build.current().isSnapshot()) { | ||
| Settings pragmas = randomPragmas(); | ||
|
|
@@ -853,7 +891,7 @@ static void addRandomPragmas(XContentBuilder builder) throws IOException { | |
| } | ||
| } | ||
|
|
||
| static Settings randomPragmas() { | ||
| private static Settings randomPragmas() { | ||
| Settings.Builder settings = Settings.builder(); | ||
| if (randomBoolean()) { | ||
| settings.put("page_size", between(1, 5)); | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.