handavid commented on code in PR #1522:
URL: https://github.com/apache/knox/pull/1522#discussion_r4619211944


##########
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);
+                }
+            }
+        } catch (Exception e) {
+            LOG.ldapRolesListFailed(e);
+            throw new LdapException(e);
+        }
+
+        final List<Entry> roleEntries = new ArrayList<>();
+        final List<Entry> resultEntries = new ArrayList<>(entries.size());
+
+        for (Entry entry : entries) {
+            try {
+                if (LdapUtils.isGroupEntry(entry)) {
+                    roleEntries.addAll(translateGroupEntry(entry, 
groupRoleAssignments));
+                } else {
+                    final String username = 
toRolesLookupId(LdapUtils.extractUsernameFromEntry(entry, "uid", "cn"));
+                    final List<String> roles = new ArrayList<>();
+                    if (userRoleAssignments.containsKey(username)) {
+                        roles.addAll(userRoleAssignments.get(username));
+                    }
+                    Attribute memberOfAttribute = entry.get("memberOf");
+                    if (memberOfAttribute != null) {
+                        for (Value groupDnValue : memberOfAttribute) {
+                            // Key on the same id the lookup branch sends to 
the backend (toRolesLookupId),
+                            // so a case-sensitive roles backend is matched 
consistently across both branches.
+                            String groupName = 
toRolesLookupId(LdapUtils.extractGroupName(new Dn(groupDnValue.getString())));

Review Comment:
   accepted suggestion



-- 
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