[
https://issues.apache.org/jira/browse/KNOX-3585?focusedWorklogId=1055771&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055771
]
ASF GitHub Bot logged work on KNOX-3585:
----------------------------------------
Author: ASF GitHub Bot
Created on: 18/Nov/26 18:55
Start Date: 18/Nov/26 18:55
Worklog Time Spent: 10m
Work Description: 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
Issue Time Tracking
-------------------
Worklog Id: (was: 1055771)
Time Spent: 50m (was: 40m)
> 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: 50m
> 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)