Skip to content

Commit

Permalink
Java doc for filter method and small fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Sam <samuel.costa@eliatra.com>
  • Loading branch information
samuelcostae committed Aug 18, 2023
1 parent 8cc81f0 commit 5c04681
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/opensearch/security/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> toBeRemoved = new ArrayList<>();

Expand All @@ -332,9 +342,7 @@ public void filterAccountsByType(SecurityDynamicConfiguration<?> configuration,
} else if (requestedAccountType.equalsIgnoreCase("service") && isServiceAccount == false) {
toBeRemoved.add(accountName);
}

}
configuration.remove(toBeRemoved);
}

}

0 comments on commit 5c04681

Please sign in to comment.