Skip to content

Commit 91582ba

Browse files
markkrjmgyongyosi
andauthored
LDAP: Make LDAP attribute mapping case-insensitive (grafana#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>
1 parent 4d8287b commit 91582ba

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pkg/services/ldap/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func getAttribute(name string, entry *ldap.Entry) string {
3434
}
3535

3636
for _, attr := range entry.Attributes {
37-
if attr.Name == name {
37+
if strings.EqualFold(attr.Name, name) {
3838
if len(attr.Values) > 0 {
3939
return attr.Values[0]
4040
}
@@ -49,7 +49,7 @@ func getArrayAttribute(name string, entry *ldap.Entry) []string {
4949
}
5050

5151
for _, attr := range entry.Attributes {
52-
if attr.Name == name && len(attr.Values) > 0 {
52+
if strings.EqualFold(attr.Name, name) && len(attr.Values) > 0 {
5353
return attr.Values
5454
}
5555
}

pkg/services/ldap/ldap_helpers_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ func TestGetAttribute(t *testing.T) {
8383
assert.Equal(t, value, result)
8484
})
8585

86+
t.Run("letter case mismatch", func(t *testing.T) {
87+
value := "roelgerrits"
88+
entry := &ldap.Entry{
89+
Attributes: []*ldap.EntryAttribute{
90+
{
91+
Name: "sAMAccountName", Values: []string{value},
92+
},
93+
},
94+
}
95+
96+
result := getAttribute("samaccountname", entry)
97+
assert.Equal(t, value, result)
98+
})
99+
86100
t.Run("no result", func(t *testing.T) {
87101
value := []string{"roelgerrits"}
88102
entry := &ldap.Entry{
@@ -124,6 +138,21 @@ func TestGetArrayAttribute(t *testing.T) {
124138
assert.EqualValues(t, value, result)
125139
})
126140

141+
t.Run("letter case mismatch", func(t *testing.T) {
142+
value := []string{"CN=Administrators,CN=Builtin,DC=grafana,DC=org"}
143+
entry := &ldap.Entry{
144+
Attributes: []*ldap.EntryAttribute{
145+
{
146+
Name: "memberOf", Values: value,
147+
},
148+
},
149+
}
150+
151+
result := getArrayAttribute("memberof", entry)
152+
153+
assert.EqualValues(t, value, result)
154+
})
155+
127156
t.Run("no result", func(t *testing.T) {
128157
value := []string{"roelgerrits"}
129158
entry := &ldap.Entry{

0 commit comments

Comments
 (0)