Skip to content

Commit

Permalink
refactor and test changes
Browse files Browse the repository at this point in the history
Signed-off-by: Sam <[email protected]>
  • Loading branch information
samuelcostae committed Aug 28, 2023
1 parent 80925f4 commit 71b83f1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ protected void handleGet(final RestChannel channel, RestRequest request, Client

String filterBy = request.param("filterBy", "all");

if (filterBy.equalsIgnoreCase("internal") || filterBy.equalsIgnoreCase("service")) {
userService.filterAccountsByType(configuration, filterBy);
if (filterBy != "all") {
try {
userService.filterAccountsByType(configuration, filterBy);
} catch (UserServiceException ex) {
badRequestResponse(channel, ex.getMessage());
}
}

// no specific resource requested, return complete config
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/opensearch/security/user/AccountType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opensearch.security.user;

public enum AccountType {
INTERNAL,
SERVICE
}
11 changes: 8 additions & 3 deletions src/main/java/org/opensearch/security/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ public static void saveAndUpdateConfigs(
* @param requestedAccountType The type of account to be kept. Should be "service" or "internal"
*
*/
public void filterAccountsByType(SecurityDynamicConfiguration<?> configuration, String requestedAccountType) {
public void filterAccountsByType(SecurityDynamicConfiguration<?> configuration, String requestedAccountType)
throws UserServiceException {
if (!requestedAccountType.equalsIgnoreCase(AccountType.INTERNAL.name())
&& !requestedAccountType.equalsIgnoreCase(AccountType.SERVICE.name())) {
throw new UserServiceException("Invalid user type {} " + requestedAccountType);
}
List<String> toBeRemoved = new ArrayList<>();

for (Map.Entry<String, ?> entry : configuration.getCEntries().entrySet()) {
Expand All @@ -337,9 +342,9 @@ public void filterAccountsByType(SecurityDynamicConfiguration<?> configuration,
final String accountName = entry.getKey();
boolean isServiceAccount = Boolean.parseBoolean(accountAttributes.getOrDefault("service", "false").toString());

if (requestedAccountType.equalsIgnoreCase("internal") && isServiceAccount) {
if (requestedAccountType.equalsIgnoreCase(AccountType.INTERNAL.name()) && isServiceAccount) {
toBeRemoved.add(accountName);
} else if (requestedAccountType.equalsIgnoreCase("service") && isServiceAccount == false) {
} else if (requestedAccountType.equalsIgnoreCase(AccountType.SERVICE.name()) && isServiceAccount == false) {
toBeRemoved.add(accountName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.message.BasicHeader;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -151,13 +152,10 @@ public void testUserFilters() throws Exception {
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

HttpResponse response;

response = rh.executePutRequest(ENDPOINT + "/internalusers/happyServiceDead", ENABLED_SERVICE_ACCOUNT_BODY);

final int SERVICE_ACCOUNTS_IN_SETTINGS = 1;
final int INTERNAL_ACCOUNTS_IN_SETTINGS = 19;
final String serviceAccountName = "JohnDoeService";
HttpResponse response;

response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=internal");

Expand All @@ -168,12 +166,27 @@ public void testUserFilters() throws Exception {
response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=service");
Assert.assertEquals(response.getBody(), HttpStatus.SC_OK, response.getStatusCode());
list = DefaultObjectMapper.readTree(response.getBody());
Assert.assertEquals(SERVICE_ACCOUNTS_IN_SETTINGS, list.size());
assertThat(list, Matchers.emptyIterable());

response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=ssas");
response = rh.executePutRequest(ENDPOINT + "/internalusers/" + serviceAccountName, ENABLED_SERVICE_ACCOUNT_BODY);

// repeat assertions after adding the service account

response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=internal");

Assert.assertEquals(response.getBody(), HttpStatus.SC_OK, response.getStatusCode());
list = DefaultObjectMapper.readTree(response.getBody());
Assert.assertEquals(INTERNAL_ACCOUNTS_IN_SETTINGS, list.size());

response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=service");
Assert.assertEquals(response.getBody(), HttpStatus.SC_OK, response.getStatusCode());
list = DefaultObjectMapper.readTree(response.getBody());
Assert.assertEquals(SERVICE_ACCOUNTS_IN_SETTINGS + INTERNAL_ACCOUNTS_IN_SETTINGS, list.size());
Assert.assertEquals(SERVICE_ACCOUNTS_IN_SETTINGS, list.size());
assertThat(response.findObjectInJson(serviceAccountName), Matchers.notNullValue());
assertThat(response.findValueInJson(serviceAccountName + ".attributes.service"), containsString("true"));

response = rh.executeGetRequest(ENDPOINT + "/internalusers?filterBy=ssas");
Assert.assertEquals(response.getBody(), HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

response = rh.executeGetRequest(ENDPOINT + "/internalusers?wrongparameter=jhondoe");
Assert.assertEquals(response.getBody(), HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private void throwIfNotValueNode(final JsonNode node) {
}
}

private JsonNode findObjectInJson(final String jsonDotPath) {
public JsonNode findObjectInJson(final String jsonDotPath) {
// Make sure its json / then parse it
if (!isJsonContentType()) {
throw new RuntimeException("Response was expected to be JSON, body was: \n" + body);
Expand Down

0 comments on commit 71b83f1

Please sign in to comment.