Skip to content

Commit

Permalink
Update checkNullElementsInArray() unit test to check both error mes…
Browse files Browse the repository at this point in the history
…sage and error code instead of only checking the error code (opensearch-project#1370)
  • Loading branch information
afazel authored and Miki committed Aug 13, 2021
1 parent d2b07f1 commit 8b5bda6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ public void checkNullElementsInArray() throws Exception{

String body = FileHelper.loadFile("restapi/actiongroup_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/CRUD_UT", body, new Header[0]);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.opensearch.security.dlic.rest.api;

import org.opensearch.common.xcontent.XContentType;
import org.opensearch.security.auditlog.impl.AuditCategory;
import org.opensearch.security.auditlog.impl.AuditMessage;
import org.opensearch.security.auditlog.integration.TestAuditlogImpl;
Expand All @@ -30,6 +31,7 @@
import org.junit.runners.Parameterized;
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;
import org.opensearch.security.test.helper.file.FileHelper;
import org.opensearch.security.dlic.rest.validation.AbstractConfigurationValidator;

import java.util.Arrays;
import java.util.List;
Expand All @@ -41,6 +43,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;


@RunWith(Parameterized.class)
public class NodesDnApiTest extends AbstractRestApiUnitTest {
private HttpResponse response;
Expand Down Expand Up @@ -117,6 +120,15 @@ private void testCrudScenarios(final int expectedStatus, final Header... headers
assertThat(response.getBody(), response.getStatusCode(), equalTo(expectedStatus));
}

private void checkNullElementsInArray(final Header headers) throws Exception{

String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/nodesdn/cluster1", body, headers);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}

@Test
public void testNodesDnApiWithDynamicConfigDisabled() throws Exception {
setup();
Expand Down Expand Up @@ -158,6 +170,12 @@ public void testNodesDnApi() throws Exception {
testCrudScenarios(HttpStatus.SC_OK, nonAdminCredsHeader);
}

{
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;
checkNullElementsInArray(nonAdminCredsHeader);
}

{
// any creds, admin certificate, disallowed key - FORBIDDEN
rh.keystore = "restapi/kirk-keystore.jks";
Expand Down Expand Up @@ -214,14 +232,4 @@ public void testNodesDnApiAuditComplianceLogging() throws Exception {
assertThat(actualCategoryCounts, equalTo(expectedCategoryCounts));
}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT+ "/nodesdn", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.security.DefaultObjectMapper;
import org.apache.http.Header;
import org.apache.http.HttpStatus;
Expand All @@ -27,7 +29,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import org.opensearch.security.dlic.rest.validation.AbstractConfigurationValidator;
import org.opensearch.security.dlic.rest.validation.AbstractConfigurationValidator.ErrorType;
import org.opensearch.security.support.SecurityJsonNode;
import org.opensearch.security.test.helper.file.FileHelper;
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;
Expand Down Expand Up @@ -365,7 +366,7 @@ public void testRolesApi() throws Exception {
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
settings = DefaultObjectMapper.readTree(response.getBody());
Assert.assertEquals(settings.get("status").asText(), "error");
Assert.assertEquals(settings.get("reason").asText(), ErrorType.INVALID_CONFIGURATION.getMessage());
Assert.assertEquals(settings.get("reason").asText(), AbstractConfigurationValidator.ErrorType.INVALID_CONFIGURATION.getMessage());

// -- PATCH
// PATCH on non-existing resource
Expand Down Expand Up @@ -531,27 +532,39 @@ public void checkNullElementsInArray() throws Exception{

String body = FileHelper.loadFile("restapi/roles_null_array_element_cluster_permissions.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/roles_null_array_element_index_permissions.json");
response = rh.executePutRequest(ENDPOINT+ "/roles/opendistro_security_role_starfleet", body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/roles_null_array_element_tenant_permissions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/roles_null_array_element_index_patterns.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/roles_null_array_element_masked_fields.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/roles_null_array_element_allowed_actions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package org.opensearch.security.dlic.rest.api;

import java.util.Arrays;
import java.util.List;

import org.apache.http.Header;
Expand Down Expand Up @@ -421,16 +420,22 @@ public void checkNullElementsInArray() throws Exception{
String body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_users.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_backend_roles.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_hosts.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ public void checkNullElementsInArray() throws Exception{

String body = FileHelper.loadFile("restapi/users_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/internalusers/picard", body, new Header[0]);
Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Assert.assertEquals(AbstractConfigurationValidator.ErrorType.NULL_ARRAY_ELEMENT.getMessage(), settings.get("reason"));
}

}

0 comments on commit 8b5bda6

Please sign in to comment.