Skip to content
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

Onboard search AD and search monitor tools to security-enabled IT #225

Merged
merged 1 commit into from
Feb 13, 2024
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
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,12 @@ integTest {
systemProperty("user", user)
systemProperty("password", password)

// Certain integ tests require system index manipulation to properly test. We exclude those
// in the security-enabled scenario since this action is prohibited by security plugin.
if (System.getProperty("https") != null && System.getProperty("https") == "true") {
filter {
excludeTestsMatching "org.opensearch.integTest.SearchAlertsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchAnomalyDetectorsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchAnomalyResultsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchMonitorsToolIT"
}
}

Expand Down Expand Up @@ -544,12 +544,12 @@ task integTestRemote(type: RestIntegTestTask) {
}
}

// Certain integ tests require system index manipulation to properly test. We exclude those
// in the security-enabled scenario since this action is prohibited by security plugin.
if (System.getProperty("https") != null && System.getProperty("https") == "true") {
filter {
excludeTestsMatching "org.opensearch.integTest.SearchAlertsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchAnomalyDetectorsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchAnomalyResultsToolIT"
excludeTestsMatching "org.opensearch.integTest.SearchMonitorsToolIT"
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/opensearch/integTest/BaseAgentToolsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,30 @@ protected String indexMonitor(String monitorAsJsonString) {
return parseFieldFromResponse(response, "_id").toString();
}

protected void deleteMonitor(String monitorId) {
Response response = makeRequest(client(), "DELETE", "_plugins/_alerting/monitors/" + monitorId, null, (String) null, null);
assertEquals(RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
}

protected String indexDetector(String detectorAsJsonString) {
Response response = makeRequest(client(), "POST", "_plugins/_anomaly_detection/detectors", null, detectorAsJsonString, null);

assertEquals(RestStatus.CREATED, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
return parseFieldFromResponse(response, "_id").toString();
}

protected void deleteDetector(String detectorId) {
Response response = makeRequest(
client(),
"DELETE",
"_plugins/_anomaly_detection/detectors/" + detectorId,
null,
(String) null,
null
);
assertEquals(RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode()));
}

@SneakyThrows
protected Map<String, Object> waitResponseMeetingCondition(
String method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@

import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.opensearch.agent.tools.utils.ToolConstants;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import lombok.SneakyThrows;

@TestMethodOrder(OrderAnnotation.class)
public class SearchAnomalyDetectorsToolIT extends BaseAgentToolsIT {
private String registerAgentRequestBody;
private String detectorsIndexMappings;
private String sampleDetector;
private String sampleIndexMappings;
private static final String detectorName = "foo-name";
private static final String registerAgentFilepath =
"org/opensearch/agent/tools/anomaly-detection/register_flow_agent_of_search_anomaly_detectors_tool_request_body.json";
private static final String detectorsIndexMappingsFilepath =
"org/opensearch/agent/tools/anomaly-detection/detectors_index_mappings.json";
private static final String sampleDetectorFilepath = "org/opensearch/agent/tools/anomaly-detection/sample_detector.json";
private static final String sampleIndexMappingsFilepath = "org/opensearch/agent/tools/anomaly-detection/sample_index_mappings.json";

Expand All @@ -38,27 +37,19 @@ public class SearchAnomalyDetectorsToolIT extends BaseAgentToolsIT {
public void setUp() {
super.setUp();
registerAgentRequestBody = Files.readString(Path.of(this.getClass().getClassLoader().getResource(registerAgentFilepath).toURI()));
detectorsIndexMappings = Files
.readString(Path.of(this.getClass().getClassLoader().getResource(detectorsIndexMappingsFilepath).toURI()));
sampleDetector = Files.readString(Path.of(this.getClass().getClassLoader().getResource(sampleDetectorFilepath).toURI()));
sampleIndexMappings = Files.readString(Path.of(this.getClass().getClassLoader().getResource(sampleIndexMappingsFilepath).toURI()));
}

@BeforeEach
@SneakyThrows
public void prepareTest() {
deleteSystemIndices();
}

@After
@SneakyThrows
public void tearDown() {
super.tearDown();
deleteExternalIndices();
deleteSystemIndices();
}

@SneakyThrows
@Order(1)
public void testSearchAnomalyDetectorsToolInFlowAgent_withNoSystemIndex() {
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "\"}}";
Expand All @@ -67,19 +58,20 @@ public void testSearchAnomalyDetectorsToolInFlowAgent_withNoSystemIndex() {
}

@SneakyThrows
@Order(2)
public void testSearchAnomalyDetectorsToolInFlowAgent_noMatching() {
setupADSystemIndices();
setupTestDetectionIndex("test-index");
ingestSampleDetector(detectorName, "test-index");
String detectorId = ingestSampleDetector(detectorName, "test-index");
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"detectorName\": \"" + detectorName + "foo" + "\"}}";
String result = executeAgent(agentId, agentInput);
assertEquals("AnomalyDetectors=[]TotalAnomalyDetectors=0", result);
deleteDetector(detectorId);
}

@SneakyThrows
@Order(3)
public void testSearchAnomalyDetectorsToolInFlowAgent_matching() {
setupADSystemIndices();
setupTestDetectionIndex("test-index");
String detectorId = ingestSampleDetector(detectorName, "test-index");
String agentId = createAgent(registerAgentRequestBody);
Expand All @@ -88,14 +80,15 @@ public void testSearchAnomalyDetectorsToolInFlowAgent_matching() {
assertTrue(result.contains(String.format("id=%s", detectorId)));
assertTrue(result.contains(String.format("name=%s", detectorName)));
assertTrue(result.contains(String.format("TotalAnomalyDetectors=%d", 1)));
deleteDetector(detectorId);
}

@SneakyThrows
@Order(4)
public void testSearchAnomalyDetectorsToolInFlowAgent_complexParams() {
setupADSystemIndices();
setupTestDetectionIndex("test-index");
String detectorId = ingestSampleDetector(detectorName, "test-index");
ingestSampleDetector(detectorName + "foo", "test-index");
String detectorIdFoo = ingestSampleDetector(detectorName + "foo", "test-index");
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"detectorName\": \""
+ detectorName
Expand All @@ -104,11 +97,8 @@ public void testSearchAnomalyDetectorsToolInFlowAgent_complexParams() {
assertTrue(result.contains(String.format("id=%s", detectorId)));
assertTrue(result.contains(String.format("name=%s", detectorName)));
assertTrue(result.contains(String.format("TotalAnomalyDetectors=%d", 1)));
}

@SneakyThrows
private void setupADSystemIndices() {
createIndexWithConfiguration(ToolConstants.AD_DETECTORS_INDEX, detectorsIndexMappings);
deleteDetector(detectorId);
deleteDetector(detectorIdFoo);
}

@SneakyThrows
Expand Down
42 changes: 26 additions & 16 deletions src/test/java/org/opensearch/integTest/SearchMonitorsToolIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
Expand All @@ -21,6 +23,7 @@
import lombok.extern.log4j.Log4j2;

@Log4j2
@TestMethodOrder(OrderAnnotation.class)
public class SearchMonitorsToolIT extends BaseAgentToolsIT {
private String registerAgentRequestBody;
private String sampleMonitor;
Expand All @@ -38,21 +41,15 @@ public void setUp() {
sampleMonitor = Files.readString(Path.of(this.getClass().getClassLoader().getResource(sampleMonitorFilepath).toURI()));
}

@BeforeEach
@SneakyThrows
public void prepareTest() {
deleteSystemIndices();
}

@After
@SneakyThrows
public void tearDown() {
super.tearDown();
deleteExternalIndices();
deleteSystemIndices();
}

@SneakyThrows
@Order(1)
public void testSearchMonitorsToolInFlowAgent_withNoSystemIndex() {
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorName\": \"" + monitorName + "\"}}";
Expand All @@ -61,29 +58,33 @@ public void testSearchMonitorsToolInFlowAgent_withNoSystemIndex() {
}

@SneakyThrows
@Order(2)
public void testSearchMonitorsToolInFlowAgent_searchById() {
String monitorId = ingestSampleMonitor(monitorName, true);

String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorId\": \"" + monitorId + "\"}}";

String result = executeAgent(agentId, agentInput);
assertTrue(result.contains(String.format("name=%s", monitorName)));
assertTrue(result.contains("TotalMonitors=1"));
deleteMonitor(monitorId);
}

@SneakyThrows
@Order(3)
public void testSearchMonitorsToolInFlowAgent_singleMonitor_noFilter() {
ingestSampleMonitor(monitorName, true);
String monitorId = ingestSampleMonitor(monitorName, true);

String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{}}";
String result = executeAgent(agentId, agentInput);
assertTrue(result.contains(String.format("name=%s", monitorName)));
assertTrue(result.contains("TotalMonitors=1"));
deleteMonitor(monitorId);
}

@SneakyThrows
@Order(4)
public void testSearchMonitorsToolInFlowAgent_singleMonitor_filter() {
String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorId\": \"" + "foo-id" + "\"}}";
Expand All @@ -92,9 +93,10 @@ public void testSearchMonitorsToolInFlowAgent_singleMonitor_filter() {
}

@SneakyThrows
@Order(5)
public void testSearchMonitorsToolInFlowAgent_multipleMonitors_noFilter() {
ingestSampleMonitor(monitorName, true);
ingestSampleMonitor(monitorName2, false);
String monitorId1 = ingestSampleMonitor(monitorName, true);
String monitorId2 = ingestSampleMonitor(monitorName2, false);

String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{}}";
Expand All @@ -104,12 +106,15 @@ public void testSearchMonitorsToolInFlowAgent_multipleMonitors_noFilter() {
assertTrue(result.contains("enabled=true"));
assertTrue(result.contains("enabled=false"));
assertTrue(result.contains("TotalMonitors=2"));
deleteMonitor(monitorId1);
deleteMonitor(monitorId2);
}

@SneakyThrows
@Order(6)
public void testSearchMonitorsToolInFlowAgent_multipleMonitors_filter() {
ingestSampleMonitor(monitorName, true);
ingestSampleMonitor(monitorName2, false);
String monitorId1 = ingestSampleMonitor(monitorName, true);
String monitorId2 = ingestSampleMonitor(monitorName2, false);

String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorName\": \"" + monitorName + "\"}}";
Expand All @@ -118,19 +123,24 @@ public void testSearchMonitorsToolInFlowAgent_multipleMonitors_filter() {
assertFalse(result.contains(String.format("name=%s", monitorName2)));
assertTrue(result.contains("enabled=true"));
assertTrue(result.contains("TotalMonitors=1"));
deleteMonitor(monitorId1);
deleteMonitor(monitorId2);
}

@SneakyThrows
@Order(7)
public void testSearchMonitorsToolInFlowAgent_multipleMonitors_complexParams() {
ingestSampleMonitor(monitorName, true);
ingestSampleMonitor(monitorName2, false);
String monitorId1 = ingestSampleMonitor(monitorName, true);
String monitorId2 = ingestSampleMonitor(monitorName2, false);

String agentId = createAgent(registerAgentRequestBody);
String agentInput = "{\"parameters\":{\"monitorName\": \""
+ monitorName
+ "\", \"enabled\": true, \"hasTriggers\": false, \"sortOrder\": \"asc\", \"sortString\": \"monitor.name.keyword\", \"size\": 10, \"startIndex\": 0 }}";
String result = executeAgent(agentId, agentInput);
assertTrue(result.contains("TotalMonitors=1"));
deleteMonitor(monitorId1);
deleteMonitor(monitorId2);
}

private String ingestSampleMonitor(String monitorName, boolean enabled) {
Expand Down
Loading
Loading