LDAP: Make LDAP attribute mapping case-insensitive (#58992)

* Make LDAP attribute mapping case-insensitive

* Add test case with attribute name different from schema's

* Add fix to getArrayAttribute also and add test with mismatched letter
case.

* Update pkg/services/ldap/helpers.go

Co-authored-by: Misi <mgyongyosi@users.noreply.github.com>
pull/59114/head
Marcos de Oliveira 3 years ago committed by GitHub
parent 4d8287b319
commit 91582ba03d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      pkg/services/ldap/helpers.go
  2. 29
      pkg/services/ldap/ldap_helpers_test.go

@ -34,7 +34,7 @@ func getAttribute(name string, entry *ldap.Entry) string {
}
for _, attr := range entry.Attributes {
if attr.Name == name {
if strings.EqualFold(attr.Name, name) {
if len(attr.Values) > 0 {
return attr.Values[0]
}
@ -49,7 +49,7 @@ func getArrayAttribute(name string, entry *ldap.Entry) []string {
}
for _, attr := range entry.Attributes {
if attr.Name == name && len(attr.Values) > 0 {
if strings.EqualFold(attr.Name, name) && len(attr.Values) > 0 {
return attr.Values
}
}

@ -83,6 +83,20 @@ func TestGetAttribute(t *testing.T) {
assert.Equal(t, value, result)
})
t.Run("letter case mismatch", func(t *testing.T) {
value := "roelgerrits"
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "sAMAccountName", Values: []string{value},
},
},
}
result := getAttribute("samaccountname", entry)
assert.Equal(t, value, result)
})
t.Run("no result", func(t *testing.T) {
value := []string{"roelgerrits"}
entry := &ldap.Entry{
@ -124,6 +138,21 @@ func TestGetArrayAttribute(t *testing.T) {
assert.EqualValues(t, value, result)
})
t.Run("letter case mismatch", func(t *testing.T) {
value := []string{"CN=Administrators,CN=Builtin,DC=grafana,DC=org"}
entry := &ldap.Entry{
Attributes: []*ldap.EntryAttribute{
{
Name: "memberOf", Values: value,
},
},
}
result := getArrayAttribute("memberof", entry)
assert.EqualValues(t, value, result)
})
t.Run("no result", func(t *testing.T) {
value := []string{"roelgerrits"}
entry := &ldap.Entry{

Loading…
Cancel
Save