Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/plugin/sql/qa/server/security/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ subprojects {
user username: "test_admin", password: "x-pack-test-password"
user username: "user1", password: 'x-pack-test-password', role: "user1"
user username: "user2", password: 'x-pack-test-password', role: "user2"
user username: "manage_user", password: 'x-pack-test-password', role: "manage_user"
}

File testArtifactsDir = project.file("$buildDir/testArtifacts")
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugin/sql/qa/server/security/roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ user2:
- write
- create_index
- indices:admin/refresh

manage_user:
cluster:
- manage
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xpack.core.XPackPlugin;
import org.elasticsearch.xpack.core.async.AsyncExecutionId;
import org.elasticsearch.xpack.sql.qa.rest.BaseRestSqlTestCase;
import org.junit.Before;

import java.io.IOException;
Expand Down Expand Up @@ -101,6 +102,23 @@ private void testCase(String user, String otherUser) throws Exception {
assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(400));
}

// user with manage privilege can check status and delete
public void testWithManager() throws IOException {
Response submitResp = submitAsyncSqlSearch("SELECT event_type FROM \"index\" WHERE val=0", TimeValue.timeValueSeconds(10), "user1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this time value safe for any case?
Could it be better to make use of some other async test mechanism like assertBusy to make sure that it's robust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular value has been c&p'ed from the similar EQL tests.

But the way the test works is to get exceptions when the "other" user tries to get to this async request. For that to work, the async task would only need to be created. Which is checked both by asserting an OK answer to the initial request, as well as by getting its status, subsequently. So the task wouldn't even have to be finished, just started, which is guaranteed by the first checks.

So the "10s" aren't actually relevant for the outcome of the test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thx!

assertOK(submitResp);
String id = extractResponseId(submitResp);
Response getResp = getAsyncSqlSearch(id, "user1");
assertOK(getResp);

Response getStatus = getAsyncSqlStatus(id, "manage_user");
assertOK(getStatus);
Map<String, Object> status = BaseRestSqlTestCase.toMap(getStatus, null);
assertEquals(200, status.get("completion_status"));

Response deleteResp = deleteAsyncSqlSearch(id, "manage_user");
assertOK(deleteResp);
}

static String extractResponseId(Response response) throws IOException {
Map<String, Object> map = toMap(response);
return (String) map.get("id");
Expand Down Expand Up @@ -148,6 +166,14 @@ static Response getAsyncSqlSearch(String id, String user) throws IOException {
final Request request = new Request("GET", "/_sql/async/" + id);
setRunAsHeader(request, user);
request.addParameter("wait_for_completion_timeout", "0ms");
request.addParameter("format", "json");
return client().performRequest(request);
}

static Response getAsyncSqlStatus(String id, String user) throws IOException {
final Request request = new Request("GET", "/_sql/async/status/" + id);
setRunAsHeader(request, user);
request.addParameter("format", "json");
return client().performRequest(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testIncorrectAcceptHeader() throws IOException {
request.setEntity(stringEntity);
expectBadRequest(
() -> toMap(client().performRequest(request), "plain"),
containsString("Invalid response content type: Accept=[application/fff]")
containsString("Invalid request content type: Accept=[application/fff]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@

import static org.elasticsearch.xpack.sql.proto.Protocol.BINARY_FORMAT_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.CLIENT_ID_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.VERSION_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.COLUMNAR_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.CURSOR_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.FETCH_SIZE_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.FIELD_MULTI_VALUE_LENIENCY_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.FILTER_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.KEEP_ALIVE_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.KEEP_ON_COMPLETION_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.MODE_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.PARAMS_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.QUERY_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.RUNTIME_MAPPINGS_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.TIME_ZONE_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.VERSION_NAME;
import static org.elasticsearch.xpack.sql.proto.Protocol.WAIT_FOR_COMPLETION_TIMEOUT_NAME;

public abstract class BaseRestSqlTestCase extends ESRestTestCase {

Expand Down Expand Up @@ -107,6 +110,21 @@ public RequestObjectBuilder binaryFormat(Boolean binaryFormat) {
return this;
}

public RequestObjectBuilder waitForCompletionTimeout(String timeout) {
request.append(field(WAIT_FOR_COMPLETION_TIMEOUT_NAME, timeout));
return this;
}

public RequestObjectBuilder keepOnCompletion(Boolean keepOnCompletion) {
request.append(field(KEEP_ON_COMPLETION_NAME, keepOnCompletion));
return this;
}

public RequestObjectBuilder keepAlive(String keepAlive) {
request.append(field(KEEP_ALIVE_NAME, keepAlive));
return this;
}

public RequestObjectBuilder fieldMultiValueLeniency(Boolean fieldMultiValueLeniency) {
request.append(field(FIELD_MULTI_VALUE_LENIENCY_NAME, fieldMultiValueLeniency));
return this;
Expand Down
Loading