Skip to content

Commit

Permalink
handle synchronous error in ldap search
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Sep 8, 2017
1 parent ff70cce commit 015f414
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/couch/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ async function syncOneLdapGroup(ctx, group) {
const couchOptions = ctx._couchOptions;
const entries = await ldapSearch({
url: couchOptions.ldapUrl,
connectionTimeout: 2000,
bindDN: couchOptions.ldapBindDN,
bindPassword: couchOptions.ldapBindPassword
}, {
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/couch.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function onGetError(ctx, e, secure) {
break;
}
if (e.message && e.message !== ctx.body) {
ctx.body += `: ${e.message}`;
ctx.body += `: ${e}`;
}
if (config.debugrest) {
ctx.body += `\n\n${e}\n${e.stack}`;
Expand Down
49 changes: 34 additions & 15 deletions src/util/LDAP.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
const ldapjs = require('ldapjs');
const debug = require('./debug')('ldap:client');

const defaultSearchOptions = {
scope: 'sub',
timeLimit: 1
};

const defaultLdapOptions = {
connectTimeout: 2000,
timeout: 2000
};

function search(ldapOptions, searchOptions) {
searchOptions = Object.assign({}, defaultSearchOptions, searchOptions);
ldapOptions = Object.assign({}, defaultLdapOptions, ldapOptions);
return new Promise((resolve, reject) => {
// ldap options should include bind options
// if client could know when it is ready
Expand All @@ -12,6 +24,7 @@ function search(ldapOptions, searchOptions) {
client.on('error', function(e) {
reject(e);
});

client.__resolve__ = function(value) {
client.destroy();
resolve(value);
Expand All @@ -22,22 +35,28 @@ function search(ldapOptions, searchOptions) {
reject(err);
};
return bind(client, ldapOptions.bindDN, ldapOptions.bindPassword).then(() => {
client.search(searchOptions.base, searchOptions, (err, res) => {
if (err) {
client.__reject__(err);
return;
}
const entries = [];
res.on('searchEntry', function (entry) {
entries.push(entry);
});
res.on('error', function (err) {
client.__reject__(err);
try {
client.search(searchOptions.DN, searchOptions, (err, res) => {
if (err) {
client.__reject__(err);
return;
}
const entries = [];
res.on('searchEntry', function (entry) {
entries.push(entry);
});
res.on('error', function (err) {
client.__reject__(err);
});
res.on('end', function () {
client.__resolve__(entries);
});
});
res.on('end', function () {
client.__resolve__(entries);
});
});
} catch(e) {
// LIBRARY!!! WHY DON'T YOU PASS ALL YOUR ERRORS IN THE CALLBACK!!!
client.__reject__(e);
}

}).catch(e => {/* Error should be handled by __reject__ */});
});
}
Expand Down
1 change: 1 addition & 0 deletions test/homedir/test-ldap/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ console.log('load configuration');

module.exports = {
ldapSync: false,
ldapUrl: 'ldap://localhost'
};

0 comments on commit 015f414

Please sign in to comment.