Skip to content

Commit

Permalink
🧹 Make sure to unbind LDAP and return cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
pemontto committed Oct 13, 2023
1 parent 3c61445 commit ff0396b
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions packages/nodes-base/nodes/Ldap/Ldap.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ export class Ldap implements INodeType {
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as ICredentialDataDecryptedObject;
const client = await createLdapClient(credentials);
try {
const client = await createLdapClient(credentials);
await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) {
return {
status: 'Error',
message: error.message,
};
} finally {
await client.unbind();
}
return {
status: 'OK',
Expand All @@ -126,11 +128,21 @@ export class Ldap implements INodeType {
try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) {
await client.unbind();
console.log(error);
return [];
}

let results;
const baseDN = this.getNodeParameter('baseDN', 0) as string;
const results = await client.search(baseDN, { sizeLimit: 200, paged: false }); // should this size limit be set in credentials?
try {
results = await client.search(baseDN, { sizeLimit: 200, paged: false }); // should this size limit be set in credentials?
} catch (error) {
console.log(error);
return [];
} finally {
await client.unbind();
}

const unique = Object.keys(Object.assign({}, ...results.searchEntries));
return unique.map((x) => ({
Expand All @@ -145,11 +157,23 @@ export class Ldap implements INodeType {
try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) {
await client.unbind();
console.log(error);
return [];
}

const baseDN = this.getNodeParameter('baseDN', 0) as string;
const results = await client.search(baseDN, { sizeLimit: 10, paged: false }); // should this size limit be set in credentials?

let results;
try {
results = await client.search(baseDN, { sizeLimit: 10, paged: false }); // should this size limit be set in credentials?
} catch (error) {
console.log(error);
return [];
} finally {
await client.unbind();
}

const objects = [];
for (const entry of results.searchEntries) {
if (typeof entry.objectClass === 'string') {
Expand Down Expand Up @@ -177,11 +201,21 @@ export class Ldap implements INodeType {
try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) {
await client.unbind();
console.log(error);
return [];
}

let results;
const baseDN = this.getNodeParameter('dn', 0) as string;
const results = await client.search(baseDN, { sizeLimit: 1, paged: false });
try {
results = await client.search(baseDN, { sizeLimit: 1, paged: false });
} catch (error) {
console.log(error);
return [];
} finally {
await client.unbind();
}

const unique = Object.keys(Object.assign({}, ...results.searchEntries));
return unique.map((x) => ({
Expand Down Expand Up @@ -218,6 +252,7 @@ export class Ldap implements INodeType {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) {
delete error.cert;
await client.unbind();
if (this.continueOnFail()) {
return [
items.map((x) => {
Expand Down Expand Up @@ -386,6 +421,7 @@ export class Ldap implements INodeType {
if (this.continueOnFail()) {
returnItems.push({ json: items[itemIndex].json, error, pairedItem: itemIndex });
} else {
await client.unbind();
if (error.context) {
error.context.itemIndex = itemIndex;
throw error;
Expand All @@ -399,6 +435,9 @@ export class Ldap implements INodeType {
if (nodeDebug) {
Logger.info(`[${this.getNode().type} | ${this.getNode().name}] - Finished`);
}

await client.unbind();

return [returnItems];
}
}

0 comments on commit ff0396b

Please sign in to comment.