GUACAMOLE-1130: Only retrieve LDAP attributes that are strictly necessary

This commit is contained in:
Edgardo Rodriguez
2020-07-26 11:56:35 -03:00
committed by Virtually Nick
parent 3a083a1b40
commit 420ffa175d
3 changed files with 25 additions and 9 deletions

View File

@@ -198,6 +198,10 @@ public class ObjectQueryService {
* The current level of referral depth for this search, used for * The current level of referral depth for this search, used for
* limiting the maximum depth to which referrals can go. * limiting the maximum depth to which referrals can go.
* *
* @param relevantAttributes
* The attribute(s) relevant to return for this search,
* if all available should be returned pass null as value.
*
* @return * @return
* A list of all results accessible to the user currently bound under * A list of all results accessible to the user currently bound under
* the given LDAP connection. * the given LDAP connection.
@@ -208,7 +212,8 @@ public class ObjectQueryService {
* guacamole.properties. * guacamole.properties.
*/ */
public List<Entry> search(LdapNetworkConnection ldapConnection, public List<Entry> search(LdapNetworkConnection ldapConnection,
Dn baseDN, ExprNode query, int searchHop) throws GuacamoleException { Dn baseDN, ExprNode query, int searchHop,
Collection<String> relevantAttributes) throws GuacamoleException {
// Refuse to follow referrals if limit has been reached // Refuse to follow referrals if limit has been reached
int maxHops = confService.getMaxReferralHops(); int maxHops = confService.getMaxReferralHops();
@@ -225,12 +230,15 @@ public class ObjectQueryService {
// Search within subtree of given base DN // Search within subtree of given base DN
SearchRequest request = ldapService.getSearchRequest(baseDN, query); SearchRequest request = ldapService.getSearchRequest(baseDN, query);
if (relevantAttributes != null) {
request.addAttributes(relevantAttributes.toArray(new String[0]));
}
// Produce list of all entries in the search result, automatically // Produce list of all entries in the search result, automatically
// following referrals if configured to do so // following referrals if configured to do so
List<Entry> entries = new ArrayList<>(); List<Entry> entries = new ArrayList<>();
try (SearchCursor results = ldapConnection.search(request)) { try (SearchCursor results = ldapConnection.search(request)) {
while (results.next()) { while (results.next()) {
// Add entry directly if no referral is involved // Add entry directly if no referral is involved
@@ -251,7 +259,7 @@ public class ObjectQueryService {
try (LdapNetworkConnection referralConnection = ldapService.bindAs(url, ldapConnection)) { try (LdapNetworkConnection referralConnection = ldapService.bindAs(url, ldapConnection)) {
if (referralConnection != null) { if (referralConnection != null) {
logger.debug("Following referral to \"{}\"...", url); logger.debug("Following referral to \"{}\"...", url);
entries.addAll(search(referralConnection, baseDN, query, searchHop + 1)); entries.addAll(search(referralConnection, baseDN, query, searchHop + 1, relevantAttributes));
} }
else else
logger.debug("Could not bind with LDAP " logger.debug("Could not bind with LDAP "
@@ -329,7 +337,7 @@ public class ObjectQueryService {
ExprNode filter, Collection<String> attributes, String attributeValue) ExprNode filter, Collection<String> attributes, String attributeValue)
throws GuacamoleException { throws GuacamoleException {
ExprNode query = generateQuery(filter, attributes, attributeValue); ExprNode query = generateQuery(filter, attributes, attributeValue);
return search(ldapConnection, baseDN, query, 0); return search(ldapConnection, baseDN, query, 0, attributes);
} }
/** /**

View File

@@ -126,7 +126,7 @@ public class ConnectionService {
// and possibly any groups the user is a member of that are // and possibly any groups the user is a member of that are
// referred to in the seeAlso attribute of the guacConfigGroup. // referred to in the seeAlso attribute of the guacConfigGroup.
List<Entry> results = queryService.search(ldapConnection, List<Entry> results = queryService.search(ldapConnection,
configurationBaseDN, connectionSearchFilter, 0); configurationBaseDN, connectionSearchFilter, 0, null);
// Return a map of all readable connections // Return a map of all readable connections
return queryService.asMap(results, (entry) -> { return queryService.asMap(results, (entry) -> {

View File

@@ -18,8 +18,8 @@
*/ */
package org.apache.guacamole.auth.ldap.group; package org.apache.guacamole.auth.ldap.group;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@@ -192,7 +192,7 @@ public class UserGroupService {
ldapConnection, ldapConnection,
userDN, userDN,
confService.getUserSearchFilter(), confService.getUserSearchFilter(),
0); 0, null);
// ... there can surely only be one // ... there can surely only be one
if (userEntries.size() != 1) if (userEntries.size() != 1)
logger.warn("user DN \"{}\" does not return unique value " logger.warn("user DN \"{}\" does not return unique value "
@@ -214,13 +214,21 @@ public class UserGroupService {
} }
} }
// Gather all attributes relevant for a group
ArrayList<String> groupAttributes = new ArrayList<String>();
groupAttributes.add(confService.getMemberAttribute());
confService.getGroupNameAttributes().forEach(
attribute -> groupAttributes.add(attribute)
);
// Get all groups the user is a member of starting at the groupBaseDN, // Get all groups the user is a member of starting at the groupBaseDN,
// excluding guacConfigGroups // excluding guacConfigGroups
return queryService.search( return queryService.search(
ldapConnection, ldapConnection,
groupBaseDN, groupBaseDN,
getGroupSearchFilter(), getGroupSearchFilter(),
Collections.singleton(confService.getMemberAttribute()), groupAttributes,
userIDorDN userIDorDN
); );