Skip to content

Commit 331c912

Browse files
authored
Request for public keys only if LDAP attribute is set (#5816)
* Update go-ldap dependency * Request for public keys only if attribute is set
1 parent 1b90692 commit 331c912

File tree

13 files changed

+293
-105
lines changed

13 files changed

+293
-105
lines changed

Gopkg.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/auth/ldap/ldap.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,17 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
247247
return nil
248248
}
249249

250+
var isAttributeSSHPublicKeySet = len(strings.TrimSpace(ls.AttributeSSHPublicKey)) > 0
251+
252+
attribs := []string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail}
253+
if isAttributeSSHPublicKeySet {
254+
attribs = append(attribs, ls.AttributeSSHPublicKey)
255+
}
256+
250257
log.Trace("Fetching attributes '%v', '%v', '%v', '%v', '%v' with filter %s and base %s", ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey, userFilter, userDN)
251258
search := ldap.NewSearchRequest(
252259
userDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
253-
[]string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey},
254-
nil)
260+
attribs, nil)
255261

256262
sr, err := l.Search(search)
257263
if err != nil {
@@ -267,11 +273,15 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
267273
return nil
268274
}
269275

276+
var sshPublicKey []string
277+
270278
username := sr.Entries[0].GetAttributeValue(ls.AttributeUsername)
271279
firstname := sr.Entries[0].GetAttributeValue(ls.AttributeName)
272280
surname := sr.Entries[0].GetAttributeValue(ls.AttributeSurname)
273281
mail := sr.Entries[0].GetAttributeValue(ls.AttributeMail)
274-
sshPublicKey := sr.Entries[0].GetAttributeValues(ls.AttributeSSHPublicKey)
282+
if isAttributeSSHPublicKeySet {
283+
sshPublicKey = sr.Entries[0].GetAttributeValues(ls.AttributeSSHPublicKey)
284+
}
275285
isAdmin := checkAdmin(l, ls, userDN)
276286

277287
if !directBind && ls.AttributesInBind {
@@ -320,11 +330,17 @@ func (ls *Source) SearchEntries() []*SearchResult {
320330

321331
userFilter := fmt.Sprintf(ls.Filter, "*")
322332

333+
var isAttributeSSHPublicKeySet = len(strings.TrimSpace(ls.AttributeSSHPublicKey)) > 0
334+
335+
attribs := []string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail}
336+
if isAttributeSSHPublicKeySet {
337+
attribs = append(attribs, ls.AttributeSSHPublicKey)
338+
}
339+
323340
log.Trace("Fetching attributes '%v', '%v', '%v', '%v', '%v' with filter %s and base %s", ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey, userFilter, ls.UserBase)
324341
search := ldap.NewSearchRequest(
325342
ls.UserBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, userFilter,
326-
[]string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail, ls.AttributeSSHPublicKey},
327-
nil)
343+
attribs, nil)
328344

329345
var sr *ldap.SearchResult
330346
if ls.UsePagedSearch() {
@@ -341,12 +357,14 @@ func (ls *Source) SearchEntries() []*SearchResult {
341357

342358
for i, v := range sr.Entries {
343359
result[i] = &SearchResult{
344-
Username: v.GetAttributeValue(ls.AttributeUsername),
345-
Name: v.GetAttributeValue(ls.AttributeName),
346-
Surname: v.GetAttributeValue(ls.AttributeSurname),
347-
Mail: v.GetAttributeValue(ls.AttributeMail),
348-
SSHPublicKey: v.GetAttributeValues(ls.AttributeSSHPublicKey),
349-
IsAdmin: checkAdmin(l, ls, v.DN),
360+
Username: v.GetAttributeValue(ls.AttributeUsername),
361+
Name: v.GetAttributeValue(ls.AttributeName),
362+
Surname: v.GetAttributeValue(ls.AttributeSurname),
363+
Mail: v.GetAttributeValue(ls.AttributeMail),
364+
IsAdmin: checkAdmin(l, ls, v.DN),
365+
}
366+
if isAttributeSSHPublicKeySet {
367+
result[i].SSHPublicKey = v.GetAttributeValues(ls.AttributeSSHPublicKey)
350368
}
351369
}
352370

vendor/gopkg.in/ldap.v2/LICENSE

Lines changed: 19 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/ldap.v2/atomic_value.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/ldap.v2/atomic_value_go13.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/gopkg.in/ldap.v2/conn.go

Lines changed: 38 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)