[ 
https://issues.apache.org/jira/browse/KNOX-3585?focusedWorklogId=1055790&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055790
 ]

ASF GitHub Bot logged work on KNOX-3585:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Nov/26 22:17
            Start Date: 18/Nov/26 22:17
    Worklog Time Spent: 10m 
      Work Description: handavid commented on code in PR #1522:
URL: https://github.com/apache/knox/pull/1522#discussion_r4621615026


##########
gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/interceptor/LDAPRolesLookupInterceptor.java:
##########
@@ -277,18 +296,125 @@ private EntryFilteringCursor 
handleSearch(SearchOperationContext ctx) throws Lda
             
ctx.setReturningAttributes(withAttributes(originalReturningAttributes, 
augmentedAttributes));
         }
 
+        int numUserAndGroupEntries = 0;
         final List<Entry> entries = new ArrayList<>();
         try (EntryFilteringCursor cursor = next(ctx)) {
             while (cursor.next()) {
+                Entry entry = cursor.get();
+                if (LdapUtils.isUserEntry(entry) || 
LdapUtils.isGroupEntry(entry)) {
+                    numUserAndGroupEntries++;
+                }
                 entries.add(cursor.get());
             }
         } catch (Exception e) {
             LOG.ldapRolesLookupFailed(LdapUtils.extractGroupName(ctx.getDn()), 
e);
             throw new LdapException(e);
         }
 
+        // Populating the roles through lookup results in one lookup request 
per entry.
+        // It is more efficient to list all the roles when the number of 
entries is large.
+        final List<Entry> resultEntries = numUserAndGroupEntries > numRoles ?
+                populateRolesThroughList(entries, augmentedAttributes) :
+                populateRolesThroughLookup(entries, augmentedAttributes);
+
+        if (!augmentedAttributes.isEmpty()) {
+            ctx.setReturningAttributes(originalReturningAttributes == null ? 
new String[0] : originalReturningAttributes);
+        }
+
+        return new EntryFilteringCursorImpl(new ListCursor<>(resultEntries), 
ctx, schemaManager);
+    }
+
+    private List<Entry> populateRolesThroughList(List<Entry> entries, 
List<String> augmentedAttributes) throws LdapException {
+        // Map of user id to the assigned roles
+        final Map<String, List<String>> userRoleAssignments = new HashMap<>();
+        // Map of group id to the assigned roles
+        final Map<String, List<String>> groupRoleAssignments = new HashMap<>();
+        try {
+            Collection<String> rolesList = rolesLookupService.listRoles();
+            numRoles = rolesList.size();
+            for (String roleName : rolesList) {
+                try {
+                    RoleAssignees assignees = 
rolesLookupService.lookupRoleAssignees(roleName);
+                    if (assignees != null) {
+                        if (assignees.getUsers() != null && 
!assignees.getUsers().isEmpty()) {
+                            for (String userName : assignees.getUsers()) {
+                                if 
(!userRoleAssignments.containsKey(userName)) {
+                                    ArrayList<String> roles = new 
ArrayList<>();
+                                    roles.add(roleName);
+                                    userRoleAssignments.put(userName, roles);
+                                } else {
+                                    
userRoleAssignments.get(userName).add(roleName);
+                                }
+
+                            }
+                        }
+                        if (assignees.getGroups() != null && 
!assignees.getGroups().isEmpty()) {
+                            for (String groupName : assignees.getGroups()) {
+                                if 
(!groupRoleAssignments.containsKey(groupName)) {
+                                    ArrayList<String> roles = new 
ArrayList<>();
+                                    roles.add(roleName);
+                                    groupRoleAssignments.put(groupName, roles);
+                                } else {
+                                    
groupRoleAssignments.get(groupName).add(roleName);
+                                }
+                            }
+                        }
+                    }
+                } catch (Exception e) {
+                    LOG.ldapRolesReverseLookupFailed(roleName, e);

Review Comment:
   I added a rethrow here, although the codepath changed a little with the 
addition of another method to get all the role assignements.





Issue Time Tracking
-------------------

    Worklog Id:     (was: 1055790)
    Time Spent: 1h 20m  (was: 1h 10m)

> Optimize roles lookup for large result sets
> -------------------------------------------
>
>                 Key: KNOX-3585
>                 URL: https://issues.apache.org/jira/browse/KNOX-3585
>             Project: Apache Knox
>          Issue Type: Improvement
>          Components: LDAP
>    Affects Versions: 3.0.0
>            Reporter: David Han
>            Assignee: David Han
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> The roles lookup interceptor makes a lookup call for each user entry. If the 
> entry set is large, this may result in 1000s of calls to the roles lookup 
> server before the results are returned.
> If the number of results are greater than the number of roles, it would be 
> faster to retrieve all roles from the role server and perform the mapping in 
> the roles lookup interceptor.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to