From 4c070e1ab6a3a314e70d21bb1b94a4f9cb35846a Mon Sep 17 00:00:00 2001 From: Stevan Buzejic Date: Tue, 20 Dec 2022 18:52:17 +0100 Subject: [PATCH] Updated test Signed-off-by: Stevan Buzejic --- .../resthandler/SecureDetectorRestApiIT.java | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/opensearch/securityanalytics/resthandler/SecureDetectorRestApiIT.java b/src/test/java/org/opensearch/securityanalytics/resthandler/SecureDetectorRestApiIT.java index 8d882782a..185afbff1 100644 --- a/src/test/java/org/opensearch/securityanalytics/resthandler/SecureDetectorRestApiIT.java +++ b/src/test/java/org/opensearch/securityanalytics/resthandler/SecureDetectorRestApiIT.java @@ -192,14 +192,16 @@ public void testCreateDetectorIndexAccess() throws IOException { String roleNameWithoutIndexPatternAccess = "test-role"; String testIndexPattern = "test*"; createUserWithDataAndCustomRole(userWithoutAccess, userWithoutAccess, roleNameWithoutIndexPatternAccess, backendRoles, clusterPermissions, indexPermissions, List.of(testIndexPattern)); - RestClient clientWithoutAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithoutAccess, userWithoutAccess).setSocketTimeout(60000).build(); + RestClient clientWithoutAccess = null; String userWithAccess = "user1"; String roleNameWithIndexPatternAccess = "test-role-1"; String windowsIndexPattern = "windows*"; createUserWithDataAndCustomRole(userWithAccess, userWithAccess, roleNameWithIndexPatternAccess, backendRoles, clusterPermissions, indexPermissions, List.of(windowsIndexPattern)); - RestClient clientWithAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithAccess, userWithAccess).setSocketTimeout(60000).build(); + RestClient clientWithAccess = null; try { + clientWithoutAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithoutAccess, userWithoutAccess).setSocketTimeout(60000).build(); + clientWithAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithAccess, userWithAccess).setSocketTimeout(60000).build(); //createUserRolesMapping("alerting_full_access", users); String index = createTestIndex(client(), randomIndex(), windowsIndexMapping(), Settings.EMPTY); @@ -225,12 +227,24 @@ public void testCreateDetectorIndexAccess() throws IOException { Response createResponse = makeRequest(clientWithAccess, "POST", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, Collections.emptyMap(), toHttpEntity(detector)); assertEquals("Create detector failed", RestStatus.CREATED, restStatus(createResponse)); + + Map responseBody = asMap(createResponse); + + String createdId = responseBody.get("_id").toString(); + int createdVersion = Integer.parseInt(responseBody.get("_version").toString()); + + assertNotEquals("response is missing Id", Detector.NO_ID, createdId); + assertTrue("incorrect version", createdVersion > 0); + assertEquals("Incorrect Location header", String.format(Locale.getDefault(), "%s/%s", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, createdId), createResponse.getHeader("Location")); + assertFalse(((Map) responseBody.get("detector")).containsKey("rule_topic_index")); + assertFalse(((Map) responseBody.get("detector")).containsKey("findings_index")); + assertFalse(((Map) responseBody.get("detector")).containsKey("alert_index")); } finally { - clientWithoutAccess.close(); + if(clientWithoutAccess!= null) clientWithoutAccess.close(); deleteUser(userWithoutAccess); deleteRole(roleNameWithoutIndexPatternAccess); - clientWithAccess.close(); + if (clientWithAccess != null) clientWithAccess.close(); deleteUser(userWithAccess); deleteRole(roleNameWithIndexPatternAccess); } @@ -248,15 +262,17 @@ public void testUpdateDetectorIndexAccess() throws IOException { String roleNameWithoutIndexPatternAccess = "test-role"; String testIndexPattern = "test*"; createUserWithDataAndCustomRole(userWithoutAccess, userWithoutAccess, roleNameWithoutIndexPatternAccess, backendRoles, clusterPermissions, indexPermissions, List.of(testIndexPattern)); - RestClient clientWithoutAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithoutAccess, userWithoutAccess).setSocketTimeout(60000).build(); + RestClient clientWithoutAccess = null; String userWithAccess = "user1"; String roleNameWithIndexPatternAccess = "test-role-1"; String windowsIndexPattern = "windows*"; createUserWithDataAndCustomRole(userWithAccess, userWithAccess, roleNameWithIndexPatternAccess, backendRoles, clusterPermissions, indexPermissions, List.of(windowsIndexPattern)); - RestClient clientWithAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithAccess, userWithAccess).setSocketTimeout(60000).build(); + RestClient clientWithAccess = null; try { + clientWithoutAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithoutAccess, userWithoutAccess).setSocketTimeout(60000).build(); + clientWithAccess = new SecureRestClientBuilder(getClusterHosts().toArray(new HttpHost[]{}), isHttps(), userWithAccess, userWithAccess).setSocketTimeout(60000).build(); //createUserRolesMapping("alerting_full_access", users); String index = createTestIndex(client(), randomIndex(), windowsIndexMapping(), Settings.EMPTY); @@ -278,6 +294,17 @@ public void testUpdateDetectorIndexAccess() throws IOException { assertEquals("Create detector failed", RestStatus.CREATED, restStatus(createResponse)); Map responseBody = asMap(createResponse); + + String createdId = responseBody.get("_id").toString(); + int createdVersion = Integer.parseInt(responseBody.get("_version").toString()); + + assertNotEquals("response is missing Id", Detector.NO_ID, createdId); + assertTrue("incorrect version", createdVersion > 0); + assertEquals("Incorrect Location header", String.format(Locale.getDefault(), "%s/%s", SecurityAnalyticsPlugin.DETECTOR_BASE_URI, createdId), createResponse.getHeader("Location")); + assertFalse(((Map) responseBody.get("detector")).containsKey("rule_topic_index")); + assertFalse(((Map) responseBody.get("detector")).containsKey("findings_index")); + assertFalse(((Map) responseBody.get("detector")).containsKey("alert_index")); + String detectorId = responseBody.get("_id").toString(); try { @@ -289,11 +316,11 @@ public void testUpdateDetectorIndexAccess() throws IOException { Response updateResponse = makeRequest(clientWithAccess, "PUT", SecurityAnalyticsPlugin.DETECTOR_BASE_URI + "/" + detectorId, Collections.emptyMap(), toHttpEntity(detector)); assertEquals("Update detector failed", RestStatus.OK, restStatus(updateResponse)); } finally { - clientWithoutAccess.close(); + if (clientWithoutAccess != null) clientWithoutAccess.close(); deleteUser(userWithoutAccess); deleteRole(roleNameWithoutIndexPatternAccess); - clientWithAccess.close(); + if (clientWithAccess != null) clientWithAccess.close(); deleteUser(userWithAccess); deleteRole(roleNameWithIndexPatternAccess); }