Skip to content

Commit 628c888

Browse files
authored
Add logging wrappers to LdapService calls (#442)
* Add logging wrappers to LdapService calls * Log auth call wrapper on debug as well - Add log statement for successful auth - we don't expect this to be called often, but if we have it enabled we are prob paying close attention to some auth-related issue - seems helpful to give ourselves more insight as to what's happening with user auth requests.
1 parent bb30fde commit 628c888

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

grails-app/services/io/xh/hoist/ldap/LdapService.groovy

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.xh.hoist.ldap
22

3+
34
import io.xh.hoist.BaseService
45
import io.xh.hoist.cache.Cache
56
import org.apache.directory.api.ldap.model.entry.Attribute
6-
import org.apache.directory.api.ldap.model.message.SearchScope
77
import org.apache.directory.api.ldap.model.exception.LdapAuthenticationException
8+
import org.apache.directory.api.ldap.model.message.SearchScope
89
import org.apache.directory.ldap.client.api.LdapConnectionConfig
9-
import org.apache.directory.ldap.client.api.NoVerificationTrustManager
1010
import org.apache.directory.ldap.client.api.LdapNetworkConnection
11+
import org.apache.directory.ldap.client.api.NoVerificationTrustManager
1112

1213
import static grails.async.Promises.task
1314
import static io.xh.hoist.util.DateTimeUtils.SECONDS
@@ -53,15 +54,21 @@ class LdapService extends BaseService {
5354
}
5455

5556
LdapPerson lookupUser(String sName) {
56-
searchOne("(sAMAccountName=$sName) ", LdapPerson, true)
57+
withDebug(["Looking up user", [sAMAccountName: sName]]) {
58+
searchOne("(sAMAccountName=$sName) ", LdapPerson, true)
59+
}
5760
}
5861

5962
List<LdapPerson> lookupGroupMembers(String dn) {
60-
lookupGroupMembersInternal(dn, true)
63+
withDebug(["Looking up group members", [dn: dn]]) {
64+
lookupGroupMembersInternal(dn, true)
65+
}
6166
}
6267

6368
List<LdapGroup> findGroups(String sNamePart) {
64-
searchMany("(sAMAccountName=*$sNamePart*)", LdapGroup, true)
69+
withDebug("Finding groups with name matching *$sNamePart") {
70+
searchMany("(sAMAccountName=*$sNamePart*)", LdapGroup, true)
71+
}
6572
}
6673

6774
/**
@@ -71,8 +78,11 @@ class LdapService extends BaseService {
7178
* otherwise, failed lookups will be logged, and resolved as null.
7279
*/
7380
Map<String, LdapGroup> lookupGroups(Set<String> dns, boolean strictMode = false) {
74-
dns.collectEntries { dn -> [dn, task { lookupGroupInternal(dn, strictMode) }] }
75-
.collectEntries { [it.key, it.value.get()] }
81+
withDebug(["Looking up groups", [dns: dns, strictMode: strictMode]]) {
82+
dns.collectEntries { dn -> [dn, task { lookupGroupInternal(dn, strictMode) }] }
83+
.collectEntries { [it.key, it.value.get()] } as Map<String, LdapGroup>
84+
}
85+
7686
}
7787

7888
/**
@@ -82,8 +92,10 @@ class LdapService extends BaseService {
8292
* otherwise, failed lookups will be logged, and resolved as an empty list.
8393
*/
8494
Map<String, List<LdapPerson>> lookupGroupMembers(Set<String> dns, boolean strictMode = false) {
85-
dns.collectEntries { dn -> [dn, task { lookupGroupMembersInternal(dn, strictMode) }] }
86-
.collectEntries { [it.key, it.value.get()] }
95+
withDebug(["Looking up group members", [dns: dns, strictMode: strictMode]]) {
96+
dns.collectEntries { dn -> [dn, task { lookupGroupMembersInternal(dn, strictMode) }] }
97+
.collectEntries { [it.key, it.value.get()] } as Map<String, List<LdapPerson>>
98+
}
8799
}
88100

89101
/**
@@ -128,24 +140,27 @@ class LdapService extends BaseService {
128140
* @return true if the password is valid and the test connection succeeds
129141
*/
130142
boolean authenticate(String username, String password) {
131-
for (Map server in config.servers) {
132-
String host = server.host
133-
List<LdapPerson> matches = doQuery(server, "(sAMAccountName=$username)", LdapPerson, true)
134-
if (matches) {
135-
if (matches.size() > 1) throw new RuntimeException("Multiple user records found for $username")
136-
LdapPerson user = matches.first()
137-
try (def conn = createConnection(host)) {
138-
conn.bind(user.distinguishedname, password)
139-
conn.unBind()
140-
return true
141-
} catch (LdapAuthenticationException ignored) {
142-
logDebug('Authentication failed, incorrect credentials', [username: username])
143-
return false
143+
withDebug(["Attempting LDAP bind to authenticate user", [username: username]]) {
144+
for (Map server in config.servers) {
145+
String host = server.host
146+
List<LdapPerson> matches = doQuery(server, "(sAMAccountName=$username)", LdapPerson, true)
147+
if (matches) {
148+
if (matches.size() > 1) throw new RuntimeException("Multiple user records found for $username")
149+
LdapPerson user = matches.first()
150+
try (def conn = createConnection(host)) {
151+
conn.bind(user.distinguishedname, password)
152+
conn.unBind()
153+
logDebug('Authentication successful', [username: username])
154+
return true
155+
} catch (LdapAuthenticationException ignored) {
156+
logDebug('Authentication failed, incorrect credentials', [username: username])
157+
return false
158+
}
144159
}
145160
}
161+
logDebug('Authentication failed, no user found', [username: username])
162+
return false
146163
}
147-
logDebug('Authentication failed, no user found', [username: username])
148-
return false
149164
}
150165

151166
//----------------------
@@ -192,7 +207,7 @@ class LdapService extends BaseService {
192207
List<T> ret = cache.get(key)
193208
if (ret != null) return ret
194209

195-
withDebug(["Querying LDAP", [host: host, filter: filter]]) {
210+
withTrace(["Querying LDAP", [host: host, filter: filter]]) {
196211
try (def conn = createConnection(host)) {
197212
String baseDn = isPerson ? server.baseUserDn : server.baseGroupDn
198213
String[] keys = objType.keys.toArray() as String[]

0 commit comments

Comments
 (0)