refactor(core): Centralize SSH Tunnel management (#9906)
Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
committed by
GitHub
parent
86018aa6e0
commit
85aa560a5d
@@ -4,7 +4,6 @@ import type {
|
||||
INodeCredentialTestResult,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { configurePostgres } from '../transport';
|
||||
|
||||
import type { PgpClient, PostgresNodeCredentials } from '../helpers/interfaces';
|
||||
@@ -15,13 +14,11 @@ export async function postgresConnectionTest(
|
||||
): Promise<INodeCredentialTestResult> {
|
||||
const credentials = credential.data as PostgresNodeCredentials;
|
||||
|
||||
let sshClientCreated: Client | undefined = new Client();
|
||||
let pgpClientCreated: PgpClient | undefined;
|
||||
|
||||
try {
|
||||
const { db, pgp, sshClient } = await configurePostgres(credentials, {}, sshClientCreated);
|
||||
const { db, pgp } = await configurePostgres.call(this, credentials, {});
|
||||
|
||||
sshClientCreated = sshClient;
|
||||
pgpClientCreated = pgp;
|
||||
|
||||
await db.connect();
|
||||
@@ -45,9 +42,6 @@ export async function postgresConnectionTest(
|
||||
message,
|
||||
};
|
||||
} finally {
|
||||
if (sshClientCreated) {
|
||||
sshClientCreated.end();
|
||||
}
|
||||
if (pgpClientCreated) {
|
||||
pgpClientCreated.end();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeLi
|
||||
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
|
||||
const options = { nodeVersion: this.getNode().typeVersion };
|
||||
|
||||
const { db, sshClient } = await configurePostgres(credentials, options);
|
||||
const { db } = await configurePostgres.call(this, credentials, options);
|
||||
|
||||
try {
|
||||
const response = await db.any('SELECT schema_name FROM information_schema.schemata');
|
||||
@@ -18,12 +18,7 @@ export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeLi
|
||||
value: schema.schema_name as string,
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
if (sshClient) {
|
||||
sshClient.end();
|
||||
}
|
||||
if (!db.$pool.ending) await db.$pool.end();
|
||||
}
|
||||
}
|
||||
@@ -31,7 +26,7 @@ export async function tableSearch(this: ILoadOptionsFunctions): Promise<INodeLis
|
||||
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
|
||||
const options = { nodeVersion: this.getNode().typeVersion };
|
||||
|
||||
const { db, sshClient } = await configurePostgres(credentials, options);
|
||||
const { db } = await configurePostgres.call(this, credentials, options);
|
||||
|
||||
const schema = this.getNodeParameter('schema', 0, {
|
||||
extractValue: true,
|
||||
@@ -49,12 +44,7 @@ export async function tableSearch(this: ILoadOptionsFunctions): Promise<INodeLis
|
||||
value: table.table_name as string,
|
||||
})),
|
||||
};
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
if (sshClient) {
|
||||
sshClient.end();
|
||||
}
|
||||
if (!db.$pool.ending) await db.$pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ export async function getColumns(this: ILoadOptionsFunctions): Promise<INodeProp
|
||||
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
|
||||
const options = { nodeVersion: this.getNode().typeVersion };
|
||||
|
||||
const { db, sshClient } = await configurePostgres(credentials, options);
|
||||
const { db } = await configurePostgres.call(this, credentials, options);
|
||||
|
||||
const schema = this.getNodeParameter('schema', 0, {
|
||||
extractValue: true,
|
||||
@@ -26,12 +26,7 @@ export async function getColumns(this: ILoadOptionsFunctions): Promise<INodeProp
|
||||
value: column.column_name,
|
||||
description: `Type: ${column.data_type.toUpperCase()}, Nullable: ${column.is_nullable}`,
|
||||
}));
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
if (sshClient) {
|
||||
sshClient.end();
|
||||
}
|
||||
if (!db.$pool.ending) await db.$pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ export async function getMappingColumns(
|
||||
): Promise<ResourceMapperFields> {
|
||||
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
|
||||
|
||||
const { db, sshClient } = await configurePostgres(credentials);
|
||||
const { db } = await configurePostgres.call(this, credentials);
|
||||
|
||||
const schema = this.getNodeParameter('schema', 0, {
|
||||
extractValue: true,
|
||||
@@ -89,12 +89,7 @@ export async function getMappingColumns(
|
||||
}),
|
||||
);
|
||||
return { fields };
|
||||
} catch (error) {
|
||||
throw error;
|
||||
} finally {
|
||||
if (sshClient) {
|
||||
sshClient.end();
|
||||
}
|
||||
if (!db.$pool.ending) await db.$pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user