From 81cbbc00cdf60fe656f5df75e733030a68f494b6 Mon Sep 17 00:00:00 2001 From: Sam <128482925+samuelcostae@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:35:08 +0100 Subject: [PATCH] Update src/main/java/org/opensearch/security/user/UserService.java Co-authored-by: Darshit Chanpura <35282393+DarshitChanpura@users.noreply.github.com> Signed-off-by: Sam <128482925+samuelcostae@users.noreply.github.com> --- .../opensearch/security/user/UserService.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/opensearch/security/user/UserService.java b/src/main/java/org/opensearch/security/user/UserService.java index 7e37086e22..c01a68fa4e 100644 --- a/src/main/java/org/opensearch/security/user/UserService.java +++ b/src/main/java/org/opensearch/security/user/UserService.java @@ -335,29 +335,23 @@ public void includeAccountsIfType(SecurityDynamicConfiguration configuration, List toBeRemoved = new ArrayList<>(); if (requestedAccountType == UserFilterType.SERVICE) { - for (Map.Entry entry : configuration.getCEntries().entrySet()) { - final InternalUserV7 internalUserEntry = (InternalUserV7) entry.getValue(); - final Map accountAttributes = internalUserEntry.getAttributes(); - final String accountName = entry.getKey(); - final boolean isServiceAccount = Boolean.parseBoolean(accountAttributes.getOrDefault("service", "false").toString()); - - if (isServiceAccount == false) { - toBeRemoved.add(accountName); - } - } + accountToRemoveFromConfig(configuration, toBeRemoved, false); } else if (requestedAccountType == UserFilterType.INTERNAL) { - - for (Map.Entry entry : configuration.getCEntries().entrySet()) { + accountToRemoveFromConfig(configuration, toBeRemoved, true); + } + configuration.remove(toBeRemoved); + } + + private void accountsToRemoveFromConfiguration(SecurityDynamicConfiguration configuration, ArrayList toBeRemoved, boolean isServiceAccountRequested) { + for (Map.Entry entry : configuration.getCEntries().entrySet()) { final InternalUserV7 internalUserEntry = (InternalUserV7) entry.getValue(); final Map accountAttributes = internalUserEntry.getAttributes(); final String accountName = entry.getKey(); final boolean isServiceAccount = Boolean.parseBoolean(accountAttributes.getOrDefault("service", "false").toString()); - if (isServiceAccount == true) { + if (isServiceAccount == isServiceAccountRequested) { toBeRemoved.add(accountName); } } - } - configuration.remove(toBeRemoved); } }