1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 23:59:47 +02:00

Discrete data loading functions

This commit is contained in:
sauls8t 2018-09-01 14:50:37 +01:00
parent e6b557f43b
commit fd167234ae
5 changed files with 133 additions and 270 deletions

View file

@ -12,12 +12,9 @@
package ldap
import (
"fmt"
"strings"
"testing"
lm "github.com/documize/community/model/auth"
ld "gopkg.in/ldap.v2"
)
// Works against https://github.com/rroemhild/docker-test-openldap
@ -27,7 +24,7 @@ var testConfigLocalLDAP = lm.LDAPConfig{
ServerType: lm.ServerTypeLDAP,
ServerHost: "127.0.0.1",
ServerPort: 389,
EncryptionType: "starttls",
EncryptionType: lm.EncryptionTypeStartTLS,
BaseDN: "ou=people,dc=planetexpress,dc=com",
BindDN: "cn=admin,dc=planetexpress,dc=com",
BindPassword: "GoodNewsEveryone",
@ -63,94 +60,24 @@ func TestUserFilter_LocalLDAP(t *testing.T) {
}
}
func TestLocalLDAPServer_UsersInGroup(t *testing.T) {
testConfigLocalLDAP.UserFilter = ""
func TestGroupFilter_LocalLDAP(t *testing.T) {
testConfigLocalLDAP.GroupFilter = "(&(objectClass=group)(|(cn=ship_crew)(cn=admin_staff)))"
groupAttrs := testConfigLocalLDAP.GetGroupFilterAttributes()
userAttrs := testConfigLocalLDAP.GetUserFilterAttributes()
l, err := connect(testConfigLocalLDAP)
e, err := executeGroupFilter(testConfigLocalLDAP)
if err != nil {
t.Error("Error: unable to dial LDAP server: ", err.Error())
t.Error("unable to exeucte group filter", err.Error())
return
}
defer l.Close()
// Authenticate with LDAP server using admin credentials.
t.Log("Binding LDAP admin user")
err = l.Bind(testConfigLocalLDAP.BindDN, testConfigLocalLDAP.BindPassword)
if err != nil {
t.Error("Error: unable to bind specified admin user to LDAP: ", err.Error())
if len(e) == 0 {
t.Error("Received zero group LDAP search entries")
return
}
searchRequest := ld.NewSearchRequest(
testConfigLocalLDAP.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
testConfigLocalLDAP.GroupFilter,
groupAttrs,
nil,
)
t.Logf("LDAP group search entries found: %d", len(e))
t.Log("LDAP search filter:", testConfigLocalLDAP.GroupFilter)
sr, err := l.Search(searchRequest)
if err != nil {
t.Error("Error: unable to execute directory search: ", err.Error())
return
}
t.Logf("LDAP search entries found: %d", len(sr.Entries))
if len(sr.Entries) == 0 {
t.Error("Received ZERO LDAP search entries")
return
}
// Get list of group members per group found.
for _, group := range sr.Entries {
t.Log("Found group", group.DN)
rawMembers := group.GetAttributeValues(testConfigLocalLDAP.AttributeGroupMember)
if len(rawMembers) == 0 {
t.Log("Error: group member attribute returned no users")
continue
}
t.Logf("LDAP group contains %d members", len(rawMembers))
for _, entry := range rawMembers {
// get CN element from DN
parts := strings.Split(entry, ",")
if len(parts) == 0 {
continue
}
filter := fmt.Sprintf("(%s)", parts[0])
usr := ld.NewSearchRequest(
testConfigLocalLDAP.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
filter,
userAttrs,
nil,
)
ue, err := l.Search(usr)
if err != nil {
t.Log("Error: unable to execute directory search for group member: ", err.Error())
continue
}
if len(ue.Entries) > 0 {
for _, ur := range ue.Entries {
t.Logf("[%s] %s (%s %s) @ %s\n",
ur.GetAttributeValue(testConfigLocalLDAP.AttributeUserRDN),
ur.GetAttributeValue("cn"),
ur.GetAttributeValue(testConfigLocalLDAP.AttributeUserFirstname),
ur.GetAttributeValue(testConfigLocalLDAP.AttributeUserLastname),
ur.GetAttributeValue(testConfigLocalLDAP.AttributeUserEmail))
}
} else {
t.Log("group member search failed:", filter)
}
}
for _, u := range e {
t.Logf("[%s] %s (%s %s) @ %s\n",
u.RemoteID, u.CN, u.Firstname, u.Lastname, u.Email)
}
}