Skip to content

Commit

Permalink
fix: npe if ldap query doesn't return attributes
Browse files Browse the repository at this point in the history
We cannot assume the LDAP server will have group attributes programmed
everytime. So handle it accordingly.

Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha committed Jan 11, 2024
1 parent 2a6bf66 commit 36fb75b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/api/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]

attributes := lc.Attributes
attributes = append(attributes, "dn")
attributes = append(attributes, lc.UserGroupAttribute)
if lc.UserGroupAttribute != "" {

Check failure on line 177 in pkg/api/ldap.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
attributes = append(attributes, lc.UserGroupAttribute)
}

searchScope := ldap.ScopeSingleLevel

Expand Down Expand Up @@ -207,7 +209,7 @@ func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]
return false, nil, nil, err
}

if len(search.Entries) > 1 {
if lc.UserGroupAttribute != "" && len(search.Entries) > 1 {
err := errors.ErrEntriesExceeded
lc.Log.Error().Err(err).Str("bindDN", lc.BindDN).Str("username", username).
Str("baseDN", lc.Base).Msg("failed to retrieve due to an excessive amount of entries")
Expand All @@ -216,8 +218,11 @@ func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]
}

userDN := search.Entries[0].DN
userAttributes := search.Entries[0].Attributes[0]
userGroups := userAttributes.Values
var userGroups []string

Check failure on line 221 in pkg/api/ldap.go

View workflow job for this annotation

GitHub Actions / lint

declarations should never be cuddled (wsl)
if len(search.Entries[0].Attributes) > 0 {

Check failure on line 222 in pkg/api/ldap.go

View workflow job for this annotation

GitHub Actions / lint

only one cuddle assignment allowed before if statement (wsl)
userAttributes := search.Entries[0].Attributes[0]
userGroups = userAttributes.Values
}
user := map[string]string{}

for _, attr := range lc.Attributes {
Expand Down

0 comments on commit 36fb75b

Please sign in to comment.