0

I have an Ldap Server running on Docker + ldapjs. This server is adding a set of records that I am trying to search for with the client. A sample user object looks like below:

    {
        user: 'cn=first.last,ou=user_group,o=main',
        info: {
            cn: 'first.last',
            email: '[email protected]'
        }
    }

The options would look like this:

      let opts = {
            scope: 'base',
            attributes: ['dn', 'sn', 'cn', 'user', 'info']
        };

I'm using this code in a class, so I bind in the constructor, after initializing the client:

    constructor(url) {
        client = ldap.createClient({
            url: url
        });
        client.on('error', (err) => {
            log.error(`${err}`);
        });
        client.bind(username, password, function (err) {
            if (err) {
                log.error(`${err}`);
            }
        });
        log.info('Client Initialized.');
    };

And my search code:

            return new Promise((resolve, reject) => {
                var record = {};
                client.search(username, opts, function (err, res) {
                    res.on('searchEntry', function (entry) {
                        log.info(`Record Retrieved: ${JSON.stringify(entry.object)}`);
                        record = entry.object;
                    });
                    res.on('error', function (err) {
                        log.error(`Error: ${err.message}`);
                    });
                    res.on('end', function (result) {
                        if (err) {
                            reject(err);
                        }
                        else {
                            log.info(`Status: ${result.status}`);
                            resolve(record);
                        }
                    });
                });
            });

The issue I'm experiencing is that the code will always resolve on end when I make a search request from the client, which means that I never get a match, although it's definitely there.

I've tried:

  • Binding inside and outside the promise instead. No difference.
  • Changing the user structure and username used in client.search. No difference.
  • Searching for only 'cn=first'. I do get an error that it doesn't exist, which is good.
  • Adding a filter in options and changing the parameters there, but still no result.

I connect to the server ok, bind is ok as well, so I think I'm either doing the search wrong, or the way I have structured the users in the server is not proper.

Added screenshot showing server logs: The user added in the entry looks like it has a different name, but I changed it to match in the data. enter image description here

4
  • @madkimachi Show us Dockerfile. Commented Oct 19, 2022 at 13:34
  • Also docker ps output,is there ldap container? Commented Oct 19, 2022 at 13:36
  • 1
    @RichardRublev Ldap Container is running ok and is reachable. I get the proper response when I search for something that doesn't exist. Commented Oct 19, 2022 at 13:38
  • @RichardRublev added a screenshot. You may not the cn is different than one I described, but that's already been changed to match the data. Commented Oct 19, 2022 at 13:44

1 Answer 1

0

I've found the issue, which was related to the structure I was using in my records, I've solved it using an ldapts client instead, but the same logic can be used in an ldapjs client:

Specifically: This is a record in my ldapjs Server:

{
 name: 'John Doe',
 uid: 'john.doe',
 dn: 'uid=john.doe, ou=users, o=server',
 email: '[email protected]',
 userprincipalname: 'cgi-doej',
}

This is how I search for it:

let attributes = ['cn'], filter = `(email=${email})`

const { searchEntries, searchReferences } = await this.client.search(searchDN, {
 scope: 'base',
 filter: filter,
 attributes: attributes
});

This has solved my issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.