From fe59057c0dee5e9a20fa01f911b717e963c54c86 Mon Sep 17 00:00:00 2001 From: 5amu Date: Sat, 20 Jan 2024 12:22:36 +0100 Subject: [PATCH] implement a generic search that returns a list of objects given a filter and desired attributes --- pkg/js/libs/ldap/ldap.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/js/libs/ldap/ldap.go b/pkg/js/libs/ldap/ldap.go index 621d721a..9d22965e 100644 --- a/pkg/js/libs/ldap/ldap.go +++ b/pkg/js/libs/ldap/ldap.go @@ -77,6 +77,35 @@ func (c *LdapClient) Connect(host string, port int, ssl, istls bool) (bool, erro return true, nil } +// Search is a method that uses the already Connect()'ed client to query the LDAP +// server, works for openldap and for Microsoft's Active Directory Ldap +// +// accepts whatever filter and returns a list of maps having provided attributes +// as keys and associated values mirroring the ones returned by ldap +func (c *LdapClient) Search(filter string, attributes ...string) ([]map[string][]string, error) { + res, err := c.Conn.Search(ldap.NewSearchRequest( + c.BaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, + 0, 0, false, filter, attributes, nil, + )) + if err != nil { + return nil, err + } + + if len(res.Entries) == 0 { + return nil, fmt.Errorf("no result found in search") + } + + var out []map[string][]string + for _, r := range res.Entries { + app := make(map[string][]string) + for _, a := range attributes { + app[a] = r.GetAttributeValues(a) + } + out = append(out, app) + } + return out, nil +} + // CollectLdapMetadata collects metadata from ldap server. func (c *LdapClient) CollectLdapMetadata(domain string, controller string) (LDAPMetadata, error) { opts := &ldapSessionOptions{