Skip to content

Commit 638b06d

Browse files
committed
fix: rewrite to regex for group filtering
Signed-off-by: Alexey Romanyuta <r9odt@yandex.ru>
1 parent aa99984 commit 638b06d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

connector/ldap/ldap.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net"
1212
"net/url"
1313
"os"
14+
"regexp"
1415
"strings"
1516

1617
"github.com/go-ldap/ldap/v3"
@@ -63,7 +64,8 @@ type UserMatcher struct {
6364
UserAttr string `json:"userAttr"`
6465
GroupAttr string `json:"groupAttr"`
6566
// Work only if UserAttr is 'memberOf' and GroupAttr is dn
66-
GroupPrefix string `json:"groupPrefix"`
67+
GroupRegexp string `json:"groupRegexp"`
68+
groupMatcher *regexp.Regexp
6769
}
6870

6971
// Config holds configuration options for LDAP logins.
@@ -293,6 +295,13 @@ func (c *Config) openConnector(logger *slog.Logger) (*ldapConnector, error) {
293295

294296
// TODO(nabokihms): remove it after deleting deprecated groupSearch options
295297
c.GroupSearch.UserMatchers = userMatchers(c, logger)
298+
for i, _ := range c.GroupSearch.UserMatchers {
299+
c.GroupSearch.UserMatchers[i].groupMatcher, err = regexp.Compile(c.GroupSearch.UserMatchers[i].GroupRegexp)
300+
if err != nil {
301+
logger.Error("Regular expression compilation error", "user_attr", c.GroupSearch.UserMatchers[i].UserAttr, "group_attr", c.GroupSearch.UserMatchers[i].GroupAttr, "err", err.Error())
302+
c.GroupSearch.UserMatchers[i].groupMatcher, _ = regexp.Compile("")
303+
}
304+
}
296305
return &ldapConnector{*c, userSearchScope, groupSearchScope, tlsConfig, logger}, nil
297306
}
298307

@@ -617,7 +626,7 @@ func (c *ldapConnector) groups(ctx context.Context, user ldap.Entry) ([]string,
617626
fmt.Sprintf(",%s", c.GroupSearch.BaseDN))
618627

619628
// Is it needed compability with GroupSearch.Filter? (r9odt)
620-
if !strings.HasPrefix(groupName, matcher.GroupPrefix) {
629+
if !matcher.groupMatcher.MatchString(groupName) {
621630
continue
622631
}
623632

0 commit comments

Comments
 (0)