From 5c0468121b0bfcca76dc0857a0a71b27f3e0f246 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 18 Aug 2023 14:53:48 +0100 Subject: [PATCH] Java doc for filter method and small fixes Signed-off-by: Sam --- .../dlic/rest/api/InternalUsersApiAction.java | 9 +++++---- .../org/opensearch/security/user/UserService.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opensearch/security/dlic/rest/api/InternalUsersApiAction.java b/src/main/java/org/opensearch/security/dlic/rest/api/InternalUsersApiAction.java index 807f531e4b..8eadf56262 100644 --- a/src/main/java/org/opensearch/security/dlic/rest/api/InternalUsersApiAction.java +++ b/src/main/java/org/opensearch/security/dlic/rest/api/InternalUsersApiAction.java @@ -27,6 +27,7 @@ import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; import org.opensearch.common.settings.Settings; +import org.opensearch.core.common.Strings; import org.opensearch.rest.RestChannel; import org.opensearch.rest.RestController; import org.opensearch.rest.RestRequest; @@ -133,12 +134,12 @@ protected void handleGet(final RestChannel channel, RestRequest request, Client String filterBy = request.param("filterBy", "all"); - if (filterBy != "internal" && filterBy != "service") { + if (filterBy.equalsIgnoreCase("internal") || filterBy.equalsIgnoreCase("service")) { userService.filterAccountsByType(configuration, filterBy); } // no specific resource requested, return complete config - if (resourcename == null || resourcename.length() == 0) { + if (Strings.isNullOrEmpty(resourcename)) { successResponse(channel, configuration); return; } @@ -200,7 +201,7 @@ protected void handlePut(RestChannel channel, final RestRequest request, final C if (userExisted && securityJsonNode.get("hash").asString() == null) { // sanity check, this should usually not happen final String hash = ((Hashed) internalUsersConfiguration.getCEntry(username)).getHash(); - if (hash == null || hash.length() == 0) { + if (Strings.isNullOrEmpty(hash)) { internalErrorResponse( channel, "Existing user " + username + " has no password, and no new password or hash was specified." @@ -255,7 +256,7 @@ protected void handlePost(final RestChannel channel, RestRequest request, Client filter(internalUsersConfiguration); // Hides hashes // no specific resource requested - if (username == null || username.length() == 0) { + if (Strings.isNullOrEmpty(username)) { notImplemented(channel, Method.POST); return; diff --git a/src/main/java/org/opensearch/security/user/UserService.java b/src/main/java/org/opensearch/security/user/UserService.java index d2ec41a9f0..4739617372 100644 --- a/src/main/java/org/opensearch/security/user/UserService.java +++ b/src/main/java/org/opensearch/security/user/UserService.java @@ -318,6 +318,16 @@ public static void saveAndUpdateConfigs( } } + /** + * Removes accounts that are not of the requested type from the SecurityDynamicConfiguration object passed. + * + * Accounts with the 'service' attribute set to true, are considered of type 'service'. + * Accounts with the 'service' attribute set to false or without the 'service' attribute, are considered of type 'internal'. + * + * @param configuration SecurityDynamicConfiguration object containing all accounts + * @param requestedAccountType The type of account to be kept. Should be "service" or "internal" + * + */ public void filterAccountsByType(SecurityDynamicConfiguration configuration, String requestedAccountType) { List toBeRemoved = new ArrayList<>(); @@ -332,9 +342,7 @@ public void filterAccountsByType(SecurityDynamicConfiguration configuration, } else if (requestedAccountType.equalsIgnoreCase("service") && isServiceAccount == false) { toBeRemoved.add(accountName); } - } configuration.remove(toBeRemoved); } - }