GUACAMOLE-234: Merge fixes for LDAP resource leak regressions.

This commit is contained in:
Mike Jumper
2019-08-12 10:15:13 -07:00
committed by GitHub
2 changed files with 17 additions and 28 deletions

View File

@@ -128,10 +128,8 @@ public class LDAPConnectionService {
public LdapNetworkConnection bindAs(Dn userDN, String password) public LdapNetworkConnection bindAs(Dn userDN, String password)
throws GuacamoleException { throws GuacamoleException {
// Obtain appropriately-configured LdapNetworkConnection instance // Get ldapConnection and try to connect and bind.
LdapNetworkConnection ldapConnection = createLDAPConnection(); try (LdapNetworkConnection ldapConnection = createLDAPConnection()) {
try {
// Connect to LDAP server // Connect to LDAP server
ldapConnection.connect(); ldapConnection.connect();
@@ -140,14 +138,7 @@ public class LDAPConnectionService {
if (confService.getEncryptionMethod() == EncryptionMethod.STARTTLS) if (confService.getEncryptionMethod() == EncryptionMethod.STARTTLS)
ldapConnection.startTls(); ldapConnection.startTls();
} // Bind using provided credentials
catch (LdapException e) {
throw new GuacamoleServerException("Error connecting to LDAP server.", e);
}
// Bind using provided credentials
try {
BindRequest bindRequest = new BindRequestImpl(); BindRequest bindRequest = new BindRequestImpl();
bindRequest.setDn(userDN); bindRequest.setDn(userDN);
bindRequest.setCredentials(password); bindRequest.setCredentials(password);
@@ -165,7 +156,6 @@ public class LDAPConnectionService {
// Disconnect if an error occurs during bind // Disconnect if an error occurs during bind
catch (LdapException e) { catch (LdapException e) {
logger.debug("Unable to bind to LDAP server.", e); logger.debug("Unable to bind to LDAP server.", e);
disconnect(ldapConnection);
throw new GuacamoleInvalidCredentialsException( throw new GuacamoleInvalidCredentialsException(
"Unable to bind to the LDAP server.", "Unable to bind to the LDAP server.",
CredentialsInfo.USERNAME_PASSWORD); CredentialsInfo.USERNAME_PASSWORD);

View File

@@ -20,6 +20,7 @@
package org.apache.guacamole.auth.ldap; package org.apache.guacamole.auth.ldap;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@@ -188,19 +189,17 @@ public class ObjectQueryService {
logger.debug("Searching \"{}\" for objects matching \"{}\".", baseDN, query); logger.debug("Searching \"{}\" for objects matching \"{}\".", baseDN, query);
try { LdapConnectionConfig ldapConnectionConfig = ldapConnection.getConfig();
LdapConnectionConfig ldapConnectionConfig = ldapConnection.getConfig(); // Search within subtree of given base DN
SearchRequest request = ldapService.getSearchRequest(baseDN,
query);
// Search within subtree of given base DN // Produce list of all entries in the search result, automatically
SearchRequest request = ldapService.getSearchRequest(baseDN, // following referrals if configured to do so
query); List<Entry> entries = new ArrayList<>();
SearchCursor results = ldapConnection.search(request); try (SearchCursor results = ldapConnection.search(request)) {
// Produce list of all entries in the search result, automatically
// following referrals if configured to do so
List<Entry> entries = new ArrayList<>();
while (results.next()) { while (results.next()) {
if (results.isEntry()) { if (results.isEntry()) {
@@ -226,7 +225,7 @@ public class ObjectQueryService {
return entries; return entries;
} }
catch (CursorException | LdapException e) { catch (CursorException | IOException | LdapException e) {
throw new GuacamoleServerException("Unable to query list of " throw new GuacamoleServerException("Unable to query list of "
+ "objects from LDAP directory.", e); + "objects from LDAP directory.", e);
} }