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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to