refactor(core): Centralize SSH Tunnel management (#9906)

Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-07-04 12:29:44 +02:00
committed by GitHub
parent 86018aa6e0
commit 85aa560a5d
25 changed files with 525 additions and 630 deletions

View File

@@ -1,25 +1,19 @@
import type {
ICredentialDataDecryptedObject,
ICredentialsDecrypted,
ICredentialTestFunctions,
INodeCredentialTestResult,
} from 'n8n-workflow';
import { Client } from 'ssh2';
import { createPool } from '../transport';
import type { MysqlNodeCredentials } from '../helpers/interfaces';
export async function mysqlConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as ICredentialDataDecryptedObject;
const credentials = credential.data as MysqlNodeCredentials;
let sshClient: Client | undefined = undefined;
if (credentials.sshTunnel) {
sshClient = new Client();
}
const pool = await createPool(credentials, {}, sshClient);
const pool = await createPool.call(this, credentials);
try {
const connection = await pool.getConnection();
@@ -30,9 +24,6 @@ export async function mysqlConnectionTest(
message: error.message,
};
} finally {
if (sshClient) {
sshClient.end();
}
await pool.end();
}

View File

@@ -1,18 +1,13 @@
import type { IDataObject, ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
import { Client } from 'ssh2';
import { createPool } from '../transport';
import type { MysqlNodeCredentials } from '../helpers/interfaces';
export async function searchTables(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
const credentials = await this.getCredentials('mySql');
const credentials = (await this.getCredentials('mySql')) as MysqlNodeCredentials;
const nodeOptions = this.getNodeParameter('options', 0) as IDataObject;
let sshClient: Client | undefined = undefined;
if (credentials.sshTunnel) {
sshClient = new Client();
}
const pool = await createPool(credentials, nodeOptions, sshClient);
const pool = await createPool.call(this, credentials, nodeOptions);
try {
const connection = await pool.getConnection();
@@ -32,12 +27,7 @@ export async function searchTables(this: ILoadOptionsFunctions): Promise<INodeLi
}));
return { results };
} catch (error) {
throw error;
} finally {
if (sshClient) {
sshClient.end();
}
await pool.end();
}
}

View File

@@ -1,18 +1,13 @@
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
import { Client } from 'ssh2';
import { createPool } from '../transport';
import { escapeSqlIdentifier } from '../helpers/utils';
import type { MysqlNodeCredentials } from '../helpers/interfaces';
export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const credentials = await this.getCredentials('mySql');
const credentials = (await this.getCredentials('mySql')) as MysqlNodeCredentials;
const nodeOptions = this.getNodeParameter('options', 0) as IDataObject;
let sshClient: Client | undefined = undefined;
if (credentials.sshTunnel) {
sshClient = new Client();
}
const pool = await createPool(credentials, nodeOptions, sshClient);
const pool = await createPool.call(this, credentials, nodeOptions);
try {
const connection = await pool.getConnection();
@@ -39,12 +34,7 @@ export async function getColumns(this: ILoadOptionsFunctions): Promise<INodeProp
column.Null as string
}`,
}));
} catch (error) {
throw error;
} finally {
if (sshClient) {
sshClient.end();
}
await pool.end();
}
}