Skip to content

Commit 43836ef

Browse files
Merge pull request #2829 from deckhouse/ldap-case-insensitive-dn
LDAP case-insensitive DN attribute
2 parents 1536947 + 777e162 commit 43836ef

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

connector/ldap/ldap.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net"
1111
"os"
12+
"strings"
1213

1314
"github.com/go-ldap/ldap/v3"
1415

@@ -347,21 +348,23 @@ func (c *ldapConnector) do(_ context.Context, f func(c *ldap.Conn) error) error
347348
return f(conn)
348349
}
349350

350-
func getAttrs(e ldap.Entry, name string) []string {
351+
func (c *ldapConnector) getAttrs(e ldap.Entry, name string) []string {
351352
for _, a := range e.Attributes {
352353
if a.Name != name {
353354
continue
354355
}
355356
return a.Values
356357
}
357-
if name == "DN" {
358+
if strings.ToLower(name) == "dn" {
358359
return []string{e.DN}
359360
}
361+
362+
c.logger.Debugf("%q attribute is not fround in entry", name)
360363
return nil
361364
}
362365

363-
func getAttr(e ldap.Entry, name string) string {
364-
if a := getAttrs(e, name); len(a) > 0 {
366+
func (c *ldapConnector) getAttr(e ldap.Entry, name string) string {
367+
if a := c.getAttrs(e, name); len(a) > 0 {
365368
return a[0]
366369
}
367370
return ""
@@ -373,25 +376,25 @@ func (c *ldapConnector) identityFromEntry(user ldap.Entry) (ident connector.Iden
373376
missing := []string{}
374377

375378
// Fill the identity struct using the attributes from the user entry.
376-
if ident.UserID = getAttr(user, c.UserSearch.IDAttr); ident.UserID == "" {
379+
if ident.UserID = c.getAttr(user, c.UserSearch.IDAttr); ident.UserID == "" {
377380
missing = append(missing, c.UserSearch.IDAttr)
378381
}
379382

380383
if c.UserSearch.NameAttr != "" {
381-
if ident.Username = getAttr(user, c.UserSearch.NameAttr); ident.Username == "" {
384+
if ident.Username = c.getAttr(user, c.UserSearch.NameAttr); ident.Username == "" {
382385
missing = append(missing, c.UserSearch.NameAttr)
383386
}
384387
}
385388

386389
if c.UserSearch.PreferredUsernameAttrAttr != "" {
387-
if ident.PreferredUsername = getAttr(user, c.UserSearch.PreferredUsernameAttrAttr); ident.PreferredUsername == "" {
390+
if ident.PreferredUsername = c.getAttr(user, c.UserSearch.PreferredUsernameAttrAttr); ident.PreferredUsername == "" {
388391
missing = append(missing, c.UserSearch.PreferredUsernameAttrAttr)
389392
}
390393
}
391394

392395
if c.UserSearch.EmailSuffix != "" {
393396
ident.Email = ident.Username + "@" + c.UserSearch.EmailSuffix
394-
} else if ident.Email = getAttr(user, c.UserSearch.EmailAttr); ident.Email == "" {
397+
} else if ident.Email = c.getAttr(user, c.UserSearch.EmailAttr); ident.Email == "" {
395398
missing = append(missing, c.UserSearch.EmailAttr)
396399
}
397400
// TODO(ericchiang): Let this value be set from an attribute.
@@ -575,13 +578,13 @@ func (c *ldapConnector) Refresh(ctx context.Context, s connector.Scopes, ident c
575578

576579
func (c *ldapConnector) groups(ctx context.Context, user ldap.Entry) ([]string, error) {
577580
if c.GroupSearch.BaseDN == "" {
578-
c.logger.Debugf("No groups returned for %q because no groups baseDN has been configured.", getAttr(user, c.UserSearch.NameAttr))
581+
c.logger.Debugf("No groups returned for %q because no groups baseDN has been configured.", c.getAttr(user, c.UserSearch.NameAttr))
579582
return nil, nil
580583
}
581584

582585
var groups []*ldap.Entry
583586
for _, matcher := range c.GroupSearch.UserMatchers {
584-
for _, attr := range getAttrs(user, matcher.UserAttr) {
587+
for _, attr := range c.getAttrs(user, matcher.UserAttr) {
585588
filter := fmt.Sprintf("(%s=%s)", matcher.GroupAttr, ldap.EscapeFilter(attr))
586589
if c.GroupSearch.Filter != "" {
587590
filter = fmt.Sprintf("(&%s%s)", c.GroupSearch.Filter, filter)
@@ -617,7 +620,7 @@ func (c *ldapConnector) groups(ctx context.Context, user ldap.Entry) ([]string,
617620

618621
groupNames := make([]string, 0, len(groups))
619622
for _, group := range groups {
620-
name := getAttr(*group, c.GroupSearch.NameAttr)
623+
name := c.getAttr(*group, c.GroupSearch.NameAttr)
621624
if name == "" {
622625
// Be obnoxious about missing attributes. If the group entry is
623626
// missing its name attribute, that indicates a misconfiguration.

connector/ldap/ldap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func TestGroupFilter(t *testing.T) {
277277
c.GroupSearch.BaseDN = "ou=TestGroupFilter,dc=example,dc=org"
278278
c.GroupSearch.UserMatchers = []UserMatcher{
279279
{
280-
UserAttr: "DN",
280+
UserAttr: "dn",
281281
GroupAttr: "member",
282282
},
283283
}

0 commit comments

Comments
 (0)