Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logs and analogy of azure GroupsDisplayNameSuffixPostFilter #19

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions app_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,18 @@ func bannedYtsaurusUser(ytUser YtsaurusUser, bannedSince time.Time) YtsaurusUser
}

func createLdapGroup(name string) LdapGroup {
name = "acme." + name
return LdapGroup{
Groupname: fmt.Sprintf("%v|all", name),
Groupname: fmt.Sprintf("acme|all.%v", name),
}
}

func createYtsaurusGroup(name string) YtsaurusGroup {
name = "acme." + name
originalName := fmt.Sprintf("%v|all", name)
originalName := fmt.Sprintf("acme|all.%v", name)
ytName := originalName
for _, replacement := range defaultGroupnameReplacements {
ytName = strings.Replace(ytName, replacement.From, replacement.To, -1)
}
return YtsaurusGroup{Name: name, SourceRaw: map[string]any{
return YtsaurusGroup{Name: ytName, SourceRaw: map[string]any{
"groupname": originalName,
}}
}
Expand Down Expand Up @@ -229,10 +227,6 @@ var (
YtsaurusGroup: createYtsaurusGroup("devs"),
Members: NewStringSetFromItems(aliceName),
},
{
YtsaurusGroup: createYtsaurusGroup("hq"),
Members: NewStringSetFromItems(carolName),
},
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ type LdapGroupsConfig struct {
MemberUIDAttributeType string `yaml:"member_uid_attribute_type"`

// A list of groupnames for which app will print more debug info in logs.
DebugGroupnames []string `yaml:"debug_groupnames"`
DebugGroupnames []string `yaml:"debug_groupnames"`
GroupsNameSuffixPostFilter string `yaml:"groups_display_name_suffix_post_filter"`
}

type LdapConfig struct {
Expand Down
42 changes: 42 additions & 0 deletions ldap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"strings"

"github.com/go-ldap/ldap/v3"
"k8s.io/utils/env"
)
Expand Down Expand Up @@ -52,6 +54,7 @@ func (l *Ldap) GetUsers() ([]SourceUser, error) {
return nil, err
}

l.logger.Infow("fetching %d users", len(res.Entries))
var users []SourceUser
for _, entry := range res.Entries {
username := entry.GetAttributeValue(l.config.Users.UsernameAttributeType)
Expand All @@ -64,6 +67,7 @@ func (l *Ldap) GetUsers() ([]SourceUser, error) {
Username: username,
UID: uid,
FirstName: firstName})
l.maybePrintDebugLogsUsers(username, "fetched_ldap_user", entry)
}
return users, nil
}
Expand All @@ -79,16 +83,54 @@ func (l *Ldap) GetGroupsWithMembers() ([]SourceGroupWithMembers, error) {
return nil, err
}

groupsSkipped := 0
var groups []SourceGroupWithMembers
for _, entry := range res.Entries {
groupname := entry.GetAttributeValue(l.config.Groups.GroupnameAttributeType)
members := entry.GetAttributeValues(l.config.Groups.MemberUIDAttributeType)

l.maybePrintDebugLogsGroups(groupname, "groupname", groupname)

if groupname == "" {
l.logger.Debugw("Skipping group with empty groupname", "group", entry)
groupsSkipped++
continue
}

if l.config.Groups.GroupsNameSuffixPostFilter != "" && !strings.HasSuffix(groupname, l.config.Groups.GroupsNameSuffixPostFilter) {
l.logger.Debugw("Skipping group because suffix doesn't match", "group", entry)
groupsSkipped++
folklinoff marked this conversation as resolved.
Show resolved Hide resolved
continue
}

l.maybePrintDebugLogsGroups(groupname, "group_members_count", len(members))

groups = append(groups, SourceGroupWithMembers{
SourceGroup: LdapGroup{
Groupname: groupname,
},
Members: NewStringSetFromItems(members...),
})
}

l.logger.Infow("Fetched groups from LDAP", "got", len(groups), "skipped", groupsSkipped)
return groups, nil
}

func (l *Ldap) maybePrintDebugLogsUsers(name string, args ...any) {
args = append([]any{"id", name}, args...)
for _, debugID := range l.config.Users.DebugUsernames {
if name == debugID {
l.logger.Debugw("Debug info", args...)
}
}
}

func (l *Ldap) maybePrintDebugLogsGroups(name string, args ...any) {
args = append([]any{"id", name}, args...)
for _, debugID := range l.config.Groups.DebugGroupnames {
if name == debugID {
l.logger.Debugw("Debug info", args...)
}
}
}
7 changes: 4 additions & 3 deletions testcontainer_openldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ func (y *OpenLdapLocal) GetConfig() (*LdapConfig, error) {
FirstNameAttributeType: ptr.String("givenName"),
},
Groups: LdapGroupsConfig{
Filter: "(objectClass=posixGroup)",
GroupnameAttributeType: "cn",
MemberUIDAttributeType: "memberUid",
Filter: "(objectClass=posixGroup)",
GroupnameAttributeType: "cn",
MemberUIDAttributeType: "memberUid",
GroupsNameSuffixPostFilter: ".devs",
},
}, nil
}
Expand Down
Loading