Skip to content

Commit

Permalink
Update EQL tests to work with security enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tvernum committed Sep 8, 2023
1 parent b16d99f commit bd252e7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,59 @@ public void prepareIndices() throws IOException {
assertOK(provisioningAdminClient().performRequest(new Request("POST", "/" + indexName + "/_refresh")));
}

protected abstract String getInexistentIndexErrorMessage();
protected abstract boolean isSecurityEnabled();

protected String getInexistentIndexErrorMessage() {
if (isSecurityEnabled()) {
return "\"root_cause\":[{\"type\":\"verification_exception\",\"reason\":\"Found 1 problem\\nline -1:-1: Unknown index ";
} else {
return "\"root_cause\":[{\"type\":\"verification_exception\",\"reason\":\"Found 1 problem\\nline -1:-1: Unknown index ";
}
}

protected String getInexistentWildcardErrorMessage() {
return getInexistentIndexErrorMessage();
if (isSecurityEnabled()) {
return """
"root_cause":[{"type":"verification_exception","reason":"Found 1 problem\\nline -1:-1: Unknown index [*,-*]"}],\
"type":"index_not_found_exception","reason":"no such index\s""";
} else {
return getInexistentIndexErrorMessage();
}
}

protected abstract void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException;
protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException {
if (isSecurityEnabled()) {
assertErrorMessage("inexistent1*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("test_eql,inexistent*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\"""");
// TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed
assertErrorMessage("inexistent", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent]\"""");
assertErrorMessage("inexistent1,inexistent2", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [null]\"""");
} else {
assertErrorMessage("inexistent1*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("test_eql,inexistent*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\"""");
assertErrorMessage("inexistent", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent]\"""");
// TODO: revisit after
// https://github.com/elastic/elasticsearch/issues/64197
// is closed
assertErrorMessage(
"inexistent1,inexistent2",
reqParameter,
"\"root_cause\":[{\"type\":\"index_not_found_exception\",\"reason\":\"no such index [null]\","
+ "\"resource.type\":\"index_expression\",\"resource.id\":[\"inexistent1\",\"inexistent2\"]}]"
);
}
}

public void testDefaultIndicesOptions() throws IOException {
assertErrorMessages(inexistentIndexNameWithWildcard, EMPTY, getInexistentWildcardErrorMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected static void deleteIndexWithProvisioningClient(String name) throws IOEx

@Override
protected Settings restClientSettings() {
return secureRemoteClientSettings();
return Settings.builder().put(super.restClientSettings()).put(secureRemoteClientSettings()).build();
}

protected static Settings secureRemoteClientSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

import org.elasticsearch.client.Request;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.eql.DataLoader;
import org.elasticsearch.test.rest.ESRestTestCase;
Expand Down Expand Up @@ -390,9 +387,4 @@ private RestHighLevelClient highLevelClient() {
return highLevelClient;
}

@Override
protected Settings restClientSettings() {
String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray()));
return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected String getInexistentIndexErrorMessage() {
return "\"root_cause\":[{\"type\":\"index_not_found_exception\",\"reason\":\"no such index ";
}

@Override
protected boolean isSecurityEnabled() {
return true;
}

protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException {
assertErrorMessage("inexistent1*", reqParameter, getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent1*") + "]\"");
assertErrorMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

package org.elasticsearch.xpack.eql;

import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.eql.EqlRestTestCase;
import org.junit.ClassRule;
Expand All @@ -24,9 +21,4 @@ protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

@Override
protected Settings restClientSettings() {
String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray()));
return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.elasticsearch.test.eql.EqlRestValidationTestCase;
import org.junit.ClassRule;

import java.io.IOException;

public class EqlRestValidationIT extends EqlRestValidationTestCase {

@ClassRule
Expand All @@ -24,27 +22,7 @@ protected String getTestRestCluster() {
}

@Override
protected String getInexistentIndexErrorMessage() {
return "\"root_cause\":[{\"type\":\"verification_exception\",\"reason\":\"Found 1 problem\\nline -1:-1: Unknown index ";
}

protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException {
assertErrorMessage("inexistent1*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("test_eql,inexistent*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\"""");
assertErrorMessage("inexistent", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent]\"""");
// TODO: revisit after
// https://github.com/elastic/elasticsearch/issues/64197
// is closed
assertErrorMessage(
"inexistent1,inexistent2",
reqParameter,
"\"root_cause\":[{\"type\":\"index_not_found_exception\",\"reason\":\"no such index [null]\","
+ "\"resource.type\":\"index_expression\",\"resource.id\":[\"inexistent1\",\"inexistent2\"]}]"
);
protected boolean isSecurityEnabled() {
return cluster.isSecurityEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import org.elasticsearch.test.eql.EqlRestValidationTestCase;
import org.junit.ClassRule;

import java.io.IOException;

import static org.elasticsearch.xpack.eql.SecurityUtils.secureClientSettings;

public class EqlRestValidationIT extends EqlRestValidationTestCase {
Expand All @@ -31,30 +29,7 @@ protected Settings restClientSettings() {
}

@Override
protected String getInexistentIndexErrorMessage() {
return "\"root_cause\":[{\"type\":\"verification_exception\",\"reason\":\"Found 1 problem\\nline -1:-1: Unknown index ";
}

@Override
protected String getInexistentWildcardErrorMessage() {
return """
"root_cause":[{"type":"verification_exception","reason":"Found 1 problem\\nline -1:-1: Unknown index [*,-*]"}],\
"type":"index_not_found_exception","reason":"no such index\s""";
protected boolean isSecurityEnabled() {
return true;
}

@Override
protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException {
assertErrorMessage("inexistent1*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\"""");
assertErrorMessage("test_eql,inexistent*", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\"""");
// TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed
assertErrorMessage("inexistent", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent]\"""");
assertErrorMessage("inexistent1,inexistent2", reqParameter, """
"root_cause":[{"type":"index_not_found_exception","reason":"no such index [null]\"""");
}

}

0 comments on commit bd252e7

Please sign in to comment.