Example #1
0
// QueryForEntries queries for LDAP with the given searchRequest
func QueryForEntries(clientConfig ldapclient.Config, query *ldap.SearchRequest) ([]*ldap.Entry, error) {
	connection, err := clientConfig.Connect()
	if err != nil {
		return nil, fmt.Errorf("could not connect to the LDAP server: %v", err)
	}
	defer connection.Close()

	if bindDN, bindPassword := clientConfig.GetBindCredentials(); len(bindDN) > 0 {
		if err := connection.Bind(bindDN, bindPassword); err != nil {
			return nil, fmt.Errorf("could not bind to the LDAP server: %v", err)
		}
	}

	glog.V(4).Infof("searching LDAP server with config %v with dn=%q and scope %v for %s requesting %v", clientConfig, query.BaseDN, query.Scope, query.Filter, query.Attributes)
	searchResult, err := connection.Search(query)
	if err != nil {
		if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
			return nil, NewNoSuchObjectError(query.BaseDN)
		}
		return nil, err
	}

	for _, entry := range searchResult.Entries {
		glog.V(4).Infof("found dn=%q ", entry.DN)
	}
	return searchResult.Entries, nil
}
Example #2
0
// QueryForEntries queries for LDAP with the given searchRequest
func QueryForEntries(clientConfig ldapclient.Config, query *ldap.SearchRequest) ([]*ldap.Entry, error) {
	connection, err := clientConfig.Connect()
	if err != nil {
		return nil, fmt.Errorf("could not connect to the LDAP server: %v", err)
	}
	defer connection.Close()

	if bindDN, bindPassword := clientConfig.GetBindCredentials(); len(bindDN) > 0 {
		if err := connection.Bind(bindDN, bindPassword); err != nil {
			return nil, fmt.Errorf("could not bind to the LDAP server: %v", err)
		}
	}

	var searchResult *ldap.SearchResult
	control := ldap.FindControl(query.Controls, ldap.ControlTypePaging)
	if control == nil {
		glog.V(4).Infof("searching LDAP server with config %v with dn=%q and scope %v for %s requesting %v", clientConfig, query.BaseDN, query.Scope, query.Filter, query.Attributes)
		searchResult, err = connection.Search(query)
	} else if pagingControl, ok := control.(*ldap.ControlPaging); ok {
		glog.V(4).Infof("searching LDAP server with config %v with dn=%q and scope %v for %s requesting %v with pageSize=%d", clientConfig, query.BaseDN, query.Scope, query.Filter, query.Attributes, pagingControl.PagingSize)
		searchResult, err = connection.SearchWithPaging(query, pagingControl.PagingSize)
	} else {
		err = fmt.Errorf("invalid paging control type: %v", control)
	}

	if err != nil {
		if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
			return nil, NewNoSuchObjectError(query.BaseDN)
		}
		return nil, err
	}

	for _, entry := range searchResult.Entries {
		glog.V(4).Infof("found dn=%q ", entry.DN)
	}
	return searchResult.Entries, nil
}