fix(Ldap Node): Fix issue with connections not closing correctly (#7432)

This commit is contained in:
pemontto 2023-10-13 12:15:54 +01:00 committed by GitHub
parent bd5ea455e1
commit c3f0be809f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 6 deletions

View file

@ -31,7 +31,6 @@ export class Ldap implements ICredentialType {
type: 'string', type: 'string',
default: '', default: '',
description: 'Distinguished Name of the user to connect as', description: 'Distinguished Name of the user to connect as',
required: true,
}, },
{ {
displayName: 'Binding Password', displayName: 'Binding Password',
@ -42,7 +41,6 @@ export class Ldap implements ICredentialType {
}, },
default: '', default: '',
description: 'Password of the user provided in the Binding DN field above', description: 'Password of the user provided in the Binding DN field above',
required: true,
}, },
{ {
displayName: 'Connection Security', displayName: 'Connection Security',
@ -90,5 +88,12 @@ export class Ldap implements ICredentialType {
type: 'string', type: 'string',
default: '', default: '',
}, },
{
displayName: 'Timeout',
description: 'Optional connection timeout in seconds',
name: 'timeout',
type: 'number',
default: 300,
},
]; ];
} }

View file

@ -39,6 +39,11 @@ export async function createLdapClient(
} }
} }
if (credentials.timeout) {
// Convert seconds to milliseconds
ldapOptions.timeout = (credentials.timeout as number) * 1000;
}
if (nodeDebug) { if (nodeDebug) {
Logger.info( Logger.info(
`[${nodeType} | ${nodeName}] - LDAP Options: ${JSON.stringify(ldapOptions, null, 2)}`, `[${nodeType} | ${nodeName}] - LDAP Options: ${JSON.stringify(ldapOptions, null, 2)}`,

View file

@ -103,14 +103,16 @@ export class Ldap implements INodeType {
credential: ICredentialsDecrypted, credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> { ): Promise<INodeCredentialTestResult> {
const credentials = credential.data as ICredentialDataDecryptedObject; const credentials = credential.data as ICredentialDataDecryptedObject;
const client = await createLdapClient(credentials);
try { try {
const client = await createLdapClient(credentials);
await client.bind(credentials.bindDN as string, credentials.bindPassword as string); await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) { } catch (error) {
return { return {
status: 'Error', status: 'Error',
message: error.message, message: error.message,
}; };
} finally {
await client.unbind();
} }
return { return {
status: 'OK', status: 'OK',
@ -126,11 +128,21 @@ export class Ldap implements INodeType {
try { try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string); await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) { } catch (error) {
await client.unbind();
console.log(error); console.log(error);
return [];
} }
let results;
const baseDN = this.getNodeParameter('baseDN', 0) as string; 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)); const unique = Object.keys(Object.assign({}, ...results.searchEntries));
return unique.map((x) => ({ return unique.map((x) => ({
@ -145,11 +157,23 @@ export class Ldap implements INodeType {
try { try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string); await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) { } catch (error) {
await client.unbind();
console.log(error); console.log(error);
return [];
} }
const baseDN = this.getNodeParameter('baseDN', 0) as string; 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 = []; const objects = [];
for (const entry of results.searchEntries) { for (const entry of results.searchEntries) {
if (typeof entry.objectClass === 'string') { if (typeof entry.objectClass === 'string') {
@ -177,11 +201,21 @@ export class Ldap implements INodeType {
try { try {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string); await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) { } catch (error) {
await client.unbind();
console.log(error); console.log(error);
return [];
} }
let results;
const baseDN = this.getNodeParameter('dn', 0) as string; 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)); const unique = Object.keys(Object.assign({}, ...results.searchEntries));
return unique.map((x) => ({ return unique.map((x) => ({
@ -218,6 +252,7 @@ export class Ldap implements INodeType {
await client.bind(credentials.bindDN as string, credentials.bindPassword as string); await client.bind(credentials.bindDN as string, credentials.bindPassword as string);
} catch (error) { } catch (error) {
delete error.cert; delete error.cert;
await client.unbind();
if (this.continueOnFail()) { if (this.continueOnFail()) {
return [ return [
items.map((x) => { items.map((x) => {
@ -386,6 +421,7 @@ export class Ldap implements INodeType {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnItems.push({ json: items[itemIndex].json, error, pairedItem: itemIndex }); returnItems.push({ json: items[itemIndex].json, error, pairedItem: itemIndex });
} else { } else {
await client.unbind();
if (error.context) { if (error.context) {
error.context.itemIndex = itemIndex; error.context.itemIndex = itemIndex;
throw error; throw error;
@ -399,6 +435,9 @@ export class Ldap implements INodeType {
if (nodeDebug) { if (nodeDebug) {
Logger.info(`[${this.getNode().type} | ${this.getNode().name}] - Finished`); Logger.info(`[${this.getNode().type} | ${this.getNode().name}] - Finished`);
} }
await client.unbind();
return [returnItems]; return [returnItems];
} }
} }