Changed order of LDAP-related functions

It makes more sense to have the function that establishes the connection
before the rest.
This commit is contained in:
Emmanuel BENOîT 2021-02-09 19:03:53 +01:00
parent 842a4be87e
commit 9bec0ad14e

48
main.go
View file

@ -210,30 +210,6 @@ func usernameFromMember(member string) string {
return member[eqPos+1 : commaPos] return member[eqPos+1 : commaPos]
} }
// Read the list of members from a LDAP group
func getGroupMembers(group string, conn *ldap.Conn, fields []string) (members []string) {
req := ldap.NewSearchRequest(group, ldap.ScopeBaseObject, ldap.NeverDerefAliases, 1, 0, false, "(objectClass=*)", fields, nil)
res, err := conn.Search(req)
if err != nil {
log.Fatalf("LDAP search for %s: %v", group, err)
}
for _, entry := range res.Entries {
for _, attr := range fields {
values := entry.GetAttributeValues(attr)
if len(values) == 0 {
continue
}
members = make([]string, len(values))
for i, value := range values {
members[i] = usernameFromMember(value)
}
break
}
}
return
}
// Establish a connection to the LDAP server // Establish a connection to the LDAP server
func getLdapConnection(cfg LdapConfig) (conn *ldap.Conn) { func getLdapConnection(cfg LdapConfig) (conn *ldap.Conn) {
tlsConfig := &tls.Config{ tlsConfig := &tls.Config{
@ -272,6 +248,30 @@ func getLdapConnection(cfg LdapConfig) (conn *ldap.Conn) {
return return
} }
// Read the list of members from a LDAP group
func getGroupMembers(group string, conn *ldap.Conn, fields []string) (members []string) {
req := ldap.NewSearchRequest(group, ldap.ScopeBaseObject, ldap.NeverDerefAliases, 1, 0, false, "(objectClass=*)", fields, nil)
res, err := conn.Search(req)
if err != nil {
log.Fatalf("LDAP search for %s: %v", group, err)
}
for _, entry := range res.Entries {
for _, attr := range fields {
values := entry.GetAttributeValues(attr)
if len(values) == 0 {
continue
}
members = make([]string, len(values))
for i, value := range values {
members[i] = usernameFromMember(value)
}
break
}
}
return
}
// Read the list of group members from the LDAP server for all groups in the mapping section. // Read the list of group members from the LDAP server for all groups in the mapping section.
func readLdapGroups(configuration Configuration) (groups GroupMembers) { func readLdapGroups(configuration Configuration) (groups GroupMembers) {
conn := getLdapConnection(configuration.Ldap) conn := getLdapConnection(configuration.Ldap)